[b82c7b3] | 1 | /* AVR USART acces functions - stolen from Extreme Electronics */
|
---|
| 2 |
|
---|
| 3 | #include <avr/io.h>
|
---|
| 4 | #include <inttypes.h>
|
---|
| 5 |
|
---|
[cd3a9ac] | 6 | #ifndef BAUD
|
---|
| 7 | #error You must define BAUD to use libuart.
|
---|
| 8 | #endif
|
---|
[b82c7b3] | 9 |
|
---|
[6390cc3] | 10 | #ifdef __AVR_ATmega128__
|
---|
| 11 | // FIXME there are two USARTs in this case and we should be able to drive either
|
---|
| 12 | #define UBRRH UBRR0H
|
---|
| 13 | #define UBRRL UBRR0L
|
---|
| 14 | #define UCSRA UCSR0A
|
---|
| 15 | #define UCSRC UCSR0C
|
---|
| 16 | #define UCSRB UCSR0B
|
---|
| 17 | #define UDR UDR0
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[b82c7b3] | 20 | //This function is used to initialize the USART
|
---|
[cd3a9ac] | 21 | void USARTInit()
|
---|
[b82c7b3] | 22 | {
|
---|
[cd3a9ac] | 23 | //Set Baud rate
|
---|
| 24 | #include <util/setbaud.h>
|
---|
| 25 | UBRRH = UBRRH_VALUE;
|
---|
| 26 | UBRRL = UBRRL_VALUE;
|
---|
| 27 | #if USE_2X
|
---|
| 28 | UCSRA |= (1 << U2X);
|
---|
| 29 | #else
|
---|
| 30 | UCSRA &= ~(1 << U2X);
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
| 33 | /*Set Frame Format
|
---|
| 34 | >> Asynchronous mode
|
---|
| 35 | >> No Parity
|
---|
| 36 | >> 1 StopBit
|
---|
| 37 | >> char size 8
|
---|
| 38 | */
|
---|
| 39 | #ifdef __AVR_ATtiny2313__
|
---|
| 40 | UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
|
---|
[6390cc3] | 41 | #elif defined __AVR_ATmega128__
|
---|
[6c47e45] | 42 | UCSR0C = (2 << UCSZ0) | (2 << UPM0) ; /* 7 bits + even parity.
|
---|
| 43 | TODO make this configurable. */
|
---|
[cd3a9ac] | 44 | #else
|
---|
| 45 | UCSRC = (1 << URSEL) | (3 << UCSZ0);
|
---|
| 46 | #endif
|
---|
| 47 |
|
---|
| 48 | //Enable The receiver and transmitter
|
---|
| 49 | UCSRB = (1 << RXEN) | (1 << TXEN);
|
---|
[b82c7b3] | 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | //This fuction writes the given "data" to
|
---|
| 54 | //the USART which then transmit it via TX line
|
---|
| 55 | void USARTWriteChar(char data)
|
---|
| 56 | {
|
---|
| 57 | //Wait untill the transmitter is ready
|
---|
[6c47e45] | 58 | while(!(UCSR0A & (1<<UDRE0)))
|
---|
[b82c7b3] | 59 | {
|
---|
| 60 | //Do nothing
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | //Now write the data to USART buffer
|
---|
| 64 |
|
---|
| 65 | UDR=data;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | //This function is used to read the available data
|
---|
| 70 | //from USART. This function will wait untill data is
|
---|
| 71 | //available.
|
---|
| 72 | char USARTReadChar()
|
---|
| 73 | {
|
---|
| 74 | //Wait untill a data is available
|
---|
[6c47e45] | 75 | while(!(UCSR0A & (1<<RXC0)))
|
---|
[b82c7b3] | 76 | {
|
---|
| 77 | //Do nothing
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | //Now USART has got data from host
|
---|
| 81 | //and is available is buffer
|
---|
| 82 |
|
---|
[6c47e45] | 83 | return UDR0;
|
---|
[b82c7b3] | 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | void USARTWriteHex(unsigned char i)
|
---|
| 88 | {
|
---|
| 89 | unsigned char k = i>>4;
|
---|
| 90 | if (k <=9) k+='0';
|
---|
| 91 | else k+=('A'-10);
|
---|
| 92 |
|
---|
| 93 | USARTWriteChar(k);
|
---|
| 94 |
|
---|
| 95 | k = i&0xF;
|
---|
| 96 | if (k <=9) k+='0';
|
---|
| 97 | else k+=('A'-10);
|
---|
| 98 |
|
---|
| 99 | USARTWriteChar(k);
|
---|
| 100 | }
|
---|