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

Contents of /trunk/Embedded/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (show annotations) (download)
Wed Dec 5 15:01:15 2007 UTC (16 years, 5 months ago) by hedin
File MIME type: text/plain
File size: 11871 byte(s)
It's alive !!!!!!!!!!!!
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, saved_data[LCD_LENGTH];
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 // if (global_serial_byte_counter == BUFFER -1)
242 // global_seriaæl_recieve_buffer[global_serial_byte_counter] = 0;
243
244 // if (data_byte != '\r' && (global_serial_byte_counter-1) < BUFFER)
245 // global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte; // fills the data_byte into our buffer.
246 global_serial_recieve_buffer[ global_serial_byte_counter] = data_byte;
247
248
249 if (data_byte == '\r') // when we meet a '\r', the transmission is done, and we fill the constxt of
250 // global_message_buffer1 into global_message_buffer2 our main buffer into
251 // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
252 // into global_message_buffer1.
253 {
254 global_recieve_done = 1; // indicates the recieve transmission is done.
255 global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
256
257 // global_message_buffer1 -> global_message_buffer2
258 strcpy(global_message_buffer2, global_message_buffer1);
259 global_message_buffer_length2 = global_message_buffer_length1;
260
261 // global_serial_recieve_buffer -> global_message_buffer1
262 strcpy(global_message_buffer1, global_serial_recieve_buffer);
263 global_message_buffer_length1 = global_serial_byte_counter;
264
265
266 global_serial_byte_counter = 0;
267 }
268 else
269 {
270 global_serial_byte_counter++;
271 }
272
273 }
274
275 void timer1_interrupt(void)
276 {
277 TMR1H = 0xEF;
278 TMR1L = 0xFF;
279 global_time_counter++;
280 global_emergency_counter++;
281 RA1 = !RA1;
282 }
283
284 void update_lcd(void)
285 {
286 lcd_clear();
287 lcd_goto(0x00);
288
289 sprintf(global_lcd_buf, "Temp: %d", global_temp);
290 lcd_puts(global_lcd_buf);
291
292 }
293
294 void send_update(void)
295 {
296 char update[40];
297 sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
298 send_sms(update);
299
300 global_sms_counter++;
301 }
302
303 void send_sms(unsigned char* payload)
304 {
305 sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
306 serial_send();
307 sprintf(global_serial_send, "%s%c", payload, 0x1A);
308 serial_send();
309 DelayMs(250);
310
311 }
312
313 void convertTemp()
314 {
315 short adVal;
316 adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
317 if( adVal >=840 )
318 global_temp = 100;
319 else
320 global_temp = (unsigned char) (adVal / 8.3886);
321 }
322
323
324
325 void eeprom_writer(void)
326 {
327 char len,i;
328
329 len = strlen(global_cell_nr);
330 eeprom_write(0, (global_time_interval/60));
331 eeprom_write(1, global_sms_counter>>8);
332 eeprom_write(2, global_sms_counter);
333 eeprom_write(3, len);
334
335 for (i=0; i<len; ++i)
336 {
337 eeprom_write(i+4, global_cell_nr[i] );
338 }
339 }
340
341 void eeprom_reader(void)
342 {
343 char len,i;
344
345 global_time_interval = eeprom_read(0);
346 global_time_interval *= 60;
347 global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
348 len = eeprom_read(3);
349
350 for (i=0; i<len; ++i)
351 {
352 global_cell_nr[i] = eeprom_read(i+4);
353 }
354
355 global_cell_nr[i] = 0; //zero terminated!
356 }
357
358 void on_initial_recieve(void)
359 {
360 char imei[16];
361 char* ptr;
362 char i;
363 char buf[2];
364
365 if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
366 {
367 global_modem_init = 1;
368
369 ptr = strstr(global_serial_recieve_buffer,"cgsn");
370 ptr +=4;
371 strncpy(imei, ptr,15);
372 imei[15] = 0;
373
374 reset_recieve_buffer();
375 }
376 }
377
378 void on_recieve(void)
379 {
380 char tmp[3];
381 char* ptr;
382 tmp[0]=0;
383
384 if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
385 return;
386
387 if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
388 {
389 ptr = strstr(global_message_buffer1,","); // finds the point just before the nr. of the SMS.
390 strcat(tmp,ptr+1); // puts that number in tmp
391 global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0]
392 strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable.
393 sms_recieved();
394 }
395 reset_recieve_buffer();
396 }
397
398 void sms_recieved(void)
399 {
400 char buf[4];
401 char i,imei;
402 char pos;
403
404 sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
405 serial_send(); // Sends the command.
406
407 while(strstr(global_message_buffer1, "OK") == 0) // stays here until we recieve a "OK" from the modem.
408 DelayMs(1);
409
410 DelayUs(10);
411
412 for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
413 {
414 buf[i] = global_message_buffer2[i];
415 }
416
417 buf[i] = 0;
418 imei = atoi(buf);
419
420 if (imei == global_imei_tversum)
421 {
422 i++; //spring over ':'
423 pos = 0;
424 for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
425 {
426 global_cell_nr[pos] = global_message_buffer2[i];
427 }
428 global_cell_nr[pos] = 0; //zero terminator
429
430 i++; //spring over ':'
431 pos=0;
432 for ( ; global_message_buffer2[i] ; ++i,++pos)
433 {
434 buf[pos] = global_message_buffer2[i];
435 }
436 buf[pos]=0;
437
438 global_time_interval = atoi(buf);
439 global_time_interval *= 60;
440 eeprom_writer(); // writes the new cell nr. and time interval to the eeprom.
441 send_sms("conf ok");
442 }
443
444
445 }
446
447
448
449 void main()
450 {
451 ////////////////////
452 // Running Init's //
453
454 // Running init for various components.
455 pic18_io_init();
456 RA5 = 1; // Indicates that the board is running inits.
457
458 rs232_init();
459 ad_init();
460 lcd_init(0);
461 interrupt_init();
462 sms_init();
463 //eeprom_init();
464 timer_init();
465 eeprom_reader();
466 ///////////////
467 // Main loop //
468
469 DelayMs(50);
470 reset_recieve_buffer();
471
472 RA5 = 0; // Inits are done, and RA1 will now work as a error notifier.
473
474 while(1)
475 {
476 // If there happends to be a critical state on the system, we send a sms.
477 if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 )
478 {
479 send_update();
480 global_emergency_counter = 0;
481 }
482 // Every X sec. a status sms is send.
483 if(global_time_counter >= 3600)
484 {
485 send_update();
486 global_time_counter = 0;
487 }
488 // To avoid buffer overrun.
489 if( global_emergency_counter > 7200 )
490 global_emergency_counter = 600;
491
492 // Checks if there has been recieved a config sms.
493 if(global_interval_changed )
494 {
495 eeprom_writer();
496 global_interval_changed = 0;
497 }
498 // Checking if A/D convertion is done, and save the data in global_Pot_??
499 if(GODONE==0)
500 {
501 global_Pot_Hi = ADRESH;
502 global_Pot_Lo = ADRESL;
503 convertTemp();
504 update_lcd();
505 GODONE = 1;
506 }
507 // Handels the recieve sms'es.
508 on_recieve();
509 }
510 }

  ViewVC Help
Powered by ViewVC 1.1.20