globalrevweek14

parent 12a947d5
Pipeline #112053 passed with stage
in 35 seconds
......@@ -12,7 +12,8 @@
// license for commercial sale available from MIT.
//
#include <avr/io.h>
#include <avr/io.h>0
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
......@@ -26,7 +27,7 @@
#define bit_delay_time 100 // bit delay for 9600 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define led_delay() _delay_ms(100) // LED flash delay
#define led_delay() _delay_ms(500) // LED flash delay
#define led_port PORTB
#define led_direction DDRB
......@@ -223,6 +224,11 @@ int main(void) {
put_char(&serial_port, serial_pin_out, 10); // new line
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
input(serial_direction, serial_pin_out);
}
}
......
//
//
// hello.bus.45.c
//
// 9600 baud serial bus hello-world
//
// Neil Gershenfeld
// 11/24/10
//
// (c) Massachusetts Institute of Technology 2010
// Permission granted for experimental and personal use;
// license for commercial sale available from MIT.
//
#include <avr/io.h>0
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for input
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 100 // bit delay for 9600 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define led_delay() _delay_ms(50) // LED flash delay
#define led_port PORTB
#define led_direction DDRB
#define led_pin (1 << PB0)
#define serial_port PORTB
#define serial_direction DDRB
#define serial_pins PINB
#define serial_pin_in (1 << PB3)
#define serial_pin_out (1 << PB4)
#define node_id '1'
void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) {
//
// read character into rxbyte on pins pin
// assumes line driver (inverts bits)
//
*rxbyte = 0;
while (pin_test(*pins,pin))
//
// wait for start bit
//
;
//
// delay to middle of first data bit
//
half_bit_delay();
bit_delay();
//
// unrolled loop to read data bits
//
if pin_test(*pins,pin)
*rxbyte |= (1 << 0);
else
*rxbyte |= (0 << 0);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 1);
else
*rxbyte |= (0 << 1);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 2);
else
*rxbyte |= (0 << 2);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 3);
else
*rxbyte |= (0 << 3);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 4);
else
*rxbyte |= (0 << 4);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 5);
else
*rxbyte |= (0 << 5);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 6);
else
*rxbyte |= (0 << 6);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 7);
else
*rxbyte |= (0 << 7);
//
// wait for stop bit
//
bit_delay();
half_bit_delay();
}
void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)
//
// start bit
//
clear(*port,pin);
bit_delay();
//
// unrolled loop to write data bits
//
if bit_test(txchar,0)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,1)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,2)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,3)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,4)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,5)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,6)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,7)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
//
// stop bit
//
set(*port,pin);
bit_delay();
//
// char delay
//
bit_delay();
}
void put_string(volatile unsigned char *port, unsigned char pin, PGM_P str) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)
//
static char chr;
static int index;
index = 0;
do {
chr = pgm_read_byte(&(str[index]));
put_char(&serial_port, serial_pin_out, chr);
++index;
} while (chr != 0);
}
void flash() {
//
// LED flash delay
//
clear(led_port, led_pin);
led_delay();
set(led_port, led_pin);
}
int main(void) {
//
// main
//
static char chr;
//
// set clock divider to /1
//
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
//
// initialize output pins
//
set(serial_port, serial_pin_out);
input(serial_direction, serial_pin_out);
set(led_port, led_pin);
output(led_direction, led_pin);
//
// main loop
//
while (1) {
get_char(&serial_pins, serial_pin_in, &chr);
flash();
if (chr == node_id) {
output(serial_direction, serial_pin_out);
static const char message[] PROGMEM = "node ";
put_string(&serial_port, serial_pin_out, (PGM_P) message);
put_char(&serial_port, serial_pin_out, chr);
put_char(&serial_port, serial_pin_out, 10); // new line
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
input(serial_direction, serial_pin_out);
}
}
}
//
//
// hello.bus.45.c
//
// 9600 baud serial bus hello-world
//
// Neil Gershenfeld
// 11/24/10
//
// (c) Massachusetts Institute of Technology 2010
// Permission granted for experimental and personal use;
// license for commercial sale available from MIT.
//
#include <avr/io.h>0
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for input
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 100 // bit delay for 9600 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define led_delay() _delay_ms(50) // LED flash delay
#define led_port PORTB
#define led_direction DDRB
#define led_pin (1 << PB0)
#define serial_port PORTB
#define serial_direction DDRB
#define serial_pins PINB
#define serial_pin_in (1 << PB3)
#define serial_pin_out (1 << PB4)
#define node_id '1'
void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) {
//
// read character into rxbyte on pins pin
// assumes line driver (inverts bits)
//
*rxbyte = 0;
while (pin_test(*pins,pin))
//
// wait for start bit
//
;
//
// delay to middle of first data bit
//
half_bit_delay();
bit_delay();
//
// unrolled loop to read data bits
//
if pin_test(*pins,pin)
*rxbyte |= (1 << 0);
else
*rxbyte |= (0 << 0);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 1);
else
*rxbyte |= (0 << 1);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 2);
else
*rxbyte |= (0 << 2);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 3);
else
*rxbyte |= (0 << 3);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 4);
else
*rxbyte |= (0 << 4);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 5);
else
*rxbyte |= (0 << 5);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 6);
else
*rxbyte |= (0 << 6);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 7);
else
*rxbyte |= (0 << 7);
//
// wait for stop bit
//
bit_delay();
half_bit_delay();
}
void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)
//
// start bit
//
clear(*port,pin);
bit_delay();
//
// unrolled loop to write data bits
//
if bit_test(txchar,0)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,1)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,2)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,3)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,4)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,5)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,6)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,7)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
//
// stop bit
//
set(*port,pin);
bit_delay();
//
// char delay
//
bit_delay();
}
void put_string(volatile unsigned char *port, unsigned char pin, PGM_P str) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)
//
static char chr;
static int index;
index = 0;
do {
chr = pgm_read_byte(&(str[index]));
put_char(&serial_port, serial_pin_out, chr);
++index;
} while (chr != 0);
}
void flash() {
//
// LED flash delay
//
clear(led_port, led_pin);
led_delay();
set(led_port, led_pin);
}
int main(void) {
//
// main
//
static char chr;
//
// set clock divider to /1
//
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
//
// initialize output pins
//
set(serial_port, serial_pin_out);
input(serial_direction, serial_pin_out);
set(led_port, led_pin);
output(led_direction, led_pin);
//
// main loop
//
while (1) {
get_char(&serial_pins, serial_pin_in, &chr);
flash();
if (chr == node_id) {
output(serial_direction, serial_pin_out);
static const char message[] PROGMEM = "node ";
put_string(&serial_port, serial_pin_out, (PGM_P) message);
put_char(&serial_port, serial_pin_out, chr);
put_char(&serial_port, serial_pin_out, 10); // new line
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
led_delay();
flash();
input(serial_direction, serial_pin_out);
}
}
}
......@@ -154,6 +154,60 @@ Then, using the Neil code in C language for serial bus communication, I uploaded
<center><p><iframe width="560" height="315" src="https://www.youtube.com/embed/2yTrt5iV9lA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p></center>
<h1 class="my-4" align>
<br>
<br>
<small> UPDATE</small>
<br>
<br>
</h1>
<br><br>
<center><p>
At the beginning, understanding Neil's code may seem complicated, but the trick is to first understand the #define that he uses, because he repeats them when he programs in C. I would say that these are the most important functions to understand the logic : <br><br>
#define output(directions,pin) (directions |= pin) // set port direction for output<br><br>
#define input(directions,pin) (directions &= (~pin)) // set port direction for input<br><br>
#define set(port,pin) (port |= pin) // set port pin<br><br>
#define clear(port,pin) (port &= (~pin)) // clear port pin<br><br>
#define node_id '0'<br><br><br><br>
Initially the Tx pin is as an input, due to the input function, previously declared, when the "id" is detected as a character (0,1,2,3), corresponding to each board, the set function will be used. to convert the PORTB of the serial communication to 0, and the pin associated with Tx will become an OUTPUT pin using the function declared above, and will execute the action corresponding to the programming, the action is in the flash void and can vary in time (delay) and number of times the LED turns on or off. When finished executing the action it will be an INPUT pin again thanks to the function, in addition, the same "stop bit" sending function makes it return to 0, this is important because if we wanted to program it using another language like Arduino, this does not it would be included and we would have to program it to return to 0 so that the communication works correctly several times.<br><br><br><br>
Modify some lines of code to vary the actions of each board when its id is called.
When the bridge is called by its id "0", it will turn on 3 times in 500 ms intervals
<br>
<img class="centrado" src="Fotos/Week14/bn1.JPG" width="" alt="">
<br>
<br>
<img class="centrado" src="Fotos/Week14/bn2.JPG" width="" alt="">
When node1 is called by its id "1", it will turn on 7 times in 50 ms intervals
<br>
<img class="centrado" src="Fotos/Week14/pn1.JPG" width="" alt="">
<br>
<br>
<img class="centrado" src="Fotos/Week14/pn2.JPG" width="" alt="">
<br>
Next a demonstration video
<br>
<iframe width="560" height="315" src="https://www.youtube.com/embed/-tuVyVtm-Jw" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<br>
</p></center>
<h1 class="my-4" align>
<br>
<br>
......@@ -170,7 +224,7 @@ My personal contribution was to manufacture my node 2 as output for the communic
<center><p>To assemble the communication cables I used 4 jumpers or dupont cables and 2x2 pinheaders, since they will communicate VCC, GND, Tx and Rx
</p></center>
<br>
<img class="centrado" src="Fotos/Week14/10.jpg" width="480" alt="">
<img class="centrado" src="Fotos/Week14/10.jpg" height="480" alt="">
<center><p>
with the help of a pliers of ression, I nail the blades on the cables </p></center>
......@@ -213,6 +267,13 @@ Then I test continuity at both ends on all the pins </p></center>
<a href="Fotos/Week14/networkingnode.brd"> Node board </a>
<br>
<a href="Fotos/Week14/Networking/Networking.ino"> Neil's C code </a>
<br>
<a href="Fotos/Week14/bridge0/bridge0.ino"> Updated code bridge </a>
<br>
<a href="Fotos/Week14/node1p/node1p.ino"> Updated code node1</a>
<br>
<br>
<br><br>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment