/[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 150 by hedin, Tue Dec 4 19:07:00 2007 UTC revision 205 by hedin, Sun Dec 9 21:01:04 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"
 // Delay.h is included inside lcd.c  
9    
 #define LCD_LENGTH 16  
 #define LCD_ROWS 2  
10  #define BUFFER 128  #define BUFFER 128
11  #define PWRFAIL RB1  #define PWRFAIL RB1
12  #define FIREDET RB2  #define FIREDET RB2
# Line 17  Line 15 
15    
16    
17  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];  
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, global_interval_changed = 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,  global_time_counter_image = 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 short global_imei_tversum;  unsigned int global_temp_update_display = 0;
25    
26  unsigned char cell_nr[15] = ""; // = "21681784";  unsigned char global_message_buffer1[BUFFER];
27    unsigned char global_message_buffer2[BUFFER];
28    unsigned char global_message_buffer_length1;
29    unsigned char global_message_buffer_length2;
30    
31    unsigned char global_lcd_buf[16];
32    
33    unsigned int global_imei_tversum;
34    
35    unsigned char global_cell_nr[15] = ""; // = "21681784";
36  bit global_modem_init = 0;  bit global_modem_init = 0;
37    bit global_has_imei = 0;
38    
39    unsigned char global_sms_recieve_number[3];
40    
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);
52    void sms_recieved(void);
53    void send_sms(const unsigned char* payload);
54    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          global_serial_byte_counter=0;          
61            global_message_buffer_length1 = 0;
62            //global_message_buffer1[0] = 0;        
63            global_message_buffer_length2 = 0;
64            //global_message_buffer2[0] = 0;        
65            
66            memset(global_message_buffer1, 0, BUFFER);
67            memset(global_message_buffer2, 0, BUFFER);
68  }        }      
69    //////////  INITS  //////////
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
# Line 67  void ad_init(void) // Nicked from H7 Line 98  void ad_init(void) // Nicked from H7
98          PCFG2=1;          PCFG2=1;
99          PCFG3=1;          PCFG3=1;
100                    
101            //Reset the A/D result registers
102            ADRESH = 0;
103            ADRESL = 0;
104            
105          //Result is right justified          //Result is right justified
106          ADFM=1;          ADFM=1;
107                    
# Line 74  void ad_init(void) // Nicked from H7 Line 109  void ad_init(void) // Nicked from H7
109          ADON=1;          ADON=1;
110  }  }
111    
 void rs232_init(void)  
 {  
         SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled  
         TRISC6  = 0;      
         TRISC7  = 1;  
112    
         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  
 }  
113    
114  void interrupt_init(void)  void interrupt_init(void)
115  {  {
# Line 97  void interrupt_init(void) Line 122  void interrupt_init(void)
122          TMR1IE  = 1;    // Enables timer 1          TMR1IE  = 1;    // Enables timer 1
123  }        }      
124    
125    
126    
127  void timer_init(void)  void timer_init(void)
128  {  {
129          TMR1CS = 1; //use external clock          TMR1CS = 1; //use external clock
# Line 111  void timer_init(void) Line 138  void timer_init(void)
138          RD16 = 0; //normal 8 bit writes          RD16 = 0; //normal 8 bit writes
139          TMR1ON = 1;          TMR1ON = 1;
140  }  }
141            
142  void pic18_io_init(void)  void rs232_init(void)
143  {  {
144          TRISA0  = 1;    // analog input          SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled
145          TRISA1  = 0;    // Output          TRISC6  = 0;    
146          TRISB1  = 1;    // TRISB1-4 Digital input          TRISC7  = 1;
         TRISB2  = 1;  
         TRISB3  = 1;  
         TRISB4  = 1;  
 }        
147    
148  void sms_init(void)          SPBRG   = 207;  // 1200 baud rate... 25 = 9600
149                                            // x = (Fosc / (16*[baud rate]) )-1
150            TXSTA   = 0x24; // Enables BRGH and TXEN inthe TXSTA register
151            RCSTA   = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
152    }
153    
154    void modem_init(void)
155  {  {
156          int i;          int i;
157            char buf[2];
158            
159            while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ;  // Waiting for the modem to be ready
160    
161          reset_recieve_buffer();          reset_recieve_buffer();
162          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
163          serial_send();          serial_send();
164          DelaySek(1);          
165          while(!global_recieve_done) ;          while (global_has_imei == 0)
166            {
167                    if (strstr(global_message_buffer1,"OK") != 0)
168                    {
169                            global_imei_tversum = 0;
170                            for (i=0; i<15; ++i)
171                            {
172                                    buf[0] = global_message_buffer2[i];
173                                    buf[1] = 0;
174                                    global_imei_tversum += atoi(buf);
175                            }
176                            
177                            global_has_imei = 1;
178                    }
179            }
180    
181          sprintf(global_serial_send,"%s", "at+cpin=8043\r");          sprintf(global_serial_send,"%s", "at+cpin?\r");
182          serial_send();          serial_send();
183            DelayMs(100);           // Delay to give the modem a chance to answer.
184                    
185          while(global_modem_init == 0)          if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0)
186            {
187                    sprintf(global_serial_send,"%s", "at+cpin=8043\r");
188                    serial_send();
189                    
190                    while(global_modem_init == 0)
191                    {
192                            on_initial_recieve();
193                    }
194            }
195            reset_recieve_buffer();
196            sprintf(global_serial_send, "at+cmgd=1,4\r");
197            serial_send();
198            while ( strstr(global_message_buffer1,"OK") == 0) ;
199    }
200    
201    void serial_send(void)
202    {
203            int i;
204            char data_byte;
205            for(i = 0; i < BUFFER; i++)
206          {          {
207                  on_initial_recieve();                  data_byte = global_serial_send[i];
208                    if( data_byte == '\r')
209                            i = (BUFFER - 1);
210                    TXREG = data_byte;
211                    while(TRMT==0) ;
212                    DelayMs(10);
213          }          }
214            DelayMs(150);
215            global_serial_send[0] = 0;
216            DelayMs(150);
217  }  }
218    
219    void on_initial_recieve(void)
220    {
221    // can't se what the outcommented lines in here sould do, that make any sence.
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)  void interrupt interrupt_handler(void)
242  {  {
243          // Finds out what interrupt have been trigged, and starts the respective function.          // Finds out what interrupt have been trigged, and starts the respective function.
# Line 159  void interrupt interrupt_handler(void) Line 254  void interrupt interrupt_handler(void)
254          }          }
255  }                }              
256    
257    //////////  INTERRUPT TRIGGED //////////
258  void serial_send(void)  void serial_recieved(void)
259  {  {
         int i;  
260          char data_byte;          char data_byte;
         for(i = 0; i < BUFFER; i++)  
         {  
                 data_byte = global_serial_send[i];  
                 if( data_byte == '\r')  
                         i = (BUFFER - 1);  
                 TXREG = data_byte;  
                 while(TRMT==0) ;  
                 DelayMs(10);  
         }  
         DelayMs(250);  
         DelayMs(250);  
 }        
   
 void serial_recieved(void)  
 {  
         char data_byte, saved_data[LCD_LENGTH];  
261                    
262          data_byte = RCREG;          data_byte = RCREG;
263                    
264          if (data_byte == '\n')          if (data_byte == '\n')          // Cant be bothered to do anyting if the byte is a '\n'.
265                  return;                  return;
   
         global_serial_recieve_buffer[global_serial_byte_counter] = data_byte;  
266                                    
267          if (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.
268          {                  return;
269                  global_recieve_done = 1;  
270                  //global_serial_byte_counter = 0;  
271                  global_serial_recieve_buffer[global_serial_byte_counter+1] = 0; //zero terminate          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.
273          else          else
274          {          {
275                  global_serial_byte_counter++;                  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
282                                                            // global_message_buffer1 into global_message_buffer2 our main buffer into
283                                                            // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
284                                                            // into global_message_buffer1.
285            {
286                    global_recieve_done = 1; // indicates the recieve transmission is done.
287                    global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
288                    
289                    // global_message_buffer1 -> global_message_buffer2
290                    strcpy(global_message_buffer2, global_message_buffer1);
291                    global_message_buffer_length2 = global_message_buffer_length1;
292                    
293                    // global_serial_recieve_buffer -> global_message_buffer1
294                    strcpy(global_message_buffer1, global_serial_recieve_buffer);
295                    global_message_buffer_length1 = global_serial_byte_counter;
296                    
297                    
298                    global_serial_byte_counter = 0;
299            }      
300  }  }
301    
302  void timer1_interrupt(void)  void timer1_interrupt(void)
# Line 207  void timer1_interrupt(void) Line 305  void timer1_interrupt(void)
305          TMR1L = 0xFF;          TMR1L = 0xFF;
306          global_time_counter++;          global_time_counter++;
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          lcd_clear();          if(global_temp_update_display != global_time_counter)
314          lcd_goto(0x00);          {
315          lcd_puts(global_temp);          //      lcd_clear();
316                    lcd_goto(0x00);
317            
318                    sprintf(global_lcd_buf, "Temp: %3d", global_temp);
319                    lcd_puts(global_lcd_buf);
320                    global_temp_update_display = global_time_counter;
321            }
322    
323  }  }
324                    
325  void send_update(void)  void send_update(void)
326  {  {
327          sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr);          char update[40];
328            sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
329            send_sms(update);
330    
331            global_sms_counter++;
332    }      
333    
334    void send_sms(const unsigned char* payload)
335    {
336            sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
337          serial_send();          serial_send();
338          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);  
339          serial_send();          serial_send();
340          DelayMs(250);          DelayMs(150);
341          global_sms_counter++;          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(void)
351  {  {
352          short adVal;          short adVal;
353          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
354          if( adVal >=840 )          if( adVal >=840 )
355                  global_temp = 100;                  global_temp = 100;
356          else          else
357                  global_temp = (adVal / 8.3886);                  global_temp = (unsigned char) (adVal / 8.3886);
358  }  }
359    
360    
# Line 246  void eeprom_writer(void) Line 363  void eeprom_writer(void)
363  {  {
364          char len,i;          char len,i;
365                    
366          len = strlen(cell_nr);          len = strlen(global_cell_nr);
367          eeprom_write(0, (global_time_interval/60));          eeprom_write(0, (global_time_interval/60));
368          eeprom_write(1, global_sms_counter>>8);          eeprom_write(1, global_sms_counter>>8);
369          eeprom_write(2, global_sms_counter);          eeprom_write(2, global_sms_counter);
# Line 254  void eeprom_writer(void) Line 371  void eeprom_writer(void)
371                    
372          for (i=0; i<len; ++i)          for (i=0; i<len; ++i)
373          {          {
374                  eeprom_write(i+4, cell_nr[i] );                  eeprom_write(i+4, global_cell_nr[i] );
375          }          }
376  }  }
377    
# Line 269  void eeprom_reader(void) Line 386  void eeprom_reader(void)
386                    
387          for (i=0; i<len; ++i)          for (i=0; i<len; ++i)
388          {          {
389                  cell_nr[i] = eeprom_read(i+4);                  global_cell_nr[i] = eeprom_read(i+4);
390          }          }
391                    
392          cell_nr[i] = 0; //zero terminated!          global_cell_nr[i] = 0; //zero terminated!
393  }        }      
394    
395  void on_initial_recieve(void)  
396    void on_recieve(void)
397  {  {
398          char imei[16];          char tmp[3];
399          char* ptr;          char* ptr;
400          char i;          tmp[0]=0;
401                            
402          if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)          if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
403                    return;
404    
405            if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
406          {          {
407                  global_modem_init = 1;                  ptr = strstr(global_message_buffer1,",");       // finds the point just before the nr. of the SMS.
408                    strcat(tmp,ptr+1);                      // puts that number in tmp
409                    global_sms_recieve_number[0] = 0;       // wanna be sure that we write the new number from global_sms_recieve_number[0]
410                    strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable.
411                    sms_recieved();
412            }      
413            reset_recieve_buffer();
414    }
415    
416                  ptr = strstr(global_serial_recieve_buffer,"cgsn");  void sms_recieved(void)
417                  ptr +=4;  {
418                  strncpy(imei, ptr,15);          char buf[4];
419                  imei[15] = 0;          char i,imei;
420            char pos;
421            
422            sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
423            serial_send();          // Sends the command.
424            
425            while(strstr(global_message_buffer1, "OK")  == 0)       // stays here until we recieve a "OK" from the modem.
426                    DelayMs(1);
427    
428                  global_imei_tversum = 0;          DelayUs(10);
429                  for (i=0; i<15; ++i)          
430            for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
431            {
432                    buf[i] = global_message_buffer2[i];
433            }
434            
435            buf[i] = 0;
436            imei = atoi(buf);
437            
438            if (imei == global_imei_tversum)
439            {
440                    i++; //spring over ':'
441                    pos = 0;
442                    for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
443                  {                  {
444                          global_imei_tversum += (imei[i] - '0');                          global_cell_nr[pos] = global_message_buffer2[i];
445                  }                  }
446                    global_cell_nr[pos] = 0; //zero terminator
447                                    
448                    i++; //spring over ':'
449                    pos=0;
450                    for ( ; global_message_buffer2[i] ; ++i,++pos)
451                    {
452                            buf[pos] = global_message_buffer2[i];
453                    }
454                    buf[pos]=0;
455                                    
456                  reset_recieve_buffer();                  global_time_interval = atoi(buf);
457                    global_time_interval *= 60;
458                    eeprom_writer();  // writes the new cell nr. and time interval to the eeprom.
459                    send_sms("conf ok");
460          }          }
461                    
462    
463  }  }
464    
465    void Delay(int time)
 void on_recieve(void)  
466  {  {
467          char tmp[17];          int wanted = (global_time_counter + time) % global_time_interval;
         if (global_recieve_done == 0)  
                 return;  
                   
         lcd_clear();  
         lcd_goto(0x00);  
         tmp[0] = 0;  
         strncat(tmp,global_serial_recieve_buffer,16);  
         lcd_puts(tmp);  
         if (strncmp(global_serial_recieve_buffer,"+WIND: 11", 3) == 0)  
         {  
                 global_modem_init = 1;  
         }  
468                    
469          reset_recieve_buffer();          while (global_time_counter < wanted) ;
470  }  }
471    
472  void main()  void main()
# Line 330  void main() Line 476  void main()
476    
477          // Running init for various components.          // Running init for various components.
478          pic18_io_init();          pic18_io_init();
479            RA5 = 1;                // Indicates that the board is running inits.
480            
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            modem_init();
489          eeprom_reader();          eeprom_reader();
490  ///////////////  ///////////////
491  // Main loop //  // Main loop //
492    
493            DelayMs(50);
494            reset_recieve_buffer();
495    
496            RA5 = 0;                        // Inits are done, and RA1 will now work as a error notifier.
497    
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 371  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                  }                  }
532                            // Handels the recieve sms'es.
533                  //if (global_recieve_done)                  on_recieve();
                 //{  
                         on_recieve();  
                 //      global_recieve_done = 0;  
                 //      global_serial_byte_counter = 0;  
                 //}  
534          }          }
535  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20