echo.ino 다시쓰기
void setup() { UART_Init(9600); // setup serial } void loop() { UART_Transmit(UART_Receive()); // Echo } void UART_Init(int baud){ // Set baud rate unsigned int ubrr = 16000000/16/baud - 1; UBRR0H = (unsigned char) (ubrr>>8); UBRR0L = (unsigned char) ubrr; // Enable receiver and transmitter UCSR0B = _BV(RXEN0) | _BV(TXEN0); // Set frame format : 8 data, 2 stop bit UCSR0C = _BV(USBS0) | _BV(UCSZ01) | _BV(UCSZ00); } void UART_Transmit(unsigned char data){ // Wait for empty transmit buffer while(!( UCSR0A & _BV(UDRE0))); // Put data into buffer, send data UDR0 = data; } unsigned char UART_Receive(void){ // Wait for data to be received while(!(UCSR0A & _BV(RXC0))); // Get and return received data from buffer return UDR0; }
led_control.ino 다시쓰기
unsigned char c; void setup() { UART_Init(9600); // setup serial DDRB = _BV(DDB5); // sets the digital pin 13(PB5) as output } void loop() { c = UART_Receive(); if(c == '1') PORTB = _BV(DDB5); else if(c == '0') PORTB = 0; } /* ↓↓↓↓↓↓↓↓↓↓↓↓↓ echo.ino와 이하 동일 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ */
참고자료
- Atmel AVR 328p datasheet - 20. USART0