First experiments with AVR USART
[mirrors/Programs.git] / avr / usart / main.c
1 #include <avr/io.h>
2 #define F_CPU 1000000UL //1MHz default internal RC oscillator setting
3 //util/delay.h and util/setbaud.h
4
5 #define USART_BAUDRATE 9600
6 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
7
8 int main (void)
9 {
10 char ReceivedByte;
11
12 UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
13 //UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes (not in attiny2313?)
14
15 UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
16 UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
17
18 for (;;) // Loop forever
19 {
20 while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
21 ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived"
22
23 while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
24 UDR = ReceivedByte; // Echo back the received byte back to the computer
25 }
26 }
27
This page took 0.370468 seconds and 4 git commands to generate.