/[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 16 by torben, Mon Jan 29 15:29:28 2007 UTC revision 17 by torben, Tue Jan 30 14:56:43 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;  unsigned char global_comm_index;
66    
67  bit global_comm_ready;  bit global_comm_ready;
68  bit global_comm_recieving;  bit global_comm_error;
69    
70    unsigned char global_comm_slipstate;
71    unsigned char global_comm_currentrate;
72    
73    unsigned char global_lcd_buffer[20];
74    unsigned char global_lcd_index;
75    
 unsigned char global_comm_state;  
76    
77    bit global_led_0;
78    bit global_led_1;
79    bit global_led_2;
80    
81    bit global_reset_baudrate;
82    
83    char global_temp;
84    unsigned char global_potmeter_hi;
85    unsigned char global_potmeter_lo;
86    
87  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
88  // Interrupt funktioner  // Interrupt funktioner
89    
90  void recieve_interrupt(void)  void recieve_interrupt(void)
91  {  {
92            unsigned char data = RCREG;    
93            RB2 = !RB2;
94            
95            switch (global_comm_slipstate)
96            {              
97                    case SlipNormal:
98                            if (data == SLIP_END)
99                            {
100                                    global_comm_ready = 1;
101                                    global_comm_slipstate = SlipStopped;
102                            }
103                            else if (data == SLIP_ESC)
104                            {
105                                    global_comm_slipstate = SlipEscaped;
106                            }
107                            else
108                                    global_comm_buffer[ global_comm_length++ ] = data;
109                            break;
110                            
111                    case SlipEscaped:
112                            if (data == SLIP_ESCAPED_ESC)
113                            {
114                                    global_comm_buffer[ global_comm_length++ ] = SLIP_ESC;
115                                    global_comm_slipstate = SlipNormal;
116                            }
117                            else if (data ==SLIP_ESCAPED_END)
118                            {
119                                    global_comm_buffer[ global_comm_length++ ] = SLIP_END;
120                                    global_comm_slipstate = SlipNormal;
121                            }
122                            else
123                            {
124                                    global_comm_slipstate = SlipError;
125                            }
126                    case SlipError:
127                            if (data == SLIP_END)
128                            {
129                                    global_comm_error = 1;
130                                    global_comm_slipstate = SlipStopped;
131                                    global_comm_ready = 1;
132                            }
133                    case SlipStopped: //vi var ikke klar til at modtage data
134                            global_comm_error = 1;
135            }
136  }  }
137    
138    /*
139  void transmit_interrupt(void)  void transmit_interrupt(void)
140  {  {
141            RB2 = !RB2;
142  }  }
143    */
144    
145  void interrupt interrupt_handler(void)  void interrupt interrupt_handler(void)
146  {  {
# Line 52  void interrupt interrupt_handler(void) Line 149  void interrupt interrupt_handler(void)
149                  recieve_interrupt();                  recieve_interrupt();
150                  RCIF = 0;                  RCIF = 0;
151          }          }
152          if (TXIF == 1)          /*if (TXIF == 1)
153          {          {
154                  transmit_interrupt();                  transmit_interrupt();
155                  TXIF = 0;                  TXIF = 0;
156          }          }
157            */
158  }  }
159    
160  ///////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////
161  // Alm funktioner  // Slip funktioner
162    
163    void slip_reset(void)
164    {
165            global_comm_error = 0;
166            global_comm_slipstate = SlipNormal;
167            global_comm_ready = 0;
168            global_comm_length = 0;
169            memset(global_comm_buffer, 0, BUFFERLEN);
170    }
171    
172    void slip_highlevel_protocol(void)
173    {
174            unsigned char cmd,target,data;
175            
176            cmd = global_comm_buffer[0] & 0x0F;
177            target = ( global_comm_buffer[0] >> 4 ) & 0x0F;
178            data = global_comm_buffer[1];
179    
180            if (cmd == CmdRead || cmd == CmdWrite)
181            {
182                    //sæt standart længde - da længden kun varierer ved læsning af potmeter
183                    if (cmd == CmdRead)
184                            global_comm_length = 2;
185                    else
186                            global_comm_length = 1;
187                            
188                    switch(target)
189                    {
190                            case TLed3:
191                                    if (cmd == CmdRead)
192                                            global_comm_buffer[1] = global_led_0;
193                                    else
194                                            global_led_0 = data;
195                                    break;
196                            case TLed4:
197                                    if(cmd == CmdRead)
198                                            global_comm_buffer[1] = global_led_1;
199                                    else
200                                            global_led_1 = data;
201                                    break;
202                            case TLed5:
203                                    if (cmd == CmdRead)
204                                            global_comm_buffer[1] = global_led_2;
205                                    else
206                                            global_led_2 = data;
207                                    break;
208                            case TSwitch2:
209                                    if (cmd == CmdRead)
210                                            global_comm_buffer[1] = RA4;
211                                    else
212                                            global_comm_error = 1;
213                                    break;
214                            case TSwitch3:
215                                    if (cmd == CmdRead)
216                                            global_comm_buffer[1] = RB0;
217                                    else
218                                            global_comm_error = 1;
219                                    break;
220                            case TPotmeter:
221                                    if (cmd == CmdRead)
222                                    {
223                                            global_comm_buffer[1] = global_potmeter_hi;
224                                            global_comm_buffer[2] = global_potmeter_lo;
225                                            global_comm_length = 3;
226                                    }
227                                    else
228                                            global_comm_error = 1;
229                                    break;
230                            case TTemp:
231                                    if (cmd == CmdRead)
232                                            global_comm_buffer[1] = global_temp;
233                                    else
234                                            global_comm_error = 1;
235                                    break;
236                            case TBaud:
237                                    if (cmd == CmdRead)
238                                            global_comm_error = 1;
239                                    else
240                                    {
241                                            //we can only handle 1200,2400,9600,19200
242                                            if (data == 0 || data == 1 || data == 3 || data == 4)
243                                                    global_reset_baudrate = 1;
244                                            else
245                                                    global_comm_error = 1;
246                                    }
247                                    break;
248                            default:
249                                    global_comm_error = 1;
250                                    
251                    }
252            }
253            else //kommandoen var noget andet end read/write
254            {
255                    global_comm_error = 1;
256            }
257            
258            
259            if (global_comm_error == 1) //we saw an error
260            {
261                    global_comm_buffer[0] = CmdNAck | target<<4;
262                    global_comm_length = 1;
263            }
264            else
265            {
266                    global_comm_buffer[0] = CmdAck | target<<4; //skriv acknowledge feltet
267            }
268    }
269    
270    void slip_send_byte(char data)
271    {
272            TXREG = data;
273            while (TRMT==0) ;
274            DelayUs(20);
275    }
276    
277    void slip_send_response(void)
278    {
279            int i;
280            for (i=0; i< global_comm_length;i++)
281            {
282                    if ( global_comm_buffer[i] == SLIP_ESC)
283                    {
284                            slip_send_byte(SLIP_ESC);
285                            slip_send_byte(SLIP_ESCAPED_ESC);
286                    }
287                    else if (global_comm_buffer[i] == SLIP_END)
288                    {
289                            slip_send_byte(SLIP_ESC);
290                            slip_send_byte(SLIP_ESCAPED_END);
291                    }
292                    else
293                    {
294                            slip_send_byte( global_comm_buffer[i]);
295                    }
296            }
297            
298            slip_send_byte(SLIP_END);
299    }
300    
301    ///////////////////////////////////////////////////////////////////
302    // Gennerelle funktioner
303    
304  void interrupt_init(void)  void interrupt_init(void)
305  {  {
306          TXIE = 1; //Enable AUX TX interrupt - testes med TXIF;          TXIE = 0; //Disable AUX TX interrupt - testes med TXIF;
307          RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;          RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;
308    
309          IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet          IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet
# Line 108  void serial_init(void) Line 347  void serial_init(void)
347                                          //      RCSTA2 = 0      No framing error                                          //      RCSTA2 = 0      No framing error
348                                          //      RCSTA1 = 0      Overrun error bit                                          //      RCSTA1 = 0      Overrun error bit
349                                          //      RCSTA0 = 0      9th bit                                         */                                          //      RCSTA0 = 0      9th bit                                         */
350                                            
351          //config completed          //config completed
352          SPEN = 1; //Enable Serial port          SPEN = 1; //Enable Serial port
353  }  }
354    
355    /* basic I/O ports */
356    void io_init(void)
357    {
358            TRISB3 = 0; //LED ports = output
359            TRISB2 = 0;
360            TRISB1 = 0;
361            
362            TRISB0 = 1; //Switch porte = input
363            TRISA0 = 1;
364    }
365    
366    void ad_init(void)
367    {
368            // AD Conversion clock
369            ADCS0 = 0;
370            ADCS1 = 0;
371            ADCS2 = 0;
372    
373            //Select AN0/RA0 for AD source
374            CHS0=0;
375            CHS1=0;
376            CHS2=0;
377            
378            //Only AN0 is selected for AD and with Vdd/Vss as limits
379            PCFG0=0;
380            PCFG1=1;
381            PCFG2=1;
382            PCFG3=1;
383            
384            //Result is right justified
385            ADFM=1;
386            
387            //Fire up for A/D converter module
388            ADON=1;
389    }
390    
391  void i2c_init(void)  void i2c_init(void)
392  {  {
393    // I2C_MODULE hører hjemme i i2c.h
394  #ifdef I2C_MODULE  #ifdef I2C_MODULE
395      SSPMode(MASTER_MODE);      SSPMode(MASTER_MODE);
396      SSPEN = 1;      SSPEN = 1;
# Line 149  char ReadTemp(void) Line 423  char ReadTemp(void)
423    
424  void main(void)  void main(void)
425  {  {
426            char lcdbuf[20];
427            int i;
428            
429            
430          lcd_init(0); //init in 4-bit mode          lcd_init(0); //init in 4-bit mode
431          i2c_init();          i2c_init();
432          serial_init(); //9600 8N1          serial_init(); //9600 8N1
433                    ad_init();
434            io_init();
435          interrupt_init();          interrupt_init();
436                    
         global_comm_length = 0;  
         global_comm_ready = 0;  
   
437    
438          global_comm_state = SlipNormal;          slip_reset(); //take SLIP FSM into normal state
439            global_comm_currentrate = Baud9600; //default baudrate = 9600
440            global_reset_baudrate = 0;
441                    
442  //      initialisation completed and we are ready to recieve commands - enable serial reception  //      initialisation completed and we are ready to recieve commands - enable serial reception
443          CREN = 1;          CREN = 1;
444                    
445          while (1)          while (1)
446          {          {
447                    if (global_comm_ready == 1)
448                    {
449                            slip_highlevel_protocol(); //parse the slip frame recieved & generate response
450                            slip_send_response();
451                            slip_reset();
452                    }
453                    
454                    if ( global_reset_baudrate == 1)
455                    {
456                    }
457                    
458                    RB1 = global_led_0;
459                    RB2 = global_led_1;
460                    RB3 = global_led_2;
461                    
462                    global_temp = ReadTemp();
463                    
464                    //potmeter
465                    if (GODONE ==0 )//hvis A/D-conv er færdig
466                    {
467                            
468                            global_potmeter_hi = ADRESH;//gem AD resultat
469                            global_potmeter_lo = ADRESL;
470                            GODONE = 1; //start ny konverering
471                    }
472                    
473                    
474                    //just for fun - blink RB3
475    //              RB3 = !RB3;
476    //              DelayMs(250);
477          }          }
478  }  }
479    

Legend:
Removed from v.16  
changed lines
  Added in v.17

  ViewVC Help
Powered by ViewVC 1.1.20