/[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 149 by hedin, Tue Dec 4 17:23:27 2007 UTC revision 150 by hedin, Tue Dec 4 19:07:00 2007 UTC
# Line 9  Line 9 
9    
10  #define LCD_LENGTH 16  #define LCD_LENGTH 16
11  #define LCD_ROWS 2  #define LCD_ROWS 2
12  #define SEND_BUFFER 128  #define BUFFER 128
13  #define PWRFAIL RB1  #define PWRFAIL RB1
14  #define FIREDET RB2  #define FIREDET RB2
15  #define FEEDING RB3  #define FEEDING RB3
# Line 18  Line 18 
18    
19  unsigned char global_Pot_Hi, global_Pot_Lo;  unsigned char global_Pot_Hi, global_Pot_Lo;
20  unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];  unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];
21  unsigned char global_serial_send[SEND_BUFFER], global_serial_recieve_buffer[SEND_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;
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 char cell_nr[15] = ""; // = "21681784";  unsigned short global_imei_tversum;
28    
29    unsigned char cell_nr[15] = ""; // = "21681784";
30    bit global_modem_init = 0;
31    
32  __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');  __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
33  __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);  __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
# Line 35  void serial_send(void); Line 37  void serial_send(void);
37  void update_lcd(void);  void update_lcd(void);
38  void convertTemp(void);  void convertTemp(void);
39  void timer1_interrupt(void);  void timer1_interrupt(void);
40    void on_recieve(void);
41    void on_initial_recieve(void);
42    
43    
44    void reset_recieve_buffer(void)
45    {
46            global_recieve_done = 0;
47            global_serial_byte_counter=0;
48    }      
49    
50  // Nicked from H7  
51  void ad_init(void)  void ad_init(void) // Nicked from H7
52  {  {
53          // AD Conversion clock          // AD Conversion clock
54          ADCS0 = 0;          ADCS0 = 0;
# Line 115  void sms_init(void) Line 126  void sms_init(void)
126  {  {
127          int i;          int i;
128    
129            reset_recieve_buffer();
130          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
131          serial_send();          serial_send();
132          DelaySek(1);          DelaySek(1);
# Line 123  void sms_init(void) Line 135  void sms_init(void)
135    
136          sprintf(global_serial_send,"%s", "at+cpin=8043\r");          sprintf(global_serial_send,"%s", "at+cpin=8043\r");
137          serial_send();          serial_send();
         DelaySek(30);  
138                    
139          update_lcd();          while(global_modem_init == 0)
140          DelaySek(5);          {
141                    on_initial_recieve();
142            }
143  }  }
144    
145    
# Line 151  void serial_send(void) Line 164  void serial_send(void)
164  {  {
165          int i;          int i;
166          char data_byte;          char data_byte;
167          for(i = 0; i < SEND_BUFFER; i++)          for(i = 0; i < BUFFER; i++)
168          {          {
169                  data_byte = global_serial_send[i];                  data_byte = global_serial_send[i];
170                  if( data_byte == '\r')                  if( data_byte == '\r')
171                          i = (SEND_BUFFER - 1);                          i = (BUFFER - 1);
172                  TXREG = data_byte;                  TXREG = data_byte;
173                  while(TRMT==0) ;                  while(TRMT==0) ;
174                  DelayMs(10);                  DelayMs(10);
# Line 170  void serial_recieved(void) Line 183  void serial_recieved(void)
183                    
184          data_byte = RCREG;          data_byte = RCREG;
185                    
186            if (data_byte == '\n')
187                    return;
188    
189          global_serial_recieve_buffer[global_serial_byte_counter] = data_byte;          global_serial_recieve_buffer[global_serial_byte_counter] = data_byte;
190          if(data_byte == '\r')                  
191            if (data_byte == '\r')
192          {          {
193                  global_recieve_done = 1;                  global_recieve_done = 1;
194                  global_serial_byte_counter = 0;                  //global_serial_byte_counter = 0;
195                    global_serial_recieve_buffer[global_serial_byte_counter+1] = 0; //zero terminate
196          }                }      
197          else          else
198          {          {
# Line 257  void eeprom_reader(void) Line 275  void eeprom_reader(void)
275          cell_nr[i] = 0; //zero terminated!          cell_nr[i] = 0; //zero terminated!
276  }        }      
277    
278    void on_initial_recieve(void)
279    {
280            char imei[16];
281            char* ptr;
282            char i;
283            
284            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
285            {
286                    global_modem_init = 1;
287    
288                    ptr = strstr(global_serial_recieve_buffer,"cgsn");
289                    ptr +=4;
290                    strncpy(imei, ptr,15);
291                    imei[15] = 0;
292    
293                    global_imei_tversum = 0;
294                    for (i=0; i<15; ++i)
295                    {
296                            global_imei_tversum += (imei[i] - '0');
297                    }
298                    
299                    
300                    reset_recieve_buffer();
301            }
302            
303    
304    }
305    
306    
307  void on_recieve(void)  void on_recieve(void)
308  {  {
309            char tmp[17];
310            if (global_recieve_done == 0)
311                    return;
312                    
313            lcd_clear();
314            lcd_goto(0x00);
315            tmp[0] = 0;
316            strncat(tmp,global_serial_recieve_buffer,16);
317            lcd_puts(tmp);
318            if (strncmp(global_serial_recieve_buffer,"+WIND: 11", 3) == 0)
319            {
320                    global_modem_init = 1;
321            }
322                    
323            reset_recieve_buffer();
324  }  }
325    
326  void main()  void main()
# Line 279  void main() Line 340  void main()
340          eeprom_reader();          eeprom_reader();
341  ///////////////  ///////////////
342  // Main loop //  // Main loop //
343  send_update();  
344    
345          while(1)          while(1)
346          {          {
347          // 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.
# Line 313  send_update(); Line 375  send_update();
375                          update_lcd();                          update_lcd();
376                          GODONE = 1;                          GODONE = 1;
377                  }                  }
378                    
379                    //if (global_recieve_done)
380                    //{
381                            on_recieve();
382                    //      global_recieve_done = 0;
383                    //      global_serial_byte_counter = 0;
384                    //}
385          }          }
386  }  }

Legend:
Removed from v.149  
changed lines
  Added in v.150

  ViewVC Help
Powered by ViewVC 1.1.20