1 //***************************************************************************
\r
3 // File........: usart.c
\r
5 // Author(s)...: ATMEL Norway
\r
7 // Target(s)...: ATmega169
\r
9 // Compiler....: avr-gcc / avr-libc
\r
11 // Description.: AVR Butterfly USART routines
\r
13 // Revisions...: 1.0
\r
15 // YYYYMMDD - VER. - COMMENT - SIGN.
\r
17 // 20030116 - 1.0 - Created - LHM
\r
18 // 2004 - corrected comment (only receiver enabled) - mt
\r
19 // 20070517 - option to enable transmit - mt
\r
20 //***************************************************************************
\r
23 //#include <inavr.h>
\r
24 //#include "iom169.h"
\r
32 /*****************************************************************************
\r
34 * Function name : USART_Init
\r
38 * Parameters : unsigned int baudrate
\r
40 * Purpose : Initialize the USART
\r
42 *****************************************************************************/
\r
43 void USART_Init(unsigned int baudrate)
\r
46 UBRRH = (unsigned char)(baudrate>>8);
\r
47 UBRRL = (unsigned char)baudrate;
\r
53 //sbiBF(DDRB, 6); // PORTB5 = Tx
\r
54 //sbiBF(DDRB, 7); // PORTB7 = Rx
\r
58 #ifdef WITH_TRANSMIT
\r
59 // enabled receive and transmit
\r
60 UCSRB = (1<<RXEN)|(1<<TXEN)|(0<<RXCIE)|(0<<UDRIE);
\r
62 // Enable receive. transmit stays disabled
\r
63 // In the default BF demo-application no data gets
\r
64 // send (only receive in vcard).
\r
65 UCSRB = (1<<RXEN)|(0<<TXEN)|(0<<RXCIE)|(0<<UDRIE);
\r
69 UCSRC = (0<<UMSEL)|(0<<UPM0)|(0<<USBS)|(3<<UCSZ0)|(0<<UCPOL);
\r
73 /*****************************************************************************
\r
75 * Function name : Usart_Tx
\r
79 * Parameters : char data: byte to send
\r
81 * Purpose : Send one byte through the USART
\r
83 *****************************************************************************/
\r
85 void USART_Tx(char data)
\r
88 while (!(UCSRA & (1<<UDRE)));
\r
95 /*****************************************************************************
\r
97 * Function name : Usart_Rx
\r
99 * Returns : char: byte received
\r
101 * Parameters : None
\r
103 * Purpose : Receives one byte from the USART
\r
105 *****************************************************************************/
\r
106 char USART_Rx(void)
\r
109 while (!(UCSRA & (1<<RXC)));
\r
110 // cbiBF(PORTB, 5);
\r
113 char USART_Poll(void)
\r
115 if (!(UCSRA & (1<<RXC)))
\r
120 int USART_printf(const char * format, ...)
\r
124 va_start(args, format);
\r
125 int result = vsprintf(buffer, format, args);
\r
128 for (char * b = buffer; *b != '\0'; ++b)
\r