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