/[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 165 by hedin, Wed Dec 5 16:00:03 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;
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 char 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  void serial_recieved(void);  void serial_recieved(void);
48    void serial_send(void);
49    void update_lcd(void);
50    void convertTemp(void);
51    void timer1_interrupt(void);
52    void on_recieve(void);
53    void on_initial_recieve(void);
54    void sms_recieved(void);
55    void send_sms(const unsigned char* payload);
56    
57    
58    void reset_recieve_buffer(void)
59    {
60            global_recieve_done = 0;
61            
62            global_message_buffer_length1 = 0;
63            //global_message_buffer1[0] = 0;        
64            global_message_buffer_length2 = 0;
65            //global_message_buffer2[0] = 0;        
66            
67            memset(global_message_buffer1, 0, BUFFER);
68            memset(global_message_buffer2, 0, BUFFER);
69    }      
70    
71  // Nicked from H7  void pic18_io_init(void)
72  void ad_init(void)  {
73            TRISA0  = 1;    // analog input
74            TRISA5  = 0;    // Output
75            TRISB1  = 1;    // TRISB1-4 Digital input
76            TRISB2  = 1;
77            TRISB3  = 1;
78            TRISB4  = 1;
79    }
80    
81    
82    
83    void ad_init(void) // Nicked from H7
84  {  {
85          // AD Conversion clock          // AD Conversion clock
86          ADCS0 = 0;          ADCS0 = 0;
# Line 38  void ad_init(void) Line 99  void ad_init(void)
99          PCFG2=1;          PCFG2=1;
100          PCFG3=1;          PCFG3=1;
101                    
102          //Result is left justified          //Result is right justified
103          ADFM=0;          ADFM=1;
104                    
105          //Fire up for A/D converter module          //Fire up for A/D converter module
106          ADON=1;          ADON=1;
# Line 65  void interrupt_init(void) Line 126  void interrupt_init(void)
126          RCIE    = 1;    // Recieve interrupt enable.          RCIE    = 1;    // Recieve interrupt enable.
127          IPEN    = 0;    // Nfo interrupt priority          IPEN    = 0;    // Nfo interrupt priority
128          TXIE    = 0;    // Serial interrupt enabled          TXIE    = 0;    // Serial interrupt enabled
129            TMR1IE  = 1;    // Enables timer 1
130  }        }      
131    
132  void pic18_io_init(void)  void timer_init(void)
133  {  {
134          TRISA0  = 1;    // analog input          TMR1CS = 1; //use external clock
135          TRISB1  = 1;    // TRISB1-4 Digital input          
136          TRISB2  = 1;          T1CKPS1 = 1; //1:8 prescale
137          TRISB3  = 1;          T1CKPS0 = 1;
138          TRISB4  = 1;          
139            TMR1H = 0xEF;
140            TMR1L = 0xFF;
141    
142            T1OSCEN = 1; //enable oscillator circuit        
143            RD16 = 0; //normal 8 bit writes
144            TMR1ON = 1;
145  }        }      
146    
147  void sms_init(void)  void sms_init(void)
148  {  {
149            int i;
150            char buf[2];
151    
152    
153            reset_recieve_buffer();
154            sprintf(global_serial_send,"at+cgsn\r");
155            serial_send();
156            DelaySek(1);
157            //while(!global_recieve_done) ;
158            
159            while (global_has_imei == 0)
160            {
161                    if (strstr(global_message_buffer1,"OK") != 0)
162                    {
163                            global_imei_tversum = 0;
164                            for (i=0; i<15; ++i)
165                            {
166                                    buf[0] = global_message_buffer2[i];
167                                    buf[1] = 0;
168                                    global_imei_tversum += atoi(buf);
169                                    //global_imei_tversum += (global_message_buffer2[i] - '0');
170                            }
171                            
172                            global_has_imei = 1;
173                    }
174            }
175    
176    
177            sprintf(global_serial_send,"%s", "at+cpin=8043\r");
178            serial_send();
179            
180            while(global_modem_init == 0)
181            {
182                    on_initial_recieve();
183            }
184            DelayMs(5);
185            
186            reset_recieve_buffer();
187            sprintf(global_serial_send, "at+cmgd=1,4\r");
188            serial_send();
189            while ( strstr(global_message_buffer1,"OK") == 0) ;
190  }  }
191    
192    
193  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
194  {  {
195          // 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 198  void interrupt interrupt_handler(void)
198                  serial_recieved();                  serial_recieved();
199                  RCIF = 0;                  RCIF = 0;
200          }          }
201            
202            if(TMR1IF == 1)                 // timer1 interrupt trigger.
203            {
204                    timer1_interrupt();
205                    TMR1IF = 0;
206            }
207  }                }              
208    
209    
   
210  void serial_send(void)  void serial_send(void)
211  {  {
212          int i;          int i;
213          char tosend[3];          char data_byte;
214          char data;          for(i = 0; i < BUFFER; i++)
         sprintf(tosend,"%s", "at\r");  
         for(i = 0; i < 3; i++)  
215          {          {
216                  data = tosend[i];                  data_byte = global_serial_send[i];
217                  TXREG = data;                  if( data_byte == '\r')
218                  while(TRMT==0)                          i = (BUFFER - 1);
219                  DelayMs(1000);                  TXREG = data_byte;
220                    while(TRMT==0) ;
221                    DelayMs(10);
222          }          }
223            DelayMs(150);
224            global_serial_send[0] = 0;
225            DelayMs(150);
226  }        }      
227    
228  void serial_recieved(void)  ///////////////////////////////////////////////////////////////////////////////////////////////////
229    // Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from,
230    // global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done.
231    void serial_recieved(void)
232  {  {
233          char data, saved_data[LCD_LENGTH];          char data_byte;
234            
235            data_byte = RCREG;
236            
237            if (data_byte == '\n')          // Cant be bothered to do anyting if the byte is a '\n'.
238                    return;
239                    
240            if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.
241                    return;
242    
243    
244            if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
245                    global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte;       // fills the data_byte into our buffer.
246                    
         data = RCREG;  
247                    
248          global_serial_recieve_buffer[global_serial_byte_counter] = data;          
249          if(data == '\r')          if (data_byte == '\r')  // when we meet a '\r', the transmission is done, and we fill the constxt of
250                                                            // global_message_buffer1 into global_message_buffer2 our main buffer into
251                                                            // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
252                                                            // into global_message_buffer1.
253          {          {
254                  global_recieve_done = 1;                  global_recieve_done = 1; // indicates the recieve transmission is done.
255                    global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
256                    
257                    // global_message_buffer1 -> global_message_buffer2
258                    strcpy(global_message_buffer2, global_message_buffer1);
259                    global_message_buffer_length2 = global_message_buffer_length1;
260                    
261                    // global_serial_recieve_buffer -> global_message_buffer1
262                    strcpy(global_message_buffer1, global_serial_recieve_buffer);
263                    global_message_buffer_length1 = global_serial_byte_counter;
264                    
265                    
266                  global_serial_byte_counter = 0;                  global_serial_byte_counter = 0;
267          }                }      
268    }
269    
270    void timer1_interrupt(void)
271    {
272            TMR1H = 0xEF;
273            TMR1L = 0xFF;
274            global_time_counter++;
275            global_emergency_counter++;
276            RA1 = !RA1;
277    }
278    
279    void update_lcd(void)
280    {
281            if(global_temp_update_display != global_time_counter)
282            {
283                    lcd_clear();
284                    lcd_goto(0x00);
285            
286                    sprintf(global_lcd_buf, "Temp: %3d", global_temp);
287                    lcd_puts(global_lcd_buf);
288                    global_temp_update_display = global_time_counter;
289            }
290    
291    }
292            
293    void send_update(void)
294    {
295            char update[40];
296            sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
297            send_sms(update);
298    
299            global_sms_counter++;
300    }      
301    
302    void send_sms(const unsigned char* payload)
303    {
304            sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
305            serial_send();
306            sprintf(global_serial_send, "%s%c", payload, 0x1A);
307            serial_send();
308            DelayMs(150);
309            
310    }      
311    
312    void convertTemp()
313    {
314            short adVal;
315            adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
316            if( adVal >=840 )
317                    global_temp = 100;
318          else          else
319                    global_temp = (unsigned char) (adVal / 8.3886);
320    }
321    
322    
323    
324    void eeprom_writer(void)
325    {
326            char len,i;
327            
328            len = strlen(global_cell_nr);
329            eeprom_write(0, (global_time_interval/60));
330            eeprom_write(1, global_sms_counter>>8);
331            eeprom_write(2, global_sms_counter);
332            eeprom_write(3, len);
333            
334            for (i=0; i<len; ++i)
335          {          {
336                  global_serial_byte_counter++;                  eeprom_write(i+4, global_cell_nr[i] );
337          }          }
338    }
339    
340    void eeprom_reader(void)
341    {
342            char len,i;
343            
344            global_time_interval = eeprom_read(0);
345            global_time_interval *= 60;
346            global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
347            len = eeprom_read(3);
348                    
349            for (i=0; i<len; ++i)
350            {
351                    global_cell_nr[i] = eeprom_read(i+4);
352            }
353            
354            global_cell_nr[i] = 0; //zero terminated!
355    }      
356    
357    void on_initial_recieve(void)
358    {
359            char imei[16];
360            char* ptr;
361            char i;
362            char buf[2];
363            
364            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
365            {
366                    global_modem_init = 1;
367    
368                    ptr = strstr(global_serial_recieve_buffer,"cgsn");
369                    ptr +=4;
370                    strncpy(imei, ptr,15);
371                    imei[15] = 0;
372                    
373                    reset_recieve_buffer();
374            }
375  }  }
376    
377    void on_recieve(void)
378    {
379            char tmp[3];
380            char* ptr;
381            tmp[0]=0;
382                    
383            if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
384                    return;
385    
386            if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
387            {
388                    ptr = strstr(global_message_buffer1,",");       // finds the point just before the nr. of the SMS.
389                    strcat(tmp,ptr+1);                                                      // puts that number in tmp
390                    global_sms_recieve_number[0] = 0;                       // wanna be sure that we write the new number from global_sms_recieve_number[0]
391                    strcat(global_sms_recieve_number, tmp);         // puts the sms number into the global variable.
392                    sms_recieved();
393            }      
394            reset_recieve_buffer();
395    }
396    
397    void sms_recieved(void)
398    {
399            char buf[4];
400            char i,imei;
401            char pos;
402            
403            sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
404            serial_send();          // Sends the command.
405                    
406            while(strstr(global_message_buffer1, "OK")  == 0)       // stays here until we recieve a "OK" from the modem.
407                    DelayMs(1);
408    
409            DelayUs(10);
410            
411            for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
412            {
413                    buf[i] = global_message_buffer2[i];
414            }
415            
416            buf[i] = 0;
417            imei = atoi(buf);
418            
419            if (imei == global_imei_tversum)
420            {
421                    i++; //spring over ':'
422                    pos = 0;
423                    for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
424                    {
425                            global_cell_nr[pos] = global_message_buffer2[i];
426                    }
427                    global_cell_nr[pos] = 0; //zero terminator
428                    
429                    i++; //spring over ':'
430                    pos=0;
431                    for ( ; global_message_buffer2[i] ; ++i,++pos)
432                    {
433                            buf[pos] = global_message_buffer2[i];
434                    }
435                    buf[pos]=0;
436                    
437                    global_time_interval = atoi(buf);
438                    global_time_interval *= 60;
439                    eeprom_writer();                        // writes the new cell nr. and time interval to the eeprom.
440                    send_sms("conf ok");
441            }
442            
443    
444    }
445    
446    
447    
448  void main()  void main()
449  {  {
450  /////////////////////////////////////////////  ////////////////////
451  // Running Init's  // Running Init's //
452    
453          // Running init for various components.          // Running init for various components.
         //AD_init();  
         ad_init();  
         rs232_init();  
454          pic18_io_init();          pic18_io_init();
455            RA5 = 1;                        // Indicates that the board is running inits.
456            
457            rs232_init();
458            ad_init();
459          lcd_init(0);          lcd_init(0);
460          interrupt_init();          interrupt_init();
461                    sms_init();
462  /////////////////////////////////////////////          //eeprom_init();
463  // Main loop          timer_init();
464            eeprom_reader();
465    ///////////////
466    // Main loop //
467    
468            DelayMs(50);
469            reset_recieve_buffer();
470    
471            RA5 = 0;                        // Inits are done, and RA1 will now work as a error notifier.
472    
473          while(1)          while(1)
474          {          {
475            // If there happends to be a critical state on the system, we send a sms.
476                    if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 )
477                    {
478                            send_update();
479                            global_emergency_counter = 0;
480                    }
481            // Every X sec. a status sms is send.
482                    if(global_time_counter >= 3600)
483                    {
484                            send_update();
485                            global_time_counter = 0;
486                    }
487            // To avoid buffer overrun.
488                    if( global_emergency_counter > 7200 )
489                            global_emergency_counter = 600;
490    
491            // Checks if there has been recieved a config sms.
492                    if(global_interval_changed )
493                    {
494                            eeprom_writer();
495                            global_interval_changed = 0;
496                    }
497          // 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_??
498                  if(GODONE==0)                  if(GODONE==0)
499                  {                  {
500                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
501                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
502                            convertTemp();
503                            update_lcd();
504                          GODONE = 1;                          GODONE = 1;
505                  }                  }
506                            // Handels the recieve sms'es.
507                  if( global_recieve_done == 1 )                  on_recieve();
                 {  
                         lcd_clear();  
                         lcd_goto(0x00);  
                         lcd_puts(global_serial_recieve_buffer);  
                         global_recieve_done = 0;  
                 }  
508          }          }
509  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20