1 | #ifndef UARTInit
|
2 | #define UARTInit
|
3 |
|
4 | #define EVEN 0
|
5 | #define ODD 1
|
6 |
|
7 |
|
8 | #include <math.h>
|
9 | #include <avr/io.h>
|
10 |
|
11 | unsigned char ReceiveUART0(void)
|
12 | {
|
13 | while (! (UCSR0A & (1 << RXC0)) );
|
14 |
|
15 | return (UDR0);
|
16 |
|
17 | }
|
18 |
|
19 | void TransmitUART0(unsigned char data)
|
20 | {
|
21 | //Wait until the Transmitter is ready
|
22 | while (! (UCSR0A & (1 << UDRE0)) );
|
23 |
|
24 | //Get that data outa here!
|
25 | UDR0 = data;
|
26 | }
|
27 |
|
28 | void InitializeUART0(int baud, char AsyncDoubleSpeed, char DataSizeInBits, char ParityEVENorODD, char StopBits, char USARTInterruptEnable)
|
29 | {
|
30 | uint16_t UBBRValue = lrint ((F_CPU / (16UL * baud) ) -1);
|
31 |
|
32 | if (AsyncDoubleSpeed == 1) UCSR0A = (1 << U2X0); //setting the U2X bit to 1 for double speed asynchronous
|
33 | //UCSRA &= ~(1 << U2X); //setting the U2X bit to 0 for normal asynchronous
|
34 |
|
35 | //Put the upper part of the baud number here (bits 8 to 11)
|
36 | UBRR0H = (unsigned char) (UBBRValue >> 8);
|
37 |
|
38 | //Put the remaining part of the baud number here
|
39 | UBRR0L = (unsigned char) UBBRValue;
|
40 |
|
41 | //Enable the receiver and transmitter
|
42 | UCSR0B = (1 << RXEN0) | (1 << TXEN0) |(1<<RXCIE0);
|
43 |
|
44 | //Enable interrupt
|
45 | //if USARTinteruptEnable
|
46 |
|
47 | if (USARTInterruptEnable) UCSR0B |= (1<<RXCIE0);
|
48 |
|
49 | //Set 2 stop bits
|
50 | if (StopBits == 2) UCSR0C = (1 << USBS0);
|
51 |
|
52 |
|
53 | if (ParityEVENorODD == EVEN) UCSR0C |= (1 << UPM01); //Sets parity to EVEN
|
54 | if (ParityEVENorODD == ODD) UCSR0C |= (3 << UPM00); //Alternative way to set parity to ODD
|
55 |
|
56 | if (DataSizeInBits == 6) UCSR0C |= (1 << UCSZ00); //6-bit data length
|
57 | if (DataSizeInBits == 7) UCSR0C |= (2 << UCSZ00); //7-bit data length, or
|
58 | if (DataSizeInBits == 8) UCSR0C |= (3 << UCSZ00); //8-bit data length, or
|
59 | if (DataSizeInBits == 9) UCSR0C |= (7 << UCSZ00); //9-bit data length
|
60 | }
|
61 |
|
62 | void InitializeUART1(int baud, char AsyncDoubleSpeed, char DataSizeInBits, char ParityEVENorODD, char StopBits, char USARTInterruptEnable )
|
63 | {
|
64 | uint16_t UBBRValue = lrint ((F_CPU / (16L * baud) ) -1);
|
65 |
|
66 | if (AsyncDoubleSpeed == 1) UCSR0A = (1 << U2X0); //setting the U2X bit to 1 for double speed asynchronous
|
67 | //UCSRA &= ~(1 << U2X); //setting the U2X bit to 0 for normal asynchronous
|
68 |
|
69 | //Put the upper part of the baud number here (bits 8 to 11)
|
70 | UBRR1H = (unsigned char) (UBBRValue >> 8);
|
71 |
|
72 | //Put the remaining part of the baud number here
|
73 | UBRR1L = (unsigned char) UBBRValue;
|
74 |
|
75 | //Enable the receiver and transmitter
|
76 | UCSR1B = (1 << RXEN1) | (1 << TXEN1);
|
77 |
|
78 | //Enable interrupt
|
79 | //if USARTinteruptEnable
|
80 |
|
81 | if (USARTInterruptEnable) UCSR1B |= (1<<RXCIE1);
|
82 |
|
83 | //Set 2 stop bits
|
84 | if (StopBits == 2) UCSR1C = (1 << USBS1);
|
85 |
|
86 |
|
87 | if (ParityEVENorODD == EVEN) UCSR1C |= (1 << UPM11); //Sets parity to EVEN
|
88 | if (ParityEVENorODD == ODD) UCSR1C |= (3 << UPM10); //Alternative way to set parity to ODD
|
89 |
|
90 | if (DataSizeInBits == 6) UCSR1C |= (1 << UCSZ10); //6-bit data length
|
91 | if (DataSizeInBits == 7) UCSR1C |= (2 << UCSZ10); //7-bit data length, or
|
92 | if (DataSizeInBits == 8) UCSR1C |= (3 << UCSZ10); //8-bit data length, or
|
93 | if (DataSizeInBits == 9) UCSR1C |= (7 << UCSZ10); //9-bit data length
|
94 | }
|
95 |
|
96 | #endif
|