i am sending 4 bytes of data from my Pc serially to my atmega16. i use UART. one technique is to use the functoins given in datasheet but they block rest of the code as they use polling. so i am using a while loop. but i am unable to understand the structure of the code when the while loop starts.. please help me in this. thnx
#include <avr/io.h>
#define FOSC 1843200// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void UART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
int main( void )
{
int i = 0;
unsigned char my_data [4];
UART_Init ( MYUBRR );
while (1)
{
if (UCSRA & (1<<RXC))
{
my_data[i] = UDR;
}
i++;
if (i == 3)
{
/*my next code here and once thats done, its again in uart receive mode*/
i = 0;
}
}
}
If you don't want to use blocking polling you need to do interrupt driven handling of incoming data. Basically, you'd write an RX-Complete-ISR with your code in it:
(Naturally, you will need to turn on interrupts in general (
sei();
) and the USART receive complete interrupt before this can receive any data.)Then, in your main loop, you can perform other tasks and check via
If your ISR has received all the bytes.
You may need to pay attention to some synchronization issues, like declaring i as
volatile
, but that's how it's done in the most performant way.