/[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 148 by hedin, Tue Dec 4 17:23:27 2007 UTC revision 238 by hedin, Tue Dec 11 14:51: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"
 // Delay.h is included inside lcd.c  
9    
10  #define LCD_LENGTH 16  #define BUFFER 160
 #define LCD_ROWS 2  
 #define SEND_BUFFER 128  
11  #define PWRFAIL RB1  #define PWRFAIL RB1
12  #define FIREDET RB2  #define FIREDET RB2
13  #define FEEDING RB3  #define FEEDING RB3
# Line 17  Line 15 
15    
16    
17  unsigned char global_Pot_Hi, global_Pot_Lo;  unsigned char global_Pot_Hi, global_Pot_Lo;
18  unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH];  unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];
19  unsigned char global_serial_send[SEND_BUFFER], global_serial_recieve_buffer[SEND_BUFFER];  bit global_recieve_done = 0;
20  bit global_recieve_done = 0, global_interval_changed = 0;  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;  
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 char cell_nr[15] = ""; // = "21681784";  unsigned int global_temp_update_display = 0;
25    
26    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;
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);
51    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)
58    {
59            global_recieve_done = 0;
60            
61            global_message_buffer_length1 = 0;
62            global_message_buffer_length2 = 0;
63            
64            memset(global_message_buffer1, 0, BUFFER);
65            memset(global_message_buffer2, 0, BUFFER);
66    }      
67    //////////  INITS  //////////
68    void pic18_io_init(void)
69    {
70            TRISA0  = 1;    // analog input
71            TRISA5  = 0;    // Output
72            TRISB1  = 1;    // TRISB1-4 Digital input
73            TRISB2  = 1;
74            TRISB3  = 1;
75            TRISB4  = 1;
76    }
77    
78  // Nicked from H7  
79  void ad_init(void)  
80    void ad_init(void) // Nicked from H7
81  {  {
82          // AD Conversion clock          // AD Conversion clock
83          ADCS0 = 0;          ADCS0 = 0;
84          ADCS1 = 0;          ADCS1 = 0;
85          ADCS2 = 0;          ADCS2 = 0;
86    
87          //Select AN0/RA0 for AD source          //Select AN0/RA0 for AD source.
88                  // In this (000) setup, it's only RA0/AN0 that does ad convertion.          // In this (000) setup, it's only RA0/AN0 that does ad convertion.
89          CHS0=0;          CHS0=0;
90          CHS1=0;          CHS1=0;
91          CHS2=0;          CHS2=0;
# Line 56  void ad_init(void) Line 96  void ad_init(void)
96          PCFG2=1;          PCFG2=1;
97          PCFG3=1;          PCFG3=1;
98                    
99            //Reset the A/D result registers
100            ADRESH = 0;
101            ADRESL = 0;
102            
103          //Result is right justified          //Result is right justified
104          ADFM=1;          ADFM=1;
105                    
# Line 63  void ad_init(void) Line 107  void ad_init(void)
107          ADON=1;          ADON=1;
108  }  }
109    
 void rs232_init(void)  
 {  
         SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled  
         TRISC6  = 0;      
         TRISC7  = 1;  
110    
         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  
 }  
111    
112  void interrupt_init(void)  void interrupt_init(void)
113  {  {
# Line 86  void interrupt_init(void) Line 120  void interrupt_init(void)
120          TMR1IE  = 1;    // Enables timer 1          TMR1IE  = 1;    // Enables timer 1
121  }        }      
122    
123    
124    
125  void timer_init(void)  void timer_init(void)
126  {  {
127          TMR1CS = 1; //use external clock          TMR1CS = 1;     //use external clock
128                    
129          T1CKPS1 = 1; //1:8 prescale          T1CKPS1 = 1;    //1:8 prescale
130          T1CKPS0 = 1;          T1CKPS0 = 1;
131                    
132          TMR1H = 0xEF;          TMR1H = 0xEF;
133          TMR1L = 0xFF;          TMR1L = 0xFF;
134    
135          T1OSCEN = 1; //enable oscillator circuit                  T1OSCEN = 1;    //enable oscillator circuit    
136          RD16 = 0; //normal 8 bit writes          RD16 = 0;       //normal 8 bit writes
137          TMR1ON = 1;          TMR1ON = 1;
138  }  }
139            
140  void pic18_io_init(void)  void rs232_init(void)
141  {  {
142          TRISA0  = 1;    // analog input          SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled
143          TRISA1  = 0;    // Output          TRISC6  = 0;    
144          TRISB1  = 1;    // TRISB1-4 Digital input          TRISC7  = 1;
         TRISB2  = 1;  
         TRISB3  = 1;  
         TRISB4  = 1;  
 }        
145    
146  void sms_init(void)          SPBRG   = 207;  // 1200 baud rate... 25 = 9600
147                                            // x = (Fosc / (16*[baud rate]) )-1
148            TXSTA   = 0x24; // Enables BRGH and TXEN inthe TXSTA register
149            RCSTA   = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
150    }
151    
152    void modem_init(void)
153  {  {
154          int i;          int i;
155            char buf[2];
156            
157            while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ;  // Waiting for the modem to be ready
158    
159            reset_recieve_buffer();
160          sprintf(global_serial_send,"at+cgsn\r");          sprintf(global_serial_send,"at+cgsn\r");
161          serial_send();          serial_send();
162          DelaySek(1);          
163          while(!global_recieve_done) ;          while (global_has_imei == 0)
164            {
165                    if (strstr(global_message_buffer1,"OK") != 0)
166                    {
167                            global_imei_tversum = 0;
168                            for (i=0; i<15; ++i)
169                            {
170                                    buf[0] = global_message_buffer2[i];
171                                    buf[1] = 0;
172                                    global_imei_tversum += atoi(buf);
173                            }
174                            
175                            global_has_imei = 1;
176                    }
177            }
178    
179          sprintf(global_serial_send,"%s", "at+cpin=8043\r");          sprintf(global_serial_send,"%s", "at+cpin?\r");
180          serial_send();          serial_send();
181          DelaySek(30);          DelayMs(100);           // Delay to give the modem a chance to answer.
182                    
183          update_lcd();          if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0)
184          DelaySek(5);          {
185                    sprintf(global_serial_send,"%s", "at+cpin=8043\r");
186                    serial_send();
187                    
188                    while(global_modem_init == 0)
189                    {
190                            on_initial_recieve();
191                    }
192            }
193            reset_recieve_buffer();
194            sprintf(global_serial_send, "at+cmgd=1,4\r");
195            serial_send();
196            while ( strstr(global_message_buffer1,"OK") == 0) ;
197    }
198    
199    void serial_send(void)
200    {
201            int i;
202            char data_byte;
203            for(i = 0; i < BUFFER; i++)
204            {
205                    data_byte = global_serial_send[i];
206                    if( data_byte == '\r')
207                            i = (BUFFER - 1);
208                    TXREG = data_byte;
209                    while(TRMT==0) ;
210                    DelayMs(10);
211            }
212            DelayMs(150);
213            global_serial_send[0] = 0;
214            DelayMs(150);
215  }  }
216    
217    void on_initial_recieve(void)
218    {
219            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
220            {
221                    global_modem_init = 1;
222                    reset_recieve_buffer();
223            }
224    }
225    
226    //////////  INTERRUPT HANDLER  //////////
227  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
228  {  {
229          // 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 146  void interrupt interrupt_handler(void) Line 240  void interrupt interrupt_handler(void)
240          }          }
241  }                }              
242    
243    //////////  INTERRUPT TRIGGED //////////
244  void serial_send(void)  void serial_recieved(void)
245  {  {
         int i;  
246          char data_byte;          char data_byte;
         for(i = 0; i < SEND_BUFFER; i++)  
         {  
                 data_byte = global_serial_send[i];  
                 if( data_byte == '\r')  
                         i = (SEND_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];  
247                    
248          data_byte = RCREG;          data_byte = RCREG;
249                    
250          global_serial_recieve_buffer[global_serial_byte_counter] = data_byte;          if (data_byte == '\n')          // Cant be bothered to do anyting if the byte is a '\n'.
251          if(data_byte == '\r')                  return;
252          {                  
253                  global_recieve_done = 1;          if (global_serial_byte_counter == 0 && data_byte == '\r')       // don't care about '\r', if it's the first byte we recieve.
254                  global_serial_byte_counter = 0;                  return;
255          }        
256    
257            if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun
258                    global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte;       // fills the data_byte into our buffer.
259          else          else
260          {          {
261                  global_serial_byte_counter++;                  global_serial_recieve_buffer[0] = 0;
262                    global_serial_byte_counter = 0;
263                    return;
264          }          }
265                    
266            
267            if (data_byte == '\r')  // when we meet a '\r', the transmission is done, and we fill the constxt of
268                                                            // global_message_buffer1 into global_message_buffer2 our main buffer into
269                                                            // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
270                                                            // into global_message_buffer1.
271            {
272                    global_recieve_done = 1; // indicates the recieve transmission is done.
273                    global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
274                    
275                    // global_message_buffer1 -> global_message_buffer2
276                    strcpy(global_message_buffer2, global_message_buffer1);
277                    global_message_buffer_length2 = global_message_buffer_length1;
278                    
279                    // global_serial_recieve_buffer -> global_message_buffer1
280                    strcpy(global_message_buffer1, global_serial_recieve_buffer);
281                    global_message_buffer_length1 = global_serial_byte_counter;
282                    
283                    
284                    global_serial_byte_counter = 0;
285            }      
286  }  }
287    
288  void timer1_interrupt(void)  void timer1_interrupt(void)
# Line 189  void timer1_interrupt(void) Line 291  void timer1_interrupt(void)
291          TMR1L = 0xFF;          TMR1L = 0xFF;
292          global_time_counter++;          global_time_counter++;
293          global_emergency_counter++;          global_emergency_counter++;
         RA1 = !RA1;  
294  }  }
295    
296    //////////  ORDENARY FUNKTIONS  //////////
297  void update_lcd(void)  void update_lcd(void)
298  {  {
299          lcd_clear();          if(global_temp_update_display != global_time_counter)
300          lcd_goto(0x00);          {
301          lcd_puts(global_temp);                  lcd_goto(0x00);
302            
303                    sprintf(global_lcd_buf, "Temp: %3d", global_temp);
304                    lcd_puts(global_lcd_buf);
305                    global_temp_update_display = global_time_counter;
306            }
307    
308  }  }
309                    
310  void send_update(void)  void send_update(void)
311  {  {
312          sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr);          char update[40];
313            sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
314            send_sms(update);
315    
316            global_sms_counter++;
317    }      
318    
319    void send_sms(const unsigned char* payload)
320    {
321            sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
322          serial_send();          serial_send();
323          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);  
324          serial_send();          serial_send();
325          DelayMs(250);          DelayMs(150);
326          global_sms_counter++;          if(global_sms_counter % 4 == 0)
327            {
328                    eeprom_writer();
329                    sprintf(global_serial_send, "at+cmgd=1,3\r");
330                    serial_send();
331                    while ( strstr(global_message_buffer1,"OK") == 0) ;
332            }
333  }        }      
334    
335  void convertTemp()  void convert_temp(void)
336  {  {
337          short adVal;          short adVal;
338          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;          adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
339          if( adVal >=840 )          if( adVal >=840 ) // 840 == 4.1V or 82% of 5V, we do this because 4.1V == 100C.
340                  global_temp = 100;                  global_temp = 100;
341          else          else
342                  global_temp = (adVal / 8.3886);                  global_temp = (unsigned char) (adVal / 8.3886);
343  }  }
344    
345    
# Line 228  void eeprom_writer(void) Line 348  void eeprom_writer(void)
348  {  {
349          char len,i;          char len,i;
350                    
351          len = strlen(cell_nr);          len = strlen(global_cell_nr);
352          eeprom_write(0, (global_time_interval/60));          eeprom_write(0, (global_time_interval/60));
353          eeprom_write(1, global_sms_counter>>8);          eeprom_write(1, global_sms_counter>>8);
354          eeprom_write(2, global_sms_counter);          eeprom_write(2, global_sms_counter);
# Line 236  void eeprom_writer(void) Line 356  void eeprom_writer(void)
356                    
357          for (i=0; i<len; ++i)          for (i=0; i<len; ++i)
358          {          {
359                  eeprom_write(i+4, cell_nr[i] );                  eeprom_write(i+4, global_cell_nr[i] );
360          }          }
361  }  }
362    
# Line 251  void eeprom_reader(void) Line 371  void eeprom_reader(void)
371                    
372          for (i=0; i<len; ++i)          for (i=0; i<len; ++i)
373          {          {
374                  cell_nr[i] = eeprom_read(i+4);                  global_cell_nr[i] = eeprom_read(i+4);
375          }          }
376                    
377          cell_nr[i] = 0; //zero terminated!          global_cell_nr[i] = 0; //zero terminated!
378  }        }      
379    
380    
381  void on_recieve(void)  void on_recieve(void)
382  {  {
383            char tmp[3];
384            char* ptr;
385            tmp[0]=0;
386                    
387            if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
388                    return;
389    
390            if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
391            {
392                    ptr = strstr(global_message_buffer1,",");       // finds the point just before the nr. of the SMS.
393                    strcat(tmp,ptr+1);                      // puts that number in tmp
394                    global_sms_recieve_number[0] = 0;       // wanna be sure that we write the new number from global_sms_recieve_number[0]
395                    strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable.
396                    sms_recieved();
397            }      
398            reset_recieve_buffer();
399    }
400    
401    void sms_recieved(void)
402    {
403            char buf[4];
404            char i,imei;
405            char pos;
406            
407            sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
408            serial_send();          // Sends the command.
409                    
410            while(strstr(global_message_buffer1, "OK")  == 0)       // stays here until we recieve a "OK" from the modem.
411                    DelayMs(1);
412    
413            DelayUs(10);
414            
415            for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
416            {
417                    buf[i] = global_message_buffer2[i];
418            }
419            
420            buf[i] = 0;
421            imei = atoi(buf);
422            
423            if (imei == global_imei_tversum)
424            {
425                    i++; //spring over ':'
426                    pos = 0;
427                    for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
428                    {
429                            global_cell_nr[pos] = global_message_buffer2[i];
430                    }
431                    global_cell_nr[pos] = 0; //zero terminator
432                    
433                    i++; //spring over ':'
434                    pos=0;
435                    for ( ; global_message_buffer2[i] ; ++i,++pos)
436                    {
437                            buf[pos] = global_message_buffer2[i];
438                    }
439                    buf[pos]=0;
440                    
441                    global_time_interval = atoi(buf);
442                    global_time_interval *= 60;
443                    eeprom_writer();  // writes the new cell nr. and time interval to the eeprom.
444                    send_sms("conf ok");
445            }
446            
447    
448    }
449    
450    void Delay(int time)
451    {
452            int wanted = (global_time_counter + time) % global_time_interval;
453            
454            while (global_time_counter < wanted) ;
455  }  }
456    
457  void main()  void main()
# Line 269  void main() Line 461  void main()
461    
462          // Running init for various components.          // Running init for various components.
463          pic18_io_init();          pic18_io_init();
464            RA5 = 1;                // Indicates that the board is running inits.
465            
466          rs232_init();          rs232_init();
467          ad_init();          ad_init();
468          lcd_init(0);          lcd_init(0);
469            lcd_clear();
470            lcd_home();
471          interrupt_init();          interrupt_init();
         sms_init();  
         //eeprom_init();  
472          timer_init();          timer_init();
473            modem_init();
474          eeprom_reader();          eeprom_reader();
475  ///////////////  ///////////////
476  // Main loop //  // Main loop //
477  send_update();  
478            DelayMs(50);
479            reset_recieve_buffer();
480    
481            RA5 = 0;                // Inits are done, and RA1 will now work as a error notifier.
482    
483          while(1)          while(1)
484          {          {
485          // 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.
486                  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 )
487                  {                  {
488                          send_update();                          send_update();
489                          global_emergency_counter = 0;                          global_emergency_counter = 0;
490                  }                  }
491                    
492          // Every X sec. a status sms is send.          // Every X sec. a status sms is send.
493                  if(global_time_counter >= 3600)                  if(global_time_counter >= global_time_interval)
494                  {                  {
495                          send_update();                          send_update();
496                          global_time_counter = 0;                          global_time_counter = 0;
# Line 298  send_update(); Line 499  send_update();
499                  if( global_emergency_counter > 7200 )                  if( global_emergency_counter > 7200 )
500                          global_emergency_counter = 600;                          global_emergency_counter = 600;
501    
         // Checks if there has been recieved a config sms.  
                 if(global_interval_changed )  
                 {  
                         eeprom_writer();  
                         global_interval_changed = 0;  
                 }  
502          // 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_??
503                  if(GODONE==0)                  if(GODONE==0)
504                  {                  {
505                          global_Pot_Hi = ADRESH;                          global_Pot_Hi = ADRESH;
506                          global_Pot_Lo = ADRESL;                          global_Pot_Lo = ADRESL;
507                          convertTemp();                          convert_temp();
508                          update_lcd();                          update_lcd();
509                          GODONE = 1;                          GODONE = 1;
510                  }                  }
511            // Handels the recieve sms'es.
512                    on_recieve();
513          }          }
514  }  }

Legend:
Removed from v.148  
changed lines
  Added in v.238

  ViewVC Help
Powered by ViewVC 1.1.20