/[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 173 by hedin, Thu Dec 6 12:30:39 2007 UTC revision 214 by hedin, Mon Dec 10 08:39:47 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    
10  #define LCD_LENGTH 16  #define BUFFER 160
 #define LCD_ROWS 2  
 #define BUFFER 128  
11  #define PWRFAIL RB1  #define PWRFAIL RB1
12  #define FIREDET RB2  #define FIREDET RB2
13  #define FEEDING RB3  #define FEEDING RB3
# Line 19  Line 16 
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;  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    
24  unsigned char global_temp_update_display = 0;  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 33  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 44  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);
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);
# Line 55  void sms_recieved(void); Line 53  void sms_recieved(void);
53  void send_sms(const unsigned char* payload);  void send_sms(const unsigned char* payload);
54  void eeprom_writer(void);  void eeprom_writer(void);
55    
56    
57  void reset_recieve_buffer(void)  void reset_recieve_buffer(void)
58  {  {
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);
66  }        }      
67    //////////  INITS  //////////
68  void pic18_io_init(void)  void pic18_io_init(void)
69  {  {
70          TRISA0  = 1;    // analog input          TRISA0  = 1;    // analog input
# Line 87  void ad_init(void) // Nicked from H7 Line 84  void ad_init(void) // Nicked from H7
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;
# Line 110  void ad_init(void) // Nicked from H7 Line 107  void ad_init(void) // Nicked from H7
107          ADON=1;          ADON=1;
108  }  }
109    
 void rs232_init(void)  
 {  
         SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled  
         TRISC6  = 0;      
         TRISC7  = 1;  
110    
         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  
 }  
111    
112  void interrupt_init(void)  void interrupt_init(void)
113  {  {
# Line 133  void interrupt_init(void) Line 120  void interrupt_init(void)
120          TMR1IE  = 1;    // Enables timer 1          TMR1IE  = 1;    // Enables timer 1
121  }        }      
122    
123    
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            
140    void rs232_init(void)
141    {
142            SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled
143            TRISC6  = 0;    
144            TRISC7  = 1;
145    
146  void sms_init(void)          SPBRG   = 207;  // 1200 baud rate... 25 = 9600
147                                            // x = (Fosc / (16*[baud rate]) )-1
148            TXSTA   = 0x24; // Enables BRGH and TXEN inthe TXSTA register
149            RCSTA   = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
150    }
151    
152    void modem_init(void)
153  {  {
154          int i;          int i;
155          char buf[2];          char buf[2];
# Line 175  void sms_init(void) Line 176  void sms_init(void)
176                  }                  }
177          }          }
178    
179            sprintf(global_serial_send,"%s", "at+cpin?\r");
         sprintf(global_serial_send,"%s", "at+cpin=8043\r");  
180          serial_send();          serial_send();
181            DelayMs(100);           // Delay to give the modem a chance to answer.
182                    
183          while(global_modem_init == 0)          if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0)
184          {          {
185                  on_initial_recieve();                  sprintf(global_serial_send,"%s", "at+cpin=8043\r");
186                    serial_send();
187                    
188                    while(global_modem_init == 0)
189                    {
190                            on_initial_recieve();
191                    }
192          }          }
         DelayMs(5);  
           
193          reset_recieve_buffer();          reset_recieve_buffer();
194          sprintf(global_serial_send, "at+cmgd=1,4\r");          sprintf(global_serial_send, "at+cmgd=1,4\r");
195          serial_send();          serial_send();
196          while ( strstr(global_message_buffer1,"OK") == 0) ;          while ( strstr(global_message_buffer1,"OK") == 0) ;
197  }  }
198    
   
 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;  
         }  
 }                
   
   
199  void serial_send(void)  void serial_send(void)
200  {  {
201          int i;          int i;
# Line 225  void serial_send(void) Line 212  void serial_send(void)
212          DelayMs(150);          DelayMs(150);
213          global_serial_send[0] = 0;          global_serial_send[0] = 0;
214          DelayMs(150);          DelayMs(150);
215  }        }
216    
217    void on_initial_recieve(void)
218    {
219            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
220            {
221                    global_modem_init = 1;
222                    reset_recieve_buffer();
223            }
224    }
225    
226    //////////  INTERRUPT HANDLER  //////////
227    void interrupt interrupt_handler(void)
228    {
229            // Finds out what interrupt have been trigged, and starts the respective function.
230            if(RCIF == 1)                   // Serial recieve interrupt
231            {
232                    serial_recieved();
233                    RCIF = 0;
234            }
235            
236            if(TMR1IF == 1)                 // timer1 interrupt trigger.
237            {
238                    timer1_interrupt();
239                    TMR1IF = 0;
240            }
241    }              
242    
243  ///////////////////////////////////////////////////////////////////////////////////////////////////  //////////  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.  
244  void serial_recieved(void)  void serial_recieved(void)
245  {  {
246          char data_byte;          char data_byte;
# Line 280  void timer1_interrupt(void) Line 291  void timer1_interrupt(void)
291          TMR1L = 0xFF;          TMR1L = 0xFF;
292          global_time_counter++;          global_time_counter++;
293          global_emergency_counter++;          global_emergency_counter++;
294          RA1 = !RA1;  //      RA1 = !RA1;
295  }  }
296    //////////  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)
# Line 313  void send_sms(const unsigned char* paylo Line 324  void send_sms(const unsigned char* paylo
324          sprintf(global_serial_send, "%s%c", payload, 0x1A);          sprintf(global_serial_send, "%s%c", payload, 0x1A);
325          serial_send();          serial_send();
326          DelayMs(150);          DelayMs(150);
327          if(global_sms_counter % 3 == 0)          if(global_sms_counter % 4 == 0)
328          {          {
329                  eeprom_writer();                  eeprom_writer();
330                  sprintf(global_serial_send, "at+cmgd=1,3\r");                  sprintf(global_serial_send, "at+cmgd=1,3\r");
# Line 322  void send_sms(const unsigned char* paylo Line 333  void send_sms(const unsigned char* paylo
333          }          }
334  }        }      
335    
336  void convertTemp()  void convert_temp(void)
337  {  {
338          short adVal;          short adVal;
339          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
# Line 367  void eeprom_reader(void) Line 378  void eeprom_reader(void)
378          global_cell_nr[i] = 0; //zero terminated!          global_cell_nr[i] = 0; //zero terminated!
379  }        }      
380    
 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();  
         }  
 }  
381    
382  void on_recieve(void)  void on_recieve(void)
383  {  {
# Line 399  void on_recieve(void) Line 391  void on_recieve(void)
391          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
392          {          {
393                  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.
394                  strcat(tmp,ptr+1);                                                      // puts that number in tmp                  strcat(tmp,ptr+1);                      // puts that number in tmp
395                  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]
396                  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.
397                  sms_recieved();                  sms_recieved();
398          }                }      
399          reset_recieve_buffer();          reset_recieve_buffer();
# Line 449  void sms_recieved(void) Line 441  void sms_recieved(void)
441                                    
442                  global_time_interval = atoi(buf);                  global_time_interval = atoi(buf);
443                  global_time_interval *= 60;                  global_time_interval *= 60;
444                  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.
445                  send_sms("conf ok");                  send_sms("conf ok");
446          }          }
447                    
448    
449  }  }
450    
451    void Delay(int time)
452    {
453            int wanted = (global_time_counter + time) % global_time_interval;
454            
455            while (global_time_counter < wanted) ;
456    }
457    
458  void main()  void main()
459  {  {
# Line 465  void main() Line 462  void main()
462    
463          // Running init for various components.          // Running init for various components.
464          pic18_io_init();          pic18_io_init();
465          RA5 = 1;                        // Indicates that the board is running inits.          RA5 = 1;                // Indicates that the board is running inits.
466                    
467          rs232_init();          rs232_init();
468          ad_init();          ad_init();
469          lcd_init(0);          lcd_init(0);
470            lcd_clear();
471            lcd_home();
472          interrupt_init();          interrupt_init();
473          timer_init();          timer_init();
474          sms_init();          modem_init();
475          eeprom_reader();          eeprom_reader();
476  ///////////////  ///////////////
477  // Main loop //  // Main loop //
# Line 480  void main() Line 479  void main()
479          DelayMs(50);          DelayMs(50);
480          reset_recieve_buffer();          reset_recieve_buffer();
481    
482          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.
483    
484          while(1)          while(1)
485          {          {
# Line 490  void main() Line 489  void main()
489                          send_update();                          send_update();
490                          global_emergency_counter = 0;                          global_emergency_counter = 0;
491                  }                  }
492                    
493          // Every X sec. a status sms is send.          // Every X sec. a status sms is send.
494                  if(global_time_counter >= 3600)                  if(global_time_counter >= global_time_interval)
495                  {                  {
496                          send_update();                          send_update();
497                          global_time_counter = 0;                          global_time_counter = 0;
# Line 500  void main() Line 500  void main()
500                  if( global_emergency_counter > 7200 )                  if( global_emergency_counter > 7200 )
501                          global_emergency_counter = 600;                          global_emergency_counter = 600;
502    
         // Checks if there has been recieved a config sms.  
                 if(global_interval_changed )  
                 {  
                         eeprom_writer();  
                         global_interval_changed = 0;  
                 }  
503          // 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_??
504                  if(GODONE==0)                  if(GODONE==0)
505                  {                  {
506                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
507                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
508                          convertTemp();                          convert_temp();
509                          update_lcd();                          update_lcd();
510                          GODONE = 1;                          GODONE = 1;
511                  }                  }

Legend:
Removed from v.173  
changed lines
  Added in v.214

  ViewVC Help
Powered by ViewVC 1.1.20