Networking and communications

In this week we have to make a network connection, either wired or wireless with at least two processors.

The boards

First of all, I needed to choose the boards I'm going to make network connection between.

Board 1 (Transmitter)

The first board I worked on was the serial bus board. I downloaded hello.bus.45.bridge.cad form the networking assigment page. Here is the design of the board:



I modified it by adding a button. Because this is the button I'm going to use to toggle the LED in the other board. Here is the modified board:



After milling the board and soldering all the components. I noticed that I connected the Rx and Tx together by mistake. I told Francicsco about it and he told me that it is a common mistake when using kokopelli so I fixed this problem by cutting the thraces and adding a wire.

The board before:





The board after:





Board 2 (Receiver)

At this point I didn't have my output devices board ready yet and I wanted to control a DC motor for this assigment. So, I went to the output devices page. And I downloaded hello.H-bridge.44.cad here is a picture of the board:



Then, I modified it using kokopelli. What I did is I added (Rx, Tx, GND, VCC) pins to make serial communications between it and the other board I'm going to choose. Also, I added an LED to be controlled by the other board and also it will help me to debug it later when I'm going to program it for the output devices assigment. Here is the modified board:



More details about the board and the problems I faced while making it will be described in the output devices assigment.

FTDI

We have an FTDI board in the lab. The bridge board will be connected to the FTDI board to allow the computer to communicate with it and with the other board connected to it.

The only modification I made to the FTDI board is changing the pins from male to female pins

The board before:





The board after:





The protocol

Francicsco told us that before programming any networking boards you need to have a protocol that will be translated to code when you start programming the boards. The protocol can be simple or very complicated. It depends of the number of processors you are trying to connect and also the number of components in each board you want to control.

An example of a protocol. Let's say I have two boards. The first one I will call it A and it has an LED. The second board I will call it B and it has only an LED too. To turn the LED ON the board must receive C, to turn the LED OFF the board must receive D. If I send A, then C, the LED of the first board will be on. If I send B, then C, the LED of the second board will be on. To turn OFF the LED of the second board I have to sent B then D.

In my situation the protocol will be so simple because I have only two boards that I want to connnect to each other. The serial bus board, board 1 will be the transmitter and the h bridge board, board 2 will be the receiver. I need only one letter in my protocol. This letter will be sent when the button in board 1 is pressed to toggle the LED in board 2.

Programming the transmitter

First of all, I downloaded hello.bus.45.c code. When I opened it, I was shocked. When I saw it I thought that I would never be able to modify the code and understand it. With the help of Francicsco I was able to understand pretty much all I need to transmit a signal. I wrote comments in the code to explain all the things I learned.

The first program I made was to transmit the letter F in a while loop every one second. It was very difficult to achieve this because I was trying to use Neil's Macros at the beginning. Then Francicsco adviced me to comment everything in the main function and then start writing my own program. Also, I faced many other problems and I will write down now all the things that you have to do when you start working with this board.

1- In the makefile, make sure it is for attiny45. For me, I used a makefile that I was using for previous weeks, and it was giving me an error, so you have to make sure that MMCU=attiny45 and avrdude -p t45 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex.

2- Also, in the makefile make sure you put the right frequency. For this board it is F_CPU = 1000000, it doesn't have an external clock.

3- Then, in hello.bus.45.c code you have to change the pins according to the board you have. #define serial_pin_in (1 << PB3) for Rx, #define serial_pin_out (1 << PB4) for Tx, and change #define led_pin (1 << PB0) if you want to use the LED.

4- Delete everything in the main function of hello.bus.45.c, as I mentioned before.

I started writing the program to transmit the letter F every one second, to send a charachter we use this line put_char(&serial_port, serial_pin_out, 'F');. And here is the main function code.

int main(void) {

    DDRB |= (1 << PB4);       // set PB4 as output, to transmit the signal

   while (1) {

    _delay_ms(1000);
      put_char(&serial_port, serial_pin_out, 'F');

      }
   }

And it worked fine. But I have few things that need to mention. At the beginning I thought the program was not working because I was waiting for Rx LED in FTDI board to blink without opening the serial monetor of Arduino IDE. Then, I realized that the program was working the whole time and I need to open the serial monetor to see Rx LED blinking.

Another problem I faced was that when I wanted to open the serial monitor of Arduino IDE. I get this error Error opening serial port '/dev/ttyUSB0'. Try consulting the documentation at http://playground.arduino.cc/Linux/All#Permission. Then I opened arduino using Sudo Arduino in the terminal.

Here is the video of the board send F every one second:





Then, I modified the main function to make the board send F only when I press the button. I added a delay so the board doesn't keep sending many F

int main(void) {

    DDRB |= (1 << PB4);       // set PB4 as output, to transmit the signal
    DDRB &= ~(1 << PB2);       // set PB2 as output, this is the button
    PORTB |= (1 << PB2); // This is to activate the pull up resistor

   while (1) {

    if (PINB & (1<<PB2)) {
    }
    else {
    put_char(&serial_port, serial_pin_out, 'F');
    _delay_ms(1000);
    }
      }
        }

Then, I tested it with the serial monitor and here is the result:


Programming the receiver

Programming it was pretty straightforward for me, after all the problems I faced while programming the transmitter.

Firstly, I needed in the to define the pins for Rx, Tx, and LED. Like I did in the transmitter. One more thing I did here is that I defined the node id #define node_id 'F'. I put F because this is the letter the board is going to receive.

Here is the main function of the program without Neil's lines that I commmented:

int main(void) {

   static char chr;
  output(led_direction, led_pin);

   while (1) {

      get_char(&serial_pins, serial_pin_in, &chr); // to get the character
        _delay_ms(100);
      if (chr == node_id) {
           PORTB ^= (1 << PB2);
      }
    }
 }

To connect the boards together, I wanted to use this cable:





Then, I realized that the pins of the two boards are not aligned so I made this cable to solve the problem:





After I connected, the two boards together. Here is the result:





Files