Skip to content

Instantly share code, notes, and snippets.

@peat-psuwit
Created April 30, 2018 15:59
Show Gist options
  • Save peat-psuwit/3022ab78004be73d6b96cc635f5ca40d to your computer and use it in GitHub Desktop.
Save peat-psuwit/3022ab78004be73d6b96cc635f5ca40d to your computer and use it in GitHub Desktop.
/*
* UartTest.c
*
* Created: 30/4/2561 20:04:00
* Author : peath
*/
#include <avr/io.h>
void UART_init() {
// 9600 baud
UBRR0 = 103;
// 8n1 stop bit 1
UCSR0A = 0; // U2X = 0
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (0 << UCSZ02);
UCSR0C = (0 << UMSEL01) | (0 << UMSEL00) | (0 << UPM01) | (0 << UPM00) |
(0 << USBS0) | (0 << UCSZ02) | (1 << UCSZ01) | (1 << UCSZ00);
}
char UART_read() {
while ( ! (UCSR0A & (1 << RXC0)) );
return UDR0;
}
void UART_write(char c) {
while (! (UCSR0A & (1 << UDRE0)) );
UDR0 = c;
}
int main(void)
{
char c;
int i;
char youSaid[] = "You said: ";
UART_init();
while (1)
{
c = UART_read();
for (i = 0; youSaid[i] != '\0'; i++)
UART_write(youSaid[i]);
UART_write(c);
UART_write('\r');
UART_write('\n');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment