Selasa, 26 Juni 2012

Testing UART of Atmega16

Here is a simple program to test the UART communication of the atmega 16

>> The UART in atmega 16 can send frames ranges from 5 bits to 9 bits
>> In this program we use the assynchronous mode
>> The frames used 8 and 9 bits.

UART  transmitter code


// USART initialization
// Communication Parameters: 8 Data, 1 Stop, Even Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0xA6;
UBRRH=0x00;
UBRRL=0x19;

while (1)
      {
      // wait for UDR register to be empty (bit 5 in UCSRA register)
      while((UCSRA & 0b00100000)==0)
      {}
      UDR = PINA;
       };



UART receiver code


// USART initialization
// Communication Parameters: 8 Data, 1 Stop, Even Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x10;
UCSRC=0xA6;
UBRRH=0x00;
UBRRL=0x19;

while (1)
      {
      // wait for reception flag to be raised (bit 7 in UCSRA)
      while ((UCSRA & 0b10000000)==0)
      {}
      // check for errors
      if (((UCSRA&0b00010000)|(UCSRA&0b00001000)|(UCSRA&0b00000100))==0)
      {
      PORTC=UDR;
      }
      else
      {
      PORTD.7=1;  // indication for error
      }
      };


>>>> Here is a link including codevision project for UART tx and rx using frames of 8 bits and 9 bits and their proteus simulation :

0 komentar:

Posting Komentar