1 |
hedin |
82 |
1: #include <pic18.h>
|
2 |
|
|
2: #include <stdio.h>
|
3 |
hedin |
148 |
3: #include <htc.h>
|
4 |
|
|
4: #include <string.h>
|
5 |
|
|
5:
|
6 |
|
|
6: #include "lcd.h"
|
7 |
|
|
7: #include "Delay.h"
|
8 |
|
|
8: // Delay.h is included inside lcd.c
|
9 |
|
|
9:
|
10 |
|
|
10: #define LCD_LENGTH 16
|
11 |
|
|
11: #define LCD_ROWS 2
|
12 |
hedin |
150 |
12: #define BUFFER 128
|
13 |
hedin |
148 |
13: #define PWRFAIL RB1
|
14 |
|
|
14: #define FIREDET RB2
|
15 |
|
|
15: #define FEEDING RB3
|
16 |
|
|
16: #define EMPTYTANK RB4
|
17 |
|
|
17:
|
18 |
|
|
18:
|
19 |
|
|
19: unsigned char global_Pot_Hi, global_Pot_Lo;
|
20 |
|
|
20: unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];
|
21 |
hedin |
150 |
21: unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];
|
22 |
hedin |
148 |
22: bit global_recieve_done = 0, global_interval_changed = 0;
|
23 |
|
|
23: unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0;
|
24 |
|
|
24: unsigned int global_emergency_counter = 600, global_time_interval = 3600;
|
25 |
|
|
25: unsigned char global_temp = 0;
|
26 |
|
|
26:
|
27 |
hedin |
151 |
27: unsigned char global_lcd_buf[16];
|
28 |
hedin |
148 |
28:
|
29 |
hedin |
151 |
29: unsigned short global_imei_tversum;
|
30 |
|
|
30:
|
31 |
|
|
31: unsigned char cell_nr[15] = ""; // = "21681784";
|
32 |
|
|
32: bit global_modem_init = 0;
|
33 |
|
|
33:
|
34 |
|
|
34: __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
|
35 |
|
|
35: __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
|
36 |
|
|
36:
|
37 |
|
|
37: void serial_recieved(void);
|
38 |
|
|
38: void serial_send(void);
|
39 |
|
|
39: void update_lcd(void);
|
40 |
|
|
40: void convertTemp(void);
|
41 |
|
|
41: void timer1_interrupt(void);
|
42 |
|
|
42: void on_recieve(void);
|
43 |
|
|
43: void on_initial_recieve(void);
|
44 |
|
|
44:
|
45 |
|
|
45:
|
46 |
|
|
46: void reset_recieve_buffer(void)
|
47 |
|
|
47: {
|
48 |
|
|
48: global_recieve_done = 0;
|
49 |
|
|
49: global_serial_byte_counter=0;
|
50 |
|
|
50: }
|
51 |
|
|
51:
|
52 |
|
|
52:
|
53 |
|
|
53: void ad_init(void) // Nicked from H7
|
54 |
|
|
54: {
|
55 |
|
|
55: // AD Conversion clock
|
56 |
|
|
56: ADCS0 = 0;
|
57 |
|
|
57: ADCS1 = 0;
|
58 |
|
|
58: ADCS2 = 0;
|
59 |
|
|
59:
|
60 |
|
|
60: //Select AN0/RA0 for AD source
|
61 |
|
|
61: // In this (000) setup, it's only RA0/AN0 that does ad convertion.
|
62 |
|
|
62: CHS0=0;
|
63 |
|
|
63: CHS1=0;
|
64 |
|
|
64: CHS2=0;
|
65 |
|
|
65:
|
66 |
|
|
66: //Only AN0 is selected for AD and with Vdd/Vss as limits
|
67 |
|
|
67: PCFG0=0;
|
68 |
|
|
68: PCFG1=1;
|
69 |
|
|
69: PCFG2=1;
|
70 |
|
|
70: PCFG3=1;
|
71 |
|
|
71:
|
72 |
|
|
72: //Result is right justified
|
73 |
|
|
73: ADFM=1;
|
74 |
|
|
74:
|
75 |
|
|
75: //Fire up for A/D converter module
|
76 |
|
|
76: ADON=1;
|
77 |
|
|
77: }
|
78 |
|
|
78:
|
79 |
|
|
79: void rs232_init(void)
|
80 |
|
|
80: {
|
81 |
|
|
81: SPEN = 0; // Serial Port Enable Bit... 0 = disabled
|
82 |
|
|
82: TRISC6 = 0;
|
83 |
|
|
83: TRISC7 = 1;
|
84 |
|
|
84:
|
85 |
|
|
85: SPBRG = 207; // 1200 baud rate... 25 = 9600
|
86 |
|
|
86: // x = (Fosc / (16*[baud rate]) )-1
|
87 |
|
|
87: TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register
|
88 |
|
|
88: RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
|
89 |
|
|
89: }
|
90 |
|
|
90:
|
91 |
|
|
91: void interrupt_init(void)
|
92 |
|
|
92: {
|
93 |
|
|
93: // Assumes that all interrupts default is 0
|
94 |
|
|
94: PEIE = 1;
|
95 |
|
|
95: GIE = 1;
|
96 |
|
|
96: RCIE = 1; // Recieve interrupt enable.
|
97 |
|
|
97: IPEN = 0; // Nfo interrupt priority
|
98 |
|
|
98: TXIE = 0; // Serial interrupt enabled
|
99 |
|
|
99: TMR1IE = 1; // Enables timer 1
|
100 |
|
|
100: }
|
101 |
|
|
101:
|
102 |
|
|
102: void timer_init(void)
|
103 |
|
|
103: {
|
104 |
|
|
104: TMR1CS = 1; //use external clock
|
105 |
|
|
105:
|
106 |
|
|
106: T1CKPS1 = 1; //1:8 prescale
|
107 |
|
|
107: T1CKPS0 = 1;
|
108 |
|
|
108:
|
109 |
|
|
109: TMR1H = 0xEF;
|
110 |
|
|
110: TMR1L = 0xFF;
|
111 |
|
|
111:
|
112 |
|
|
112: T1OSCEN = 1; //enable oscillator circuit
|
113 |
|
|
113: RD16 = 0; //normal 8 bit writes
|
114 |
|
|
114: TMR1ON = 1;
|
115 |
|
|
115: }
|
116 |
|
|
116:
|
117 |
|
|
117: void pic18_io_init(void)
|
118 |
|
|
118: {
|
119 |
|
|
119: TRISA0 = 1; // analog input
|
120 |
|
|
120: TRISA1 = 0; // Output
|
121 |
|
|
121: TRISB1 = 1; // TRISB1-4 Digital input
|
122 |
|
|
122: TRISB2 = 1;
|
123 |
|
|
123: TRISB3 = 1;
|
124 |
|
|
124: TRISB4 = 1;
|
125 |
|
|
125: }
|
126 |
|
|
126:
|
127 |
|
|
127: void sms_init(void)
|
128 |
|
|
128: {
|
129 |
|
|
129: int i;
|
130 |
|
|
130:
|
131 |
|
|
131: reset_recieve_buffer();
|
132 |
|
|
132: sprintf(global_serial_send,"at+cgsn\r");
|
133 |
|
|
133: serial_send();
|
134 |
|
|
134: DelaySek(1);
|
135 |
|
|
135: while(!global_recieve_done) ;
|
136 |
|
|
136:
|
137 |
|
|
137:
|
138 |
|
|
138: sprintf(global_serial_send,"%s", "at+cpin=8043\r");
|
139 |
|
|
139: serial_send();
|
140 |
|
|
140:
|
141 |
|
|
141: while(global_modem_init == 0)
|
142 |
|
|
142: {
|
143 |
|
|
143: on_initial_recieve();
|
144 |
|
|
144: }
|
145 |
|
|
145: }
|
146 |
|
|
146:
|
147 |
|
|
147:
|
148 |
|
|
148: void interrupt interrupt_handler(void)
|
149 |
|
|
149: {
|
150 |
|
|
150: // Finds out what interrupt have been trigged, and starts the respective function.
|
151 |
|
|
151: if(RCIF == 1) // Serial recieve interrupt
|
152 |
|
|
152: {
|
153 |
|
|
153: serial_recieved();
|
154 |
|
|
154: RCIF = 0;
|
155 |
|
|
155: }
|
156 |
|
|
156:
|
157 |
|
|
157: if(TMR1IF == 1) // timer1 interrupt trigger.
|
158 |
|
|
158: {
|
159 |
|
|
159: timer1_interrupt();
|
160 |
|
|
160: TMR1IF = 0;
|
161 |
|
|
161: }
|
162 |
|
|
162: }
|
163 |
|
|
163:
|
164 |
|
|
164:
|
165 |
|
|
165: void serial_send(void)
|
166 |
|
|
166: {
|
167 |
|
|
167: int i;
|
168 |
|
|
168: char data_byte;
|
169 |
|
|
169: for(i = 0; i < BUFFER; i++)
|
170 |
|
|
170: {
|
171 |
|
|
171: data_byte = global_serial_send[i];
|
172 |
|
|
172: if( data_byte == '\r')
|
173 |
|
|
173: i = (BUFFER - 1);
|
174 |
|
|
174: TXREG = data_byte;
|
175 |
|
|
175: while(TRMT==0) ;
|
176 |
|
|
176: DelayMs(10);
|
177 |
|
|
177: }
|
178 |
|
|
178: DelayMs(250);
|
179 |
|
|
179: DelayMs(250);
|
180 |
|
|
180: }
|
181 |
|
|
181:
|
182 |
|
|
182: void serial_recieved(void)
|
183 |
|
|
183: {
|
184 |
|
|
184: char data_byte, saved_data[LCD_LENGTH];
|
185 |
hedin |
150 |
185:
|
186 |
hedin |
151 |
186: data_byte = RCREG;
|
187 |
|
|
187:
|
188 |
|
|
188: if (data_byte == '\n')
|
189 |
|
|
189: return;
|
190 |
hedin |
150 |
190:
|
191 |
hedin |
151 |
191: if (global_serial_byte_counter == 0 && data_byte == '\r')
|
192 |
|
|
192: return;
|
193 |
|
|
193:
|
194 |
|
|
194: global_serial_recieve_buffer[global_serial_byte_counter] = data_byte;
|
195 |
|
|
195:
|
196 |
|
|
196: if (data_byte == '\r')
|
197 |
|
|
197: {
|
198 |
|
|
198: global_recieve_done = 1;
|
199 |
|
|
199: //global_serial_byte_counter = 0;
|
200 |
|
|
200: global_serial_recieve_buffer[global_serial_byte_counter+1] = 0; //zero terminate
|
201 |
|
|
201: }
|
202 |
|
|
202: else
|
203 |
|
|
203: {
|
204 |
|
|
204: global_serial_byte_counter++;
|
205 |
|
|
205: }
|
206 |
|
|
206:
|
207 |
|
|
207: }
|
208 |
|
|
208:
|
209 |
|
|
209: void timer1_interrupt(void)
|
210 |
|
|
210: {
|
211 |
|
|
211: TMR1H = 0xEF;
|
212 |
|
|
212: TMR1L = 0xFF;
|
213 |
|
|
213: global_time_counter++;
|
214 |
|
|
214: global_emergency_counter++;
|
215 |
|
|
215: RA1 = !RA1;
|
216 |
|
|
216: }
|
217 |
|
|
217:
|
218 |
|
|
218: void update_lcd(void)
|
219 |
|
|
219: {
|
220 |
|
|
220: lcd_clear();
|
221 |
|
|
221: lcd_goto(0x00);
|
222 |
|
|
222:
|
223 |
|
|
223: sprintf(global_lcd_buf, "%d", global_temp);
|
224 |
|
|
224: lcd_puts(global_lcd_buf);
|
225 |
|
|
225:
|
226 |
|
|
226: }
|
227 |
|
|
227:
|
228 |
|
|
228: void send_update(void)
|
229 |
|
|
229: {
|
230 |
|
|
230: sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr);
|
231 |
|
|
231: serial_send();
|
232 |
|
|
232: sprintf(global_serial_send, "%d:%d:%d:%d:%d:%d%c", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL, 0x1A);
|
233 |
|
|
233: lcd_goto(40);
|
234 |
|
|
234: lcd_puts(global_serial_send);
|
235 |
|
|
235: serial_send();
|
236 |
|
|
236: DelayMs(250);
|
237 |
|
|
237: global_sms_counter++;
|
238 |
|
|
238: }
|
239 |
|
|
239:
|
240 |
|
|
240: void convertTemp()
|
241 |
|
|
241: {
|
242 |
|
|
242: short adVal;
|
243 |
|
|
243: adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
|
244 |
|
|
244: if( adVal >=840 )
|
245 |
|
|
245: global_temp = 100;
|
246 |
|
|
246: else
|
247 |
|
|
247: global_temp = (adVal / 8.3886);
|
248 |
|
|
248: }
|
249 |
|
|
249:
|
250 |
|
|
250:
|
251 |
|
|
251:
|
252 |
|
|
252: void eeprom_writer(void)
|
253 |
|
|
253: {
|
254 |
|
|
254: char len,i;
|
255 |
|
|
255:
|
256 |
|
|
256: len = strlen(cell_nr);
|
257 |
|
|
257: eeprom_write(0, (global_time_interval/60));
|
258 |
|
|
258: eeprom_write(1, global_sms_counter>>8);
|
259 |
|
|
259: eeprom_write(2, global_sms_counter);
|
260 |
|
|
260: eeprom_write(3, len);
|
261 |
|
|
261:
|
262 |
|
|
262: for (i=0; i<len; ++i)
|
263 |
|
|
263: {
|
264 |
|
|
264: eeprom_write(i+4, cell_nr[i] );
|
265 |
|
|
265: }
|
266 |
|
|
266: }
|
267 |
|
|
267:
|
268 |
|
|
268: void eeprom_reader(void)
|
269 |
|
|
269: {
|
270 |
|
|
270: char len,i;
|
271 |
|
|
271:
|
272 |
|
|
272: global_time_interval = eeprom_read(0);
|
273 |
|
|
273: global_time_interval *= 60;
|
274 |
|
|
274: global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
|
275 |
|
|
275: len = eeprom_read(3);
|
276 |
|
|
276:
|
277 |
|
|
277: for (i=0; i<len; ++i)
|
278 |
|
|
278: {
|
279 |
|
|
279: cell_nr[i] = eeprom_read(i+4);
|
280 |
|
|
280: }
|
281 |
|
|
281:
|
282 |
|
|
282: cell_nr[i] = 0; //zero terminated!
|
283 |
|
|
283: }
|
284 |
|
|
284:
|
285 |
|
|
285: void on_initial_recieve(void)
|
286 |
|
|
286: {
|
287 |
|
|
287: char imei[16];
|
288 |
|
|
288: char* ptr;
|
289 |
|
|
289: char i;
|
290 |
|
|
290:
|
291 |
|
|
291: if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
|
292 |
|
|
292: {
|
293 |
|
|
293: global_modem_init = 1;
|
294 |
|
|
294:
|
295 |
|
|
295: ptr = strstr(global_serial_recieve_buffer,"cgsn");
|
296 |
|
|
296: ptr +=4;
|
297 |
|
|
297: strncpy(imei, ptr,15);
|
298 |
|
|
298: imei[15] = 0;
|
299 |
|
|
299:
|
300 |
|
|
300: global_imei_tversum = 0;
|
301 |
|
|
301: for (i=0; i<15; ++i)
|
302 |
|
|
302: {
|
303 |
|
|
303: global_imei_tversum += (imei[i] - '0');
|
304 |
|
|
304: }
|
305 |
|
|
305:
|
306 |
|
|
306:
|
307 |
|
|
307: reset_recieve_buffer();
|
308 |
|
|
308: }
|
309 |
|
|
309: }
|
310 |
|
|
310:
|
311 |
|
|
311:
|
312 |
|
|
312: void on_recieve(void)
|
313 |
|
|
313: {
|
314 |
|
|
314: char tmp[17];
|
315 |
|
|
315: char* ptr;
|
316 |
|
|
316: tmp[0]=0;
|
317 |
|
|
317:
|
318 |
|
|
318: if (global_recieve_done == 0)
|
319 |
|
|
319: return;
|
320 |
|
|
320:
|
321 |
|
|
321: lcd_goto(0x00);
|
322 |
|
|
322: lcd_puts(global_serial_recieve_buffer);
|
323 |
|
|
323:
|
324 |
|
|
324: if (strstr(global_serial_recieve_buffer,"CMTI") == 0)
|
325 |
|
|
325: {
|
326 |
|
|
326: ptr = strstr(global_serial_recieve_buffer,"'");
|
327 |
|
|
327: strcat(tmp,ptr);
|
328 |
|
|
328:
|
329 |
|
|
329:
|
330 |
|
|
330: lcd_goto(0x40);
|
331 |
|
|
331: lcd_puts(tmp);
|
332 |
|
|
332:
|
333 |
|
|
333: }
|
334 |
|
|
334:
|
335 |
|
|
335: reset_recieve_buffer();
|
336 |
|
|
336: }
|
337 |
|
|
337:
|
338 |
|
|
338: void main()
|
339 |
|
|
339: {
|
340 |
|
|
340: ////////////////////
|
341 |
|
|
341: // Running Init's //
|
342 |
|
|
342:
|
343 |
|
|
343: // Running init for various components.
|
344 |
|
|
344: pic18_io_init();
|
345 |
|
|
345: rs232_init();
|
346 |
|
|
346: ad_init();
|
347 |
|
|
347: lcd_init(0);
|
348 |
|
|
348: interrupt_init();
|
349 |
|
|
349: sms_init();
|
350 |
|
|
350: //eeprom_init();
|
351 |
|
|
351: timer_init();
|
352 |
|
|
352: eeprom_reader();
|
353 |
|
|
353: ///////////////
|
354 |
|
|
354: // Main loop //
|
355 |
|
|
355:
|
356 |
|
|
356: DelayMs(50);
|
357 |
|
|
357: reset_recieve_buffer();
|
358 |
|
|
358:
|
359 |
|
|
359: while(1)
|
360 |
|
|
360: {
|
361 |
|
|
361: // If there happends to be a critical state on the system, we send a sms.
|
362 |
|
|
362: if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 )
|
363 |
|
|
363: {
|
364 |
|
|
364: send_update();
|
365 |
|
|
365: global_emergency_counter = 0;
|
366 |
|
|
366: }
|
367 |
|
|
367: // Every X sec. a status sms is send.
|
368 |
|
|
368: if(global_time_counter >= 3600)
|
369 |
|
|
369: {
|
370 |
|
|
370: send_update();
|
371 |
|
|
371: global_time_counter = 0;
|
372 |
|
|
372: }
|
373 |
|
|
373: // To avoid buffer overrun.
|
374 |
|
|
374: if( global_emergency_counter > 7200 )
|
375 |
|
|
375: global_emergency_counter = 600;
|
376 |
|
|
376:
|
377 |
|
|
377: // Checks if there has been recieved a config sms.
|
378 |
|
|
378: if(global_interval_changed )
|
379 |
|
|
379: {
|
380 |
|
|
380: eeprom_writer();
|
381 |
|
|
381: global_interval_changed = 0;
|
382 |
|
|
382: }
|
383 |
|
|
383: // Checking if A/D convertion is done, and save the data in global_Pot_??
|
384 |
|
|
384: if(GODONE==0)
|
385 |
|
|
385: {
|
386 |
|
|
386: global_Pot_Hi = ADRESH;
|
387 |
|
|
387: global_Pot_Lo = ADRESL;
|
388 |
|
|
388: convertTemp();
|
389 |
|
|
389: //update_lcd();
|
390 |
|
|
390: GODONE = 1;
|
391 |
|
|
391: }
|
392 |
|
|
392: // Handels the recieve sms'es.
|
393 |
|
|
393: on_recieve();
|
394 |
|
|
394: }
|
395 |
|
|
395: }
|