1 |
#include <pic18.h>
|
2 |
|
3 |
#include "barcode.h"
|
4 |
#include "globals.h"
|
5 |
|
6 |
unsigned char barcode_length;
|
7 |
bit barcode_ready;
|
8 |
|
9 |
void barcode_init(void)
|
10 |
{
|
11 |
SPEN = 0; //Make sure serial port is disabled
|
12 |
|
13 |
// snupset fra Kim H Pedersens serialmain.c
|
14 |
// Set Port C bit 6 output (TxD) and bit 7 input (RxD)
|
15 |
TRISC6 = 0;
|
16 |
TRISC7 = 1;
|
17 |
|
18 |
SPBRG = 25; // x = Fosc/(16 * Baud Rate) - 1
|
19 |
// x = 4000000/(16 * 9600) - 1
|
20 |
// x = 25.0417 ~ 25
|
21 |
// Baud Rate = Fosc/(16 * (x+1))
|
22 |
// Baud Rate = 4000000/(16 * (25+1))
|
23 |
// Baud Rate = 9615
|
24 |
TXSTA = 0x24; // TXSTA7 = 0 Don't care in asynchronous
|
25 |
// TXSTA6 = 0 8-bit transmission
|
26 |
// TXSTA5 = 1 Transmit enabled
|
27 |
// TXSTA4 = 0 Asynchronous mode
|
28 |
// TXSTA3 = 0 Unimplemented
|
29 |
// TXSTA2 = 1 High speed
|
30 |
// TXSTA1 = 0 TSR empty
|
31 |
// TXSTA0 = 0 9th bit */
|
32 |
RCSTA = 0x90; // RCSTA7 = 0 Serial port disabled
|
33 |
// RCSTA6 = 0 8-bit reception
|
34 |
// RCSTA5 = 0 Don't care in asynchronous
|
35 |
// RCSTA4 = 1 Receiver enabled
|
36 |
// RCSTA3 = 0 Don't care (8-bit operation)
|
37 |
// RCSTA2 = 0 No framing error
|
38 |
// RCSTA1 = 0 Overrun error bit
|
39 |
// RCSTA0 = 0 9th bit */
|
40 |
|
41 |
//config completed
|
42 |
SPEN = 1; //Enable Serial port
|
43 |
|
44 |
//Set module-scope variables
|
45 |
barcode_length = 0;
|
46 |
barcode_ready = 0;
|
47 |
|
48 |
//Configure interrupts
|
49 |
TXIE = 0; //Disable AUX TX interrupt - testes med TXIF;
|
50 |
RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;
|
51 |
|
52 |
IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet
|
53 |
|
54 |
PEIE = 1; // PEIE = PEriphal Interrupt Enable
|
55 |
|
56 |
TXIF = 0; //nulstil intterupt flag
|
57 |
RCIF = 0;
|
58 |
|
59 |
GIE = 1; //Global interrupt enable bit
|
60 |
|
61 |
}
|
62 |
|
63 |
void barcode_reset(void)
|
64 |
{
|
65 |
barcode_length = 0;
|
66 |
barcode_ready = 0;
|
67 |
}
|
68 |
|
69 |
unsigned char barcode_is_ready(void)
|
70 |
{
|
71 |
return barcode_ready;
|
72 |
}
|
73 |
|
74 |
unsigned char barcode_get_length()
|
75 |
{
|
76 |
return barcode_length;
|
77 |
}
|
78 |
|
79 |
/* interrupt entry function - barcode is the only module using interrupts */
|
80 |
void interrupt barcode_interrupt(void)
|
81 |
{
|
82 |
char data = RCREG;
|
83 |
RCIF = 0;
|
84 |
|
85 |
if (barcode_ready == 0)
|
86 |
{
|
87 |
if (data == '\r')
|
88 |
{
|
89 |
return;
|
90 |
}
|
91 |
else if (data == '\n')
|
92 |
{
|
93 |
barcode_ready = 1;
|
94 |
}
|
95 |
else
|
96 |
{
|
97 |
global_barcode_buffer[barcode_length++] = RCREG;
|
98 |
}
|
99 |
}
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
}
|