/[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 137 by hedin, Mon Dec 3 14:24:50 2007 UTC revision 175 by hedin, Thu Dec 6 13:36:07 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 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_data;  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,  global_time_counter_image = 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    
27    unsigned int global_temp_update_display = 0;
28    
29    unsigned char global_message_buffer1[BUFFER];
30    unsigned char global_message_buffer2[BUFFER];
31    unsigned char global_message_buffer_length1;
32    unsigned char global_message_buffer_length2;
33    
34    unsigned char global_lcd_buf[16];
35    
36    unsigned short global_imei_tversum;
37    
38    unsigned char global_cell_nr[15] = ""; // = "21681784";
39    bit global_modem_init = 0;
40    bit global_has_imei = 0;
41    
42    unsigned char global_sms_recieve_number[3];
43    
44    __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8');
45    __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0);
46    
47    
48  void serial_recieved(void);  void serial_recieved(void);
49    void serial_send(void);
50    void update_lcd(void);
51    void convertTemp(void);
52    void timer1_interrupt(void);
53    void on_recieve(void);
54    void on_initial_recieve(void);
55    void sms_recieved(void);
56    void send_sms(const unsigned char* payload);
57    void eeprom_writer(void);
58    
59    
60    void reset_recieve_buffer(void)
61    {
62            global_recieve_done = 0;
63            
64            global_message_buffer_length1 = 0;
65            //global_message_buffer1[0] = 0;        
66            global_message_buffer_length2 = 0;
67            //global_message_buffer2[0] = 0;        
68            
69            memset(global_message_buffer1, 0, BUFFER);
70            memset(global_message_buffer2, 0, BUFFER);
71    }      
72    //////////  INITS  //////////
73    void pic18_io_init(void)
74    {
75            TRISA0  = 1;    // analog input
76            TRISA5  = 0;    // Output
77            TRISB1  = 1;    // TRISB1-4 Digital input
78            TRISB2  = 1;
79            TRISB3  = 1;
80            TRISB4  = 1;
81    }
82    
83    
84  // Nicked from H7  
85  void ad_init(void)  void ad_init(void) // Nicked from H7
86  {  {
87          // AD Conversion clock          // AD Conversion clock
88          ADCS0 = 0;          ADCS0 = 0;
# Line 38  void ad_init(void) Line 101  void ad_init(void)
101          PCFG2=1;          PCFG2=1;
102          PCFG3=1;          PCFG3=1;
103                    
104          //Result is left justified          //Reset the A/D result registers
105          ADFM=0;          ADRESH = 0;
106            ADRESL = 0;
107            
108            //Result is right justified
109            ADFM=1;
110                    
111          //Fire up for A/D converter module          //Fire up for A/D converter module
112          ADON=1;          ADON=1;
113  }  }
114    
 void rs232_init(void)  
 {  
         SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled  
         TRISC6  = 0;      
         TRISC7  = 1;  
115    
         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  
 }  
116    
117  void interrupt_init(void)  void interrupt_init(void)
118  {  {
# Line 65  void interrupt_init(void) Line 122  void interrupt_init(void)
122          RCIE    = 1;    // Recieve interrupt enable.          RCIE    = 1;    // Recieve interrupt enable.
123          IPEN    = 0;    // Nfo interrupt priority          IPEN    = 0;    // Nfo interrupt priority
124          TXIE    = 0;    // Serial interrupt enabled          TXIE    = 0;    // Serial interrupt enabled
125            TMR1IE  = 1;    // Enables timer 1
126  }        }      
127    
128  void pic18_io_init(void)  
129    
130    void timer_init(void)
131  {  {
132          TRISA0  = 1;    // analog input          TMR1CS = 1; //use external clock
133          TRISB1  = 1;    // TRISB1-4 Digital input          
134          TRISB2  = 1;          T1CKPS1 = 1; //1:8 prescale
135          TRISB3  = 1;          T1CKPS0 = 1;
136          TRISB4  = 1;          
137  }                TMR1H = 0xEF;
138            TMR1L = 0xFF;
139    
140            T1OSCEN = 1; //enable oscillator circuit        
141            RD16 = 0; //normal 8 bit writes
142            TMR1ON = 1;
143    }
144            
145    void rs232_init(void)
146    {
147            SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled
148            TRISC6  = 0;    
149            TRISC7  = 1;
150    
151            SPBRG   = 207;  // 1200 baud rate... 25 = 9600
152                                            // x = (Fosc / (16*[baud rate]) )-1
153            TXSTA   = 0x24; // Enables BRGH and TXEN inthe TXSTA register
154            RCSTA   = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
155    }
156    
157  void sms_init(void)  void sms_init(void)
158  {  {
159            int i;
160            char buf[2];
161            
162            while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ;  // Waiting for the modem to be ready
163    
164            reset_recieve_buffer();
165            sprintf(global_serial_send,"at+cgsn\r");
166            serial_send();
167            
168            while (global_has_imei == 0)
169            {
170                    if (strstr(global_message_buffer1,"OK") != 0)
171                    {
172                            global_imei_tversum = 0;
173                            for (i=0; i<15; ++i)
174                            {
175                                    buf[0] = global_message_buffer2[i];
176                                    buf[1] = 0;
177                                    global_imei_tversum += atoi(buf);
178                            }
179                            
180                            global_has_imei = 1;
181                    }
182            }
183    
184            sprintf(global_serial_send,"%s", "at+cpin?\r");
185            serial_send();
186            DelayMs(100);           // To give the modem a chance to answer.
187            
188            if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0)
189            {
190                    sprintf(global_serial_send,"%s", "at+cpin=8043\r");
191                    serial_send();
192                    
193                    while(global_modem_init == 0)
194                    {
195                            on_initial_recieve();
196                    }
197            }
198            reset_recieve_buffer();
199            sprintf(global_serial_send, "at+cmgd=1,4\r");
200            serial_send();
201            while ( strstr(global_message_buffer1,"OK") == 0) ;
202  }  }
203    
204    void serial_send(void)
205    {
206            int i;
207            char data_byte;
208            for(i = 0; i < BUFFER; i++)
209            {
210                    data_byte = global_serial_send[i];
211                    if( data_byte == '\r')
212                            i = (BUFFER - 1);
213                    TXREG = data_byte;
214                    while(TRMT==0) ;
215                    DelayMs(10);
216            }
217            DelayMs(150);
218            global_serial_send[0] = 0;
219            DelayMs(150);
220    }      
221    //////////  INTERRUPT HANDLER  //////////
222  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
223  {  {
224          // 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 88  void interrupt interrupt_handler(void) Line 227  void interrupt interrupt_handler(void)
227                  serial_recieved();                  serial_recieved();
228                  RCIF = 0;                  RCIF = 0;
229          }          }
230            
231            if(TMR1IF == 1)                 // timer1 interrupt trigger.
232            {
233                    timer1_interrupt();
234                    TMR1IF = 0;
235            }
236  }                }              
237    
238    //////////  INTERRUPT TRIGGED //////////
239    void serial_recieved(void)
240    {
241            char data_byte;
242            
243            data_byte = RCREG;
244            
245            if (data_byte == '\n')          // Cant be bothered to do anyting if the byte is a '\n'.
246                    return;
247                    
248            if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.
249                    return;
250    
251    
252  void serial_send(void)          if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
253                    global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte;       // fills the data_byte into our buffer.
254            else
255            {
256                    global_serial_recieve_buffer[0] = 0;
257                    global_serial_byte_counter = 0;
258                    return;
259            }
260            
261            
262            if (data_byte == '\r')  // when we meet a '\r', the transmission is done, and we fill the constxt of
263                                                            // global_message_buffer1 into global_message_buffer2 our main buffer into
264                                                            // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
265                                                            // into global_message_buffer1.
266            {
267                    global_recieve_done = 1; // indicates the recieve transmission is done.
268                    global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
269                    
270                    // global_message_buffer1 -> global_message_buffer2
271                    strcpy(global_message_buffer2, global_message_buffer1);
272                    global_message_buffer_length2 = global_message_buffer_length1;
273                    
274                    // global_serial_recieve_buffer -> global_message_buffer1
275                    strcpy(global_message_buffer1, global_serial_recieve_buffer);
276                    global_message_buffer_length1 = global_serial_byte_counter;
277                    
278                    
279                    global_serial_byte_counter = 0;
280            }      
281    }
282    
283    void timer1_interrupt(void)
284  {  {
285          int i;          TMR1H = 0xEF;
286          char tosend[3];          TMR1L = 0xFF;
287          char data;          global_time_counter++;
288          sprintf(tosend,"%s", "at\r");          global_emergency_counter++;
289          for(i = 0; i < 3; i++)          RA1 = !RA1;
290    }
291    //////////  ORDENARY FUNKTIONS  //////////
292    void update_lcd(void)
293    {
294            if(global_temp_update_display != global_time_counter)
295          {          {
296                  data = tosend[i];          //      lcd_clear();
297                  TXREG = data;                  lcd_goto(0x00);
298                  while(TRMT==0)          
299                  DelayMs(1000);                  sprintf(global_lcd_buf, "Temp: %3d", global_temp);
300                    lcd_puts(global_lcd_buf);
301                    global_temp_update_display = global_time_counter;
302            }
303    
304    }
305            
306    void send_update(void)
307    {
308            char update[40];
309            sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
310            send_sms(update);
311    
312            global_sms_counter++;
313    }      
314    
315    void send_sms(const unsigned char* payload)
316    {
317            sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
318            serial_send();
319            sprintf(global_serial_send, "%s%c", payload, 0x1A);
320            serial_send();
321            DelayMs(150);
322            if(global_sms_counter % 3 == 0)
323            {
324                    eeprom_writer();
325                    sprintf(global_serial_send, "at+cmgd=1,3\r");
326                    serial_send();
327                    while ( strstr(global_message_buffer1,"OK") == 0) ;
328          }          }
329  }        }      
330    
331  void serial_recieved(void)  void convertTemp()
332    {
333            short adVal;
334            adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
335            if( adVal >=840 )
336                    global_temp = 100;
337            else
338                    global_temp = (unsigned char) (adVal / 8.3886);
339    }
340    
341    
342    
343    void eeprom_writer(void)
344  {  {
345          char data, saved_data[LCD_LENGTH];          char len,i;
346                    
347          data = RCREG;          len = strlen(global_cell_nr);
348            eeprom_write(0, (global_time_interval/60));
349            eeprom_write(1, global_sms_counter>>8);
350            eeprom_write(2, global_sms_counter);
351            eeprom_write(3, len);
352                    
353          global_serial_recieve_buffer[global_serial_byte_counter] = data;          for (i=0; i<len; ++i)
         if(data == '\r')  
354          {          {
355                  global_recieve_done = 1;                  eeprom_write(i+4, global_cell_nr[i] );
356                  global_serial_byte_counter = 0;          }
357    }
358    
359    void eeprom_reader(void)
360    {
361            char len,i;
362            
363            global_time_interval = eeprom_read(0);
364            global_time_interval *= 60;
365            global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
366            len = eeprom_read(3);
367            
368            for (i=0; i<len; ++i)
369            {
370                    global_cell_nr[i] = eeprom_read(i+4);
371            }
372            
373            global_cell_nr[i] = 0; //zero terminated!
374    }      
375    
376    void on_initial_recieve(void)
377    {
378            char imei[16];
379            char* ptr;
380            char i;
381            char buf[2];
382            
383            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
384            {
385                    global_modem_init = 1;
386    
387                    ptr = strstr(global_serial_recieve_buffer,"cgsn");
388                    ptr +=4;
389                    strncpy(imei, ptr,15);
390                    imei[15] = 0;
391                    
392                    reset_recieve_buffer();
393            }
394    }
395    
396    void on_recieve(void)
397    {
398            char tmp[3];
399            char* ptr;
400            tmp[0]=0;
401                    
402            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                    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          else          reset_recieve_buffer();
414    }
415    
416    void sms_recieved(void)
417    {
418            char buf[4];
419            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            DelayUs(10);
429            
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                  global_serial_byte_counter++;                  i++; //spring over ':'
441                    pos = 0;
442                    for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
443                    {
444                            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                    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)
466    {
467            int wanted = (global_time_counter + time) % global_time_interval;
468                    
469            while (global_time_counter < wanted) ;
470    }
471    
472  void main()  void main()
473  {  {
474  /////////////////////////////////////////////  ////////////////////
475  // Running Init's  // Running Init's //
476    
477          // Running init for various components.          // Running init for various components.
         //AD_init();  
         ad_init();  
         rs232_init();  
478          pic18_io_init();          pic18_io_init();
479            RA5 = 1;                        // Indicates that the board is running inits.
480            
481            rs232_init();
482            ad_init();
483          lcd_init(0);          lcd_init(0);
484            lcd_clear();
485            lcd_home();
486          interrupt_init();          interrupt_init();
487                    timer_init();
488  /////////////////////////////////////////////          sms_init();
489  // Main loop          eeprom_reader();
490    ///////////////
491    // 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.
501                    if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING ==1 ) && global_emergency_counter >= 600 )
502                    {
503                            send_update();
504                            global_emergency_counter = 0;
505                    }
506                    
507            // Every X sec. a status sms is send.
508                    if(global_time_counter >= global_time_interval)
509                    {
510                            send_update();
511                            global_time_counter = 0;
512                    }
513            // To avoid buffer overrun.
514                    if( global_emergency_counter > 7200 )
515                            global_emergency_counter = 600;
516    
517            // Checks if there has been recieved a config sms.
518                    if(global_interval_changed )
519                    {
520                            eeprom_writer();
521                            global_interval_changed = 0;
522                    }
523          // 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_??
524                  if(GODONE==0)                  if(GODONE==0)
525                  {                  {
526                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
527                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
528                            convertTemp();
529                            update_lcd();
530                          GODONE = 1;                          GODONE = 1;
531                  }                  }
532                            // Handels the recieve sms'es.
533                  if( global_recieve_done == 1 )                  on_recieve();
                 {  
                         lcd_clear();  
                         lcd_goto(0x00);  
                         lcd_puts(global_serial_recieve_buffer);  
                         global_recieve_done = 0;  
                 }  
534          }          }
535  }  }

Legend:
Removed from v.137  
changed lines
  Added in v.175

  ViewVC Help
Powered by ViewVC 1.1.20