1 | void uart1_init_x(uint32_t baudrate, uint8_t send, uint8_t receive, uint8_t numDatabits, uint8_t parity, uint8_t uartIntTx, uint8_t uartIntRx, uint8_t numStopbits)
|
2 | {
|
3 | UBRR1 = FOSZ/16/baudrate - 1;
|
4 |
|
5 | if(send)
|
6 | UCSR1B |= (1<<TXEN1); //activate sender
|
7 | if(receive)
|
8 | UCSR1B |= (1<<RXEN1); //activate receiver
|
9 |
|
10 | if(numDatabits >= 5 && numDatabits <= 8)
|
11 | {
|
12 | UCSR1C &= ~( (1<<UCSZ11) | (1<<UCSZ10));
|
13 | UCSR1C |= ((numDatabits - 5)<<UCSZ10); //choose 5, 6, 7 or 8 data bits, //1 stop bit
|
14 | }
|
15 | else
|
16 | {
|
17 | UCSR1C |= (3<<UCSZ10); //set 8 databits (if numDatabits is invalid)
|
18 | if(numDatabits == 9)
|
19 | UCSR1B |= (1<<UCSZ12); //set 9 databits
|
20 | }
|
21 |
|
22 | //check for even (2) or odd (3) parity
|
23 | if((parity == 2) || (parity == 3))
|
24 | UCSR1C |= (parity << UPM10);
|
25 |
|
26 | if(uartIntTx)
|
27 | {
|
28 | UCSR1B |= (1<<TXCIE1);
|
29 | sei();
|
30 | }
|
31 |
|
32 | if(uartIntRx)
|
33 | {
|
34 | UCSR1B |= (1<<RXCIE1);
|
35 | sei();
|
36 | }
|
37 |
|
38 | sendReady = 1;
|
39 |
|
40 | if(numStopbits==2)
|
41 | UCSR1C |=(1<<USBS1);
|
42 | }
|
43 |
|
44 | //sends one character via the uart
|
45 | void uart1_putc(uint8_t c)
|
46 | {
|
47 | while ( (UCSR1A & (1<<UDRE1)) == 0); // wait until transmit buffer is empty
|
48 | UDR1 = c;
|
49 | }
|
50 |
|
51 | uint8_t uart1_getc()
|
52 | {
|
53 | uint8_t recdata;
|
54 |
|
55 | // wait until data have been received
|
56 | while( (UCSR1A & (1<<RXC1)) == 0);
|
57 |
|
58 | // get data from the uart
|
59 | recdata = UDR1;
|
60 |
|
61 | return recdata;
|
62 | }
|