6 |
|
|
7 |
#include "lcd.h" |
#include "lcd.h" |
8 |
#include "Delay.h" |
#include "Delay.h" |
|
// Delay.h is included inside lcd.c |
|
9 |
|
|
10 |
#define BUFFER 128 |
#define BUFFER 160 |
11 |
#define PWRFAIL RB1 |
#define PWRFAIL RB1 |
12 |
#define FIREDET RB2 |
#define FIREDET RB2 |
13 |
#define FEEDING RB3 |
#define FEEDING RB3 |
16 |
|
|
17 |
unsigned char global_Pot_Hi, global_Pot_Lo; |
unsigned char global_Pot_Hi, global_Pot_Lo; |
18 |
unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; |
unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; |
19 |
bit global_recieve_done = 0, global_interval_changed = 0; |
bit global_recieve_done = 0; |
20 |
unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0, global_time_counter_image = 0; |
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; |
unsigned int global_emergency_counter = 600, global_time_interval = 3600; |
22 |
unsigned char global_temp = 0; |
unsigned char global_temp = 0; |
23 |
|
|
45 |
void serial_recieved(void); |
void serial_recieved(void); |
46 |
void serial_send(void); |
void serial_send(void); |
47 |
void update_lcd(void); |
void update_lcd(void); |
48 |
void convertTemp(void); |
void convert_temp(void); |
49 |
void timer1_interrupt(void); |
void timer1_interrupt(void); |
50 |
void on_recieve(void); |
void on_recieve(void); |
51 |
void on_initial_recieve(void); |
void on_initial_recieve(void); |
59 |
global_recieve_done = 0; |
global_recieve_done = 0; |
60 |
|
|
61 |
global_message_buffer_length1 = 0; |
global_message_buffer_length1 = 0; |
|
//global_message_buffer1[0] = 0; |
|
62 |
global_message_buffer_length2 = 0; |
global_message_buffer_length2 = 0; |
|
//global_message_buffer2[0] = 0; |
|
63 |
|
|
64 |
memset(global_message_buffer1, 0, BUFFER); |
memset(global_message_buffer1, 0, BUFFER); |
65 |
memset(global_message_buffer2, 0, BUFFER); |
memset(global_message_buffer2, 0, BUFFER); |
84 |
ADCS1 = 0; |
ADCS1 = 0; |
85 |
ADCS2 = 0; |
ADCS2 = 0; |
86 |
|
|
87 |
//Select AN0/RA0 for AD source |
//Select AN0/RA0 for AD source. |
88 |
// In this (000) setup, it's only RA0/AN0 that does ad convertion. |
// In this (000) setup, it's only RA0/AN0 that does ad convertion. |
89 |
CHS0=0; |
CHS0=0; |
90 |
CHS1=0; |
CHS1=0; |
91 |
CHS2=0; |
CHS2=0; |
124 |
|
|
125 |
void timer_init(void) |
void timer_init(void) |
126 |
{ |
{ |
127 |
TMR1CS = 1; //use external clock |
TMR1CS = 1; //use external clock |
128 |
|
|
129 |
T1CKPS1 = 1; //1:8 prescale |
T1CKPS1 = 1; //1:8 prescale |
130 |
T1CKPS0 = 1; |
T1CKPS0 = 1; |
131 |
|
|
132 |
TMR1H = 0xEF; |
TMR1H = 0xEF; |
133 |
TMR1L = 0xFF; |
TMR1L = 0xFF; |
134 |
|
|
135 |
T1OSCEN = 1; //enable oscillator circuit |
T1OSCEN = 1; //enable oscillator circuit |
136 |
RD16 = 0; //normal 8 bit writes |
RD16 = 0; //normal 8 bit writes |
137 |
TMR1ON = 1; |
TMR1ON = 1; |
138 |
} |
} |
139 |
|
|
149 |
RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register |
RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register |
150 |
} |
} |
151 |
|
|
152 |
void sms_init(void) |
void modem_init(void) |
153 |
{ |
{ |
154 |
int i; |
int i; |
155 |
char buf[2]; |
char buf[2]; |
216 |
|
|
217 |
void on_initial_recieve(void) |
void on_initial_recieve(void) |
218 |
{ |
{ |
|
char imei[16]; |
|
|
char* ptr; |
|
|
char i; |
|
|
char buf[2]; |
|
|
|
|
219 |
if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) |
if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) |
220 |
{ |
{ |
221 |
global_modem_init = 1; |
global_modem_init = 1; |
|
|
|
|
ptr = strstr(global_serial_recieve_buffer,"cgsn"); |
|
|
ptr +=4; |
|
|
strncpy(imei, ptr,15); |
|
|
imei[15] = 0; |
|
|
|
|
222 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
223 |
} |
} |
224 |
} |
} |
291 |
TMR1L = 0xFF; |
TMR1L = 0xFF; |
292 |
global_time_counter++; |
global_time_counter++; |
293 |
global_emergency_counter++; |
global_emergency_counter++; |
|
RA1 = !RA1; |
|
294 |
} |
} |
295 |
|
|
296 |
////////// ORDENARY FUNKTIONS ////////// |
////////// ORDENARY FUNKTIONS ////////// |
297 |
void update_lcd(void) |
void update_lcd(void) |
298 |
{ |
{ |
299 |
if(global_temp_update_display != global_time_counter) |
if(global_temp_update_display != global_time_counter) |
300 |
{ |
{ |
|
// lcd_clear(); |
|
301 |
lcd_goto(0x00); |
lcd_goto(0x00); |
302 |
|
|
303 |
sprintf(global_lcd_buf, "Temp: %3d", global_temp); |
sprintf(global_lcd_buf, "Temp: %3d", global_temp); |
332 |
} |
} |
333 |
} |
} |
334 |
|
|
335 |
void convert_temp() |
void convert_temp(void) |
336 |
{ |
{ |
337 |
short adVal; |
short adVal; |
338 |
adVal = (global_Pot_Hi << 8) | global_Pot_Lo; |
adVal = (global_Pot_Hi << 8) | global_Pot_Lo; |
339 |
if( adVal >=840 ) |
if( adVal >=840 ) // 840 == 4.1V or 82% of 5V, we do this because 4.1V == 100C. |
340 |
global_temp = 100; |
global_temp = 100; |
341 |
else |
else |
342 |
global_temp = (unsigned char) (adVal / 8.3886); |
global_temp = (unsigned char) (adVal / 8.3886); |
390 |
if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS |
if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS |
391 |
{ |
{ |
392 |
ptr = strstr(global_message_buffer1,","); // finds the point just before the nr. of the SMS. |
ptr = strstr(global_message_buffer1,","); // finds the point just before the nr. of the SMS. |
393 |
strcat(tmp,ptr+1); // puts that number in tmp |
strcat(tmp,ptr+1); // puts that number in tmp |
394 |
global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0] |
global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0] |
395 |
strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable. |
strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable. |
396 |
sms_recieved(); |
sms_recieved(); |
397 |
} |
} |
398 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
440 |
|
|
441 |
global_time_interval = atoi(buf); |
global_time_interval = atoi(buf); |
442 |
global_time_interval *= 60; |
global_time_interval *= 60; |
443 |
eeprom_writer(); // writes the new cell nr. and time interval to the eeprom. |
eeprom_writer(); // writes the new cell nr. and time interval to the eeprom. |
444 |
send_sms("conf ok"); |
send_sms("conf ok"); |
445 |
} |
} |
446 |
|
|
461 |
|
|
462 |
// Running init for various components. |
// Running init for various components. |
463 |
pic18_io_init(); |
pic18_io_init(); |
464 |
RA5 = 1; // Indicates that the board is running inits. |
RA5 = 1; // Indicates that the board is running inits. |
465 |
|
|
466 |
rs232_init(); |
rs232_init(); |
467 |
ad_init(); |
ad_init(); |
470 |
lcd_home(); |
lcd_home(); |
471 |
interrupt_init(); |
interrupt_init(); |
472 |
timer_init(); |
timer_init(); |
473 |
sms_init(); |
modem_init(); |
474 |
eeprom_reader(); |
eeprom_reader(); |
475 |
/////////////// |
/////////////// |
476 |
// Main loop // |
// Main loop // |
478 |
DelayMs(50); |
DelayMs(50); |
479 |
reset_recieve_buffer(); |
reset_recieve_buffer(); |
480 |
|
|
481 |
RA5 = 0; // Inits are done, and RA1 will now work as a error notifier. |
RA5 = 0; // Inits are done, and RA1 will now work as a error notifier. |
482 |
|
|
483 |
while(1) |
while(1) |
484 |
{ |
{ |
499 |
if( global_emergency_counter > 7200 ) |
if( global_emergency_counter > 7200 ) |
500 |
global_emergency_counter = 600; |
global_emergency_counter = 600; |
501 |
|
|
|
// Checks if there has been recieved a config sms. |
|
|
if(global_interval_changed ) |
|
|
{ |
|
|
eeprom_writer(); |
|
|
global_interval_changed = 0; |
|
|
} |
|
502 |
// Checking if A/D convertion is done, and save the data in global_Pot_?? |
// Checking if A/D convertion is done, and save the data in global_Pot_?? |
503 |
if(GODONE==0) |
if(GODONE==0) |
504 |
{ |
{ |