/[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 165 by hedin, Wed Dec 5 16:00:03 2007 UTC revision 178 by hedin, Thu Dec 6 14:19:51 2007 UTC
# Line 8  Line 8 
8  #include "Delay.h"  #include "Delay.h"
9  // Delay.h is included inside lcd.c  // Delay.h is included inside lcd.c
10    
 #define LCD_LENGTH 16  
 #define LCD_ROWS 2  
11  #define BUFFER 128  #define BUFFER 128
12  #define PWRFAIL RB1  #define PWRFAIL RB1
13  #define FIREDET RB2  #define FIREDET RB2
# Line 20  Line 18 
18  unsigned char global_Pot_Hi, global_Pot_Lo;  unsigned char global_Pot_Hi, global_Pot_Lo;
19  unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];  unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];
20  bit global_recieve_done = 0, global_interval_changed = 0;  bit global_recieve_done = 0, global_interval_changed = 0;
21  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;
22  unsigned int global_emergency_counter = 600, global_time_interval = 3600;  unsigned int global_emergency_counter = 600, global_time_interval = 3600;
23  unsigned char global_temp = 0;  unsigned char global_temp = 0;
24    
25  unsigned char global_temp_update_display = 0;  unsigned int global_temp_update_display = 0;
26    
27  unsigned char global_message_buffer1[BUFFER];  unsigned char global_message_buffer1[BUFFER];
28  unsigned char global_message_buffer2[BUFFER];  unsigned char global_message_buffer2[BUFFER];
# Line 33  unsigned char global_message_buffer_leng Line 31  unsigned char global_message_buffer_leng
31    
32  unsigned char global_lcd_buf[16];  unsigned char global_lcd_buf[16];
33    
34  unsigned short global_imei_tversum;  unsigned int global_imei_tversum;
35    
36  unsigned char global_cell_nr[15] = ""; // = "21681784";  unsigned char global_cell_nr[15] = ""; // = "21681784";
37  bit global_modem_init = 0;  bit global_modem_init = 0;
# Line 44  unsigned char global_sms_recieve_number[ Line 42  unsigned char global_sms_recieve_number[
42  __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');  __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
43  __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);  __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
44    
45    
46  void serial_recieved(void);  void serial_recieved(void);
47  void serial_send(void);  void serial_send(void);
48  void update_lcd(void);  void update_lcd(void);
# Line 53  void on_recieve(void); Line 52  void on_recieve(void);
52  void on_initial_recieve(void);  void on_initial_recieve(void);
53  void sms_recieved(void);  void sms_recieved(void);
54  void send_sms(const unsigned char* payload);  void send_sms(const unsigned char* payload);
55    void eeprom_writer(void);
56    
57    
58  void reset_recieve_buffer(void)  void reset_recieve_buffer(void)
# Line 67  void reset_recieve_buffer(void) Line 67  void reset_recieve_buffer(void)
67          memset(global_message_buffer1, 0, BUFFER);          memset(global_message_buffer1, 0, BUFFER);
68          memset(global_message_buffer2, 0, BUFFER);          memset(global_message_buffer2, 0, BUFFER);
69  }        }      
70    //////////  INITS  //////////
71  void pic18_io_init(void)  void pic18_io_init(void)
72  {  {
73          TRISA0  = 1;    // analog input          TRISA0  = 1;    // analog input
# Line 99  void ad_init(void) // Nicked from H7 Line 99  void ad_init(void) // Nicked from H7
99          PCFG2=1;          PCFG2=1;
100          PCFG3=1;          PCFG3=1;
101                    
102            //Reset the A/D result registers
103            ADRESH = 0;
104            ADRESL = 0;
105            
106          //Result is right justified          //Result is right justified
107          ADFM=1;          ADFM=1;
108                    
# Line 106  void ad_init(void) // Nicked from H7 Line 110  void ad_init(void) // Nicked from H7
110          ADON=1;          ADON=1;
111  }  }
112    
 void rs232_init(void)  
 {  
         SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled  
         TRISC6  = 0;      
         TRISC7  = 1;  
113    
         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  
 }  
114    
115  void interrupt_init(void)  void interrupt_init(void)
116  {  {
# Line 129  void interrupt_init(void) Line 123  void interrupt_init(void)
123          TMR1IE  = 1;    // Enables timer 1          TMR1IE  = 1;    // Enables timer 1
124  }        }      
125    
126    
127    
128  void timer_init(void)  void timer_init(void)
129  {  {
130          TMR1CS = 1; //use external clock          TMR1CS = 1; //use external clock
# Line 142  void timer_init(void) Line 138  void timer_init(void)
138          T1OSCEN = 1; //enable oscillator circuit                  T1OSCEN = 1; //enable oscillator circuit        
139          RD16 = 0; //normal 8 bit writes          RD16 = 0; //normal 8 bit writes
140          TMR1ON = 1;          TMR1ON = 1;
141  }        }
142            
143    void rs232_init(void)
144    {
145            SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled
146            TRISC6  = 0;    
147            TRISC7  = 1;
148    
149            SPBRG   = 207;  // 1200 baud rate... 25 = 9600
150                                            // x = (Fosc / (16*[baud rate]) )-1
151            TXSTA   = 0x24; // Enables BRGH and TXEN inthe TXSTA register
152            RCSTA   = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
153    }
154    
155  void sms_init(void)  void sms_init(void)
156  {  {
157          int i;          int i;
158          char buf[2];          char buf[2];
159            
160            while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ;  // Waiting for the modem to be ready
161    
162          reset_recieve_buffer();          reset_recieve_buffer();
163          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
164          serial_send();          serial_send();
         DelaySek(1);  
         //while(!global_recieve_done) ;  
165                    
166          while (global_has_imei == 0)          while (global_has_imei == 0)
167          {          {
# Line 166  void sms_init(void) Line 173  void sms_init(void)
173                                  buf[0] = global_message_buffer2[i];                                  buf[0] = global_message_buffer2[i];
174                                  buf[1] = 0;                                  buf[1] = 0;
175                                  global_imei_tversum += atoi(buf);                                  global_imei_tversum += atoi(buf);
                                 //global_imei_tversum += (global_message_buffer2[i] - '0');  
176                          }                          }
177                                                    
178                          global_has_imei = 1;                          global_has_imei = 1;
179                  }                  }
180          }          }
181    
182            sprintf(global_serial_send,"%s", "at+cpin?\r");
         sprintf(global_serial_send,"%s", "at+cpin=8043\r");  
183          serial_send();          serial_send();
184            DelayMs(100);           // Delay to give the modem a chance to answer.
185                    
186          while(global_modem_init == 0)          if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0)
187          {          {
188                  on_initial_recieve();                  sprintf(global_serial_send,"%s", "at+cpin=8043\r");
189                    serial_send();
190                    
191                    while(global_modem_init == 0)
192                    {
193                            on_initial_recieve();
194                    }
195          }          }
         DelayMs(5);  
           
196          reset_recieve_buffer();          reset_recieve_buffer();
197          sprintf(global_serial_send, "at+cmgd=1,4\r");          sprintf(global_serial_send, "at+cmgd=1,4\r");
198          serial_send();          serial_send();
199          while ( strstr(global_message_buffer1,"OK") == 0) ;          while ( strstr(global_message_buffer1,"OK") == 0) ;
200  }  }
201    
   
 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;  
         }  
 }                
   
   
202  void serial_send(void)  void serial_send(void)
203  {  {
204          int i;          int i;
# Line 223  void serial_send(void) Line 215  void serial_send(void)
215          DelayMs(150);          DelayMs(150);
216          global_serial_send[0] = 0;          global_serial_send[0] = 0;
217          DelayMs(150);          DelayMs(150);
218  }        }
219    
220    void on_initial_recieve(void)
221    {
222            char imei[16];
223            char* ptr;
224            char i;
225            char buf[2];
226            
227            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
228            {
229                    global_modem_init = 1;
230    
231                    ptr = strstr(global_serial_recieve_buffer,"cgsn");
232                    ptr +=4;
233                    strncpy(imei, ptr,15);
234                    imei[15] = 0;
235                    
236                    reset_recieve_buffer();
237            }
238    }
239    
240    //////////  INTERRUPT HANDLER  //////////
241    void interrupt interrupt_handler(void)
242    {
243            // Finds out what interrupt have been trigged, and starts the respective function.
244            if(RCIF == 1)                   // Serial recieve interrupt
245            {
246                    serial_recieved();
247                    RCIF = 0;
248            }
249            
250            if(TMR1IF == 1)                 // timer1 interrupt trigger.
251            {
252                    timer1_interrupt();
253                    TMR1IF = 0;
254            }
255    }              
256    
257  ///////////////////////////////////////////////////////////////////////////////////////////////////  //////////  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.  
258  void serial_recieved(void)  void serial_recieved(void)
259  {  {
260          char data_byte;          char data_byte;
# Line 243  void serial_recieved(void) Line 270  void serial_recieved(void)
270    
271          if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun          if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
272                  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.
273                    else
274            {
275                    global_serial_recieve_buffer[0] = 0;
276                    global_serial_byte_counter = 0;
277                    return;
278            }
279                    
280                    
281          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 275  void timer1_interrupt(void) Line 307  void timer1_interrupt(void)
307          global_emergency_counter++;          global_emergency_counter++;
308          RA1 = !RA1;          RA1 = !RA1;
309  }  }
310    //////////  ORDENARY FUNKTIONS  //////////
311  void update_lcd(void)  void update_lcd(void)
312  {  {
313          if(global_temp_update_display != global_time_counter)          if(global_temp_update_display != global_time_counter)
314          {          {
315                  lcd_clear();          //      lcd_clear();
316                  lcd_goto(0x00);                  lcd_goto(0x00);
317                    
318                  sprintf(global_lcd_buf, "Temp: %3d", global_temp);                  sprintf(global_lcd_buf, "Temp: %3d", global_temp);
# Line 306  void send_sms(const unsigned char* paylo Line 338  void send_sms(const unsigned char* paylo
338          sprintf(global_serial_send, "%s%c", payload, 0x1A);          sprintf(global_serial_send, "%s%c", payload, 0x1A);
339          serial_send();          serial_send();
340          DelayMs(150);          DelayMs(150);
341                    if(global_sms_counter % 4 == 0)
342            {
343                    eeprom_writer();
344                    sprintf(global_serial_send, "at+cmgd=1,3\r");
345                    serial_send();
346                    while ( strstr(global_message_buffer1,"OK") == 0) ;
347            }
348  }        }      
349    
350  void convertTemp()  void convert_temp()
351  {  {
352          short adVal;          short adVal;
353          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
# Line 354  void eeprom_reader(void) Line 392  void eeprom_reader(void)
392          global_cell_nr[i] = 0; //zero terminated!          global_cell_nr[i] = 0; //zero terminated!
393  }        }      
394    
 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();  
         }  
 }  
395    
396  void on_recieve(void)  void on_recieve(void)
397  {  {
# Line 443  void sms_recieved(void) Line 462  void sms_recieved(void)
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  {  {
# Line 457  void main() Line 481  void main()
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 //
# Line 473  void main() Line 498  void main()
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;
# Line 499  void main() Line 525  void main()
525                  {                  {
526                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
527                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
528                          convertTemp();                          convert_temp();
529                          update_lcd();                          update_lcd();
530                          GODONE = 1;                          GODONE = 1;
531                  }                  }

Legend:
Removed from v.165  
changed lines
  Added in v.178

  ViewVC Help
Powered by ViewVC 1.1.20