Today i am writing about AVR microcontroller comunication using uart. It is going be short post. Last couple days I have been working on AVR microcontroller comunication with a PC. Unfortunately it didn’t work. I have been looking for bad connections in circuits and the problem wasn’t in the circuit. After couple of hours of looking I decided to check the code. I couldn’t find a mistake so i changed the code for most simplified version I could write:
#define UART_BAUD 9600
#define __BAUD_PRESCALE ((F_CPU+UART_BAUD*8UL) / (16UL*UART_BAUD)-1)
void USART_Transmit(char data);
void USART_Init(uint16_t baud);
int main(void)
{
USART_Init(__BAUD_PRESCALE);
while(1)
{
USART_Transmit('d');
USART_Transmit('a');
USART_Transmit('t');
USART_Transmit('a');
USART_Transmit(0x0d); // CR (Carriage Return) character
USART_Transmit(0x0a); // LF (line feed) character
_delay_ms(1000);
}
}
// definition of sending function
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}
And it worked 🙂 And lesson for today is – always check microcontroller with the most simply code you can write and then look for mistakes in circuit. Don’t do another way. This functions are from the AVR documentation, I didn’t change in transmitting function anything, I changed only initialisation for diffrent puspose.