/[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 160 by hedin, Wed Dec 5 15:01:15 2007 UTC revision 173 by hedin, Thu Dec 6 12:30:39 2007 UTC
# Line 24  unsigned int global_serial_byte_counter Line 24  unsigned int global_serial_byte_counter
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 global_temp_update_display = 0;
28    
29  unsigned char global_message_buffer1[BUFFER];  unsigned char global_message_buffer1[BUFFER];
30  unsigned char global_message_buffer2[BUFFER];  unsigned char global_message_buffer2[BUFFER];
31  unsigned char global_message_buffer_length1;  unsigned char global_message_buffer_length1;
# Line 50  void timer1_interrupt(void); Line 52  void timer1_interrupt(void);
52  void on_recieve(void);  void on_recieve(void);
53  void on_initial_recieve(void);  void on_initial_recieve(void);
54  void sms_recieved(void);  void sms_recieved(void);
55  void send_sms(unsigned char* payload);  void send_sms(const unsigned char* payload);
56    void eeprom_writer(void);
57    
58  void reset_recieve_buffer(void)  void reset_recieve_buffer(void)
59  {  {
# Line 97  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 146  void sms_init(void) Line 152  void sms_init(void)
152  {  {
153          int i;          int i;
154          char buf[2];          char buf[2];
155            
156            while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ;  // Waiting for the modem to be ready
157    
158          reset_recieve_buffer();          reset_recieve_buffer();
159          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
160          serial_send();          serial_send();
         DelaySek(1);  
         //while(!global_recieve_done) ;  
161                    
162          while (global_has_imei == 0)          while (global_has_imei == 0)
163          {          {
# Line 164  void sms_init(void) Line 169  void sms_init(void)
169                                  buf[0] = global_message_buffer2[i];                                  buf[0] = global_message_buffer2[i];
170                                  buf[1] = 0;                                  buf[1] = 0;
171                                  global_imei_tversum += atoi(buf);                                  global_imei_tversum += atoi(buf);
                                 //global_imei_tversum += (global_message_buffer2[i] - '0');  
172                          }                          }
173                                                    
174                          global_has_imei = 1;                          global_has_imei = 1;
# Line 218  void serial_send(void) Line 222  void serial_send(void)
222                  while(TRMT==0) ;                  while(TRMT==0) ;
223                  DelayMs(10);                  DelayMs(10);
224          }          }
225          DelayMs(250);          DelayMs(150);
226          global_serial_send[0] = 0;          global_serial_send[0] = 0;
227          DelayMs(250);          DelayMs(150);
228  }        }      
229    
230  ///////////////////////////////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////////////////////////////
# Line 228  void serial_send(void) Line 232  void serial_send(void)
232  // global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done.  // global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done.
233  void serial_recieved(void)  void serial_recieved(void)
234  {  {
235          char data_byte, saved_data[LCD_LENGTH];          char data_byte;
236                    
237          data_byte = RCREG;          data_byte = RCREG;
238                    
# Line 238  void serial_recieved(void) Line 242  void serial_recieved(void)
242          if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.          if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.
243                  return;                  return;
244    
 //      if (global_serial_byte_counter == BUFFER -1)  
 //              global_seriaæl_recieve_buffer[global_serial_byte_counter] = 0;  
245    
246  //      if (data_byte != '\r' && (global_serial_byte_counter-1) < BUFFER)                if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
247  //              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.
248          global_serial_recieve_buffer[ global_serial_byte_counter] = data_byte;          else
249            {
250                    global_serial_recieve_buffer[0] = 0;
251                    global_serial_byte_counter = 0;
252                    return;
253            }
254                    
255                    
256          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 265  void serial_recieved(void) Line 272  void serial_recieved(void)
272                                    
273                  global_serial_byte_counter = 0;                  global_serial_byte_counter = 0;
274          }                }      
         else  
         {  
                 global_serial_byte_counter++;  
         }  
           
275  }  }
276    
277  void timer1_interrupt(void)  void timer1_interrupt(void)
# Line 283  void timer1_interrupt(void) Line 285  void timer1_interrupt(void)
285    
286  void update_lcd(void)  void update_lcd(void)
287  {  {
288          lcd_clear();          if(global_temp_update_display != global_time_counter)
289          lcd_goto(0x00);          {
290            //      lcd_clear();
291                    lcd_goto(0x00);
292                    
293          sprintf(global_lcd_buf, "Temp: %d", global_temp);                  sprintf(global_lcd_buf, "Temp: %3d", global_temp);
294          lcd_puts(global_lcd_buf);                  lcd_puts(global_lcd_buf);
295                    global_temp_update_display = global_time_counter;
296            }
297    
298  }  }
299                    
# Line 300  void send_update(void) Line 306  void send_update(void)
306          global_sms_counter++;          global_sms_counter++;
307  }        }      
308    
309  void send_sms(unsigned char* payload)  void send_sms(const unsigned char* payload)
310  {  {
311          sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);          sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
312          serial_send();          serial_send();
313          sprintf(global_serial_send, "%s%c", payload, 0x1A);          sprintf(global_serial_send, "%s%c", payload, 0x1A);
314          serial_send();          serial_send();
315          DelayMs(250);          DelayMs(150);
316                    if(global_sms_counter % 3 == 0)
317            {
318                    eeprom_writer();
319                    sprintf(global_serial_send, "at+cmgd=1,3\r");
320                    serial_send();
321                    while ( strstr(global_message_buffer1,"OK") == 0) ;
322            }
323  }        }      
324    
325  void convertTemp()  void convertTemp()
# Line 459  void main() Line 471  void main()
471          ad_init();          ad_init();
472          lcd_init(0);          lcd_init(0);
473          interrupt_init();          interrupt_init();
         sms_init();  
         //eeprom_init();  
474          timer_init();          timer_init();
475            sms_init();
476          eeprom_reader();          eeprom_reader();
477  ///////////////  ///////////////
478  // Main loop //  // Main loop //
# Line 474  void main() Line 485  void main()
485          while(1)          while(1)
486          {          {
487          // 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.
488                  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 )
489                  {                  {
490                          send_update();                          send_update();
491                          global_emergency_counter = 0;                          global_emergency_counter = 0;

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

  ViewVC Help
Powered by ViewVC 1.1.20