avr-c USART Receive

avr-c USART Receive
  1. Arduino UNO
#define F_CPU 16000000UL #include <avr/io.h> #include <stdio.h> #include <util/delay.h> #include <string.h> #define BAUD 9600 void USART_setup(void); int main(void) { USART_setup(); DDRB = 0xFF; while (1) { if ( (UCSR0A & (1<<RXC0)) ) { char data = UDR0; if (data == '1') { PORTB |= (1<<PORTB0); } else if (data == '2') { PORTB |= (1<<PORTB1); } else if (data == '3') { PORTB |= (1<<PORTB2); } else if (data == '4') { PORTB |= (1<<PORTB3); } else if (data == '5') { PORTB |= (1<<PORTB4); } else if (data == '6') { PORTB |= (1<<PORTB5); } else if (data == '0') { PORTB = 0x00; } } } return 0; } void USART_setup(void) { int speed = ((F_CPU/16/BAUD)-1); // set boud rate UBRR0H = (speed>>8); UBRR0L = speed; UCSR0B = (1<<RXEN0); // Bit 4 – RXEN0: Receiver Enable 0 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); // USART Character Size / Data Order = 8-bit }