UART -Chip to chip communication

Thread Starter

Samarth Kapila

Joined Jan 12, 2015
2
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:
#include<avr/io.h>
#include<util/delay.h>

void main()
{
  DDRB=(1<<PINB1)|(0<<PINB0); //PINB0:switch(input)  && PINB1:led(output)
  PORTB=1<<PINB0;//(set PINB0 HIGH)
  int pressed=0;
  int pressed_confidence_level=0;
  int released_confidence_level=0;

  // set the register
   UCSRA=0;
  unsigned int ubrr=5;//(baud rate=9600bps)
  UBRRL=(unsigned char)ubrr;
  UBRRH=(unsigned char)(ubrr>>8);

  // set reciver and transmitter
    UCSRB=(1<<RXEN)|(1<<TXEN);

  //set data format and stop bits
  UCSRC|=(1<<URSEL)|(3<<UCSZ0);

while(1)
  {  if(bit_is_clear(PINB,0))  // logic: when switch is pressed
  // PINB0 will get low
  //  enter in the if-statement loop
  {
    pressed_confidence_level++;
   released_confidence_level=0;
   if(pressed_confidence_level>100) // use to avoid debounce
   {
    if(pressed==0)

   {  pressed=1;
    PORTB^=(1<<PINB1); // toggle the led
  while(!(UCSRA&(1<<UDRE)));
  UDR=0b00001111;// data will be send
  }
  pressed_confidence_level=0;
  }
   }
   else
   {  released_confidence_level++;
    pressed_confidence_level=0;
  if(released_confidence_level>100)
  {
    pressed=0;
    released_confidence_level=0;
    }
    }
    }
  }

for receiver
source code


Code:
#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 1000000UL

int main(void)
{
  DDRB|=1<<PINB0;//set the pin as output
  UCSRA=0;

  //set baud rate
  unsigned int ubrr=5;
  UBRRL=(unsigned char)ubrr;
  UBRRH=(unsigned char)(ubrr>>8);

  // set reciver and transmitter
    UCSRB=(1<<RXEN)|(1<<TXEN);

  //set data format and stop bits
  UCSRC|=(1<<URSEL)|(3<<UCSZ0);

  unsigned char data;

  while(1)
  {
  while(!(UCSRA&(1<<RXC)))
  {
  }

  data=UDR;// data is received

if(data==0b00001111)
     PORTB^=(1<<PINB0);// toggle the led
  }
  }
 
Last edited by a moderator:

Thread Starter

Samarth Kapila

Joined Jan 12, 2015
2
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
 

Brownout

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