Embedded Networking and Communications [15]

Assignment

  • Design and build a wired &/or wireless network connecting at least two processors

For this reason, you decide to create 3 boards if you are connected to the middle of the serial port, if you have been running a bridge and that is a bridge that is data from the computer. fall node if it identifies a unique identification, in case case we have Node 0,1 and 2, where the zero corresponds to the bridge.

webpage jekyll webpage jekyll

I have reduced the size of the board as much as the cnc has allowed me, since the idea is that these nodes are as small as possible, the only one that has been bigger is the bridge, since it has a connection for the usb cable.

webpage jekyll webpage jekyll

Here we see the final file to use to create the file that needs cnc in this case is the bridge, as it is appreciated is a little larger than the nodes.

Brige
webpage jekyll webpage jekyll
Node
webpage jekyll webpage jekyll

The program is the same for all (bridge / nodes), we must only change the variable of line 33, by the value 0,1 or 2.

I have also modified the program, I have changed the name of the Node by Task, and I have changed the type of flash for another longer and with different blink time.

1
   #define node_id '0'
1
   #define led_delay2() _delay_ms(500) // LED flash delay
1
2
3
4
5
6
7
8
9
10
11
void flash() {
 
   clear(led_port, led_pin);
   led_delay2();
   set(led_port, led_pin);
   led_delay();
   clear(led_port, led_pin);
   led_delay2();
   set(led_port, led_pin);

   }
1
   static const char message[] PROGMEM = "Task "; //before "Node"

Program

it uses two pins with the serial protocol, and when the value compares in the main program with the id of the node that we have defined, if coindide enters the si and calls the flash function again and writes by console "Task" + the id of the node.

1
2
3
 get_char(&serial_pins, serial_pin_in, &chr);
      flash();
      if (chr == node_id)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// 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>
#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(100) // LED flash delay
#define led_delay2() _delay_ms(500) // 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 '0'

void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) {
  
   
   *rxbyte = 0;
   while (pin_test(*pins,pin))
      ;
   half_bit_delay();
   bit_delay();
   
   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);
 
   bit_delay();
   half_bit_delay();
   }

void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
  
   clear(*port,pin);
   bit_delay();
   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();
  
   set(*port,pin);
   bit_delay();
  
   bit_delay();
   }

void put_string(volatile unsigned char *port, unsigned char pin, PGM_P str) {
  
   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() {
 
   clear(led_port, led_pin);
   led_delay2();
   set(led_port, led_pin);
   led_delay();
   clear(led_port, led_pin);
   led_delay2();
   set(led_port, led_pin);

   }

int main(void) {
  
   static char chr;
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   set(serial_port, serial_pin_out);
   input(serial_direction, serial_pin_out);
   set(led_port, led_pin);
   output(led_direction, led_pin);
   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 = "Task ";
         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();
         input(serial_direction, serial_pin_out);
         }
      }
   }

Once programer the different boards, we have to connect them to each other by means of a cable that we have constructed especially for them, in this way the connection between the bridge and the nodes is made.

webpage jekyll

In this video we can see how the nodes are working using the Arduino console, if we send a 0 the blink flashes twice, if instead we send a 1, we see that it blinks twice the node that has assigned the id 1, and so on with the different nodes.

Download

Bridge

Node

Program