| 1 | #include <avr/io.h> |
| 2 | #define F_CPU 1000000UL //1MHz default internal RC oscillator setting |
| 3 | //#include <avr/iotn2313.h> |
| 4 | //#include <avr/interrupt.h> |
| 5 | //util/delay.h and util/setbaud.h |
| 6 | |
| 7 | #define USART_BAUDRATE 9600 |
| 8 | #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) |
| 9 | |
| 10 | #define LED PD5 |
| 11 | |
| 12 | int main (void) |
| 13 | { |
| 14 | DDRD |= (1 << LED); |
| 15 | |
| 16 | char ReceivedByte; |
| 17 | |
| 18 | UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry |
| 19 | //UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes (not in attiny2313?) |
| 20 | UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); |
| 21 | //Hint: stty -F /dev/ttyUSB1 9600 sc7 |
| 22 | |
| 23 | UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register |
| 24 | UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register |
| 25 | |
| 26 | for (;;) // Loop forever |
| 27 | { |
| 28 | while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR |
| 29 | ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived" |
| 30 | |
| 31 | PORTD ^= (1 << LED); //Toggle LED after each received byte |
| 32 | |
| 33 | while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it |
| 34 | UDR = ReceivedByte; // Echo back the received byte back to the computer |
| 35 | } |
| 36 | } |
| 37 | |