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

Diff of /trunk/Embedded/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 163 by hedin, Wed Dec 5 15:39:55 2007 UTC revision 201 by hedin, Sun Dec 9 15:07:32 2007 UTC
# Line 6  Line 6 
6    
7  #include "lcd.h"  #include "lcd.h"
8  #include "Delay.h"  #include "Delay.h"
 // Delay.h is included inside lcd.c  
9    
 #define LCD_LENGTH 16  
 #define LCD_ROWS 2  
10  #define BUFFER 128  #define BUFFER 128
11  #define PWRFAIL RB1  #define PWRFAIL RB1
12  #define FIREDET RB2  #define FIREDET RB2
# Line 20  Line 17 
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, global_interval_changed = 0;
20  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;
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  unsigned char global_temp_old = 0;  
24    unsigned int global_temp_update_display = 0;
25    
26  unsigned char global_message_buffer1[BUFFER];  unsigned char global_message_buffer1[BUFFER];
27  unsigned char global_message_buffer2[BUFFER];  unsigned char global_message_buffer2[BUFFER];
# Line 32  unsigned char global_message_buffer_leng Line 30  unsigned char global_message_buffer_leng
30    
31  unsigned char global_lcd_buf[16];  unsigned char global_lcd_buf[16];
32    
33  unsigned short global_imei_tversum;  unsigned int global_imei_tversum;
34    
35  unsigned char global_cell_nr[15] = ""; // = "21681784";  unsigned char global_cell_nr[15] = ""; // = "21681784";
36  bit global_modem_init = 0;  bit global_modem_init = 0;
# Line 43  unsigned char global_sms_recieve_number[ Line 41  unsigned char global_sms_recieve_number[
41  __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');  __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
42  __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);  __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
43    
44    
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);
# Line 52  void on_recieve(void); Line 51  void on_recieve(void);
51  void on_initial_recieve(void);  void on_initial_recieve(void);
52  void sms_recieved(void);  void sms_recieved(void);
53  void send_sms(const unsigned char* payload);  void send_sms(const unsigned char* payload);
54    void eeprom_writer(void);
55    
56    
57  void reset_recieve_buffer(void)  void reset_recieve_buffer(void)
# Line 66  void reset_recieve_buffer(void) Line 66  void reset_recieve_buffer(void)
66          memset(global_message_buffer1, 0, BUFFER);          memset(global_message_buffer1, 0, BUFFER);
67          memset(global_message_buffer2, 0, BUFFER);          memset(global_message_buffer2, 0, BUFFER);
68  }        }      
69    //////////  INITS  //////////
70  void pic18_io_init(void)  void pic18_io_init(void)
71  {  {
72          TRISA0  = 1;    // analog input          TRISA0  = 1;    // analog input
# Line 98  void ad_init(void) // Nicked from H7 Line 98  void ad_init(void) // Nicked from H7
98          PCFG2=1;          PCFG2=1;
99          PCFG3=1;          PCFG3=1;
100                    
101            //Reset the A/D result registers
102            ADRESH = 0;
103            ADRESL = 0;
104            
105          //Result is right justified          //Result is right justified
106          ADFM=1;          ADFM=1;
107                    
# Line 105  void ad_init(void) // Nicked from H7 Line 109  void ad_init(void) // Nicked from H7
109          ADON=1;          ADON=1;
110  }  }
111    
 void rs232_init(void)  
 {  
         SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled  
         TRISC6  = 0;      
         TRISC7  = 1;  
112    
         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  
 }  
113    
114  void interrupt_init(void)  void interrupt_init(void)
115  {  {
# Line 128  void interrupt_init(void) Line 122  void interrupt_init(void)
122          TMR1IE  = 1;    // Enables timer 1          TMR1IE  = 1;    // Enables timer 1
123  }        }      
124    
125    
126    
127  void timer_init(void)  void timer_init(void)
128  {  {
129          TMR1CS = 1; //use external clock          TMR1CS = 1; //use external clock
# Line 141  void timer_init(void) Line 137  void timer_init(void)
137          T1OSCEN = 1; //enable oscillator circuit                  T1OSCEN = 1; //enable oscillator circuit        
138          RD16 = 0; //normal 8 bit writes          RD16 = 0; //normal 8 bit writes
139          TMR1ON = 1;          TMR1ON = 1;
140  }        }
141            
142    void rs232_init(void)
143    {
144            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 sms_init(void)  void modem_init(void)
155  {  {
156          int i;          int i;
157          char buf[2];          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();          reset_recieve_buffer();
162          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
163          serial_send();          serial_send();
         DelaySek(1);  
         //while(!global_recieve_done) ;  
164                    
165          while (global_has_imei == 0)          while (global_has_imei == 0)
166          {          {
# Line 165  void sms_init(void) Line 172  void sms_init(void)
172                                  buf[0] = global_message_buffer2[i];                                  buf[0] = global_message_buffer2[i];
173                                  buf[1] = 0;                                  buf[1] = 0;
174                                  global_imei_tversum += atoi(buf);                                  global_imei_tversum += atoi(buf);
                                 //global_imei_tversum += (global_message_buffer2[i] - '0');  
175                          }                          }
176                                                    
177                          global_has_imei = 1;                          global_has_imei = 1;
178                  }                  }
179          }          }
180    
181            sprintf(global_serial_send,"%s", "at+cpin?\r");
         sprintf(global_serial_send,"%s", "at+cpin=8043\r");  
182          serial_send();          serial_send();
183            DelayMs(100);           // Delay to give the modem a chance to answer.
184                    
185          while(global_modem_init == 0)          if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0)
186          {          {
187                  on_initial_recieve();                  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          }          }
         DelayMs(5);  
           
195          reset_recieve_buffer();          reset_recieve_buffer();
196          sprintf(global_serial_send, "at+cmgd=1,4\r");          sprintf(global_serial_send, "at+cmgd=1,4\r");
197          serial_send();          serial_send();
198          while ( strstr(global_message_buffer1,"OK") == 0) ;          while ( strstr(global_message_buffer1,"OK") == 0) ;
199  }  }
200    
   
 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;  
         }  
 }                
   
   
201  void serial_send(void)  void serial_send(void)
202  {  {
203          int i;          int i;
# Line 222  void serial_send(void) Line 214  void serial_send(void)
214          DelayMs(150);          DelayMs(150);
215          global_serial_send[0] = 0;          global_serial_send[0] = 0;
216          DelayMs(150);          DelayMs(150);
217  }        }
218    
219  ///////////////////////////////////////////////////////////////////////////////////////////////////  void on_initial_recieve(void)
220  // Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from,  {
221  // global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done.          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)
241    {
242            // Finds out what interrupt have been trigged, and starts the respective function.
243            if(RCIF == 1)                   // Serial recieve interrupt
244            {
245                    serial_recieved();
246                    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)  void serial_recieved(void)
258  {  {
259          char data_byte;          char data_byte;
# Line 242  void serial_recieved(void) Line 269  void serial_recieved(void)
269    
270          if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun          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.                  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          if (data_byte == '\r')  // when we meet a '\r', the transmission is done, and we fill the constxt of
# Line 274  void timer1_interrupt(void) Line 306  void timer1_interrupt(void)
306          global_emergency_counter++;          global_emergency_counter++;
307          RA1 = !RA1;          RA1 = !RA1;
308  }  }
309    //////////  ORDENARY FUNKTIONS  //////////
310  void update_lcd(void)  void update_lcd(void)
311  {  {
312          //lcd_clear();          if(global_temp_update_display != global_time_counter)
313          lcd_goto(0x00);          {
314            //      lcd_clear();
315                    lcd_goto(0x00);
316                    
317          sprintf(global_lcd_buf, "Temp: %3d", global_temp);                  sprintf(global_lcd_buf, "Temp: %3d", global_temp);
318          lcd_puts(global_lcd_buf);                  lcd_puts(global_lcd_buf);
319                    global_temp_update_display = global_time_counter;
320            }
321    
322  }  }
323                    
# Line 301  void send_sms(const unsigned char* paylo Line 337  void send_sms(const unsigned char* paylo
337          sprintf(global_serial_send, "%s%c", payload, 0x1A);          sprintf(global_serial_send, "%s%c", payload, 0x1A);
338          serial_send();          serial_send();
339          DelayMs(150);          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 convertTemp()  void convert_temp()
350  {  {
351          short adVal;          short adVal;
352          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
# Line 349  void eeprom_reader(void) Line 391  void eeprom_reader(void)
391          global_cell_nr[i] = 0; //zero terminated!          global_cell_nr[i] = 0; //zero terminated!
392  }        }      
393    
 void on_initial_recieve(void)  
 {  
         char imei[16];  
         char* ptr;  
         char i;  
         char buf[2];  
           
         if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)  
         {  
                 global_modem_init = 1;  
   
                 ptr = strstr(global_serial_recieve_buffer,"cgsn");  
                 ptr +=4;  
                 strncpy(imei, ptr,15);  
                 imei[15] = 0;  
                   
                 reset_recieve_buffer();  
         }  
 }  
394    
395  void on_recieve(void)  void on_recieve(void)
396  {  {
# Line 381  void on_recieve(void) Line 404  void on_recieve(void)
404          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
405          {          {
406                  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.
407                  strcat(tmp,ptr+1);                                                      // puts that number in tmp                  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]                  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.                  strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable.
410                  sms_recieved();                  sms_recieved();
411          }                }      
412          reset_recieve_buffer();          reset_recieve_buffer();
# Line 431  void sms_recieved(void) Line 454  void sms_recieved(void)
454                                    
455                  global_time_interval = atoi(buf);                  global_time_interval = atoi(buf);
456                  global_time_interval *= 60;                  global_time_interval *= 60;
457                  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.
458                  send_sms("conf ok");                  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  {  {
# Line 447  void main() Line 475  void main()
475    
476          // Running init for various components.          // Running init for various components.
477          pic18_io_init();          pic18_io_init();
478          RA5 = 1;                        // Indicates that the board is running inits.          RA5 = 1;                // Indicates that the board is running inits.
479                    
480          rs232_init();          rs232_init();
481          ad_init();          ad_init();
482          lcd_init(0);          lcd_init(0);
483            lcd_clear();
484            lcd_home();
485          interrupt_init();          interrupt_init();
         sms_init();  
         //eeprom_init();  
486          timer_init();          timer_init();
487            modem_init();
488          eeprom_reader();          eeprom_reader();
489  ///////////////  ///////////////
490  // Main loop //  // Main loop //
# Line 468  void main() Line 497  void main()
497          while(1)          while(1)
498          {          {
499          // 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.
500                  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 )
501                  {                  {
502                          send_update();                          send_update();
503                          global_emergency_counter = 0;                          global_emergency_counter = 0;
504                  }                  }
505                    
506          // Every X sec. a status sms is send.          // Every X sec. a status sms is send.
507                  if(global_time_counter >= 3600)                  if(global_time_counter >= global_time_interval)
508                  {                  {
509                          send_update();                          send_update();
510                          global_time_counter = 0;                          global_time_counter = 0;
# Line 494  void main() Line 524  void main()
524                  {                  {
525                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
526                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
527                          convertTemp();                          convert_temp();
528                          if (global_temp != global_temp_old)                          update_lcd();
                         {  
                                 update_lcd();  
                                 global_temp_old = global_temp;  
                         }  
529                          GODONE = 1;                          GODONE = 1;
530                  }                  }
531          // Handels the recieve sms'es.          // Handels the recieve sms'es.

Legend:
Removed from v.163  
changed lines
  Added in v.201

  ViewVC Help
Powered by ViewVC 1.1.20