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

Contents of /trunk/Embedded/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 214 - (show annotations) (download)
Mon Dec 10 08:39:47 2007 UTC (16 years, 5 months ago) by hedin
File MIME type: text/plain
File size: 11878 byte(s)
Modified BUFFER from 128 to 160.

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

  ViewVC Help
Powered by ViewVC 1.1.20