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