/[H7]/trunk/PIC/main.c
ViewVC logotype

Diff of /trunk/PIC/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 10 by torben, Mon Jan 29 15:29:28 2007 UTC revision 22 by torben, Tue Jan 30 22:49:03 2007 UTC
# Line 3  Line 3 
3  ///////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////
4  //Includes  //Includes
5  #include <pic18.h>  #include <pic18.h>
6    #include <string.h>
7    
8  #include "delay.h"  #include "delay.h"
9  #include "i2c.h"  #include "i2c.h"
10  #include "lcd.h"  #include "lcd.h"
# Line 11  Line 13 
13  //Defines  //Defines
14  #define TC74 0x9A /* I2C TC74 IC */  #define TC74 0x9A /* I2C TC74 IC */
15    
16  //////////////  #define SLIP_END 192
17  // Enums  #define SLIP_ESC 219
18    #define SLIP_ESCAPED_END 220
19    #define SLIP_ESCAPED_ESC 221
20    
21    #define BUFFERLEN 16
22    
23    //////////////////////////////////////////////////////////////////
24    // Enumerations
25    
26  enum SlipState{  enum SlipState{
27          SlipNormal,          SlipNormal,
# Line 21  enum SlipState{ Line 30  enum SlipState{
30          SlipStopped          SlipStopped
31  };  };
32    
33    enum BaudRates{
34            Baud1200,
35            Baud2400,
36            Baud4800,
37            Baud9600,
38            Baud19200
39    };
40    
41    enum Commands{
42            CmdRead,
43            CmdWrite,
44            CmdAck,
45            CmdNAck
46    };
47    
48    enum Targets {
49            TLed3, //0
50            TLed4,
51            TLed5,
52            TSwitch2,
53            TSwitch3,
54            TPotmeter,
55            TTemp,
56            TBaud = 10
57    };
58    
59  //////////////////////////////////////////////////////////////////  //////////////////////////////////////////////////////////////////
60  //Globale data  //Globale data
61  // Alle globale variabler bruger "global_" som prefix  // Alle globale variabler bruger "global_" som prefix
62  unsigned char global_comm_buffer[10];  
63    unsigned char global_comm_buffer[BUFFERLEN];
64  unsigned char global_comm_length;  unsigned char global_comm_length;
65  unsigned char global_comm_index;  
66  bit global_comm_ready;  bit global_comm_ready;
67  bit global_comm_recieving;  bit global_comm_error;
68    
69    unsigned char global_comm_slipstate;
70    unsigned char global_comm_currentrate;
71    
72    unsigned char global_lcd_buffer[20];
73    unsigned char global_lcd_index;
74    
75    
76  unsigned char global_comm_state;  bit global_led_0;
77    bit global_led_1;
78    bit global_led_2;
79    
80    bit global_reset_baudrate;
81    
82    char global_temp;
83    unsigned char global_potmeter_hi;
84    unsigned char global_potmeter_lo;
85    
86  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
87  // Interrupt funktioner  // Interrupt funktioner
88    
89  void recieve_interrupt(void)  void recieve_interrupt(void)
90  {  {
91            unsigned char data = RCREG;    
92            
93            switch (global_comm_slipstate)
94            {              
95                    case SlipNormal:
96                            if (data == SLIP_END)
97                            {
98                                    global_comm_ready = 1;
99                                    global_comm_slipstate = SlipStopped;
100                            }
101                            else if (data == SLIP_ESC)
102                            {
103                                    global_comm_slipstate = SlipEscaped;
104                            }
105                            else
106                                    global_comm_buffer[ global_comm_length++ ] = data;
107                            break;
108                            
109                    case SlipEscaped:
110                            if (data == SLIP_ESCAPED_ESC)
111                            {
112                                    global_comm_buffer[ global_comm_length++ ] = SLIP_ESC;
113                                    global_comm_slipstate = SlipNormal;
114                            }
115                            else if (data ==SLIP_ESCAPED_END)
116                            {
117                                    global_comm_buffer[ global_comm_length++ ] = SLIP_END;
118                                    global_comm_slipstate = SlipNormal;
119                            }
120                            else
121                            {
122                                    global_comm_slipstate = SlipError;
123                            }
124                    case SlipError:
125                            if (data == SLIP_END)
126                            {
127                                    global_comm_error = 1;
128                                    global_comm_slipstate = SlipStopped;
129                                    global_comm_ready = 1;
130                            }
131                    case SlipStopped: //vi var ikke klar til at modtage data
132                            global_comm_error = 1;
133            }
134  }  }
135    
136    /*
137  void transmit_interrupt(void)  void transmit_interrupt(void)
138  {  {
139            RB2 = !RB2;
140    }
141    */
142    //Timer1 er en 16 bit timer, der kører med en 1:8 prescaler,
143    // og er styret fra en ekstern 32768 Hz krystal
144    //Når at registrene preloades med 0xEFFF vil det resultere i en
145    //timer overflow ca hvert sekund
146    void timer1_interrupt(void)
147    {
148            RB2 = !RB2;
149            TMR1H = 0xEF;
150            TMR1L = 0xFF;
151  }  }
   
152  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
153  {  {
154          if (RCIF == 1)          if (RCIF == 1)
# Line 52  void interrupt interrupt_handler(void) Line 156  void interrupt interrupt_handler(void)
156                  recieve_interrupt();                  recieve_interrupt();
157                  RCIF = 0;                  RCIF = 0;
158          }          }
159          if (TXIF == 1)          /*if (TXIF == 1)
160          {          {
161                  transmit_interrupt();                  transmit_interrupt();
162                  TXIF = 0;                  TXIF = 0;
163          }          }
164            */
165            
166            if (TMR1IF == 1)
167            {
168                    timer1_interrupt();
169                    TMR1IF = 0;
170            }
171    }
172    
173    ///////////////////////////////////////////////////////////////////
174    // Slip funktioner
175    
176    void slip_reset(void)
177    {
178            global_comm_error = 0;
179            global_comm_slipstate = SlipNormal;
180            global_comm_ready = 0;
181            global_comm_length = 0;
182            memset(global_comm_buffer, 0, BUFFERLEN);
183    }
184    
185    void slip_highlevel_protocol(void)
186    {
187            unsigned char cmd,target,data;
188            
189            cmd = global_comm_buffer[0] & 0x0F;
190            target = ( global_comm_buffer[0] >> 4 ) & 0x0F;
191            data = global_comm_buffer[1];
192    
193            if (cmd == CmdRead || cmd == CmdWrite)
194            {
195                    //sæt standart længde - da længden kun varierer ved læsning af potmeter
196                    if (cmd == CmdRead)
197                            global_comm_length = 2;
198                    else
199                            global_comm_length = 1;
200                            
201                    switch(target)
202                    {
203                            case TLed3:
204                                    if (cmd == CmdRead)
205                                            global_comm_buffer[1] = global_led_0;
206                                    else
207                                            global_led_0 = data;
208                                    break;
209                            case TLed4:
210                                    if(cmd == CmdRead)
211                                            global_comm_buffer[1] = global_led_1;
212                                    else
213                                            global_led_1 = data;
214                                    break;
215                            case TLed5:
216                                    if (cmd == CmdRead)
217                                            global_comm_buffer[1] = global_led_2;
218                                    else
219                                            global_led_2 = data;
220                                    break;
221                            case TSwitch2:
222                                    if (cmd == CmdRead)
223                                            global_comm_buffer[1] = RA4;
224                                    else
225                                            global_comm_error = 1;
226                                    break;
227                            case TSwitch3:
228                                    if (cmd == CmdRead)
229                                            global_comm_buffer[1] = RB0;
230                                    else
231                                            global_comm_error = 1;
232                                    break;
233                            case TPotmeter:
234                                    if (cmd == CmdRead)
235                                    {
236                                            global_comm_buffer[1] = global_potmeter_hi;
237                                            global_comm_buffer[2] = global_potmeter_lo;
238                                            global_comm_length = 3;
239                                    }
240                                    else
241                                            global_comm_error = 1;
242                                    break;
243                            case TTemp:
244                                    if (cmd == CmdRead)
245                                            global_comm_buffer[1] = global_temp;
246                                    else
247                                            global_comm_error = 1;
248                                    break;
249                            case TBaud:
250                                    if (cmd == CmdRead)
251                                            global_comm_error = 1;
252                                    else
253                                    {
254                                            //we can only handle 1200,2400,9600,19200
255                                            if (data == 0 || data == 1 || data == 3 || data == 4)
256                                                    global_reset_baudrate = 1;
257                                            else
258                                                    global_comm_error = 1;
259                                    }
260                                    break;
261                            default:
262                                    global_comm_error = 1;
263                                    
264                    }
265            }
266            else //kommandoen var noget andet end read/write
267            {
268                    global_comm_error = 1;
269            }
270            
271            
272            if (global_comm_error == 1) //we saw an error
273            {
274                    global_comm_buffer[0] = CmdNAck | target<<4;
275                    global_comm_length = 1;
276            }
277            else
278            {
279                    global_comm_buffer[0] = CmdAck | target<<4; //skriv acknowledge feltet
280            }
281    }
282    
283    void slip_send_byte(char data)
284    {
285            TXREG = data;
286            while (TRMT==0) ;
287            DelayUs(20);
288    }
289    
290    void slip_send_response(void)
291    {
292            int i;
293            for (i=0; i< global_comm_length;i++)
294            {
295                    if ( global_comm_buffer[i] == SLIP_ESC)
296                    {
297                            slip_send_byte(SLIP_ESC);
298                            slip_send_byte(SLIP_ESCAPED_ESC);
299                    }
300                    else if (global_comm_buffer[i] == SLIP_END)
301                    {
302                            slip_send_byte(SLIP_ESC);
303                            slip_send_byte(SLIP_ESCAPED_END);
304                    }
305                    else
306                    {
307                            slip_send_byte( global_comm_buffer[i]);
308                    }
309            }
310            
311            slip_send_byte(SLIP_END);
312  }  }
313    
314  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
315  // Alm funktioner  // Gennerelle funktioner
316    
317  void interrupt_init(void)  void interrupt_init(void)
318  {  {
319          TXIE = 1; //Enable AUX TX interrupt - testes med TXIF;          TXIE = 0; //Disable AUX TX interrupt - testes med TXIF;
320          RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;          RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;
321    
322          IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet          IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet
# Line 73  void interrupt_init(void) Line 325  void interrupt_init(void)
325                                    
326          TXIF = 0; //nulstil intterupt flag          TXIF = 0; //nulstil intterupt flag
327          RCIF = 0;          RCIF = 0;
328            
329            TMR1IE = 1; // Timer 1
330    
331          GIE = 1; //Global interrupt enable bit          GIE = 1; //Global interrupt enable bit
332  }  }
# Line 108  void serial_init(void) Line 362  void serial_init(void)
362                                          //      RCSTA2 = 0      No framing error                                          //      RCSTA2 = 0      No framing error
363                                          //      RCSTA1 = 0      Overrun error bit                                          //      RCSTA1 = 0      Overrun error bit
364                                          //      RCSTA0 = 0      9th bit                                         */                                          //      RCSTA0 = 0      9th bit                                         */
365                                            
366          //config completed          //config completed
367          SPEN = 1; //Enable Serial port          SPEN = 1; //Enable Serial port
368  }  }
369    
370    /* basic I/O ports */
371    void io_init(void)
372    {
373            TRISB3 = 0; //LED ports = output
374            TRISB2 = 0;
375            TRISB1 = 0;
376            
377            TRISB0 = 1; //Switch porte = input
378            TRISA0 = 1;
379    }
380    
381    void ad_init(void)
382    {
383            // AD Conversion clock
384            ADCS0 = 0;
385            ADCS1 = 0;
386            ADCS2 = 0;
387    
388            //Select AN0/RA0 for AD source
389            CHS0=0;
390            CHS1=0;
391            CHS2=0;
392            
393            //Only AN0 is selected for AD and with Vdd/Vss as limits
394            PCFG0=0;
395            PCFG1=1;
396            PCFG2=1;
397            PCFG3=1;
398            
399            //Result is right justified
400            ADFM=1;
401            
402            //Fire up for A/D converter module
403            ADON=1;
404    }
405    
406  void i2c_init(void)  void i2c_init(void)
407  {  {
408    // I2C_MODULE hører hjemme i i2c.h
409  #ifdef I2C_MODULE  #ifdef I2C_MODULE
410      SSPMode(MASTER_MODE);      SSPMode(MASTER_MODE);
411      SSPEN = 1;      SSPEN = 1;
# Line 129  void i2c_init(void) Line 418  void i2c_init(void)
418  #endif  #endif
419  }  }
420    
421    void timer_init(void)
422    {
423            TMR1CS = 1; //use external clock
424            
425            T1CKPS1 = 1; //1:8 prescale
426            T1CKPS0 = 1;
427            
428            TMR1H = 0xEF;
429            TMR1L = 0xFF;
430    
431            T1OSCEN = 1; //enable oscillator circuit        
432            RD16 = 0; //normal 8 bit writes
433            TMR1ON = 1;
434    }
435    
436  char ReadTemp(void)  char ReadTemp(void)
437  {  {
# Line 152  void main(void) Line 454  void main(void)
454          lcd_init(0); //init in 4-bit mode          lcd_init(0); //init in 4-bit mode
455          i2c_init();          i2c_init();
456          serial_init(); //9600 8N1          serial_init(); //9600 8N1
457                    ad_init();
458            io_init();
459            timer_init();
460          interrupt_init();          interrupt_init();
461                    
         global_comm_length = 0;  
         global_comm_ready = 0;  
   
462    
463          global_comm_state = SlipNormal;          slip_reset(); //take SLIP FSM into normal state
464            global_comm_currentrate = Baud9600; //default baudrate = 9600
465            global_reset_baudrate = 0;
466                    
467  //      initialisation completed and we are ready to recieve commands - enable serial reception  //      initialisation completed and we are ready to recieve commands - enable serial reception
468          CREN = 1;          CREN = 1;
469                    
470          while (1)          while (1)
471          {          {
472                    if (global_comm_ready == 1)
473                    {
474                            slip_highlevel_protocol(); //parse the slip frame recieved & generate response
475                            slip_send_response();
476                            slip_reset();
477                    }
478                    
479                    if ( global_reset_baudrate == 1)
480                    {
481                    }
482                    
483                    RB1 = global_led_0;
484            //      RB2 = global_led_1;
485                    RB3 = global_led_2;
486                    
487                    global_temp = ReadTemp();
488                    
489                    //potmeter
490                    if (GODONE ==0 )//hvis A/D-conv er færdig
491                    {
492                            
493                            global_potmeter_hi = ADRESH;//gem AD resultat
494                            global_potmeter_lo = ADRESL;
495                            GODONE = 1; //start ny konverering
496                    }
497          }          }
498  }  }
499    

Legend:
Removed from v.10  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.20