/[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 2 by torben, Mon Jan 29 09:45:25 2007 UTC revision 20 by torben, Tue Jan 30 22:16:53 2007 UTC
# Line 1  Line 1 
1  /* H7 -projekt opgave */  /* H7 -projekt opgave */
2    
3    ///////////////////////////////////////////////////////////////////////
4    //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"
11    
12    /////////////////////////////////////////////////////////////////////
13    //Defines
14  #define TC74 0x9A /* I2C TC74 IC */  #define TC74 0x9A /* I2C TC74 IC */
15    
16    #define SLIP_END 192
17    #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{
27            SlipNormal,
28            SlipEscaped,
29            SlipError,
30            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
61    // Alle globale variabler bruger "global_" som prefix
62    
63    unsigned char global_comm_buffer[BUFFERLEN];
64    unsigned char global_comm_length;
65    
66    bit global_comm_ready;
67    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    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
88    
89    void recieve_interrupt(void)
90    {
91            unsigned char data = RCREG;    
92            RB2 = !RB2;
93            
94            switch (global_comm_slipstate)
95            {              
96                    case SlipNormal:
97                            if (data == SLIP_END)
98                            {
99                                    global_comm_ready = 1;
100                                    global_comm_slipstate = SlipStopped;
101                            }
102                            else if (data == SLIP_ESC)
103                            {
104                                    global_comm_slipstate = SlipEscaped;
105                            }
106                            else
107                                    global_comm_buffer[ global_comm_length++ ] = data;
108                            break;
109                            
110                    case SlipEscaped:
111                            if (data == SLIP_ESCAPED_ESC)
112                            {
113                                    global_comm_buffer[ global_comm_length++ ] = SLIP_ESC;
114                                    global_comm_slipstate = SlipNormal;
115                            }
116                            else if (data ==SLIP_ESCAPED_END)
117                            {
118                                    global_comm_buffer[ global_comm_length++ ] = SLIP_END;
119                                    global_comm_slipstate = SlipNormal;
120                            }
121                            else
122                            {
123                                    global_comm_slipstate = SlipError;
124                            }
125                    case SlipError:
126                            if (data == SLIP_END)
127                            {
128                                    global_comm_error = 1;
129                                    global_comm_slipstate = SlipStopped;
130                                    global_comm_ready = 1;
131                            }
132                    case SlipStopped: //vi var ikke klar til at modtage data
133                            global_comm_error = 1;
134            }
135    }
136    
137    /*
138    void transmit_interrupt(void)
139    {
140            RB2 = !RB2;
141    }
142    */
143    
144    void interrupt interrupt_handler(void)
145    {
146            if (RCIF == 1)
147            {
148                    recieve_interrupt();
149                    RCIF = 0;
150            }
151            /*if (TXIF == 1)
152            {
153                    transmit_interrupt();
154                    TXIF = 0;
155            }
156            */
157    }
158    
159    ///////////////////////////////////////////////////////////////////
160    // Slip funktioner
161    
162    void slip_reset(void)
163    {
164            global_comm_error = 0;
165            global_comm_slipstate = SlipNormal;
166            global_comm_ready = 0;
167            global_comm_length = 0;
168            memset(global_comm_buffer, 0, BUFFERLEN);
169    }
170    
171    void slip_highlevel_protocol(void)
172    {
173            unsigned char cmd,target,data;
174            
175            cmd = global_comm_buffer[0] & 0x0F;
176            target = ( global_comm_buffer[0] >> 4 ) & 0x0F;
177            data = global_comm_buffer[1];
178    
179            if (cmd == CmdRead || cmd == CmdWrite)
180            {
181                    //sæt standart længde - da længden kun varierer ved læsning af potmeter
182                    if (cmd == CmdRead)
183                            global_comm_length = 2;
184                    else
185                            global_comm_length = 1;
186                            
187                    switch(target)
188                    {
189                            case TLed3:
190                                    if (cmd == CmdRead)
191                                            global_comm_buffer[1] = global_led_0;
192                                    else
193                                            global_led_0 = data;
194                                    break;
195                            case TLed4:
196                                    if(cmd == CmdRead)
197                                            global_comm_buffer[1] = global_led_1;
198                                    else
199                                            global_led_1 = data;
200                                    break;
201                            case TLed5:
202                                    if (cmd == CmdRead)
203                                            global_comm_buffer[1] = global_led_2;
204                                    else
205                                            global_led_2 = data;
206                                    break;
207                            case TSwitch2:
208                                    if (cmd == CmdRead)
209                                            global_comm_buffer[1] = RA4;
210                                    else
211                                            global_comm_error = 1;
212                                    break;
213                            case TSwitch3:
214                                    if (cmd == CmdRead)
215                                            global_comm_buffer[1] = RB0;
216                                    else
217                                            global_comm_error = 1;
218                                    break;
219                            case TPotmeter:
220                                    if (cmd == CmdRead)
221                                    {
222                                            global_comm_buffer[1] = global_potmeter_hi;
223                                            global_comm_buffer[2] = global_potmeter_lo;
224                                            global_comm_length = 3;
225                                    }
226                                    else
227                                            global_comm_error = 1;
228                                    break;
229                            case TTemp:
230                                    if (cmd == CmdRead)
231                                            global_comm_buffer[1] = global_temp;
232                                    else
233                                            global_comm_error = 1;
234                                    break;
235                            case TBaud:
236                                    if (cmd == CmdRead)
237                                            global_comm_error = 1;
238                                    else
239                                    {
240                                            //we can only handle 1200,2400,9600,19200
241                                            if (data == 0 || data == 1 || data == 3 || data == 4)
242                                                    global_reset_baudrate = 1;
243                                            else
244                                                    global_comm_error = 1;
245                                    }
246                                    break;
247                            default:
248                                    global_comm_error = 1;
249                                    
250                    }
251            }
252            else //kommandoen var noget andet end read/write
253            {
254                    global_comm_error = 1;
255            }
256            
257            
258            if (global_comm_error == 1) //we saw an error
259            {
260                    global_comm_buffer[0] = CmdNAck | target<<4;
261                    global_comm_length = 1;
262            }
263            else
264            {
265                    global_comm_buffer[0] = CmdAck | target<<4; //skriv acknowledge feltet
266            }
267    }
268    
269    void slip_send_byte(char data)
270    {
271            TXREG = data;
272            while (TRMT==0) ;
273            DelayUs(20);
274    }
275    
276    void slip_send_response(void)
277    {
278            int i;
279            for (i=0; i< global_comm_length;i++)
280            {
281                    if ( global_comm_buffer[i] == SLIP_ESC)
282                    {
283                            slip_send_byte(SLIP_ESC);
284                            slip_send_byte(SLIP_ESCAPED_ESC);
285                    }
286                    else if (global_comm_buffer[i] == SLIP_END)
287                    {
288                            slip_send_byte(SLIP_ESC);
289                            slip_send_byte(SLIP_ESCAPED_END);
290                    }
291                    else
292                    {
293                            slip_send_byte( global_comm_buffer[i]);
294                    }
295            }
296            
297            slip_send_byte(SLIP_END);
298    }
299    
300    ///////////////////////////////////////////////////////////////////
301    // Gennerelle funktioner
302    
303    void interrupt_init(void)
304    {
305            TXIE = 0; //Disable AUX TX interrupt - testes med TXIF;
306            RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;
307    
308            IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet
309            
310            PEIE = 1; // PEIE = PEriphal Interrupt Enable
311                    
312            TXIF = 0; //nulstil intterupt flag
313            RCIF = 0;
314    
315            GIE = 1; //Global interrupt enable bit
316    }
317    
318    void serial_init(void)
319    {
320            SPEN = 0; //Make sure serial port is disabled
321            
322            // snupset fra  Kim H Pedersens serialmain.c
323            // Set Port C bit 6 output (TxD) and bit 7 input (RxD)
324        TRISC6 = 0;
325        TRISC7 = 1;
326    
327        SPBRG = 25;         //      x = Fosc/(16 * Baud Rate) - 1
328                                            //      x = 4000000/(16 * 9600) - 1
329                                            //      x = 25.0417 ~ 25
330                                            //      Baud Rate = Fosc/(16 * (x+1))
331                                            //      Baud Rate = 4000000/(16 * (25+1))
332                                            //      Baud Rate = 9615
333        TXSTA = 0x24;       //      TXSTA7 = 0      Don't care in asynchronous
334                                            //      TXSTA6 = 0      8-bit transmission
335                                            //      TXSTA5 = 1      Transmit enabled
336                                            //      TXSTA4 = 0      Asynchronous mode
337                                            //      TXSTA3 = 0      Unimplemented
338                                            //      TXSTA2 = 1      High speed
339                                            //      TXSTA1 = 0      TSR empty
340                                            //      TXSTA0 = 0      9th bit                                         */
341        RCSTA = 0x90;       //      RCSTA7 = 0      Serial port disabled
342                                            //      RCSTA6 = 0      8-bit reception
343                                            //      RCSTA5 = 0      Don't care in asynchronous
344                                            //      RCSTA4 = 1      Receiver enabled
345                                            //      RCSTA3 = 0      Don't care (8-bit operation)
346                                            //      RCSTA2 = 0      No framing error
347                                            //      RCSTA1 = 0      Overrun error bit
348                                            //      RCSTA0 = 0      9th bit                                         */
349    
350            //config completed
351            SPEN = 1; //Enable Serial port
352    }
353    
354    /* basic I/O ports */
355    void io_init(void)
356    {
357            TRISB3 = 0; //LED ports = output
358            TRISB2 = 0;
359            TRISB1 = 0;
360            
361            TRISB0 = 1; //Switch porte = input
362            TRISA0 = 1;
363    }
364    
365    void ad_init(void)
366    {
367            // AD Conversion clock
368            ADCS0 = 0;
369            ADCS1 = 0;
370            ADCS2 = 0;
371    
372            //Select AN0/RA0 for AD source
373            CHS0=0;
374            CHS1=0;
375            CHS2=0;
376            
377            //Only AN0 is selected for AD and with Vdd/Vss as limits
378            PCFG0=0;
379            PCFG1=1;
380            PCFG2=1;
381            PCFG3=1;
382            
383            //Result is right justified
384            ADFM=1;
385            
386            //Fire up for A/D converter module
387            ADON=1;
388    }
389    
390  void i2c_init(void)  void i2c_init(void)
391  {  {
392    // I2C_MODULE hører hjemme i i2c.h
393  #ifdef I2C_MODULE  #ifdef I2C_MODULE
394      SSPMode(MASTER_MODE);      SSPMode(MASTER_MODE);
395      SSPEN = 1;      SSPEN = 1;
# Line 22  void i2c_init(void) Line 402  void i2c_init(void)
402  #endif  #endif
403  }  }
404    
405    
406    
407  char ReadTemp(void)  char ReadTemp(void)
408  {  {
409          char temp;          char temp;
410          i2c_WriteTo(TC74);          i2c_WriteTo(TC74); //write to, will automatically send a start condition
411          i2c_PutByte(0x00); //tell TC74 we want to read          i2c_PutByte(0x00); //tell TC74 we want to read
412          i2c_ReadFrom(TC74);          i2c_ReadFrom(TC74);
413          temp = i2c_GetByte(I2C_LAST);          temp = i2c_GetByte(I2C_LAST);
414          i2c_Stop(); //assert a stop condition on SDA & SCL          i2c_Stop(); //assert a stop condition on SDA & SCL
415          return temp;          return temp;
416  }  }
417    
418    
419    ///////////////////////////////////////////////////////////////////
420    //Main
421    
422    
423  void main(void)  void main(void)
424  {  {
425          lcd_init(0); //init in 4-bit mode          lcd_init(0); //init in 4-bit mode
426          i2c_init();          i2c_init();
427            serial_init(); //9600 8N1
428            ad_init();
429            io_init();
430            interrupt_init();
431            
432    
433            slip_reset(); //take SLIP FSM into normal state
434            global_comm_currentrate = Baud9600; //default baudrate = 9600
435            global_reset_baudrate = 0;
436            
437    //      initialisation completed and we are ready to recieve commands - enable serial reception
438            CREN = 1;
439                    
440          while (1)          while (1)
441          {          {
442                    if (global_comm_ready == 1)
443                    {
444                            slip_highlevel_protocol(); //parse the slip frame recieved & generate response
445                            slip_send_response();
446                            slip_reset();
447                    }
448                    
449                    if ( global_reset_baudrate == 1)
450                    {
451                    }
452                    
453                    RB1 = global_led_0;
454                    RB2 = global_led_1;
455                    RB3 = global_led_2;
456                    
457                    global_temp = ReadTemp();
458                    
459                    //potmeter
460                    if (GODONE ==0 )//hvis A/D-conv er færdig
461                    {
462                            
463                            global_potmeter_hi = ADRESH;//gem AD resultat
464                            global_potmeter_lo = ADRESL;
465                            GODONE = 1; //start ny konverering
466                    }
467          }          }
468  }  }
469    

Legend:
Removed from v.2  
changed lines
  Added in v.20

  ViewVC Help
Powered by ViewVC 1.1.20