4 |
//Includes |
//Includes |
5 |
#include <pic18.h> |
#include <pic18.h> |
6 |
#include <string.h> |
#include <string.h> |
7 |
|
#include <stdio.h> |
8 |
|
|
9 |
#include "delay.h" |
#include "delay.h" |
10 |
#include "i2c.h" |
#include "i2c.h" |
68 |
bit global_comm_error; |
bit global_comm_error; |
69 |
|
|
70 |
unsigned char global_comm_slipstate; |
unsigned char global_comm_slipstate; |
71 |
unsigned char global_comm_currentrate; |
unsigned char global_comm_baudrate; |
72 |
|
|
73 |
unsigned char global_lcd_buffer[20]; |
unsigned char global_lcd_buffer[2][BUFFERLEN]; |
|
unsigned char global_lcd_index; |
|
74 |
|
|
75 |
|
|
76 |
bit global_led_0; |
bit global_led_0; |
90 |
{ |
{ |
91 |
unsigned char data = RCREG; |
unsigned char data = RCREG; |
92 |
|
|
93 |
|
if (global_comm_length == BUFFERLEN) //avoid buffer overrun |
94 |
|
global_comm_slipstate == SlipError; |
95 |
|
|
96 |
switch (global_comm_slipstate) |
switch (global_comm_slipstate) |
97 |
{ |
{ |
98 |
case SlipNormal: |
case SlipNormal: |
148 |
//timer overflow ca hvert sekund |
//timer overflow ca hvert sekund |
149 |
void timer1_interrupt(void) |
void timer1_interrupt(void) |
150 |
{ |
{ |
|
RB2 = !RB2; |
|
151 |
TMR1H = 0xEF; |
TMR1H = 0xEF; |
152 |
TMR1L = 0xFF; |
TMR1L = 0xFF; |
153 |
} |
} |
175 |
/////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////// |
176 |
// Slip funktioner |
// Slip funktioner |
177 |
|
|
178 |
|
|
179 |
void slip_reset(void) |
void slip_reset(void) |
180 |
{ |
{ |
181 |
global_comm_error = 0; |
global_comm_error = 0; |
223 |
break; |
break; |
224 |
case TSwitch2: |
case TSwitch2: |
225 |
if (cmd == CmdRead) |
if (cmd == CmdRead) |
226 |
global_comm_buffer[1] = RA4; |
global_comm_buffer[1] = !RA4; |
227 |
else |
else |
228 |
global_comm_error = 1; |
global_comm_error = 1; |
229 |
break; |
break; |
230 |
case TSwitch3: |
case TSwitch3: |
231 |
if (cmd == CmdRead) |
if (cmd == CmdRead) |
232 |
global_comm_buffer[1] = RB0; |
global_comm_buffer[1] = !RB0; |
233 |
else |
else |
234 |
global_comm_error = 1; |
global_comm_error = 1; |
235 |
break; |
break; |
256 |
{ |
{ |
257 |
//we can only handle 1200,2400,9600,19200 |
//we can only handle 1200,2400,9600,19200 |
258 |
if (data == 0 || data == 1 || data == 3 || data == 4) |
if (data == 0 || data == 1 || data == 3 || data == 4) |
259 |
|
{ |
260 |
global_reset_baudrate = 1; |
global_reset_baudrate = 1; |
261 |
|
global_comm_baudrate = data; |
262 |
|
} |
263 |
else |
else |
264 |
global_comm_error = 1; |
global_comm_error = 1; |
265 |
} |
} |
318 |
} |
} |
319 |
|
|
320 |
/////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////// |
321 |
// Gennerelle funktioner |
// init funktioner |
322 |
|
|
323 |
void interrupt_init(void) |
void interrupt_init(void) |
324 |
{ |
{ |
439 |
TMR1ON = 1; |
TMR1ON = 1; |
440 |
} |
} |
441 |
|
|
442 |
|
/////////////////////////////////////////////////////////////////// |
443 |
|
// Gennerelle funktioner |
444 |
|
|
445 |
|
|
446 |
char ReadTemp(void) |
char ReadTemp(void) |
447 |
{ |
{ |
448 |
char temp; |
char temp; |
455 |
} |
} |
456 |
|
|
457 |
|
|
458 |
|
|
459 |
|
void update_lcd(void) |
460 |
|
{ |
461 |
|
static char current_row = 0; |
462 |
|
static char current_char = 0; |
463 |
|
|
464 |
|
if ( current_char >= BUFFERLEN || global_lcd_buffer[current_row][current_char] == 0 ) |
465 |
|
{ |
466 |
|
current_row++; |
467 |
|
current_char = 0; |
468 |
|
lcd_goto(0x40); |
469 |
|
} |
470 |
|
|
471 |
|
if (current_row >= 2) |
472 |
|
{ |
473 |
|
sprintf(global_lcd_buffer[1], "T=%02d, P=%04d", global_temp, (global_potmeter_hi<<8) | global_potmeter_lo); |
474 |
|
lcd_goto(0x00); |
475 |
|
current_row = 0; |
476 |
|
} |
477 |
|
|
478 |
|
lcd_putch(global_lcd_buffer[current_row][current_char]); |
479 |
|
current_char++; |
480 |
|
} |
481 |
|
|
482 |
|
void reset_baudrate(void) |
483 |
|
{ |
484 |
|
SPEN = 0; //disable serial port |
485 |
|
|
486 |
|
//set baudrate generator, i henhold til side 171 af PIC18F452 dokumentationen |
487 |
|
switch (global_comm_baudrate) |
488 |
|
{ |
489 |
|
case Baud1200: |
490 |
|
SPBRG = 207; |
491 |
|
strcpy(global_lcd_buffer[0],"Baud=1200 "); |
492 |
|
break; |
493 |
|
case Baud2400: |
494 |
|
SPBRG = 103; |
495 |
|
strcpy(global_lcd_buffer[0],"Baud=2400 "); |
496 |
|
break; |
497 |
|
case Baud9600: |
498 |
|
SPBRG = 25; |
499 |
|
strcpy(global_lcd_buffer[0],"Baud=9600 "); |
500 |
|
break; |
501 |
|
case Baud19200: |
502 |
|
SPBRG = 12; |
503 |
|
strcpy(global_lcd_buffer[0],"Baud=19200"); |
504 |
|
break; |
505 |
|
default: |
506 |
|
strcpy(global_lcd_buffer[0],"Baud=9600 "); |
507 |
|
SPBRG = 25; // this should not be possible, but default to 9600 anyway |
508 |
|
} |
509 |
|
SPEN = 1; // enable the serial port again |
510 |
|
} |
511 |
|
|
512 |
/////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////// |
513 |
//Main |
//Main |
514 |
|
|
525 |
|
|
526 |
|
|
527 |
slip_reset(); //take SLIP FSM into normal state |
slip_reset(); //take SLIP FSM into normal state |
528 |
global_comm_currentrate = Baud9600; //default baudrate = 9600 |
global_comm_baudrate = Baud9600; //default baudrate = 9600 (allready set in serial_init) |
529 |
global_reset_baudrate = 0; |
global_reset_baudrate = 0; |
530 |
|
|
531 |
// initialisation completed and we are ready to recieve commands - enable serial reception |
// initialisation completed and we are ready to recieve commands - enable serial reception |
532 |
CREN = 1; |
CREN = 1; |
533 |
|
|
534 |
|
strcpy(global_lcd_buffer[0], "Baud=9600 "); |
535 |
while (1) |
while (1) |
536 |
{ |
{ |
537 |
if (global_comm_ready == 1) |
if (global_comm_ready == 1) |
543 |
|
|
544 |
if ( global_reset_baudrate == 1) |
if ( global_reset_baudrate == 1) |
545 |
{ |
{ |
546 |
|
reset_baudrate(); |
547 |
|
global_reset_baudrate = 0; |
548 |
} |
} |
549 |
|
|
550 |
RB1 = global_led_0; |
RB1 = global_led_0; |
551 |
// RB2 = global_led_1; |
RB2 = global_led_1; |
552 |
RB3 = global_led_2; |
RB3 = global_led_2; |
553 |
|
|
554 |
global_temp = ReadTemp(); |
global_temp = ReadTemp(); |
561 |
global_potmeter_lo = ADRESL; |
global_potmeter_lo = ADRESL; |
562 |
GODONE = 1; //start ny konverering |
GODONE = 1; //start ny konverering |
563 |
} |
} |
564 |
|
|
565 |
|
update_lcd(); |
566 |
} |
} |
567 |
} |
} |
568 |
|
|