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

Annotation of /trunk/PIC/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (hide annotations) (download)
Tue Jan 30 14:56:43 2007 UTC (17 years, 4 months ago) by torben
File MIME type: text/plain
File size: 9653 byte(s)
Implementet SLIP protocol send + recieve
Also made high_level protocol handlers
1 torben 2 /* H7 -projekt opgave */
2 torben 10
3     ///////////////////////////////////////////////////////////////////////
4     //Includes
5 torben 2 #include <pic18.h>
6 torben 17 #include <string.h>
7    
8 torben 2 #include "delay.h"
9     #include "i2c.h"
10     #include "lcd.h"
11    
12 torben 10 /////////////////////////////////////////////////////////////////////
13     //Defines
14 torben 2 #define TC74 0x9A /* I2C TC74 IC */
15    
16 torben 17 #define SLIP_END 192
17     #define SLIP_ESC 219
18     #define SLIP_ESCAPED_END 220
19     #define SLIP_ESCAPED_ESC 221
20 torben 10
21 torben 17 #define BUFFERLEN 16
22    
23     //////////////////////////////////////////////////////////////////
24     // Enumerations
25    
26 torben 10 enum SlipState{
27     SlipNormal,
28     SlipEscaped,
29     SlipError,
30     SlipStopped
31     };
32    
33 torben 17 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 torben 10 //////////////////////////////////////////////////////////////////
60     //Globale data
61     // Alle globale variabler bruger "global_" som prefix
62 torben 17
63     unsigned char global_comm_buffer[BUFFERLEN];
64 torben 10 unsigned char global_comm_length;
65     unsigned char global_comm_index;
66 torben 17
67 torben 10 bit global_comm_ready;
68 torben 17 bit global_comm_error;
69 torben 10
70 torben 17 unsigned char global_comm_slipstate;
71     unsigned char global_comm_currentrate;
72 torben 10
73 torben 17 unsigned char global_lcd_buffer[20];
74     unsigned char global_lcd_index;
75 torben 10
76    
77 torben 17 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 torben 10 ///////////////////////////////////////////////////////////////////
88     // Interrupt funktioner
89    
90     void recieve_interrupt(void)
91     {
92 torben 17 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 torben 10 }
137    
138 torben 17 /*
139 torben 10 void transmit_interrupt(void)
140     {
141 torben 17 RB2 = !RB2;
142 torben 10 }
143 torben 17 */
144 torben 10
145     void interrupt interrupt_handler(void)
146     {
147     if (RCIF == 1)
148     {
149     recieve_interrupt();
150     RCIF = 0;
151     }
152 torben 17 /*if (TXIF == 1)
153 torben 10 {
154     transmit_interrupt();
155     TXIF = 0;
156     }
157 torben 17 */
158 torben 10 }
159    
160     ///////////////////////////////////////////////////////////////////
161 torben 17 // Slip funktioner
162 torben 10
163 torben 17 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 torben 10 void interrupt_init(void)
305     {
306 torben 17 TXIE = 0; //Disable AUX TX interrupt - testes med TXIF;
307 torben 10 RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;
308    
309     IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet
310    
311     PEIE = 1; // PEIE = PEriphal Interrupt Enable
312    
313     TXIF = 0; //nulstil intterupt flag
314     RCIF = 0;
315    
316     GIE = 1; //Global interrupt enable bit
317     }
318    
319     void serial_init(void)
320     {
321     SPEN = 0; //Make sure serial port is disabled
322    
323     // snupset fra Kim H Pedersens serialmain.c
324     // Set Port C bit 6 output (TxD) and bit 7 input (RxD)
325     TRISC6 = 0;
326     TRISC7 = 1;
327    
328     SPBRG = 25; // x = Fosc/(16 * Baud Rate) - 1
329     // x = 4000000/(16 * 9600) - 1
330     // x = 25.0417 ~ 25
331     // Baud Rate = Fosc/(16 * (x+1))
332     // Baud Rate = 4000000/(16 * (25+1))
333     // Baud Rate = 9615
334     TXSTA = 0x24; // TXSTA7 = 0 Don't care in asynchronous
335     // TXSTA6 = 0 8-bit transmission
336     // TXSTA5 = 1 Transmit enabled
337     // TXSTA4 = 0 Asynchronous mode
338     // TXSTA3 = 0 Unimplemented
339     // TXSTA2 = 1 High speed
340     // TXSTA1 = 0 TSR empty
341     // TXSTA0 = 0 9th bit */
342     RCSTA = 0x90; // RCSTA7 = 0 Serial port disabled
343     // RCSTA6 = 0 8-bit reception
344     // RCSTA5 = 0 Don't care in asynchronous
345     // RCSTA4 = 1 Receiver enabled
346     // RCSTA3 = 0 Don't care (8-bit operation)
347     // RCSTA2 = 0 No framing error
348     // RCSTA1 = 0 Overrun error bit
349     // RCSTA0 = 0 9th bit */
350 torben 17
351 torben 10 //config completed
352     SPEN = 1; //Enable Serial port
353     }
354    
355 torben 17 /* 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 torben 10
366 torben 17 void ad_init(void)
367     {
368     // AD Conversion clock
369     ADCS0 = 0;
370     ADCS1 = 0;
371     ADCS2 = 0;
372 torben 10
373 torben 17 //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 torben 2 void i2c_init(void)
392     {
393 torben 17 // I2C_MODULE hører hjemme i i2c.h
394 torben 2 #ifdef I2C_MODULE
395     SSPMode(MASTER_MODE);
396     SSPEN = 1;
397     CKP = 1;
398     #else
399     SCL_DIR = I2C_OUTPUT;
400     SDA_DIR = I2C_OUTPUT;
401     SDA = 0;
402     SCL = 0;
403     #endif
404     }
405    
406 torben 10
407    
408 torben 2 char ReadTemp(void)
409     {
410     char temp;
411 torben 10 i2c_WriteTo(TC74); //write to, will automatically send a start condition
412 torben 2 i2c_PutByte(0x00); //tell TC74 we want to read
413     i2c_ReadFrom(TC74);
414     temp = i2c_GetByte(I2C_LAST);
415     i2c_Stop(); //assert a stop condition on SDA & SCL
416     return temp;
417 torben 10 }
418    
419    
420     ///////////////////////////////////////////////////////////////////
421     //Main
422 torben 2
423 torben 10
424 torben 2 void main(void)
425     {
426 torben 17 char lcdbuf[20];
427     int i;
428    
429    
430 torben 2 lcd_init(0); //init in 4-bit mode
431     i2c_init();
432 torben 10 serial_init(); //9600 8N1
433 torben 17 ad_init();
434     io_init();
435 torben 10 interrupt_init();
436    
437    
438 torben 17 slip_reset(); //take SLIP FSM into normal state
439     global_comm_currentrate = Baud9600; //default baudrate = 9600
440     global_reset_baudrate = 0;
441 torben 10
442     // initialisation completed and we are ready to recieve commands - enable serial reception
443     CREN = 1;
444    
445 torben 2 while (1)
446     {
447 torben 17 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 torben 2 }
478     }

  ViewVC Help
Powered by ViewVC 1.1.20