Skip to content

Instantly share code, notes, and snippets.

@rubberduck203
Created April 21, 2018 15:37
Show Gist options
  • Save rubberduck203/0233adb39ab20fd013918c88ebcb85b1 to your computer and use it in GitHub Desktop.
Save rubberduck203/0233adb39ab20fd013918c88ebcb85b1 to your computer and use it in GitHub Desktop.
AVR UART
#include <avr/io.h>
#include <util/delay.h>
/* 9600 8n1 com */
#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
int main(void)
{
/*Set baud rate */
UBRR0H = (unsigned char)(BAUDRATE>>8);
UBRR0L = (unsigned char)BAUDRATE;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
for(;;) {
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = 'u';
/* wait half a sec before sending again*/
_delay_ms(500);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment