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

Contents of /trunk/Embedded/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20