/ main.cc
main.cc
1 #include <avr/interrupt.h> 2 #include <avr/io.h> 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <util/delay.h> 6 #include <avr/pgmspace.h> 7 8 #define PIN_LED (5) // onboard LED is PORTD5 9 10 static int serial_putc(char c, FILE *stream) { 11 if (c == '\n') { 12 serial_putc('\r', stream); 13 } 14 loop_until_bit_is_set(UCSR0A, UDRE0); 15 UDR0 = c; 16 return 0; 17 } 18 19 static int serial_getc(FILE *stream) { 20 loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ 21 return UDR0; 22 } 23 24 void serial_install_stdio() { 25 static FILE uart_stdio; 26 fdev_setup_stream(&uart_stdio, serial_putc, serial_getc, _FDEV_SETUP_RW); 27 stdin = stdout = stderr = &uart_stdio; 28 29 #define BAUD 57600 30 #include <util/setbaud.h> 31 UBRR0 = UBRR_VALUE; 32 #if USE_2X 33 UCSR0A |= (1<<U2X0); 34 #else 35 UCSR0A &= ~(1<<U2X0); 36 #endif 37 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); /* 8-bit data */ 38 UCSR0B = (1<<RXEN0) | (1<<TXEN0); /* enable transmitter and receiver */ 39 } 40 41 int main() { 42 serial_install_stdio(); 43 DDRD |= (1<<PIN_LED); 44 printf_P(PSTR("HELLO AVR - press any key to toggle LED. characters will be echoed\n")); 45 while(1) { int c = getchar(); putchar(c); PINB = (1<<PIN_LED); } 46 } 47 48 /* 49 Copying and distribution of this file, with or without modification, 50 are permitted in any medium without royalty provided the copyright 51 notice and this notice are preserved. This file is offered as-is, 52 without any warranty. 53 */