Created
April 30, 2018 15:59
-
-
Save peat-psuwit/3022ab78004be73d6b96cc635f5ca40d to your computer and use it in GitHub Desktop.
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
/* | |
* 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