Last active
October 8, 2019 00:25
-
-
Save edp1096/aee0e0645f9e8b26ba29fbc03f4d2f7d to your computer and use it in GitHub Desktop.
atmega128 시리얼 에코 테스트
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 F_CPU 16000000UL | |
| #define USART_BAUDRATE 9600 // baud rate | |
| #define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) // Clock, bps to UBRR | |
| #include <avr/io.h> | |
| #include <stdio.h> | |
| void initUART() | |
| { | |
| // 16bit UBRRn - Split by each 8bit | |
| UBRR1H = (uint8_t)(UBRR_VALUE>>8); | |
| UBRR1L = (uint8_t) UBRR_VALUE; | |
| UCSR1C |= (1 << UCSZ10) | (1 << UCSZ11); // Data bit - 8bit | |
| UCSR1C &= ~((1 << UPM11) | (1 << UPM10)); // Parity bit - none | |
| UCSR1C &= ~(1 << USBS1); // stop bit - 1bit | |
| UCSR1B=(1 << RXEN1) | (1 << TXEN1); // Enable send, receive | |
| } | |
| void sendByte(uint8_t data) { | |
| while(!(UCSR1A & (1 << UDRE1))){}; // Wait for sending | |
| UDR1 = data; | |
| } | |
| uint8_t readByte(void) { | |
| while(!(UCSR1A & (1 << RXC1))){}; // Wait for receiving | |
| return UDR1; | |
| } | |
| void sendString(const char str[]) { | |
| uint8_t i = 0; | |
| while (str[i]) { | |
| sendByte(str[i]); | |
| i++; | |
| } | |
| } | |
| int main(void) | |
| { | |
| initUART(); | |
| char c; | |
| while(1) | |
| { | |
| c = readByte(); | |
| sendByte(c); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment