/[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 155 by hedin, Wed Dec 5 09:53:16 2007 UTC revision 163 by hedin, Wed Dec 5 15:39:55 2007 UTC
# Line 2  Line 2 
2  #include <stdio.h>  #include <stdio.h>
3  #include <htc.h>  #include <htc.h>
4  #include <string.h>  #include <string.h>
5    #include <stdlib.h>
6    
7  #include "lcd.h"  #include "lcd.h"
8  #include "Delay.h"  #include "Delay.h"
# Line 17  Line 18 
18    
19    
20  unsigned char global_Pot_Hi, global_Pot_Lo;  unsigned char global_Pot_Hi, global_Pot_Lo;
 unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];  
21  unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[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    unsigned char global_temp_old = 0;
27    
28  unsigned char global_message_buffer[BUFFER];  unsigned char global_message_buffer1[BUFFER];
29  unsigned char global_message_buffer_length;  unsigned char global_message_buffer2[BUFFER];
30    unsigned char global_message_buffer_length1;
31    unsigned char global_message_buffer_length2;
32    
33  unsigned char global_lcd_buf[16];  unsigned char global_lcd_buf[16];
34    
35  unsigned short global_imei_tversum;  unsigned short global_imei_tversum;
36    
37  unsigned char cell_nr[15] = ""; // = "21681784";  unsigned char global_cell_nr[15] = ""; // = "21681784";
38  bit global_modem_init = 0;  bit global_modem_init = 0;
39    bit global_has_imei = 0;
40    
41  unsigned char global_sms_recieve_number[3];  unsigned char global_sms_recieve_number[3];
42    
# Line 47  void timer1_interrupt(void); Line 51  void timer1_interrupt(void);
51  void on_recieve(void);  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);
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_serial_byte_counter=0;          global_message_buffer_length1 = 0;
62          global_serial_recieve_buffer[0] = 0;          //global_message_buffer1[0] = 0;        
63            global_message_buffer_length2 = 0;
64            //global_message_buffer2[0] = 0;        
65                    
66          global_message_buffer_length = 0;          memset(global_message_buffer1, 0, BUFFER);
67          global_message_buffer[0] = 0;            memset(global_message_buffer2, 0, BUFFER);
68  }        }      
69    
70    void pic18_io_init(void)
71    {
72            TRISA0  = 1;    // analog input
73            TRISA5  = 0;    // Output
74            TRISB1  = 1;    // TRISB1-4 Digital input
75            TRISB2  = 1;
76            TRISB3  = 1;
77            TRISB4  = 1;
78    }
79    
80    
81    
82  void ad_init(void) // Nicked from H7  void ad_init(void) // Nicked from H7
83  {  {
# Line 123  void timer_init(void) Line 141  void timer_init(void)
141          T1OSCEN = 1; //enable oscillator circuit                  T1OSCEN = 1; //enable oscillator circuit        
142          RD16 = 0; //normal 8 bit writes          RD16 = 0; //normal 8 bit writes
143          TMR1ON = 1;          TMR1ON = 1;
 }  
   
 void pic18_io_init(void)  
 {  
         TRISA0  = 1;    // analog input  
         TRISA1  = 0;    // Output  
         TRISB1  = 1;    // TRISB1-4 Digital input  
         TRISB2  = 1;  
         TRISB3  = 1;  
         TRISB4  = 1;  
144  }        }      
145    
146  void sms_init(void)  void sms_init(void)
147  {  {
148          int i;          int i;
149            char buf[2];
150    
151    
152          reset_recieve_buffer();          reset_recieve_buffer();
153          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
154          serial_send();          serial_send();
155          DelaySek(1);          DelaySek(1);
156          while(!global_recieve_done) ;          //while(!global_recieve_done) ;
157            
158            while (global_has_imei == 0)
159            {
160                    if (strstr(global_message_buffer1,"OK") != 0)
161                    {
162                            global_imei_tversum = 0;
163                            for (i=0; i<15; ++i)
164                            {
165                                    buf[0] = global_message_buffer2[i];
166                                    buf[1] = 0;
167                                    global_imei_tversum += atoi(buf);
168                                    //global_imei_tversum += (global_message_buffer2[i] - '0');
169                            }
170                            
171                            global_has_imei = 1;
172                    }
173            }
174    
175    
176          sprintf(global_serial_send,"%s", "at+cpin=8043\r");          sprintf(global_serial_send,"%s", "at+cpin=8043\r");
# Line 153  void sms_init(void) Line 180  void sms_init(void)
180          {          {
181                  on_initial_recieve();                  on_initial_recieve();
182          }          }
183            DelayMs(5);
184            
185            reset_recieve_buffer();
186            sprintf(global_serial_send, "at+cmgd=1,4\r");
187            serial_send();
188            while ( strstr(global_message_buffer1,"OK") == 0) ;
189  }  }
190    
191    
# Line 186  void serial_send(void) Line 219  void serial_send(void)
219                  while(TRMT==0) ;                  while(TRMT==0) ;
220                  DelayMs(10);                  DelayMs(10);
221          }          }
222          DelayMs(250);          DelayMs(150);
223          DelayMs(250);          global_serial_send[0] = 0;
224            DelayMs(150);
225  }        }      
226    
227  void serial_recieved(void)  ///////////////////////////////////////////////////////////////////////////////////////////////////
228    // Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from,
229    // global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done.
230    void serial_recieved(void)
231  {  {
232          char data_byte, saved_data[LCD_LENGTH];          char data_byte;
233                    
234          data_byte = RCREG;          data_byte = RCREG;
235                    
236          if (data_byte == '\n')          if (data_byte == '\n')          // Cant be bothered to do anyting if the byte is a '\n'.
237                  return;                  return;
238                                    
239          if (global_serial_byte_counter == 0 && data_byte == '\r')          if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.
240                  return;                  return;
241    
242          global_serial_recieve_buffer[global_serial_byte_counter] = data_byte;  
243                            if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
244          if (data_byte == '\r')                  global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte;       // fills the data_byte into our buffer.
245    
246            
247            
248            if (data_byte == '\r')  // when we meet a '\r', the transmission is done, and we fill the constxt of
249                                                            // global_message_buffer1 into global_message_buffer2 our main buffer into
250                                                            // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
251                                                            // into global_message_buffer1.
252          {          {
253                  global_recieve_done = 1;                  global_recieve_done = 1; // indicates the recieve transmission is done.
                 //global_serial_byte_counter = 0;  
254                  global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate                  global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
255                                    
256                  strcpy(global_message_buffer, global_serial_recieve_buffer);                  // global_message_buffer1 -> global_message_buffer2
257                  global_message_buffer_length = global_serial_byte_counter;                  strcpy(global_message_buffer2, global_message_buffer1);
258                    global_message_buffer_length2 = global_message_buffer_length1;
259                    
260                    // global_serial_recieve_buffer -> global_message_buffer1
261                    strcpy(global_message_buffer1, global_serial_recieve_buffer);
262                    global_message_buffer_length1 = global_serial_byte_counter;
263                    
264                    
265                    global_serial_byte_counter = 0;
266          }                }      
         else  
         {  
                 global_serial_byte_counter++;  
         }  
           
267  }  }
268    
269  void timer1_interrupt(void)  void timer1_interrupt(void)
# Line 231  void timer1_interrupt(void) Line 277  void timer1_interrupt(void)
277    
278  void update_lcd(void)  void update_lcd(void)
279  {  {
280          lcd_clear();          //lcd_clear();
281          lcd_goto(0x00);          lcd_goto(0x00);
282                    
283          sprintf(global_lcd_buf, "%d", global_temp);          sprintf(global_lcd_buf, "Temp: %3d", global_temp);
284          lcd_puts(global_lcd_buf);          lcd_puts(global_lcd_buf);
285    
286  }  }
287                    
288  void send_update(void)  void send_update(void)
289  {  {
290          sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr);          char update[40];
291            sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
292            send_sms(update);
293    
294            global_sms_counter++;
295    }      
296    
297    void send_sms(const unsigned char* payload)
298    {
299            sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
300          serial_send();          serial_send();
301          sprintf(global_serial_send, "%d:%d:%d:%d:%d:%d%c", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL, 0x1A);          sprintf(global_serial_send, "%s%c", payload, 0x1A);
         lcd_goto(40);  
         lcd_puts(global_serial_send);  
302          serial_send();          serial_send();
303          DelayMs(250);          DelayMs(150);
304          global_sms_counter++;          
305  }        }      
306    
307  void convertTemp()  void convertTemp()
# Line 258  void convertTemp() Line 311  void convertTemp()
311          if( adVal >=840 )          if( adVal >=840 )
312                  global_temp = 100;                  global_temp = 100;
313          else          else
314                  global_temp = (adVal / 8.3886);                  global_temp = (unsigned char) (adVal / 8.3886);
315  }  }
316    
317    
# Line 267  void eeprom_writer(void) Line 320  void eeprom_writer(void)
320  {  {
321          char len,i;          char len,i;
322                    
323          len = strlen(cell_nr);          len = strlen(global_cell_nr);
324          eeprom_write(0, (global_time_interval/60));          eeprom_write(0, (global_time_interval/60));
325          eeprom_write(1, global_sms_counter>>8);          eeprom_write(1, global_sms_counter>>8);
326          eeprom_write(2, global_sms_counter);          eeprom_write(2, global_sms_counter);
# Line 275  void eeprom_writer(void) Line 328  void eeprom_writer(void)
328                    
329          for (i=0; i<len; ++i)          for (i=0; i<len; ++i)
330          {          {
331                  eeprom_write(i+4, cell_nr[i] );                  eeprom_write(i+4, global_cell_nr[i] );
332          }          }
333  }  }
334    
# Line 290  void eeprom_reader(void) Line 343  void eeprom_reader(void)
343                    
344          for (i=0; i<len; ++i)          for (i=0; i<len; ++i)
345          {          {
346                  cell_nr[i] = eeprom_read(i+4);                  global_cell_nr[i] = eeprom_read(i+4);
347          }          }
348                    
349          cell_nr[i] = 0; //zero terminated!          global_cell_nr[i] = 0; //zero terminated!
350  }        }      
351    
352  void on_initial_recieve(void)  void on_initial_recieve(void)
# Line 301  void on_initial_recieve(void) Line 354  void on_initial_recieve(void)
354          char imei[16];          char imei[16];
355          char* ptr;          char* ptr;
356          char i;          char i;
357            char buf[2];
358                    
359          if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)          if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
360          {          {
# Line 310  void on_initial_recieve(void) Line 364  void on_initial_recieve(void)
364                  ptr +=4;                  ptr +=4;
365                  strncpy(imei, ptr,15);                  strncpy(imei, ptr,15);
366                  imei[15] = 0;                  imei[15] = 0;
   
                 global_imei_tversum = 0;  
                 for (i=0; i<15; ++i)  
                 {  
                         global_imei_tversum += (imei[i] - '0');  
                 }  
                   
367                                    
368                  reset_recieve_buffer();                  reset_recieve_buffer();
369          }          }
370  }  }
371    
   
372  void on_recieve(void)  void on_recieve(void)
373  {  {
374          char tmp[17];          char tmp[3];
375          char* ptr;          char* ptr;
376          tmp[0]=0;          tmp[0]=0;
377                                    
378          if (global_recieve_done == 0 || global_message_buffer_length == 0)          if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
379                  return;                  return;
380    
381                    if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
         if (strstr(global_serial_recieve_buffer,"CMTI") != 0)  
382          {          {
383                  ptr = strstr(global_serial_recieve_buffer,",");                  ptr = strstr(global_message_buffer1,",");       // finds the point just before the nr. of the SMS.
384                  strcat(tmp,ptr+1);                  strcat(tmp,ptr+1);                                                      // puts that number in tmp
385                  sprintf(global_sms_recieve_number,"%s", tmp);                  global_sms_recieve_number[0] = 0;                       // wanna be sure that we write the new number from global_sms_recieve_number[0]
386                  lcd_clear();                  strcat(global_sms_recieve_number, tmp);         // puts the sms number into the global variable.
                 lcd_puts(global_sms_recieve_number);  
387                  sms_recieved();                  sms_recieved();
388          }          }      
         else if (strstr(global_message_buffer,"CMTI") != 0)  
         {  
                 ptr = strstr(global_message_buffer,",");  
                 strcat(tmp,ptr+1);  
                 lcd_clear();  
                 lcd_puts(tmp);  
         }  
           
           
389          reset_recieve_buffer();          reset_recieve_buffer();
390  }  }
391    
392  void sms_recieved(void)  void sms_recieved(void)
393  {  {
394          sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number);          char buf[4];
395          serial_send();          char i,imei;
396            char pos;
397            
398            sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
399            serial_send();          // Sends the command.
400            
401            while(strstr(global_message_buffer1, "OK")  == 0)       // stays here until we recieve a "OK" from the modem.
402                    DelayMs(1);
403    
404            DelayUs(10);
405            
406            for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
407            {
408                    buf[i] = global_message_buffer2[i];
409            }
410            
411            buf[i] = 0;
412            imei = atoi(buf);
413            
414            if (imei == global_imei_tversum)
415            {
416                    i++; //spring over ':'
417                    pos = 0;
418                    for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
419                    {
420                            global_cell_nr[pos] = global_message_buffer2[i];
421                    }
422                    global_cell_nr[pos] = 0; //zero terminator
423                    
424                    i++; //spring over ':'
425                    pos=0;
426                    for ( ; global_message_buffer2[i] ; ++i,++pos)
427                    {
428                            buf[pos] = global_message_buffer2[i];
429                    }
430                    buf[pos]=0;
431                    
432                    global_time_interval = atoi(buf);
433                    global_time_interval *= 60;
434                    eeprom_writer();                        // writes the new cell nr. and time interval to the eeprom.
435                    send_sms("conf ok");
436            }
437            
438    
439  }  }
440    
441    
442    
443  void main()  void main()
444  {  {
445  ////////////////////  ////////////////////
# Line 367  void main() Line 447  void main()
447    
448          // Running init for various components.          // Running init for various components.
449          pic18_io_init();          pic18_io_init();
450            RA5 = 1;                        // Indicates that the board is running inits.
451            
452          rs232_init();          rs232_init();
453          ad_init();          ad_init();
454          lcd_init(0);          lcd_init(0);
# Line 381  void main() Line 463  void main()
463          DelayMs(50);          DelayMs(50);
464          reset_recieve_buffer();          reset_recieve_buffer();
465    
466            RA5 = 0;                        // Inits are done, and RA1 will now work as a error notifier.
467    
468          while(1)          while(1)
469          {          {
470          // 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 411  void main() Line 495  void main()
495                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
496                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
497                          convertTemp();                          convertTemp();
498                          //update_lcd();                          if (global_temp != global_temp_old)
499                            {
500                                    update_lcd();
501                                    global_temp_old = global_temp;
502                            }
503                          GODONE = 1;                          GODONE = 1;
504                  }                  }
505          // Handels the recieve sms'es.          // Handels the recieve sms'es.

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

  ViewVC Help
Powered by ViewVC 1.1.20