2925 |
poskozby |
1 |
/* |
|
|
2 |
* uart.c |
|
|
3 |
* |
|
|
4 |
* Created: 29.11.2011 19:24:29 |
|
|
5 |
* Author: Zbynek |
|
|
6 |
* poskocil@email.cz |
|
|
7 |
* Funkce pro praci s USARTem - inicializace, prijem a odeslani znaku |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
#include <avr/io.h> |
|
|
11 |
#include <stdlib.h> |
|
|
12 |
#include "uart.h" |
|
|
13 |
#include "LED.h" |
|
|
14 |
#include "Globals.h" |
|
|
15 |
#include <avr/interrupt.h> |
|
|
16 |
#include <string.h> |
|
|
17 |
#include <util/delay.h> |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
/************************************************************************ |
|
|
23 |
* Funkce: SIGNAL |
|
|
24 |
* Ucel: Funkce se vola pri prijmuti znaku od USART0 |
|
|
25 |
* Vstup: Zdroj preruseni |
|
|
26 |
* Vystup: Zadny |
|
|
27 |
************************************************************************/ |
|
|
28 |
SIGNAL (USART_RX_vect) |
|
|
29 |
{ |
|
|
30 |
char znak = UDR0; |
|
|
31 |
if (RX0_buffer.zaplneno_bytu < VELIKOST_PRIJIMACIHO_BUFFERU-1) |
|
|
32 |
{ |
|
|
33 |
RX0_buffer.obsah[RX0_buffer.zaplneno_bytu] = znak; |
|
|
34 |
RX0_buffer.obsah[RX0_buffer.zaplneno_bytu+1] = 0; // konec stringu |
|
|
35 |
RX0_buffer.zaplneno_bytu++; |
|
|
36 |
} |
|
|
37 |
else |
|
|
38 |
{ |
|
|
39 |
USART0_smaz_buffer(); |
|
|
40 |
RX0_buffer.obsah[RX0_buffer.zaplneno_bytu] = znak; |
|
|
41 |
RX0_buffer.zaplneno_bytu++; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
if (zacatek_zpravy != 1 && strstr(RX0_buffer.obsah,"$GPGGA") != 0) |
|
|
45 |
{ |
|
|
46 |
zacatek_zpravy = 1; |
|
|
47 |
USART0_smaz_buffer(); // aby se zprava ukladala od zacatku bufferu |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
if (znak == '*' && zacatek_zpravy == 1) // cela zprava je prijata |
|
|
51 |
{ |
|
|
52 |
//USART0_posli_retezec(RX0_buffer.obsah); |
|
|
53 |
strcpy(NMEA_retezec.obsah,RX0_buffer.obsah); // NMEA zprava se prekopiruje do NMEA_retezec, aby pri pozadavku na vycteni dat byla data v kuse a nenastal pripad, ze bude NMEA zprava teprve vycitana. |
|
|
54 |
NMEA_retezec.zaplneno_bytu = RX0_buffer.zaplneno_bytu; |
|
|
55 |
zacatek_zpravy = 0; |
|
|
56 |
} |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
/************************************************************************ |
|
|
63 |
* Funkce: dekoduj_zpravu_GPS |
|
|
64 |
* Ucel: Z RX0_buffer vytahne informace o soucasne poloze, je tam ulozena cela veta RMC |
|
|
65 |
* Vstup: Zadny |
|
|
66 |
* Vystup: Zadny |
|
|
67 |
************************************************************************/ |
|
|
68 |
void dekoduj_zpravu_GPS (void) |
|
|
69 |
{ |
|
|
70 |
cli(); |
|
|
71 |
|
|
|
72 |
short i, j = 0, k = 0, l = 0; |
|
|
73 |
short pocet_carek = 0; |
|
|
74 |
for ( i = 0; i < NMEA_retezec.zaplneno_bytu; i++) |
|
|
75 |
{ |
|
|
76 |
if (pocet_carek == 1) |
|
|
77 |
{ |
|
|
78 |
UTC_time[l] = NMEA_retezec.obsah[i]; |
|
|
79 |
l++; |
|
|
80 |
} |
|
|
81 |
if (pocet_carek == 2 || pocet_carek == 3) |
|
|
82 |
{ |
|
|
83 |
Latitude[k] = NMEA_retezec.obsah[i]; |
|
|
84 |
k++; |
|
|
85 |
} |
|
|
86 |
if (pocet_carek == 4 || pocet_carek == 5) |
|
|
87 |
{ |
|
|
88 |
|
|
|
89 |
Longitude[j] = NMEA_retezec.obsah[i]; |
|
|
90 |
j++; |
|
|
91 |
} |
|
|
92 |
if (pocet_carek == 6) |
|
|
93 |
{ |
|
|
94 |
UTC_time[l-1] = 0; |
|
|
95 |
Latitude[k-1] = 0; |
|
|
96 |
Longitude[j-1] = 0; |
|
|
97 |
|
|
|
98 |
Status_GPS = NMEA_retezec.obsah[i-1]; |
|
|
99 |
j = 0; |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
if (pocet_carek == 9) |
|
|
103 |
{ |
|
|
104 |
Altitude[j] = NMEA_retezec.obsah[i]; |
|
|
105 |
j++; |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
if (NMEA_retezec.obsah[i] == ',') |
|
|
109 |
{ |
|
|
110 |
pocet_carek++; |
|
|
111 |
} |
|
|
112 |
if (pocet_carek >9) |
|
|
113 |
{ |
|
|
114 |
Altitude[j-1] = 0; // konec Stringu |
|
|
115 |
break; // nema vyznam dal zapisovat, potrebne je zapsano v Altitude |
|
|
116 |
} |
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
sei(); |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
/************************************************************************ |
|
|
123 |
* Funkce: USART0_smaz_buffer |
|
|
124 |
* Ucel: Smaze (zaplni nulami) prijimaci buffer |
|
|
125 |
* Vstup: Jaky buffer ma smazat (pro USART 0 nebo 1) |
|
|
126 |
* Vystup: Zadny |
|
|
127 |
************************************************************************/ |
|
|
128 |
void USART0_smaz_buffer (void) |
|
|
129 |
{ |
|
|
130 |
cli(); |
|
|
131 |
unsigned char i; |
|
|
132 |
|
|
|
133 |
for (i=0; i <= VELIKOST_PRIJIMACIHO_BUFFERU;i++) // maze se cely buffer pro jistotu |
|
|
134 |
{ |
|
|
135 |
RX0_buffer.obsah[i] = 0; |
|
|
136 |
} |
|
|
137 |
RX0_buffer.zaplneno_bytu = 0; |
|
|
138 |
|
|
|
139 |
|
|
|
140 |
sei(); |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
/************************************************************************ |
|
|
144 |
* Funkce: USART_Init() |
|
|
145 |
* Ucel: Inicializuje oba USARTy a nastavi rychslost v baud, je treba definovat pozadovanou rychlost pomoci #define UARTn_BAUD_RATE |
|
|
146 |
* Zapne preruseni pri prichodu noveho znaku a globalni preruseni |
|
|
147 |
* Vstup: Frekvence procesoru |
|
|
148 |
* Vystup: Zadny |
|
|
149 |
************************************************************************/ |
|
|
150 |
void USART_Init(void) |
|
|
151 |
{ |
|
|
152 |
unsigned int baud; |
|
|
153 |
baud = F_CPU/16/UART0_BAUD_RATE -1; |
|
|
154 |
/* Set baud rate */ |
|
|
155 |
UBRR0H = (unsigned char)(baud>>8); |
|
|
156 |
UBRR0L = (unsigned char)baud; |
|
|
157 |
/* Enable receiver and transmitter, enable interrupt after receiving a new byte */ |
|
|
158 |
UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); |
|
|
159 |
/* Set frame format: 8data, 1stop bit */ |
|
|
160 |
UCSR0C = (3<<UCSZ00); |
|
|
161 |
|
|
|
162 |
USART0_posli_znak('A'); |
|
|
163 |
USART0_posli_znak('K'); |
|
|
164 |
USART0_posli_znak('\n'); |
|
|
165 |
USART0_posli_znak('\r'); |
|
|
166 |
//delay_ms(3000); // modulu se musi dat cas na vypocet baud rychlosti |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
/************************************************************************ |
|
|
170 |
* Funkce: USART0_posli_znak() |
|
|
171 |
* Ucel: Posle znak po seriove lince, hned pote, co se uvolni vysilaci buffer |
|
|
172 |
* Vstup: Znak, ktery se ma odeslat |
|
|
173 |
* Vystup: Zadny |
|
|
174 |
************************************************************************/ |
|
|
175 |
void USART0_posli_znak( unsigned char data ) |
|
|
176 |
{ |
|
|
177 |
/* Wait for empty transmit buffer */ |
|
|
178 |
while ( !( UCSR0A & (1<<UDRE0)) ) |
|
|
179 |
; |
|
|
180 |
/* Put data into buffer, sends the data */ |
|
|
181 |
UDR0 = data; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
/************************************************************************ |
|
|
185 |
* Funkce: USART0_posli_retezec() |
|
|
186 |
* Ucel: Odesle postupne znak po znaku zadany retezec pres USART0 |
|
|
187 |
* Vstup: Retezec k odeslani |
|
|
188 |
* Vystup: Zadny |
|
|
189 |
************************************************************************/ |
|
|
190 |
void USART0_posli_retezec (const unsigned char *retezec ) |
|
|
191 |
{ |
|
|
192 |
while (*retezec) |
|
|
193 |
USART0_posli_znak(*retezec++); |
|
|
194 |
|
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
/************************************************************************ |
|
|
198 |
* Funkce: USART0_prijmi_znak() |
|
|
199 |
* Ucel: Prijme znak ze seriove linky, pokud je nejaky neprecteny znak |
|
|
200 |
* Vstup: Zadny |
|
|
201 |
* Vystup: Prijaty znak |
|
|
202 |
************************************************************************/ |
|
|
203 |
unsigned char USART0_prijmi_znak( void ) |
|
|
204 |
{ |
|
|
205 |
/* Wait for data to be received */ |
|
|
206 |
while ( !(UCSR0A & (1<<RXC0)) ) |
|
|
207 |
; |
|
|
208 |
/* Get and return received data from buffer */ |
|
|
209 |
return UDR0; |
|
|
210 |
} |