/[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 82 by hedin, Wed Nov 28 17:00:45 2007 UTC revision 167 by hedin, Wed Dec 5 16:11:12 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"
9    // Delay.h is included inside lcd.c
10    
11    #define LCD_LENGTH 16
12    #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;
21    unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER];
22    bit global_recieve_done = 0, global_interval_changed = 0;
23    unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0;
24    unsigned int global_emergency_counter = 600, global_time_interval = 3600;
25    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);
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    void pic18_io_init(void)
72    {
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
86            ADCS0 = 0;
87            ADCS1 = 0;
88            ADCS2 = 0;
89    
90            //Select AN0/RA0 for AD source
91                    // In this (000) setup, it's only RA0/AN0 that does ad convertion.
92            CHS0=0;
93            CHS1=0;
94            CHS2=0;
95            
96            //Only AN0 is selected for AD and with Vdd/Vss as limits
97            PCFG0=0;
98            PCFG1=1;
99            PCFG2=1;
100            PCFG3=1;
101            
102            //Result is right justified
103            ADFM=1;
104            
105            //Fire up for A/D converter module
106            ADON=1;
107    }
108    
109  void rs232_init(void)  void rs232_init(void)
110  {  {
111          SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled          SPEN    = 0;    // Serial Port Enable Bit... 0 = disabled
112          TRISC6  = 0;          TRISC6  = 0;    
113          TRISC7  = 1;          TRISC7  = 1;
114          SYNC    = 0; // SYNC switches between async(0) and sync(1) mode.  
115          SPBRG   = 25;          SPBRG   = 207;  // 1200 baud rate... 25 = 9600
116          TXSTA   = 0x24;                                          // x = (Fosc / (16*[baud rate]) )-1
117          RCSTA   = 0x90;          TXSTA   = 0x24; // Enables BRGH and TXEN inthe TXSTA register
118          SPEN    = 1;          RCSTA   = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register
119  }  }
120    
121  void interrupt_init(void)  void interrupt_init(void)
# Line 22  void interrupt_init(void) Line 123  void interrupt_init(void)
123          // Assumes that all interrupts default is 0          // Assumes that all interrupts default is 0
124          PEIE    = 1;          PEIE    = 1;
125          GIE             = 1;          GIE             = 1;
126          RCIE    = 1;          RCIE    = 1;    // Recieve interrupt enable.
127            IPEN    = 0;    // Nfo interrupt priority
128            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;          TMR1CS = 1; //use external clock
135          TRISB1  = 1;          
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 interrupt_recieve_handler(void)  void sms_init(void)
148  {  {
149          // Handle recieve inputs...          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)
 // Finds out what interrupt have been trigged, and starts the respective function.  
194  {  {
195          if(RCIF == 1)          // Finds out what interrupt have been trigged, and starts the respective function.
196            if(RCIF == 1)                   // Serial recieve interrupt
197          {          {
198                  interrupt_recieve_handler();                  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)
211    {
212            int i;
213            char data_byte;
214            for(i = 0; i < BUFFER; i++)
215            {
216                    data_byte = global_serial_send[i];
217                    if( data_byte == '\r')
218                            i = (BUFFER - 1);
219                    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    ///////////////////////////////////////////////////////////////////////////////////////////////////
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_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            else
247            {
248                    global_serial_recieve_buffer[0] = 0;
249                    global_serial_byte_counter = 0;
250                    return;
251            }
252                    
253            
254            if (data_byte == '\r')  // when we meet a '\r', the transmission is done, and we fill the constxt of
255                                                            // global_message_buffer1 into global_message_buffer2 our main buffer into
256                                                            // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer
257                                                            // into global_message_buffer1.
258            {
259                    global_recieve_done = 1; // indicates the recieve transmission is done.
260                    global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate
261                    
262                    // global_message_buffer1 -> global_message_buffer2
263                    strcpy(global_message_buffer2, global_message_buffer1);
264                    global_message_buffer_length2 = global_message_buffer_length1;
265                    
266                    // global_serial_recieve_buffer -> global_message_buffer1
267                    strcpy(global_message_buffer1, global_serial_recieve_buffer);
268                    global_message_buffer_length1 = global_serial_byte_counter;
269                    
270                    
271                    global_serial_byte_counter = 0;
272            }      
273    }
274    
275    void timer1_interrupt(void)
276    {
277            TMR1H = 0xEF;
278            TMR1L = 0xFF;
279            global_time_counter++;
280            global_emergency_counter++;
281            RA1 = !RA1;
282    }
283    
284    void update_lcd(void)
285    {
286            if(global_temp_update_display != global_time_counter)
287            {
288            //      lcd_clear();
289                    lcd_goto(0x00);
290            
291                    sprintf(global_lcd_buf, "Temp: %3d", global_temp);
292                    lcd_puts(global_lcd_buf);
293                    global_temp_update_display = global_time_counter;
294            }
295    
296    }
297            
298    void send_update(void)
299    {
300            char update[40];
301            sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL);
302            send_sms(update);
303    
304            global_sms_counter++;
305    }      
306    
307    void send_sms(const unsigned char* payload)
308    {
309            sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr);
310            serial_send();
311            sprintf(global_serial_send, "%s%c", payload, 0x1A);
312            serial_send();
313            DelayMs(150);
314            
315    }      
316    
317    void convertTemp()
318    {
319            short adVal;
320            adVal = (global_Pot_Hi << 8) | global_Pot_Lo;
321            if( adVal >=840 )
322                    global_temp = 100;
323            else
324                    global_temp = (unsigned char) (adVal / 8.3886);
325    }
326    
327    
328    
329    void eeprom_writer(void)
330    {
331            char len,i;
332            
333            len = strlen(global_cell_nr);
334            eeprom_write(0, (global_time_interval/60));
335            eeprom_write(1, global_sms_counter>>8);
336            eeprom_write(2, global_sms_counter);
337            eeprom_write(3, len);
338            
339            for (i=0; i<len; ++i)
340            {
341                    eeprom_write(i+4, global_cell_nr[i] );
342            }
343    }
344    
345    void eeprom_reader(void)
346    {
347            char len,i;
348            
349            global_time_interval = eeprom_read(0);
350            global_time_interval *= 60;
351            global_sms_counter = (eeprom_read(1)<<8) | eeprom_read(2);
352            len = eeprom_read(3);
353            
354            for (i=0; i<len; ++i)
355            {
356                    global_cell_nr[i] = eeprom_read(i+4);
357            }
358            
359            global_cell_nr[i] = 0; //zero terminated!
360    }      
361    
362    void on_initial_recieve(void)
363    {
364            char imei[16];
365            char* ptr;
366            char i;
367            char buf[2];
368            
369            if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0)
370            {
371                    global_modem_init = 1;
372    
373                    ptr = strstr(global_serial_recieve_buffer,"cgsn");
374                    ptr +=4;
375                    strncpy(imei, ptr,15);
376                    imei[15] = 0;
377                    
378                    reset_recieve_buffer();
379            }
380    }
381    
382    void on_recieve(void)
383    {
384            char tmp[3];
385            char* ptr;
386            tmp[0]=0;
387                    
388            if (global_recieve_done == 0 || global_message_buffer_length1 == 0)
389                    return;
390    
391            if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS
392            {
393                    ptr = strstr(global_message_buffer1,",");       // finds the point just before the nr. of the SMS.
394                    strcat(tmp,ptr+1);                                                      // puts that number in tmp
395                    global_sms_recieve_number[0] = 0;                       // wanna be sure that we write the new number from global_sms_recieve_number[0]
396                    strcat(global_sms_recieve_number, tmp);         // puts the sms number into the global variable.
397                    sms_recieved();
398            }      
399            reset_recieve_buffer();
400    }
401    
402    void sms_recieved(void)
403    {
404            char buf[4];
405            char i,imei;
406            char pos;
407            
408            sprintf(global_serial_send, "AT+CMGR=%s\r", global_sms_recieve_number); // formates the variable that sends commands to the SMS modem.
409            serial_send();          // Sends the command.
410            
411            while(strstr(global_message_buffer1, "OK")  == 0)       // stays here until we recieve a "OK" from the modem.
412                    DelayMs(1);
413    
414            DelayUs(10);
415            
416            for (i=0; global_message_buffer2[i] != ':' && global_message_buffer2[i] != 0; ++i)
417            {
418                    buf[i] = global_message_buffer2[i];
419            }
420            
421            buf[i] = 0;
422            imei = atoi(buf);
423            
424            if (imei == global_imei_tversum)
425            {
426                    i++; //spring over ':'
427                    pos = 0;
428                    for ( ; global_message_buffer2[i] != ':'; ++i, ++pos)
429                    {
430                            global_cell_nr[pos] = global_message_buffer2[i];
431                    }
432                    global_cell_nr[pos] = 0; //zero terminator
433                    
434                    i++; //spring over ':'
435                    pos=0;
436                    for ( ; global_message_buffer2[i] ; ++i,++pos)
437                    {
438                            buf[pos] = global_message_buffer2[i];
439                    }
440                    buf[pos]=0;
441                    
442                    global_time_interval = atoi(buf);
443                    global_time_interval *= 60;
444                    eeprom_writer();                        // writes the new cell nr. and time interval to the eeprom.
445                    send_sms("conf ok");
446            }
447            
448    
449    }
450    
451    
452    
453  void main()  void main()
454  {  {
455          rs232_init();  ////////////////////
456    // Running Init's //
457    
458            // Running init for various components.
459          pic18_io_init();          pic18_io_init();
460            RA5 = 1;                        // Indicates that the board is running inits.
461            
462            rs232_init();
463            ad_init();
464            lcd_init(0);
465            interrupt_init();
466            sms_init();
467            //eeprom_init();
468            timer_init();
469            eeprom_reader();
470    ///////////////
471    // Main loop //
472    
473            DelayMs(50);
474            reset_recieve_buffer();
475    
476            RA5 = 0;                        // Inits are done, and RA1 will now work as a error notifier.
477    
478            while(1)
479            {
480            // If there happends to be a critical state on the system, we send a sms.
481                    if( (global_temp >= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 )
482                    {
483                            send_update();
484                            global_emergency_counter = 0;
485                    }
486            // Every X sec. a status sms is send.
487                    if(global_time_counter >= 3600)
488                    {
489                            send_update();
490                            global_time_counter = 0;
491                    }
492            // To avoid buffer overrun.
493                    if( global_emergency_counter > 7200 )
494                            global_emergency_counter = 600;
495    
496            // Checks if there has been recieved a config sms.
497                    if(global_interval_changed )
498                    {
499                            eeprom_writer();
500                            global_interval_changed = 0;
501                    }
502            // Checking if A/D convertion is done, and save the data in global_Pot_??
503                    if(GODONE==0)
504                    {
505                            global_Pot_Hi = ADRESH;
506                            global_Pot_Lo = ADRESL;
507                            convertTemp();
508                            update_lcd();
509                            GODONE = 1;
510                    }
511            // Handels the recieve sms'es.
512                    on_recieve();
513            }
514  }  }

Legend:
Removed from v.82  
changed lines
  Added in v.167

  ViewVC Help
Powered by ViewVC 1.1.20