UART -Chip to chip communication

Discussion in 'Embedded Systems and Microcontrollers' started by Samarth Kapila, Jan 12, 2015.

  1. Samarth Kapila

    Thread Starter New Member

    Jan 12, 2015
    2
    0
    hello

    I was learning Uart but i got a problem.Now ,i am in no state to go further.please help me


    LOGIC
    whenever i press the switch button,data transmits from one chip to anotherand Led_1 glow s up .data contains a 8 bit binary number i.e 00001111.the secound chip(microcontroller) should receive it and Led_2 attaches to secound microcontoller starts to glow after verfication.
    (in other words LED 1 denotes that data has been send
    and Led2 denotes that data is received after checking(the binary number))


    PROBLEM

    but after each pressing i found that data is transmited and received. but the binary number is tansmitted is 00000000,inspite of sending 00001111.Even i changed the number but the result was same.

    The given below is the source code
    (the code is copied .i have taken from some website.I have done some changes as per by datasheet (atmega 16).

    thank you in advance

    for transmitter
    source code

    Code (Text):
    1.  
    2. #include<avr/io.h>
    3. #include<util/delay.h>
    4.  
    5. void main()
    6. {
    7.   DDRB=(1<<PINB1)|(0<<PINB0); //PINB0:switch(input)  && PINB1:led(output)
    8.   PORTB=1<<PINB0;//(set PINB0 HIGH)
    9.   int pressed=0;
    10.   int pressed_confidence_level=0;
    11.   int released_confidence_level=0;
    12.  
    13.   // set the register
    14.    UCSRA=0;
    15.   unsigned int ubrr=5;//(baud rate=9600bps)
    16.   UBRRL=(unsigned char)ubrr;
    17.   UBRRH=(unsigned char)(ubrr>>8);
    18.  
    19.   // set reciver and transmitter
    20.     UCSRB=(1<<RXEN)|(1<<TXEN);
    21.  
    22.   //set data format and stop bits
    23.   UCSRC|=(1<<URSEL)|(3<<UCSZ0);
    24.  
    25. while(1)
    26.   {  if(bit_is_clear(PINB,0))  // logic: when switch is pressed
    27.   // PINB0 will get low
    28.   //  enter in the if-statement loop
    29.   {
    30.     pressed_confidence_level++;
    31.    released_confidence_level=0;
    32.    if(pressed_confidence_level>100) // use to avoid debounce
    33.    {
    34.     if(pressed==0)
    35.  
    36.    {  pressed=1;
    37.     PORTB^=(1<<PINB1); // toggle the led
    38.   while(!(UCSRA&(1<<UDRE)));
    39.   UDR=0b00001111;// data will be send
    40.   }
    41.   pressed_confidence_level=0;
    42.   }
    43.    }
    44.    else
    45.    {  released_confidence_level++;
    46.     pressed_confidence_level=0;
    47.   if(released_confidence_level>100)
    48.   {
    49.     pressed=0;
    50.     released_confidence_level=0;
    51.     }
    52.     }
    53.     }
    54.   }
    55.  
    56.  

    for receiver
    source code


    Code (Text):
    1.  
    2. #include<avr/io.h>
    3. #include<util/delay.h>
    4. #define F_CPU 1000000UL
    5.  
    6. int main(void)
    7. {
    8.   DDRB|=1<<PINB0;//set the pin as output
    9.   UCSRA=0;
    10.  
    11.   //set baud rate
    12.   unsigned int ubrr=5;
    13.   UBRRL=(unsigned char)ubrr;
    14.   UBRRH=(unsigned char)(ubrr>>8);
    15.  
    16.   // set reciver and transmitter
    17.     UCSRB=(1<<RXEN)|(1<<TXEN);
    18.  
    19.   //set data format and stop bits
    20.   UCSRC|=(1<<URSEL)|(3<<UCSZ0);
    21.  
    22.   unsigned char data;
    23.  
    24.   while(1)
    25.   {
    26.   while(!(UCSRA&(1<<RXC)))
    27.   {
    28.   }
    29.  
    30.   data=UDR;// data is received
    31.  
    32. if(data==0b00001111)
    33.      PORTB^=(1<<PINB0);// toggle the led
    34.   }
    35.   }
    36.  
    37.  
     
    Last edited by a moderator: Jan 12, 2015
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,375
    494
    There are lots of AVR chips.
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Test the transmitter and receiver separately using Hyperterminal.
     
  4. Samarth Kapila

    Thread Starter New Member

    Jan 12, 2015
    2
    0
    You want to say that I should use some different AVR chips.May be my one of the two chips is not working properly.
    Is it possible that only UDR register is not working ?

    Thank you
     
  5. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    How do you know the data is "00000000"? If the 'receive' LED lights, then the data is correct because you check before lighting it.
     
Loading...