/[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 139 by hedin, Mon Dec 3 17:19:35 2007 UTC revision 163 by hedin, Wed Dec 5 15:39:55 2007 UTC
# Line 1  Line 1 
1  #include <pic18.h>  #include <pic18.h>
2  #include <stdio.h>  #include <stdio.h>
3    #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"
9  // Delay.h is included inside lcd.c  // Delay.h is included inside lcd.c
10    
11  #define LCD_LENGTH 16  #define LCD_LENGTH 16
12  #define LCD_ROWS 2  #define LCD_ROWS 2
13  #define SEND_BUFFER 128  #define BUFFER 128
14    #define PWRFAIL RB1
15    #define FIREDET RB2
16    #define FEEDING RB3
17    #define EMPTYTANK RB4
18    
19    
20  unsigned char global_Pot_Hi, global_Pot_Lo;  unsigned char global_Pot_Hi, global_Pot_Lo;
21  unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];  unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];
22  unsigned char global_serial_send[SEND_BUFFER];  bit global_recieve_done = 0, global_interval_changed = 0;
23  unsigned char global_serial_recieve_buffer[LCD_LENGTH];  unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0;
24  bit global_recieve_done = 0;  unsigned int global_emergency_counter = 600, global_time_interval = 3600;
25  int global_serial_byte_counter = 0;  unsigned char global_temp = 0;
26    unsigned char global_temp_old = 0;
27    
28    unsigned char global_message_buffer1[BUFFER];
29    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];
34    
35    unsigned short global_imei_tversum;
36    
37    unsigned char global_cell_nr[15] = ""; // = "21681784";
38    bit global_modem_init = 0;
39    bit global_has_imei = 0;
40    
41    unsigned char global_sms_recieve_number[3];
42    
43    __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
44    __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
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);
49    void convertTemp(void);
50    void timer1_interrupt(void);
51    void on_recieve(void);
52    void on_initial_recieve(void);
53    void sms_recieved(void);
54    void send_sms(const unsigned char* payload);
55    
56    
57    void reset_recieve_buffer(void)
58    {
59            global_recieve_done = 0;
60            
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    
70  // Nicked from H7  void pic18_io_init(void)
71  void ad_init(void)  {
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
83  {  {
84          // AD Conversion clock          // AD Conversion clock
85          ADCS0 = 0;          ADCS0 = 0;
# Line 41  void ad_init(void) Line 98  void ad_init(void)
98          PCFG2=1;          PCFG2=1;
99          PCFG3=1;          PCFG3=1;
100                    
101          //Result is left justified          //Result is right justified
102          ADFM=0;          ADFM=1;
103                    
104          //Fire up for A/D converter module          //Fire up for A/D converter module
105          ADON=1;          ADON=1;
# Line 68  void interrupt_init(void) Line 125  void interrupt_init(void)
125          RCIE    = 1;    // Recieve interrupt enable.          RCIE    = 1;    // Recieve interrupt enable.
126          IPEN    = 0;    // Nfo interrupt priority          IPEN    = 0;    // Nfo interrupt priority
127          TXIE    = 0;    // Serial interrupt enabled          TXIE    = 0;    // Serial interrupt enabled
128            TMR1IE  = 1;    // Enables timer 1
129  }        }      
130    
131  void pic18_io_init(void)  void timer_init(void)
132  {  {
133          TRISA0  = 1;    // analog input          TMR1CS = 1; //use external clock
134          TRISB1  = 1;    // TRISB1-4 Digital input          
135          TRISB2  = 1;          T1CKPS1 = 1; //1:8 prescale
136          TRISB3  = 1;          T1CKPS0 = 1;
137          TRISB4  = 1;          
138            TMR1H = 0xEF;
139            TMR1L = 0xFF;
140    
141            T1OSCEN = 1; //enable oscillator circuit        
142            RD16 = 0; //normal 8 bit writes
143            TMR1ON = 1;
144  }        }      
145    
146  void sms_init(void)  void sms_init(void)
147  {  {
148          int i = 1;          int i;
149          sprintf(global_serial_send,"%s", "at+cpin=8043\r");          char buf[2];
150    
151    
152            reset_recieve_buffer();
153            sprintf(global_serial_send,"at+cgsn\r");
154          serial_send();          serial_send();
155          DelaySek(60);          DelaySek(1);
156            //while(!global_recieve_done) ;
157                    
158          update_lcd();          while (global_has_imei == 0)
159          DelaySek(5);          {
160                            if (strstr(global_message_buffer1,"OK") != 0)
161          sprintf(global_serial_send,"%s%s", "at+cmgs=","\"22337617\"\r");                  {
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");
177          serial_send();          serial_send();
         DelayMs(5000);  
178                    
179          sprintf(global_serial_send,"%s%d%c","Dette er test nr: ", i, 0x1A);          while(global_modem_init == 0)
180          serial_send();          {
181          DelayMs(5000);                  on_initial_recieve();
182          i++;          }
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    
192  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
193  {  {
194          // 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 108  void interrupt interrupt_handler(void) Line 197  void interrupt interrupt_handler(void)
197                  serial_recieved();                  serial_recieved();
198                  RCIF = 0;                  RCIF = 0;
199          }          }
200            
201            if(TMR1IF == 1)                 // timer1 interrupt trigger.
202            {
203                    timer1_interrupt();
204                    TMR1IF = 0;
205            }
206  }                }              
207    
208    
   
209  void serial_send(void)  void serial_send(void)
210  {  {
211          int i;          int i;
212  //      char tosend[3];          char data_byte;
213          char data;          for(i = 0; i < BUFFER; i++)
214  //      sprintf(tosend,"%s", "at\r");          {
215          for(i = 0; i < SEND_BUFFER; i++)                  data_byte = global_serial_send[i];
216          {                  if( data_byte == '\r')
217                  data = global_serial_send[i];                          i = (BUFFER - 1);
218                  if( data == '\r')                  TXREG = data_byte;
                         i = (SEND_BUFFER - 1);  
                 TXREG = data;  
219                  while(TRMT==0) ;                  while(TRMT==0) ;
220                  DelayMs(1000);                  DelayMs(10);
221          }          }
222            DelayMs(150);
223            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, saved_data[LCD_LENGTH];          char data_byte;
233                    
234          data = RCREG;          data_byte = RCREG;
235                    
236          global_serial_recieve_buffer[global_serial_byte_counter] = data;          if (data_byte == '\n')          // Cant be bothered to do anyting if the byte is a '\n'.
237          if(data == '\r')                  return;
238                    
239            if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.
240                    return;
241    
242    
243            if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
244                    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.
254                    global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
255                    
256                    // global_message_buffer1 -> global_message_buffer2
257                    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;                  global_serial_byte_counter = 0;
266          }                }      
267    }
268    
269    void timer1_interrupt(void)
270    {
271            TMR1H = 0xEF;
272            TMR1L = 0xFF;
273            global_time_counter++;
274            global_emergency_counter++;
275            RA1 = !RA1;
276    }
277    
278    void update_lcd(void)
279    {
280            //lcd_clear();
281            lcd_goto(0x00);
282            
283            sprintf(global_lcd_buf, "Temp: %3d", global_temp);
284            lcd_puts(global_lcd_buf);
285    
286    }
287            
288    void send_update(void)
289    {
290            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();
301            sprintf(global_serial_send, "%s%c", payload, 0x1A);
302            serial_send();
303            DelayMs(150);
304            
305    }      
306    
307    void convertTemp()
308    {
309            short adVal;
310            adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
311            if( adVal >=840 )
312                    global_temp = 100;
313          else          else
314                    global_temp = (unsigned char) (adVal / 8.3886);
315    }
316    
317    
318    
319    void eeprom_writer(void)
320    {
321            char len,i;
322            
323            len = strlen(global_cell_nr);
324            eeprom_write(0, (global_time_interval/60));
325            eeprom_write(1, global_sms_counter>>8);
326            eeprom_write(2, global_sms_counter);
327            eeprom_write(3, len);
328            
329            for (i=0; i<len; ++i)
330          {          {
331                  global_serial_byte_counter++;                  eeprom_write(i+4, global_cell_nr[i] );
332          }          }
           
333  }  }
334  void update_lcd(void)  
335    void eeprom_reader(void)
336  {  {
337          if( global_recieve_done == 1 )          char len,i;
338            
339            global_time_interval = eeprom_read(0);
340            global_time_interval *= 60;
341            global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
342            len = eeprom_read(3);
343            
344            for (i=0; i<len; ++i)
345          {          {
346                  lcd_clear();                  global_cell_nr[i] = eeprom_read(i+4);
                 lcd_goto(0x00);  
                 lcd_puts(global_serial_recieve_buffer);  
                 global_recieve_done = 0;  
347          }          }
348            
349            global_cell_nr[i] = 0; //zero terminated!
350    }      
351    
352    void on_initial_recieve(void)
353    {
354            char imei[16];
355            char* ptr;
356            char i;
357            char buf[2];
358            
359            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
360            {
361                    global_modem_init = 1;
362    
363                    ptr = strstr(global_serial_recieve_buffer,"cgsn");
364                    ptr +=4;
365                    strncpy(imei, ptr,15);
366                    imei[15] = 0;
367                    
368                    reset_recieve_buffer();
369            }
370    }
371    
372    void on_recieve(void)
373    {
374            char tmp[3];
375            char* ptr;
376            tmp[0]=0;
377                    
378            if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
379                    return;
380    
381            if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
382            {
383                    ptr = strstr(global_message_buffer1,",");       // finds the point just before the nr. of the SMS.
384                    strcat(tmp,ptr+1);                                                      // puts that number in tmp
385                    global_sms_recieve_number[0] = 0;                       // wanna be sure that we write the new number from global_sms_recieve_number[0]
386                    strcat(global_sms_recieve_number, tmp);         // puts the sms number into the global variable.
387                    sms_recieved();
388            }      
389            reset_recieve_buffer();
390  }  }
391    
392    void sms_recieved(void)
393    {
394            char buf[4];
395            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  /////////////////////////////////////////////  ////////////////////
446  // Running Init's  // Running Init's //
447    
448          // Running init for various components.          // Running init for various components.
         //AD_init();  
         ad_init();  
         rs232_init();  
449          pic18_io_init();          pic18_io_init();
450            RA5 = 1;                        // Indicates that the board is running inits.
451            
452            rs232_init();
453            ad_init();
454          lcd_init(0);          lcd_init(0);
         lcd_clear();  
455          interrupt_init();          interrupt_init();
456          sms_init();          sms_init();
457  /////////////////////////////////////////////          //eeprom_init();
458  // Main loop          timer_init();
459            eeprom_reader();
460    ///////////////
461    // Main loop //
462    
463            DelayMs(50);
464            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.
471                    if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 )
472                    {
473                            send_update();
474                            global_emergency_counter = 0;
475                    }
476            // Every X sec. a status sms is send.
477                    if(global_time_counter >= 3600)
478                    {
479                            send_update();
480                            global_time_counter = 0;
481                    }
482            // To avoid buffer overrun.
483                    if( global_emergency_counter > 7200 )
484                            global_emergency_counter = 600;
485    
486            // Checks if there has been recieved a config sms.
487                    if(global_interval_changed )
488                    {
489                            eeprom_writer();
490                            global_interval_changed = 0;
491                    }
492          // Checking if A/D convertion is done, and save the data in global_Pot_??          // Checking if A/D convertion is done, and save the data in global_Pot_??
493                  if(GODONE==0)                  if(GODONE==0)
494                  {                  {
495                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
496                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
497                            convertTemp();
498                            if (global_temp != global_temp_old)
499                            {
500                                    update_lcd();
501                                    global_temp_old = global_temp;
502                            }
503                          GODONE = 1;                          GODONE = 1;
504                  }                  }
505                  update_lcd();          // Handels the recieve sms'es.
506                    on_recieve();
507          }          }
508  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20