20 |
unsigned char global_Pot_Hi, global_Pot_Lo; |
unsigned char global_Pot_Hi, global_Pot_Lo; |
21 |
unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; |
unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; |
22 |
bit global_recieve_done = 0, global_interval_changed = 0; |
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; |
unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0, global_time_counter_image = 0; |
24 |
unsigned int global_emergency_counter = 600, global_time_interval = 3600; |
unsigned int global_emergency_counter = 600, global_time_interval = 3600; |
25 |
unsigned char global_temp = 0; |
unsigned char global_temp = 0; |
26 |
|
|
27 |
|
unsigned int global_temp_update_display = 0; |
28 |
|
|
29 |
unsigned char global_message_buffer1[BUFFER]; |
unsigned char global_message_buffer1[BUFFER]; |
30 |
unsigned char global_message_buffer2[BUFFER]; |
unsigned char global_message_buffer2[BUFFER]; |
31 |
unsigned char global_message_buffer_length1; |
unsigned char global_message_buffer_length1; |
44 |
__EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8'); |
__EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8'); |
45 |
__EEPROM_DATA( '1', '7', '8', '4',0,0,0,0); |
__EEPROM_DATA( '1', '7', '8', '4',0,0,0,0); |
46 |
|
|
47 |
|
|
48 |
void serial_recieved(void); |
void serial_recieved(void); |
49 |
void serial_send(void); |
void serial_send(void); |
50 |
void update_lcd(void); |
void update_lcd(void); |
53 |
void on_recieve(void); |
void on_recieve(void); |
54 |
void on_initial_recieve(void); |
void on_initial_recieve(void); |
55 |
void sms_recieved(void); |
void sms_recieved(void); |
56 |
void send_sms(unsigned char* payload); |
void send_sms(const unsigned char* payload); |
57 |
|
void eeprom_writer(void); |
58 |
|
|
59 |
|
|
60 |
void reset_recieve_buffer(void) |
void reset_recieve_buffer(void) |
69 |
memset(global_message_buffer1, 0, BUFFER); |
memset(global_message_buffer1, 0, BUFFER); |
70 |
memset(global_message_buffer2, 0, BUFFER); |
memset(global_message_buffer2, 0, BUFFER); |
71 |
} |
} |
72 |
|
////////// INITS ////////// |
73 |
void pic18_io_init(void) |
void pic18_io_init(void) |
74 |
{ |
{ |
75 |
TRISA0 = 1; // analog input |
TRISA0 = 1; // analog input |
101 |
PCFG2=1; |
PCFG2=1; |
102 |
PCFG3=1; |
PCFG3=1; |
103 |
|
|
104 |
|
//Reset the A/D result registers |
105 |
|
ADRESH = 0; |
106 |
|
ADRESL = 0; |
107 |
|
|
108 |
//Result is right justified |
//Result is right justified |
109 |
ADFM=1; |
ADFM=1; |
110 |
|
|
112 |
ADON=1; |
ADON=1; |
113 |
} |
} |
114 |
|
|
|
void rs232_init(void) |
|
|
{ |
|
|
SPEN = 0; // Serial Port Enable Bit... 0 = disabled |
|
|
TRISC6 = 0; |
|
|
TRISC7 = 1; |
|
115 |
|
|
|
SPBRG = 207; // 1200 baud rate... 25 = 9600 |
|
|
// x = (Fosc / (16*[baud rate]) )-1 |
|
|
TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register |
|
|
RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register |
|
|
} |
|
116 |
|
|
117 |
void interrupt_init(void) |
void interrupt_init(void) |
118 |
{ |
{ |
125 |
TMR1IE = 1; // Enables timer 1 |
TMR1IE = 1; // Enables timer 1 |
126 |
} |
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
void timer_init(void) |
void timer_init(void) |
131 |
{ |
{ |
132 |
TMR1CS = 1; //use external clock |
TMR1CS = 1; //use external clock |
140 |
T1OSCEN = 1; //enable oscillator circuit |
T1OSCEN = 1; //enable oscillator circuit |
141 |
RD16 = 0; //normal 8 bit writes |
RD16 = 0; //normal 8 bit writes |
142 |
TMR1ON = 1; |
TMR1ON = 1; |
143 |
} |
} |
144 |
|
|
145 |
|
void rs232_init(void) |
146 |
|
{ |
147 |
|
SPEN = 0; // Serial Port Enable Bit... 0 = disabled |
148 |
|
TRISC6 = 0; |
149 |
|
TRISC7 = 1; |
150 |
|
|
151 |
|
SPBRG = 207; // 1200 baud rate... 25 = 9600 |
152 |
|
// x = (Fosc / (16*[baud rate]) )-1 |
153 |
|
TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register |
154 |
|
RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register |
155 |
|
} |
156 |
|
|
157 |
void sms_init(void) |
void sms_init(void) |
158 |
{ |
{ |
159 |
int i; |
int i; |
160 |
char buf[2]; |
char buf[2]; |
161 |
|
|
162 |
|
while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ; // Waiting for the modem to be ready |
163 |
|
|
164 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
165 |
sprintf(global_serial_send,"at+cgsn\r"); |
sprintf(global_serial_send,"at+cgsn\r"); |
166 |
serial_send(); |
serial_send(); |
|
DelaySek(1); |
|
|
//while(!global_recieve_done) ; |
|
167 |
|
|
168 |
while (global_has_imei == 0) |
while (global_has_imei == 0) |
169 |
{ |
{ |
175 |
buf[0] = global_message_buffer2[i]; |
buf[0] = global_message_buffer2[i]; |
176 |
buf[1] = 0; |
buf[1] = 0; |
177 |
global_imei_tversum += atoi(buf); |
global_imei_tversum += atoi(buf); |
|
//global_imei_tversum += (global_message_buffer2[i] - '0'); |
|
178 |
} |
} |
179 |
|
|
180 |
global_has_imei = 1; |
global_has_imei = 1; |
181 |
} |
} |
182 |
} |
} |
183 |
|
|
184 |
|
sprintf(global_serial_send,"%s", "at+cpin?\r"); |
|
sprintf(global_serial_send,"%s", "at+cpin=8043\r"); |
|
185 |
serial_send(); |
serial_send(); |
186 |
|
DelayMs(100); // To give the modem a chance to answer. |
187 |
|
|
188 |
while(global_modem_init == 0) |
if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0) |
189 |
{ |
{ |
190 |
on_initial_recieve(); |
sprintf(global_serial_send,"%s", "at+cpin=8043\r"); |
191 |
|
serial_send(); |
192 |
|
|
193 |
|
while(global_modem_init == 0) |
194 |
|
{ |
195 |
|
on_initial_recieve(); |
196 |
|
} |
197 |
} |
} |
|
DelayMs(5); |
|
|
|
|
198 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
199 |
sprintf(global_serial_send, "at+cmgd=1,4\r"); |
sprintf(global_serial_send, "at+cmgd=1,4\r"); |
200 |
serial_send(); |
serial_send(); |
201 |
while ( strstr(global_message_buffer1,"OK") == 0) ; |
while ( strstr(global_message_buffer1,"OK") == 0) ; |
202 |
} |
} |
203 |
|
|
|
|
|
|
void interrupt interrupt_handler(void) |
|
|
{ |
|
|
// Finds out what interrupt have been trigged, and starts the respective function. |
|
|
if(RCIF == 1) // Serial recieve interrupt |
|
|
{ |
|
|
serial_recieved(); |
|
|
RCIF = 0; |
|
|
} |
|
|
|
|
|
if(TMR1IF == 1) // timer1 interrupt trigger. |
|
|
{ |
|
|
timer1_interrupt(); |
|
|
TMR1IF = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
204 |
void serial_send(void) |
void serial_send(void) |
205 |
{ |
{ |
206 |
int i; |
int i; |
214 |
while(TRMT==0) ; |
while(TRMT==0) ; |
215 |
DelayMs(10); |
DelayMs(10); |
216 |
} |
} |
217 |
DelayMs(250); |
DelayMs(150); |
218 |
global_serial_send[0] = 0; |
global_serial_send[0] = 0; |
219 |
DelayMs(250); |
DelayMs(150); |
220 |
} |
} |
221 |
|
////////// INTERRUPT HANDLER ////////// |
222 |
|
void interrupt interrupt_handler(void) |
223 |
|
{ |
224 |
|
// Finds out what interrupt have been trigged, and starts the respective function. |
225 |
|
if(RCIF == 1) // Serial recieve interrupt |
226 |
|
{ |
227 |
|
serial_recieved(); |
228 |
|
RCIF = 0; |
229 |
|
} |
230 |
|
|
231 |
|
if(TMR1IF == 1) // timer1 interrupt trigger. |
232 |
|
{ |
233 |
|
timer1_interrupt(); |
234 |
|
TMR1IF = 0; |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
////////// INTERRUPT TRIGGED ////////// |
|
// Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from, |
|
|
// global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done. |
|
239 |
void serial_recieved(void) |
void serial_recieved(void) |
240 |
{ |
{ |
241 |
char data_byte; |
char data_byte; |
251 |
|
|
252 |
if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun |
if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun |
253 |
global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte; // fills the data_byte into our buffer. |
global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte; // fills the data_byte into our buffer. |
254 |
|
else |
255 |
|
{ |
256 |
|
global_serial_recieve_buffer[0] = 0; |
257 |
|
global_serial_byte_counter = 0; |
258 |
|
return; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
if (data_byte == '\r') // when we meet a '\r', the transmission is done, and we fill the constxt of |
if (data_byte == '\r') // when we meet a '\r', the transmission is done, and we fill the constxt of |
288 |
global_emergency_counter++; |
global_emergency_counter++; |
289 |
RA1 = !RA1; |
RA1 = !RA1; |
290 |
} |
} |
291 |
|
////////// ORDENARY FUNKTIONS ////////// |
292 |
void update_lcd(void) |
void update_lcd(void) |
293 |
{ |
{ |
294 |
lcd_clear(); |
if(global_temp_update_display != global_time_counter) |
295 |
lcd_goto(0x00); |
{ |
296 |
|
// lcd_clear(); |
297 |
|
lcd_goto(0x00); |
298 |
|
|
299 |
sprintf(global_lcd_buf, "Temp: %d", global_temp); |
sprintf(global_lcd_buf, "Temp: %3d", global_temp); |
300 |
lcd_puts(global_lcd_buf); |
lcd_puts(global_lcd_buf); |
301 |
|
global_temp_update_display = global_time_counter; |
302 |
|
} |
303 |
|
|
304 |
} |
} |
305 |
|
|
312 |
global_sms_counter++; |
global_sms_counter++; |
313 |
} |
} |
314 |
|
|
315 |
void send_sms(unsigned char* payload) |
void send_sms(const unsigned char* payload) |
316 |
{ |
{ |
317 |
sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr); |
sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr); |
318 |
serial_send(); |
serial_send(); |
319 |
sprintf(global_serial_send, "%s%c", payload, 0x1A); |
sprintf(global_serial_send, "%s%c", payload, 0x1A); |
320 |
serial_send(); |
serial_send(); |
321 |
DelayMs(250); |
DelayMs(150); |
322 |
|
if(global_sms_counter % 3 == 0) |
323 |
|
{ |
324 |
|
eeprom_writer(); |
325 |
|
sprintf(global_serial_send, "at+cmgd=1,3\r"); |
326 |
|
serial_send(); |
327 |
|
while ( strstr(global_message_buffer1,"OK") == 0) ; |
328 |
|
} |
329 |
} |
} |
330 |
|
|
331 |
void convertTemp() |
void convertTemp() |
462 |
|
|
463 |
} |
} |
464 |
|
|
465 |
|
void Delay(int time) |
466 |
|
{ |
467 |
|
int wanted = (global_time_counter + time) % global_time_interval; |
468 |
|
|
469 |
|
while (global_time_counter < wanted) ; |
470 |
|
} |
471 |
|
|
472 |
void main() |
void main() |
473 |
{ |
{ |
481 |
rs232_init(); |
rs232_init(); |
482 |
ad_init(); |
ad_init(); |
483 |
lcd_init(0); |
lcd_init(0); |
484 |
|
lcd_clear(); |
485 |
|
lcd_home(); |
486 |
interrupt_init(); |
interrupt_init(); |
|
sms_init(); |
|
|
//eeprom_init(); |
|
487 |
timer_init(); |
timer_init(); |
488 |
|
sms_init(); |
489 |
eeprom_reader(); |
eeprom_reader(); |
490 |
/////////////// |
/////////////// |
491 |
// Main loop // |
// Main loop // |
498 |
while(1) |
while(1) |
499 |
{ |
{ |
500 |
// If there happends to be a critical state on the system, we send a sms. |
// If there happends to be a critical state on the system, we send a sms. |
501 |
if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 ) |
if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING ==1 ) && global_emergency_counter >= 600 ) |
502 |
{ |
{ |
503 |
send_update(); |
send_update(); |
504 |
global_emergency_counter = 0; |
global_emergency_counter = 0; |
505 |
} |
} |
506 |
|
|
507 |
// Every X sec. a status sms is send. |
// Every X sec. a status sms is send. |
508 |
if(global_time_counter >= 3600) |
if(global_time_counter >= global_time_interval) |
509 |
{ |
{ |
510 |
send_update(); |
send_update(); |
511 |
global_time_counter = 0; |
global_time_counter = 0; |
526 |
global_Pot_Hi = ADRESH; |
global_Pot_Hi = ADRESH; |
527 |
global_Pot_Lo = ADRESL; |
global_Pot_Lo = ADRESL; |
528 |
convertTemp(); |
convertTemp(); |
529 |
if (global_time_counter % 5 == 0) |
update_lcd(); |
|
update_lcd(); |
|
530 |
GODONE = 1; |
GODONE = 1; |
531 |
} |
} |
532 |
// Handels the recieve sms'es. |
// Handels the recieve sms'es. |