Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
964 | - | 1 | /**************************************************************************** |
2 | UART INITIALIZATION |
||
3 | |||
4 | This Program enables you to send data from the ATmega32 serial port to |
||
5 | the PC so that the data can be read on hyperterminal. This is very |
||
6 | useful for debugging purposes when the development system has no |
||
7 | display of its own. |
||
8 | |||
9 | To send data through the UART printf() is used. |
||
10 | |||
11 | uart.c and uart.h must be included in the main project, and the function |
||
12 | uart_init() called to enable serial port output. |
||
13 | |||
14 | In order to print floating point values, you will need to add the line |
||
15 | "-Wl,-u,vfprintf -lprintf_flt -lm" to AVR Studio4 linker options by going to |
||
16 | Project->Configuration Options->Custom Options |
||
17 | |||
18 | Read the "Detailed Description" section of |
||
19 | http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html for |
||
20 | more detail. |
||
21 | |||
22 | *******************************************************************************/ |
||
23 | #include <avr/io.h> |
||
24 | #include <avr/interrupt.h> |
||
25 | #include <stdio.h> |
||
26 | #include "uart.h" |
||
27 | |||
28 | int put_uart_char(char c, FILE *fd) |
||
29 | { |
||
30 | while (!(UCSRA & _BV(UDRE))); //Wait for transmit buffer to become empty. |
||
31 | UDR = c; //Write the character to the UART. |
||
32 | return 0; |
||
33 | } |
||
34 | |||
35 | void uart_init(void) |
||
36 | { |
||
37 | //UART Init |
||
38 | |||
39 | UCSRB |= (1<<TXEN); // UART TX einschalten |
||
40 | UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // Asynchron 8N1 |
||
41 | |||
42 | UBRRH = UBRR_VAL >> 8; |
||
43 | UBRRL = UBRR_VAL & 0xFF; |
||
44 | |||
45 | |||
46 | |||
47 | //Call fdevopen. This is what binds printf to put_uart_char. The first |
||
48 | //parameter is the address of our function that will output a single |
||
49 | //character, the second parameter is an optional parameter that is |
||
50 | //used for get functions, ie. receiving a character from the UART. |
||
51 | //This is the function that uses malloc. |
||
52 | |||
53 | fdevopen(&put_uart_char, NULL); |
||
54 | |||
55 | //Enable the interrupts globally. |
||
56 | |||
57 | SREG |= _BV(SREG_I); |
||
58 | } |
||
59 | |||
60 | |||
61 | |||
62 | |||
63 |