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

Contents of /trunk/PIC/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download)
Tue Jan 30 14:56:43 2007 UTC (17 years, 3 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 /* H7 -projekt opgave */
2
3 ///////////////////////////////////////////////////////////////////////
4 //Includes
5 #include <pic18.h>
6 #include <string.h>
7
8 #include "delay.h"
9 #include "i2c.h"
10 #include "lcd.h"
11
12 /////////////////////////////////////////////////////////////////////
13 //Defines
14 #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 unsigned char global_comm_index;
66
67 bit global_comm_ready;
68 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
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
89
90 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)
140 {
141 RB2 = !RB2;
142 }
143 */
144
145 void interrupt interrupt_handler(void)
146 {
147 if (RCIF == 1)
148 {
149 recieve_interrupt();
150 RCIF = 0;
151 }
152 /*if (TXIF == 1)
153 {
154 transmit_interrupt();
155 TXIF = 0;
156 }
157 */
158 }
159
160 ///////////////////////////////////////////////////////////////////
161 // 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)
305 {
306 TXIE = 0; //Disable AUX TX interrupt - testes med TXIF;
307 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
351 //config completed
352 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)
392 {
393 // I2C_MODULE hører hjemme i i2c.h
394 #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
407
408 char ReadTemp(void)
409 {
410 char temp;
411 i2c_WriteTo(TC74); //write to, will automatically send a start condition
412 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 }
418
419
420 ///////////////////////////////////////////////////////////////////
421 //Main
422
423
424 void main(void)
425 {
426 char lcdbuf[20];
427 int i;
428
429
430 lcd_init(0); //init in 4-bit mode
431 i2c_init();
432 serial_init(); //9600 8N1
433 ad_init();
434 io_init();
435 interrupt_init();
436
437
438 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
443 CREN = 1;
444
445 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 }

  ViewVC Help
Powered by ViewVC 1.1.20