/[H9]/trunk/Embedded/main.c
ViewVC logotype

Contents of /trunk/Embedded/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 162 - (show annotations) (download)
Wed Dec 5 15:20:07 2007 UTC (16 years, 5 months ago) by hedin
File MIME type: text/plain
File size: 11643 byte(s)
Prevent recieve buffer overrun
1 #include <pic18.h>
2 #include <stdio.h>
3 #include <htc.h>
4 #include <string.h>
5 #include <stdlib.h>
6
7 #include "lcd.h"
8 #include "Delay.h"
9 // Delay.h is included inside lcd.c
10
11 #define LCD_LENGTH 16
12 #define LCD_ROWS 2
13 #define BUFFER 128
14 #define PWRFAIL RB1
15 #define FIREDET RB2
16 #define FEEDING RB3
17 #define EMPTYTANK RB4
18
19
20 unsigned char global_Pot_Hi, global_Pot_Lo;
21 unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];
22 bit global_recieve_done = 0, global_interval_changed = 0;
23 unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0;
24 unsigned int global_emergency_counter = 600, global_time_interval = 3600;
25 unsigned char global_temp = 0;
26
27 unsigned char global_message_buffer1[BUFFER];
28 unsigned char global_message_buffer2[BUFFER];
29 unsigned char global_message_buffer_length1;
30 unsigned char global_message_buffer_length2;
31
32 unsigned char global_lcd_buf[16];
33
34 unsigned short global_imei_tversum;
35
36 unsigned char global_cell_nr[15] = ""; // = "21681784";
37 bit global_modem_init = 0;
38 bit global_has_imei = 0;
39
40 unsigned char global_sms_recieve_number[3];
41
42 __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
43 __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
44
45 void serial_recieved(void);
46 void serial_send(void);
47 void update_lcd(void);
48 void convertTemp(void);
49 void timer1_interrupt(void);
50 void on_recieve(void);
51 void on_initial_recieve(void);
52 void sms_recieved(void);
53 void send_sms(unsigned char* payload);
54
55
56 void reset_recieve_buffer(void)
57 {
58 global_recieve_done = 0;
59
60 global_message_buffer_length1 = 0;
61 //global_message_buffer1[0] = 0;
62 global_message_buffer_length2 = 0;
63 //global_message_buffer2[0] = 0;
64
65 memset(global_message_buffer1, 0, BUFFER);
66 memset(global_message_buffer2, 0, BUFFER);
67 }
68
69 void pic18_io_init(void)
70 {
71 TRISA0 = 1; // analog input
72 TRISA5 = 0; // Output
73 TRISB1 = 1; // TRISB1-4 Digital input
74 TRISB2 = 1;
75 TRISB3 = 1;
76 TRISB4 = 1;
77 }
78
79
80
81 void ad_init(void) // Nicked from H7
82 {
83 // AD Conversion clock
84 ADCS0 = 0;
85 ADCS1 = 0;
86 ADCS2 = 0;
87
88 //Select AN0/RA0 for AD source
89 // In this (000) setup, it's only RA0/AN0 that does ad convertion.
90 CHS0=0;
91 CHS1=0;
92 CHS2=0;
93
94 //Only AN0 is selected for AD and with Vdd/Vss as limits
95 PCFG0=0;
96 PCFG1=1;
97 PCFG2=1;
98 PCFG3=1;
99
100 //Result is right justified
101 ADFM=1;
102
103 //Fire up for A/D converter module
104 ADON=1;
105 }
106
107 void rs232_init(void)
108 {
109 SPEN = 0; // Serial Port Enable Bit... 0 = disabled
110 TRISC6 = 0;
111 TRISC7 = 1;
112
113 SPBRG = 207; // 1200 baud rate... 25 = 9600
114 // x = (Fosc / (16*[baud rate]) )-1
115 TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register
116 RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
117 }
118
119 void interrupt_init(void)
120 {
121 // Assumes that all interrupts default is 0
122 PEIE = 1;
123 GIE = 1;
124 RCIE = 1; // Recieve interrupt enable.
125 IPEN = 0; // Nfo interrupt priority
126 TXIE = 0; // Serial interrupt enabled
127 TMR1IE = 1; // Enables timer 1
128 }
129
130 void timer_init(void)
131 {
132 TMR1CS = 1; //use external clock
133
134 T1CKPS1 = 1; //1:8 prescale
135 T1CKPS0 = 1;
136
137 TMR1H = 0xEF;
138 TMR1L = 0xFF;
139
140 T1OSCEN = 1; //enable oscillator circuit
141 RD16 = 0; //normal 8 bit writes
142 TMR1ON = 1;
143 }
144
145 void sms_init(void)
146 {
147 int i;
148 char buf[2];
149
150
151 reset_recieve_buffer();
152 sprintf(global_serial_send,"at+cgsn\r");
153 serial_send();
154 DelaySek(1);
155 //while(!global_recieve_done) ;
156
157 while (global_has_imei == 0)
158 {
159 if (strstr(global_message_buffer1,"OK") != 0)
160 {
161 global_imei_tversum = 0;
162 for (i=0; i<15; ++i)
163 {
164 buf[0] = global_message_buffer2[i];
165 buf[1] = 0;
166 global_imei_tversum += atoi(buf);
167 //global_imei_tversum += (global_message_buffer2[i] - '0');
168 }
169
170 global_has_imei = 1;
171 }
172 }
173
174
175 sprintf(global_serial_send,"%s", "at+cpin=8043\r");
176 serial_send();
177
178 while(global_modem_init == 0)
179 {
180 on_initial_recieve();
181 }
182 DelayMs(5);
183
184 reset_recieve_buffer();
185 sprintf(global_serial_send, "at+cmgd=1,4\r");
186 serial_send();
187 while ( strstr(global_message_buffer1,"OK") == 0) ;
188 }
189
190
191 void interrupt interrupt_handler(void)
192 {
193 // Finds out what interrupt have been trigged, and starts the respective function.
194 if(RCIF == 1) // Serial recieve interrupt
195 {
196 serial_recieved();
197 RCIF = 0;
198 }
199
200 if(TMR1IF == 1) // timer1 interrupt trigger.
201 {
202 timer1_interrupt();
203 TMR1IF = 0;
204 }
205 }
206
207
208 void serial_send(void)
209 {
210 int i;
211 char data_byte;
212 for(i = 0; i < BUFFER; i++)
213 {
214 data_byte = global_serial_send[i];
215 if( data_byte == '\r')
216 i = (BUFFER - 1);
217 TXREG = data_byte;
218 while(TRMT==0) ;
219 DelayMs(10);
220 }
221 DelayMs(250);
222 global_serial_send[0] = 0;
223 DelayMs(250);
224 }
225
226 ///////////////////////////////////////////////////////////////////////////////////////////////////
227 // Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from,
228 // global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done.
229 void serial_recieved(void)
230 {
231 char data_byte;
232
233 data_byte = RCREG;
234
235 if (data_byte == '\n') // Cant be bothered to do anyting if the byte is a '\n'.
236 return;
237
238 if (global_serial_byte_counter == 0 && data_byte == '\r') // don't care about '\r', if it's the first byte we recieve.
239 return;
240
241
242 if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
243 global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte; // fills the data_byte into our buffer.
244
245
246
247 if (data_byte == '\r') // when we meet a '\r', the transmission is done, and we fill the constxt of
248 // global_message_buffer1 into global_message_buffer2 our main buffer into
249 // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
250 // into global_message_buffer1.
251 {
252 global_recieve_done = 1; // indicates the recieve transmission is done.
253 global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
254
255 // global_message_buffer1 -> global_message_buffer2
256 strcpy(global_message_buffer2, global_message_buffer1);
257 global_message_buffer_length2 = global_message_buffer_length1;
258
259 // global_serial_recieve_buffer -> global_message_buffer1
260 strcpy(global_message_buffer1, global_serial_recieve_buffer);
261 global_message_buffer_length1 = global_serial_byte_counter;
262
263
264 global_serial_byte_counter = 0;
265 }
266 }
267
268 void timer1_interrupt(void)
269 {
270 TMR1H = 0xEF;
271 TMR1L = 0xFF;
272 global_time_counter++;
273 global_emergency_counter++;
274 RA1 = !RA1;
275 }
276
277 void update_lcd(void)
278 {
279 lcd_clear();
280 lcd_goto(0x00);
281
282 sprintf(global_lcd_buf, "Temp: %d", global_temp);
283 lcd_puts(global_lcd_buf);
284
285 }
286
287 void send_update(void)
288 {
289 char update[40];
290 sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
291 send_sms(update);
292
293 global_sms_counter++;
294 }
295
296 void send_sms(unsigned char* payload)
297 {
298 sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
299 serial_send();
300 sprintf(global_serial_send, "%s%c", payload, 0x1A);
301 serial_send();
302 DelayMs(250);
303
304 }
305
306 void convertTemp()
307 {
308 short adVal;
309 adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
310 if( adVal >=840 )
311 global_temp = 100;
312 else
313 global_temp = (unsigned char) (adVal / 8.3886);
314 }
315
316
317
318 void eeprom_writer(void)
319 {
320 char len,i;
321
322 len = strlen(global_cell_nr);
323 eeprom_write(0, (global_time_interval/60));
324 eeprom_write(1, global_sms_counter>>8);
325 eeprom_write(2, global_sms_counter);
326 eeprom_write(3, len);
327
328 for (i=0; i<len; ++i)
329 {
330 eeprom_write(i+4, global_cell_nr[i] );
331 }
332 }
333
334 void eeprom_reader(void)
335 {
336 char len,i;
337
338 global_time_interval = eeprom_read(0);
339 global_time_interval *= 60;
340 global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
341 len = eeprom_read(3);
342
343 for (i=0; i<len; ++i)
344 {
345 global_cell_nr[i] = eeprom_read(i+4);
346 }
347
348 global_cell_nr[i] = 0; //zero terminated!
349 }
350
351 void on_initial_recieve(void)
352 {
353 char imei[16];
354 char* ptr;
355 char i;
356 char buf[2];
357
358 if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
359 {
360 global_modem_init = 1;
361
362 ptr = strstr(global_serial_recieve_buffer,"cgsn");
363 ptr +=4;
364 strncpy(imei, ptr,15);
365 imei[15] = 0;
366
367 reset_recieve_buffer();
368 }
369 }
370
371 void on_recieve(void)
372 {
373 char tmp[3];
374 char* ptr;
375 tmp[0]=0;
376
377 if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
378 return;
379
380 if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
381 {
382 ptr = strstr(global_message_buffer1,","); // finds the point just before the nr. of the SMS.
383 strcat(tmp,ptr+1); // puts that number in tmp
384 global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0]
385 strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable.
386 sms_recieved();
387 }
388 reset_recieve_buffer();
389 }
390
391 void sms_recieved(void)
392 {
393 char buf[4];
394 char i,imei;
395 char pos;
396
397 sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
398 serial_send(); // Sends the command.
399
400 while(strstr(global_message_buffer1, "OK") == 0) // stays here until we recieve a "OK" from the modem.
401 DelayMs(1);
402
403 DelayUs(10);
404
405 for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
406 {
407 buf[i] = global_message_buffer2[i];
408 }
409
410 buf[i] = 0;
411 imei = atoi(buf);
412
413 if (imei == global_imei_tversum)
414 {
415 i++; //spring over ':'
416 pos = 0;
417 for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
418 {
419 global_cell_nr[pos] = global_message_buffer2[i];
420 }
421 global_cell_nr[pos] = 0; //zero terminator
422
423 i++; //spring over ':'
424 pos=0;
425 for ( ; global_message_buffer2[i] ; ++i,++pos)
426 {
427 buf[pos] = global_message_buffer2[i];
428 }
429 buf[pos]=0;
430
431 global_time_interval = atoi(buf);
432 global_time_interval *= 60;
433 eeprom_writer(); // writes the new cell nr. and time interval to the eeprom.
434 send_sms("conf ok");
435 }
436
437
438 }
439
440
441
442 void main()
443 {
444 ////////////////////
445 // Running Init's //
446
447 // Running init for various components.
448 pic18_io_init();
449 RA5 = 1; // Indicates that the board is running inits.
450
451 rs232_init();
452 ad_init();
453 lcd_init(0);
454 interrupt_init();
455 sms_init();
456 //eeprom_init();
457 timer_init();
458 eeprom_reader();
459 ///////////////
460 // Main loop //
461
462 DelayMs(50);
463 reset_recieve_buffer();
464
465 RA5 = 0; // Inits are done, and RA1 will now work as a error notifier.
466
467 while(1)
468 {
469 // If there happends to be a critical state on the system, we send a sms.
470 if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 )
471 {
472 send_update();
473 global_emergency_counter = 0;
474 }
475 // Every X sec. a status sms is send.
476 if(global_time_counter >= 3600)
477 {
478 send_update();
479 global_time_counter = 0;
480 }
481 // To avoid buffer overrun.
482 if( global_emergency_counter > 7200 )
483 global_emergency_counter = 600;
484
485 // Checks if there has been recieved a config sms.
486 if(global_interval_changed )
487 {
488 eeprom_writer();
489 global_interval_changed = 0;
490 }
491 // Checking if A/D convertion is done, and save the data in global_Pot_??
492 if(GODONE==0)
493 {
494 global_Pot_Hi = ADRESH;
495 global_Pot_Lo = ADRESL;
496 convertTemp();
497 if (global_time_counter % 5 == 0)
498 update_lcd();
499 GODONE = 1;
500 }
501 // Handels the recieve sms'es.
502 on_recieve();
503 }
504 }

  ViewVC Help
Powered by ViewVC 1.1.20