1 |
/* H7 -projekt opgave */ |
2 |
|
3 |
///////////////////////////////////////////////////////////////////////
|
4 |
//Includes |
5 |
#include <pic18.h>
|
6 |
#include "delay.h" |
7 |
#include "i2c.h"
|
8 |
#include "lcd.h" |
9 |
|
10 |
/////////////////////////////////////////////////////////////////////
|
11 |
//Defines
|
12 |
#define TC74 0x9A /* I2C TC74 IC */
|
13 |
|
14 |
//////////////
|
15 |
// Enums
|
16 |
|
17 |
enum SlipState{
|
18 |
SlipNormal,
|
19 |
SlipEscaped,
|
20 |
SlipError,
|
21 |
SlipStopped
|
22 |
};
|
23 |
|
24 |
//////////////////////////////////////////////////////////////////
|
25 |
//Globale data
|
26 |
// Alle globale variabler bruger "global_" som prefix
|
27 |
unsigned char global_comm_buffer[10];
|
28 |
unsigned char global_comm_length;
|
29 |
unsigned char global_comm_index;
|
30 |
bit global_comm_ready;
|
31 |
bit global_comm_recieving;
|
32 |
|
33 |
unsigned char global_comm_state;
|
34 |
|
35 |
|
36 |
|
37 |
///////////////////////////////////////////////////////////////////
|
38 |
// Interrupt funktioner
|
39 |
|
40 |
void recieve_interrupt(void)
|
41 |
{
|
42 |
}
|
43 |
|
44 |
void transmit_interrupt(void)
|
45 |
{
|
46 |
}
|
47 |
|
48 |
void interrupt interrupt_handler(void)
|
49 |
{
|
50 |
if (RCIF == 1)
|
51 |
{
|
52 |
recieve_interrupt();
|
53 |
RCIF = 0;
|
54 |
}
|
55 |
if (TXIF == 1)
|
56 |
{
|
57 |
transmit_interrupt();
|
58 |
TXIF = 0;
|
59 |
}
|
60 |
}
|
61 |
|
62 |
///////////////////////////////////////////////////////////////////
|
63 |
// Alm funktioner
|
64 |
|
65 |
void interrupt_init(void)
|
66 |
{
|
67 |
TXIE = 1; //Enable AUX TX interrupt - testes med TXIF;
|
68 |
RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF;
|
69 |
|
70 |
IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet
|
71 |
|
72 |
PEIE = 1; // PEIE = PEriphal Interrupt Enable
|
73 |
|
74 |
TXIF = 0; //nulstil intterupt flag
|
75 |
RCIF = 0;
|
76 |
|
77 |
GIE = 1; //Global interrupt enable bit
|
78 |
}
|
79 |
|
80 |
void serial_init(void)
|
81 |
{
|
82 |
SPEN = 0; //Make sure serial port is disabled
|
83 |
|
84 |
// snupset fra Kim H Pedersens serialmain.c
|
85 |
// Set Port C bit 6 output (TxD) and bit 7 input (RxD)
|
86 |
TRISC6 = 0;
|
87 |
TRISC7 = 1;
|
88 |
|
89 |
SPBRG = 25; // x = Fosc/(16 * Baud Rate) - 1
|
90 |
// x = 4000000/(16 * 9600) - 1
|
91 |
// x = 25.0417 ~ 25
|
92 |
// Baud Rate = Fosc/(16 * (x+1))
|
93 |
// Baud Rate = 4000000/(16 * (25+1))
|
94 |
// Baud Rate = 9615
|
95 |
TXSTA = 0x24; // TXSTA7 = 0 Don't care in asynchronous
|
96 |
// TXSTA6 = 0 8-bit transmission
|
97 |
// TXSTA5 = 1 Transmit enabled
|
98 |
// TXSTA4 = 0 Asynchronous mode
|
99 |
// TXSTA3 = 0 Unimplemented
|
100 |
// TXSTA2 = 1 High speed
|
101 |
// TXSTA1 = 0 TSR empty
|
102 |
// TXSTA0 = 0 9th bit */
|
103 |
RCSTA = 0x90; // RCSTA7 = 0 Serial port disabled
|
104 |
// RCSTA6 = 0 8-bit reception
|
105 |
// RCSTA5 = 0 Don't care in asynchronous
|
106 |
// RCSTA4 = 1 Receiver enabled
|
107 |
// RCSTA3 = 0 Don't care (8-bit operation)
|
108 |
// RCSTA2 = 0 No framing error
|
109 |
// RCSTA1 = 0 Overrun error bit
|
110 |
// RCSTA0 = 0 9th bit */
|
111 |
|
112 |
//config completed
|
113 |
SPEN = 1; //Enable Serial port
|
114 |
}
|
115 |
|
116 |
|
117 |
|
118 |
void i2c_init(void)
|
119 |
{
|
120 |
#ifdef I2C_MODULE |
121 |
SSPMode(MASTER_MODE); |
122 |
SSPEN = 1; |
123 |
CKP = 1; |
124 |
#else |
125 |
SCL_DIR = I2C_OUTPUT; |
126 |
SDA_DIR = I2C_OUTPUT; |
127 |
SDA = 0; |
128 |
SCL = 0; |
129 |
#endif
|
130 |
}
|
131 |
|
132 |
|
133 |
|
134 |
char ReadTemp(void)
|
135 |
{
|
136 |
char temp;
|
137 |
i2c_WriteTo(TC74); //write to, will automatically send a start condition
|
138 |
i2c_PutByte(0x00); //tell TC74 we want to read
|
139 |
i2c_ReadFrom(TC74);
|
140 |
temp = i2c_GetByte(I2C_LAST);
|
141 |
i2c_Stop(); //assert a stop condition on SDA & SCL
|
142 |
return temp;
|
143 |
}
|
144 |
|
145 |
|
146 |
///////////////////////////////////////////////////////////////////
|
147 |
//Main
|
148 |
|
149 |
|
150 |
void main(void)
|
151 |
{
|
152 |
lcd_init(0); //init in 4-bit mode
|
153 |
i2c_init();
|
154 |
serial_init(); //9600 8N1
|
155 |
|
156 |
interrupt_init();
|
157 |
|
158 |
global_comm_length = 0;
|
159 |
global_comm_ready = 0;
|
160 |
|
161 |
|
162 |
global_comm_state = SlipNormal;
|
163 |
|
164 |
// initialisation completed and we are ready to recieve commands - enable serial reception
|
165 |
CREN = 1;
|
166 |
|
167 |
while (1)
|
168 |
{
|
169 |
} |
170 |
} |