Skip to content

Instantly share code, notes, and snippets.

@electronut
Last active December 17, 2015 12:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electronut/5610483 to your computer and use it in GitHub Desktop.
Save electronut/5610483 to your computer and use it in GitHub Desktop.
ATmega168 serial communications (transmit only) - most code is from the data sheet.
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
void USART_Init(unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/*Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1 stop bit */
UCSR0C = (1<<UCSZ00) | (1 << UCSZ01);
}
void USART_Transmit(unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
// write null terminated string
void serial_write_str(const char* str)
{
int len = strlen(str);
int i;
for (i = 0; i < len; i++) {
USART_Transmit(str[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment