1 | /*************************************************************************
|
2 | Title: example program for the Interrupt controlled UART library
|
3 | Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
|
4 | File: $Id: test_uart.c,v 1.5 2012/09/14 17:59:08 peter Exp $
|
5 | Software: AVR-GCC 3.4, AVRlibc 1.4
|
6 | Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz
|
7 |
|
8 | DESCRIPTION:
|
9 | This example shows how to use the UART library uart.c
|
10 |
|
11 | *************************************************************************/
|
12 | #include <stdlib.h>
|
13 | #include <avr/io.h>
|
14 | #include <avr/interrupt.h>
|
15 | #include <avr/pgmspace.h>
|
16 | #include <util/delay.h>
|
17 |
|
18 | #include "uart.h"
|
19 |
|
20 |
|
21 | /* define CPU frequency in Mhz here if not defined in Makefile */
|
22 | #ifndef F_CPU
|
23 | #define F_CPU 4000000UL
|
24 | #endif
|
25 |
|
26 | /* 9600 baud */
|
27 | #define UART_BAUD_RATE 9600
|
28 |
|
29 |
|
30 | int main(void)
|
31 | {
|
32 | unsigned int c;
|
33 | char buffer[7];
|
34 | int num=134;
|
35 |
|
36 | lcd_init(); // lcd init
|
37 | vWillkommen(); // Begrüssungstext
|
38 | _delay_ms(2000); // warte, damit Anzeige nicht gleich weg ist
|
39 | DDRA = 0xFF; // PORTA Ausgang
|
40 | PORTA = 0xFF; // Alle auf EIN
|
41 |
|
42 |
|
43 | /*
|
44 | * Initialize UART library, pass baudrate and AVR cpu clock
|
45 | * with the macro
|
46 | * UART_BAUD_SELECT() (normal speed mode )
|
47 | * or
|
48 | * UART_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode)
|
49 | */
|
50 | uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
51 |
|
52 | /*
|
53 | * now enable interrupt, since UART library is interrupt controlled
|
54 | */
|
55 | sei();
|
56 |
|
57 | /*
|
58 | * Transmit string to UART
|
59 | * The string is buffered by the uart library in a circular buffer
|
60 | * and one character at a time is transmitted to the UART using interrupts.
|
61 | * uart_puts() blocks if it can not write the whole string to the circular
|
62 | * buffer
|
63 | */
|
64 | uart_puts("String stored in SRAM\n");
|
65 |
|
66 | /*
|
67 | * Transmit string from program memory to UART
|
68 | */
|
69 | uart_puts_P("String stored in FLASH\n");
|
70 |
|
71 |
|
72 | /*
|
73 | * Use standard avr-libc functions to convert numbers into string
|
74 | * before transmitting via UART
|
75 | */
|
76 | itoa( num, buffer, 10); // convert interger into string (decimal format)
|
77 | uart_puts(buffer); // and transmit string to UART
|
78 |
|
79 |
|
80 | /*
|
81 | * Transmit single character to UART
|
82 | */
|
83 | uart_putc('\r');
|
84 |
|
85 | for(;;)
|
86 | {
|
87 | /*
|
88 | * Get received character from ringbuffer
|
89 | * uart_getc() returns in the lower byte the received character and
|
90 | * in the higher byte (bitmask) the last receive error
|
91 | * UART_NO_DATA is returned when no data is available.
|
92 | *
|
93 | */
|
94 | c = uart_getc();
|
95 | if ( c & UART_NO_DATA )
|
96 | {
|
97 | /*
|
98 | * no data available from UART
|
99 | */
|
100 | // hier etwas mit c machen z.B. auf PORT ausgeben
|
101 |
|
102 | }
|
103 | else
|
104 | {
|
105 | /*
|
106 | * new data available from UART
|
107 | * check for Frame or Overrun error
|
108 | */
|
109 | if ( c & UART_FRAME_ERROR )
|
110 | {
|
111 | /* Framing Error detected, i.e no stop bit detected */
|
112 | uart_puts_P("UART Frame Error: ");
|
113 | }
|
114 | if ( c & UART_OVERRUN_ERROR )
|
115 | {
|
116 | /*
|
117 | * Overrun, a character already present in the UART UDR register was
|
118 | * not read by the interrupt handler before the next character arrived,
|
119 | * one or more received characters have been dropped
|
120 | */
|
121 | uart_puts_P("UART Overrun Error: ");
|
122 | }
|
123 | if ( c & UART_BUFFER_OVERFLOW )
|
124 | {
|
125 | /*
|
126 | * We are not reading the receive buffer fast enough,
|
127 | * one or more received character have been dropped
|
128 | */
|
129 | uart_puts_P("Buffer overflow error: ");
|
130 |
|
131 |
|
132 | }
|
133 | /*
|
134 | * send received character back
|
135 | */
|
136 | uart_putc( (unsigned char)c );
|
137 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
138 | // ab hier mein Teil der Programmierung
|
139 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
140 | ID_Werte(&c); //Daten des ID Tag's auf dem LCD ausgeben *-*-*-*-*-*-*-*-*-*-*-*-*-*
|
141 |
|
142 | /* void ID_Werte(char *wert)
|
143 | {
|
144 | lcd_clear();
|
145 | // itoa(WERT,Value,10); // umwandeln einer dez.-Zahl (WERT zB 11) in ASCII-Zeichen 11 (LCD kann nur ASCII-Zeichen ausgeben!!!)
|
146 | // itoa(-12,Value,10); // umwandeln der Zahl -12 in ASCII -12 (LCD kann nur ASCII-Zeichen ausgeben!!!)
|
147 | lcd_pos(2,0);
|
148 | lcd_text("ID-Wert:");
|
149 | lcd_pos(3,10);
|
150 | lcd_text(wert);
|
151 | }
|
152 | */
|
153 | //+++++++++++++++++++++++++++++ ENDE meiner Programmierung +++++++++++++++++++++++++++++++++
|
154 | }
|
155 | }
|
156 |
|
157 | }
|