/* avr_uart.h provide functions to set up hardware Copyright 2005 EIT European Infotech AB www.eit.se. This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. History 2005-02-16 Modified for atmega8 and renamed. Used some code from an GPL avr platform written by Lukas Karrer. Henrik Björkman 2005-07-01 cleanup Henrik 2005-07-09 only some comments changed Henrik 2005-07-09 Renamed uart_packet to uart_buf, moved it to its own file. Henrik 2012-05-12 Removed buffered mode. Keeping only stream mode. Henrik 2012-05-21 Restored some extra features. Henrik */ // We might also want to do: // Redirecting STDIN and STDOUT to UART // Read more at http://www.appelsiini.net/2011/simple-usart-with-avr-libc /* ------------------------------------------------------------------------- * BUGS * ------------------------------------------------------------------------- */ /* // port_or is not defined in iomacro.h (which gets included in io.h) while // __port_or has a definition. This seems to be a bug in iomacro.h #define port_or __port_or // ditto #define port_and __port_and */ /* ------------------------------------------------------------------------- * includes * ------------------------------------------------------------------------- */ #include #include #include //#include #include #include "avr_cfg.h" #include "avr_misc.h" #include "avr_uart.h" #define USE_INTERRUPT_FOR_RECEIVE #define USE_INTERRUPT_FOR_TRANSMIT #ifndef UART_BAUDRATE #error need to know baudrate, typically set it like this: #define UART_BAUDRATE 19200 #endif #ifndef AVR_FOSC #error need to know clock frequency to set baudrate, typically: #define AVR_FOSC 16000000L #endif /* ------------------------------------------------------------------------- * local variables and defines * ------------------------------------------------------------------------- */ #if ((defined __AVR_ATmega88__) || (defined __AVR_ATmega168__) || (defined __AVR_ATmega328P__)) // OK #else #error Unknown or not yet supported device #endif #define UART_OUTBUF_EMPTY() (!( UCSR0A & (1<>8); UBRR0L = (unsigned char)ubrr_value; // Setting this bit UCSR0A bit 1 (U2Xn) the baudrate is doubled // Remember to adjust calculation of ubrr if this is used. //UCSR0A |= _BV(1); // Or is it better like this: UCSR0A |= _BV(U2X0); // Clearing it is default so ignoring this step is possible (but if your boot loader set it things will get confused) UCSR0A &= ~_BV(1); /* Enable receiver and transmitter */ UCSR0B = (1< 0) { uart_putchar(*data_ptr); data_ptr++; n_data--; } } /* debug_P */ /* In order to print a string stored in program memory use this function */ /* The call should look like this: uart_debug_P(PSTR("foo was here\n")); */ void uart_print_P(const char *addr) { char c; while ((c = pgm_read_byte(addr++))) { uart_putchar(c); } }