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 | #ifndef UART_H |
||
24 | #define UART_H |
||
25 | #include <avr/io.h> |
||
26 | #include <avr/interrupt.h> |
||
27 | #include <stdio.h> |
||
28 | |||
29 | |||
30 | #ifndef F_CPU |
||
31 | /* In neueren Version der WinAVR/Mfile Makefile-Vorlage kann |
||
32 | F_CPU im Makefile definiert werden, eine nochmalige Definition |
||
33 | hier wuerde zu einer Compilerwarnung fuehren. Daher "Schutz" durch |
||
34 | #ifndef/#endif |
||
35 | |||
36 | Dieser "Schutz" kann zu Debugsessions führen, wenn AVRStudio |
||
37 | verwendet wird und dort eine andere, nicht zur Hardware passende |
||
38 | Taktrate eingestellt ist: Dann wird die folgende Definition |
||
39 | nicht verwendet, sondern stattdessen der Defaultwert (8 MHz?) |
||
40 | von AVRStudio - daher Ausgabe einer Warnung falls F_CPU |
||
41 | noch nicht definiert: */ |
||
42 | #warning "F_CPU war noch nicht definiert, wird nun nachgeholt mit 4000000" |
||
43 | #define F_CPU 4000000UL // Systemtakt in Hz - Definition als unsigned long beachten |
||
44 | // Ohne ergeben sich unten Fehler in der Berechnung |
||
45 | #endif |
||
46 | |||
47 | #define BAUD 9600UL // Baudrate |
||
48 | |||
49 | // Berechnungen |
||
50 | #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden |
||
51 | #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate |
||
52 | #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler. |
||
53 | |||
54 | #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010)) |
||
55 | #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch! |
||
56 | #endif |
||
57 | |||
58 | |||
59 | int put_uart_char(char c, FILE *fd); |
||
60 | |||
61 | //No need to call this function directly, it's just here for printf(). |
||
62 | //Outputs a single character to the UART when the transmit buffer is empty. |
||
63 | //This is the function that we bind to printf so it can output the string |
||
64 | //that printf generates. The FILE parameter is required by printf for behind- |
||
65 | //the-scenes bookkeeping. *Note that we can write a function that will put a |
||
66 | //single char anywhere we want -- parallel interface, SPI, I2C, etc.* |
||
67 | |||
68 | void uart_init(void); |
||
69 | |||
70 | //Set up the registers that control the UART and bind put_uart_char |
||
71 | //as our output function. |
||
72 | |||
73 | #endif |