Rev 1 | Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Regler für Brushless-Motoren |
||
3 | // + ATMEGA8 mit 8MHz |
||
4 | // + (c) 01.2007 Holger Buss |
||
5 | // + Nur für den privaten Gebrauch |
||
6 | // + Keine Garantie auf Fehlerfreiheit |
||
7 | // + Kommerzielle Nutzung nur mit meiner Zustimmung |
||
8 | // + Der Code ist für die Hardware BL_Ctrl V1.0 entwickelt worden |
||
9 | // + www.mikrocontroller.com |
||
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
11 | |||
12 | #include "main.h" |
||
13 | #include "uart.h" |
||
14 | |||
15 | #define MAX_SENDE_BUFF 100 |
||
16 | #define MAX_EMPFANGS_BUFF 100 |
||
17 | |||
18 | unsigned volatile char SIO_Sollwert = 0; |
||
19 | unsigned volatile char SioTmp = 0; |
||
20 | unsigned volatile char SendeBuffer[MAX_SENDE_BUFF]; |
||
21 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
||
22 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
||
23 | unsigned volatile char UebertragungAbgeschlossen = 1; |
||
24 | unsigned char GetVersionAnforderung = 0,DebugTextAnforderung = 0,DebugGetAnforderung = 0, KompassAntwort = 0; |
||
25 | unsigned char MeineSlaveAdresse; |
||
26 | unsigned char MotorTest[4] = {0,0,0,0}; |
||
27 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
||
28 | unsigned char PcZugriff; |
||
29 | |||
30 | struct str_DebugOut DebugOut; |
||
31 | struct str_WinkelOut WinkelOut; |
||
32 | struct str_ExternControl ExternControl; |
||
33 | struct str_VersionInfo VersionInfo; |
||
34 | |||
35 | |||
3 | ingob | 36 | |
1 | ingob | 37 | int Debug_Timer; |
38 | |||
39 | const unsigned char ANALOG_TEXT[32][16] = |
||
40 | { |
||
41 | //1234567890123456 |
||
42 | "Magnet N ", //0 |
||
43 | "Magnet R ", |
||
44 | "Magnet Z ", |
||
45 | "Roh N ", |
||
46 | "Roh R ", |
||
47 | "Roh Z ", //5 |
||
48 | "Lage N ", |
||
49 | "Lage R ", |
||
50 | " ", |
||
51 | "Spannung ", |
||
52 | "Empfang ", //10 |
||
53 | "Analog11 ", |
||
54 | "Motor_Vorne ", |
||
55 | "Motor_Hinten ", |
||
56 | "Kaompass ", |
||
57 | "Motor_Rechts ", //15 |
||
58 | "Acc_Z ", |
||
59 | "MittelAccNick ", |
||
60 | "MittelAccRoll ", |
||
61 | "IntegralErrNick ", |
||
62 | "IntegralErrRoll ", //20 |
||
63 | "MittelIntNick ", |
||
64 | "MittelIntRoll ", |
||
65 | "NeutralNick ", |
||
66 | "RollOffset ", |
||
67 | "IntRoll*Faktor ", //25 |
||
68 | "Analog26 ", |
||
69 | "DirektAusglRoll ", |
||
70 | "MesswertRoll ", |
||
71 | "AusgleichRoll ", |
||
72 | "I-LageRoll ", //30 |
||
73 | "StickRoll " |
||
74 | }; |
||
75 | |||
76 | |||
77 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
78 | //++ Sende-Part der Datenübertragung |
||
79 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
80 | SIGNAL(INT_VEC_TX) |
||
81 | { |
||
82 | static unsigned int ptr = 0; |
||
83 | unsigned char tmp_tx; |
||
84 | if(!UebertragungAbgeschlossen) |
||
85 | { |
||
86 | ptr++; // die [0] wurde schon gesendet |
||
87 | tmp_tx = SendeBuffer[ptr]; |
||
88 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
||
89 | { |
||
90 | ptr = 0; |
||
91 | UebertragungAbgeschlossen = 1; |
||
92 | } |
||
93 | UDR = tmp_tx; |
||
94 | } |
||
95 | else ptr = 0; |
||
96 | } |
||
97 | |||
98 | void SendUart(void) |
||
99 | { |
||
100 | static unsigned int ptr = 0; |
||
101 | unsigned char tmp_tx; |
||
102 | if(!(USR & 0x40)) return; |
||
103 | if(!UebertragungAbgeschlossen) |
||
104 | { |
||
105 | ptr++; // die [0] wurde schon gesendet |
||
106 | tmp_tx = SendeBuffer[ptr]; |
||
107 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
||
108 | { |
||
109 | ptr = 0; |
||
110 | UebertragungAbgeschlossen = 1; |
||
111 | } |
||
112 | USR |= (1<TXC0); |
||
113 | UDR = tmp_tx; |
||
114 | } |
||
115 | else ptr = 0; |
||
116 | } |
||
117 | |||
118 | // -------------------------------------------------------------------------- |
||
119 | void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max) // Wohin mit den Daten; Wie lang; Wo im RxdBuffer |
||
120 | { |
||
121 | unsigned char a,b,c,d; |
||
122 | unsigned char ptr = 0; |
||
123 | unsigned char x,y,z; |
||
124 | while(len) |
||
125 | { |
||
126 | a = RxdBuffer[ptrIn++] - '='; |
||
127 | b = RxdBuffer[ptrIn++] - '='; |
||
128 | c = RxdBuffer[ptrIn++] - '='; |
||
129 | d = RxdBuffer[ptrIn++] - '='; |
||
130 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
131 | |||
132 | x = (a << 2) | (b >> 4); |
||
133 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
134 | z = ((c & 0x03) << 6) | d; |
||
135 | |||
136 | if(len--) ptrOut[ptr++] = x; else break; |
||
137 | if(len--) ptrOut[ptr++] = y; else break; |
||
138 | if(len--) ptrOut[ptr++] = z; else break; |
||
139 | } |
||
140 | |||
141 | } |
||
142 | |||
143 | |||
144 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
145 | //++ Empfangs-Part der Datenübertragung |
||
146 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
147 | SIGNAL(INT_VEC_RX) |
||
148 | { |
||
149 | static unsigned int crc; |
||
150 | static unsigned char crc1,crc2,buf_ptr; |
||
151 | static unsigned char UartState = 0; |
||
152 | unsigned char CrcOkay = 0; |
||
153 | |||
154 | SioTmp = UDR; |
||
155 | if(buf_ptr >= MAX_EMPFANGS_BUFF) UartState = 0; |
||
156 | if(SioTmp == '\r' && UartState == 2) |
||
157 | { |
||
158 | UartState = 0; |
||
159 | crc -= RxdBuffer[buf_ptr-2]; |
||
160 | crc -= RxdBuffer[buf_ptr-1]; |
||
161 | crc %= 4096; |
||
162 | crc1 = '=' + crc / 64; |
||
163 | crc2 = '=' + crc % 64; |
||
164 | CrcOkay = 0; |
||
165 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; }; |
||
166 | if(CrcOkay) // Datensatz schon verarbeitet |
||
167 | { |
||
168 | NeuerDatensatzEmpfangen = 1; |
||
169 | AnzahlEmpfangsBytes = buf_ptr; |
||
170 | RxdBuffer[buf_ptr] = '\r'; |
||
171 | if((RxdBuffer[2] == 'R')) wdt_enable(WDTO_250MS); // Reset-Commando |
||
172 | // uart_putchar(RxdBuffer[2]); |
||
173 | } |
||
174 | } |
||
175 | else |
||
176 | switch(UartState) |
||
177 | { |
||
178 | case 0: |
||
179 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
||
180 | buf_ptr = 0; |
||
181 | RxdBuffer[buf_ptr++] = SioTmp; |
||
182 | crc = SioTmp; |
||
183 | break; |
||
184 | case 1: // Adresse auswerten |
||
185 | UartState++; |
||
186 | RxdBuffer[buf_ptr++] = SioTmp; |
||
187 | crc += SioTmp; |
||
188 | break; |
||
189 | case 2: // Eingangsdaten sammeln |
||
190 | RxdBuffer[buf_ptr] = SioTmp; |
||
191 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
||
192 | else UartState = 0; |
||
193 | crc += SioTmp; |
||
194 | break; |
||
195 | default: |
||
196 | UartState = 0; |
||
197 | break; |
||
198 | } |
||
199 | |||
200 | }; |
||
201 | |||
202 | |||
203 | // -------------------------------------------------------------------------- |
||
204 | void AddCRC(unsigned int wieviele) |
||
205 | { |
||
206 | unsigned int tmpCRC = 0,i; |
||
207 | for(i = 0; i < wieviele;i++) |
||
208 | { |
||
209 | tmpCRC += SendeBuffer[i]; |
||
210 | } |
||
211 | tmpCRC %= 4096; |
||
212 | SendeBuffer[i++] = '=' + tmpCRC / 64; |
||
213 | SendeBuffer[i++] = '=' + tmpCRC % 64; |
||
214 | SendeBuffer[i++] = '\r'; |
||
215 | UebertragungAbgeschlossen = 0; |
||
216 | UDR = SendeBuffer[0]; |
||
217 | } |
||
218 | |||
219 | |||
220 | // -------------------------------------------------------------------------- |
||
221 | void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len) |
||
222 | { |
||
223 | unsigned int pt = 0; |
||
224 | unsigned char a,b,c; |
||
225 | unsigned char ptr = 0; |
||
226 | |||
227 | SendeBuffer[pt++] = '#'; // Startzeichen |
||
228 | SendeBuffer[pt++] = modul; // Adresse (a=0; b=1,...) |
||
229 | SendeBuffer[pt++] = cmd; // Commando |
||
230 | |||
231 | while(len) |
||
232 | { |
||
233 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
||
234 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
||
235 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
||
236 | SendeBuffer[pt++] = '=' + (a >> 2); |
||
237 | SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
238 | SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
239 | SendeBuffer[pt++] = '=' + ( c & 0x3f); |
||
240 | } |
||
241 | AddCRC(pt); |
||
242 | } |
||
243 | |||
244 | |||
245 | |||
246 | //############################################################################ |
||
247 | //Routine für die Serielle Ausgabe |
||
248 | int uart_putchar (char c) |
||
249 | //############################################################################ |
||
250 | { |
||
251 | if (c == '\n') |
||
252 | uart_putchar('\r'); |
||
253 | //Warten solange bis Zeichen gesendet wurde |
||
254 | loop_until_bit_is_set(USR, UDRE); |
||
255 | //Ausgabe des Zeichens |
||
256 | UDR = c; |
||
257 | |||
258 | return (0); |
||
259 | } |
||
260 | |||
261 | // -------------------------------------------------------------------------- |
||
262 | void WriteProgramData(unsigned int pos, unsigned char wert) |
||
263 | { |
||
264 | } |
||
265 | |||
266 | //############################################################################ |
||
267 | //INstallation der Seriellen Schnittstelle |
||
268 | void UART_Init (void) |
||
269 | //############################################################################ |
||
270 | { |
||
271 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
||
272 | |||
273 | UCR=(1 << TXEN) | (1 << RXEN); |
||
274 | // UART Double Speed (U2X) |
||
275 | USR |= (1<<U2X); |
||
276 | // RX-Interrupt Freigabe |
||
277 | |||
278 | UCSRB |= (1<<RXCIE); // serieller Empfangsinterrupt |
||
279 | |||
280 | // TX-Interrupt Freigabe |
||
281 | UCSRB |= (1<<TXCIE); |
||
282 | |||
283 | //Teiler wird gesetzt |
||
284 | UBRR= (SYSCLK / (BAUD_RATE * 8L) -1 ); |
||
285 | //öffnet einen Kanal für printf (STDOUT) |
||
286 | // fdevopen (uart_putchar, NULL); |
||
287 | Debug_Timer = SetDelay(200); |
||
288 | // Version beim Start ausgeben (nicht schön, aber geht... ) |
||
289 | uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':'); |
||
290 | uart_putchar ('V');uart_putchar (0x30 + VERSION_HAUPTVERSION);uart_putchar ('.');uart_putchar (0x30 + VERSION_NEBENVERSION/10); uart_putchar (0x30 + VERSION_NEBENVERSION%10); |
||
291 | uart_putchar ('\n'); |
||
292 | } |
||
293 | |||
294 | |||
295 | void BearbeiteRxDaten(void) |
||
296 | { |
||
297 | if(!NeuerDatensatzEmpfangen) return; |
||
298 | // unsigned int tmp_int_arr1[1]; |
||
299 | // unsigned int tmp_int_arr2[2]; |
||
300 | // unsigned int tmp_int_arr3[3]; |
||
301 | unsigned char tmp_char_arr2[2]; |
||
302 | // unsigned char tmp_char_arr3[3]; |
||
303 | // unsigned char tmp_char_arr4[4]; |
||
304 | //if(!MotorenEin) |
||
305 | PcZugriff = 255; |
||
306 | |||
307 | switch(RxdBuffer[2]) |
||
308 | { |
||
309 | case 'w':// Lagewinkel |
||
310 | Decode64((unsigned char *) &WinkelOut,sizeof(WinkelOut),3,AnzahlEmpfangsBytes); |
||
311 | DebugOut.Analog[15]++; |
||
312 | KompassAntwort = 1; |
||
313 | LED_ON; |
||
314 | break; |
||
315 | case 'c': |
||
316 | case 'b': |
||
317 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
||
318 | WinkelOut.Winkel[0] = ExternControl.Par1; |
||
319 | WinkelOut.Winkel[1] = ExternControl.Par2; |
||
320 | break; |
||
321 | case 'v': // Version-Anforderung und Ausbaustufe |
||
322 | GetVersionAnforderung = 1; |
||
323 | PC_Connected = 255; |
||
324 | break; |
||
325 | |||
326 | case 'a':// Texte der Analogwerte |
||
327 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
328 | DebugTextAnforderung = tmp_char_arr2[0]; |
||
329 | PC_Connected = 255; |
||
330 | break; |
||
331 | case 'g':// "Get"-Anforderung für Debug-Daten |
||
332 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
333 | PC_Connected = 255; |
||
334 | DebugGetAnforderung = 1; |
||
335 | break; |
||
336 | case 'h':// x-1 Displayzeilen |
||
337 | PC_Connected = 255; |
||
338 | break; |
||
339 | /* |
||
340 | case 'b': |
||
341 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
||
342 | RemoteTasten |= ExternControl.RemoteTasten; |
||
343 | ConfirmFrame = ExternControl.Frame; |
||
344 | break; |
||
345 | case 'c': |
||
346 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
||
347 | RemoteTasten |= ExternControl.RemoteTasten; |
||
348 | ConfirmFrame = ExternControl.Frame; |
||
349 | DebugDataAnforderung = 1; |
||
350 | break; |
||
351 | case 'h':// x-1 Displayzeilen |
||
352 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
353 | RemoteTasten |= tmp_char_arr2[0]; |
||
354 | if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten |
||
355 | DebugDisplayAnforderung = 1; |
||
356 | break; |
||
357 | case 't':// Motortest |
||
358 | Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes); |
||
359 | break; |
||
360 | case 'k':// Keys von DubWise |
||
361 | Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes); |
||
362 | ConfirmFrame = DubWiseKeys[3]; |
||
363 | break; |
||
364 | case 'q':// "Get"-Anforderung für Settings |
||
365 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
366 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
||
367 | if(tmp_char_arr2[0] != 0xff) |
||
368 | { |
||
369 | if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; |
||
370 | ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
371 | SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
||
372 | } |
||
373 | else |
||
374 | SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
||
375 | |||
376 | break; |
||
377 | |||
378 | case 'l': |
||
379 | case 'm': |
||
380 | case 'n': |
||
381 | case 'o': |
||
382 | case 'p': // Parametersatz speichern |
||
383 | Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes); |
||
384 | WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
385 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1); // aktiven Datensatz merken |
||
386 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
||
387 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
||
388 | Piep(GetActiveParamSetNumber()); |
||
389 | break; |
||
390 | */ |
||
391 | |||
392 | } |
||
393 | // DebugOut.AnzahlZyklen = Debug_Timer_Intervall; |
||
394 | NeuerDatensatzEmpfangen = 0; |
||
395 | } |
||
396 | |||
397 | |||
398 | //--------------------------------------------------------------------------------------------- |
||
399 | void DatenUebertragung(void) |
||
400 | { |
||
401 | if((CheckDelay(Debug_Timer) && UebertragungAbgeschlossen)) // im Singlestep-Betrieb in jedem Schtitt senden |
||
402 | { |
||
403 | SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut)); |
||
404 | Debug_Timer = SetDelay(250); // Sendeintervall |
||
405 | } |
||
406 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
||
407 | { |
||
408 | SendOutData('V',MeineSlaveAdresse,(unsigned char *) &VersionInfo,sizeof(VersionInfo)); |
||
409 | GetVersionAnforderung = 0; |
||
410 | } |
||
411 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
||
412 | { |
||
413 | SendOutData('A',DebugTextAnforderung + '0',(unsigned char *) ANALOG_TEXT[DebugTextAnforderung],16); |
||
414 | DebugTextAnforderung = 255; |
||
415 | } |
||
416 | if(DebugGetAnforderung && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
417 | { |
||
418 | SendOutData('G',MeineSlaveAdresse,(unsigned char *) &ExternControl,sizeof(ExternControl)); |
||
419 | DebugGetAnforderung = 0; |
||
420 | } |
||
421 | if(KompassAntwort && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
||
422 | { |
||
3 | ingob | 423 | SendOutData('K',MeineSlaveAdresse,(unsigned char *) &I2C_Heading,sizeof(I2C_Heading)); |
1 | ingob | 424 | KompassAntwort = 0; |
425 | } |
||
426 | } |
||
427 |