Last active
December 17, 2015 12:29
-
-
Save electronut/5610483 to your computer and use it in GitHub Desktop.
ATmega168 serial communications (transmit only) - most code is from the data sheet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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