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

Annotation of /trunk/PIC/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20