1 |
hedin |
109 |
#include <pic18.h>
|
2 |
|
|
#include "MyI2C.h"
|
3 |
|
|
#include "delay.h"
|
4 |
|
|
|
5 |
|
|
#define LED RB3
|
6 |
|
|
|
7 |
|
|
/****************************************************************************
|
8 |
|
|
* Function:InitI2C *
|
9 |
|
|
* Purpose: Initialize 100 kHz I2C communication *
|
10 |
|
|
****************************************************************************/
|
11 |
|
|
void InitI2C(void)
|
12 |
|
|
{
|
13 |
|
|
SSPADD = 0x0A; // 100 kHz I2C bus
|
14 |
hedin |
111 |
//SSPADD = 0x28; // 100 kHz I2C bus
|
15 |
hedin |
109 |
SSPCON1 = 0x28; // Enable I2C bus
|
16 |
|
|
StopI2C();
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
/****************************************************************************
|
20 |
|
|
* Function:StopI2C *
|
21 |
|
|
* Purpose: End communication with I2C slave *
|
22 |
|
|
* Send stop signal on I2C bus *
|
23 |
|
|
* Input: Nothing *
|
24 |
|
|
* Output: Nothing *
|
25 |
|
|
****************************************************************************/
|
26 |
|
|
void StopI2C(void)
|
27 |
|
|
{
|
28 |
|
|
SSPIF = 0; // Clear interrupt flag
|
29 |
|
|
PEN = 1; // Send Stop condition
|
30 |
|
|
|
31 |
|
|
while (SSPIF == 0); // Wait until stop condition complete
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
/****************************************************************************
|
35 |
|
|
* Function:WaitForWrite EEProm *
|
36 |
|
|
* Purpose: Wait for read write acces in EEProm *
|
37 |
|
|
* Send start signal on I2C bus *
|
38 |
|
|
* Send <I2CADD> on I2C bus *
|
39 |
|
|
* Receive acknowledge from I2C slave *
|
40 |
|
|
* Input: char I2CADD: Address of I2C slave to start communication with *
|
41 |
|
|
* RWStruct *IOStruct2 ptr for data struct with Progress flag *
|
42 |
|
|
* Output: 0: No communication started (missing acknowledge) *
|
43 |
|
|
* 1: Communication started (acknowledge) *
|
44 |
|
|
****************************************************************************/
|
45 |
|
|
char WaitForWrite(char I2CADD, char *Progress)
|
46 |
|
|
{
|
47 |
|
|
int BreakCount = 5000;
|
48 |
|
|
|
49 |
|
|
do{
|
50 |
|
|
SSPIF = 0; // Clear interrupt flag
|
51 |
|
|
RSEN = 1; // Initiate start condition
|
52 |
|
|
while (SSPIF == 0)
|
53 |
|
|
LED = LED^1; // Wait until Start condition complete
|
54 |
|
|
|
55 |
|
|
SSPIF = 0; // Clear interrupt flag
|
56 |
|
|
SSPBUF = I2CADD; // Send address of slave device
|
57 |
|
|
|
58 |
|
|
while (SSPIF == 0)
|
59 |
|
|
LED = LED^1; // Wait until address has been sent
|
60 |
|
|
|
61 |
|
|
if(BreakCount-- == 0){
|
62 |
|
|
LED = 0;
|
63 |
|
|
*Progress = 0;
|
64 |
|
|
return 0; // Return 0 if device didn't acknowledge
|
65 |
|
|
}
|
66 |
|
|
}while(ACKSTAT == 1);
|
67 |
|
|
|
68 |
|
|
LED = 0;
|
69 |
|
|
*Progress = 0;
|
70 |
|
|
return 1; // Return 1 if device acknowledged
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/****************************************************************************
|
74 |
|
|
* Function:StartI2C *
|
75 |
|
|
* Purpose: Initiate communication with I2C slave *
|
76 |
|
|
* Send start signal on I2C bus *
|
77 |
|
|
* Send <I2CADD> on I2C bus *
|
78 |
|
|
* Receive acknowledge from I2C slave *
|
79 |
|
|
* Input: char I2CADD: Address of I2C slave to start communication with *
|
80 |
|
|
* Output: 0: No communication started (missing acknowledge) *
|
81 |
|
|
* 1: Communication started (acknowledge) *
|
82 |
|
|
****************************************************************************/
|
83 |
|
|
char StartI2C(char I2CADD)
|
84 |
|
|
{
|
85 |
|
|
SSPIF = 0; // Clear interrupt flag
|
86 |
|
|
RSEN = 1; // Initiate start condition
|
87 |
|
|
|
88 |
|
|
while (SSPIF == 0)
|
89 |
|
|
LED = LED^1; // Wait until Start condition complete
|
90 |
|
|
|
91 |
|
|
SSPIF = 0; // Clear interrupt flag
|
92 |
|
|
SSPBUF = I2CADD; // Send address of slave device
|
93 |
|
|
|
94 |
|
|
while (SSPIF == 0)
|
95 |
|
|
LED = LED^1; // Wait until address has been sent
|
96 |
|
|
|
97 |
|
|
LED = 0;
|
98 |
|
|
|
99 |
|
|
if (ACKSTAT == 0)
|
100 |
|
|
return 1; // Return 1 if device acknowledged
|
101 |
|
|
else
|
102 |
|
|
return 0; // Return 0 if device didn't acknowledge
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
/****************************************************************************
|
106 |
|
|
* Function:ReStartI2C *
|
107 |
|
|
* Purpose: Initiate communication with I2C slave *
|
108 |
|
|
* Send start signal on I2C bus *
|
109 |
|
|
* Send <I2CADD> on I2C bus *
|
110 |
|
|
* Receive acknowledge from I2C slave *
|
111 |
|
|
* Input: char I2CADD: Address of I2C slave to start communication with *
|
112 |
|
|
* Output: 0: No communication started (missing acknowledge) *
|
113 |
|
|
* 1: Communication started (acknowledge) *
|
114 |
|
|
****************************************************************************/
|
115 |
|
|
char ReStartI2C(char I2CADD)
|
116 |
|
|
{
|
117 |
|
|
SSPIF = 0; // Clear interrupt flag
|
118 |
|
|
RSEN = 1; // Initiate start condition
|
119 |
|
|
|
120 |
|
|
while (SSPIF == 0)
|
121 |
|
|
LED = LED^1; // Wait until Start condition complete
|
122 |
|
|
|
123 |
|
|
SSPIF = 0; // Clear interrupt flag
|
124 |
|
|
SSPBUF = I2CADD; // Send address of slave device
|
125 |
|
|
|
126 |
|
|
while (SSPIF == 0)
|
127 |
|
|
LED = LED^1; // Wait until address has been sent
|
128 |
|
|
|
129 |
|
|
LED = 0;
|
130 |
|
|
|
131 |
|
|
if (ACKSTAT == 0)
|
132 |
|
|
return 1; // Return 1 if device acknowledged
|
133 |
|
|
else
|
134 |
|
|
return 0; // Return 0 if device didn't acknowledge
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/****************************************************************************
|
138 |
|
|
* Function:WriteI2CByte *
|
139 |
|
|
* Purpose: Send 1 byte to I2C slave *
|
140 |
|
|
* Input: 1 Data byte *
|
141 |
|
|
* Output: 0: Communication did not succeed *
|
142 |
|
|
* 1: Communication succeeded *
|
143 |
|
|
****************************************************************************/
|
144 |
|
|
char WriteI2CByte(char I2CByte)
|
145 |
|
|
{
|
146 |
|
|
SSPIF = 0;
|
147 |
|
|
SSPBUF = I2CByte;
|
148 |
|
|
// while(SSPIF == 0);
|
149 |
|
|
Delay10us(1);
|
150 |
|
|
|
151 |
|
|
if (ACKSTAT == 0)
|
152 |
|
|
return 1; // Return 1 if device acknowledged
|
153 |
|
|
else
|
154 |
|
|
return 0; // Return 0 if device didn't acknowledge
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
/****************************************************************************
|
158 |
|
|
* Function:ReadI2CByte *
|
159 |
|
|
* Purpose: Send 1 byte to I2C slave *
|
160 |
|
|
* Input: 1 Data byte *
|
161 |
|
|
* Output: 0: Communication did not succeed *
|
162 |
|
|
* 1: Communication succeeded *
|
163 |
|
|
****************************************************************************/
|
164 |
|
|
char ReadI2CByte(char LastRead)
|
165 |
|
|
{
|
166 |
|
|
char ReadChar=0;
|
167 |
|
|
|
168 |
|
|
SSPIF = 0; // Clear interrupt flag
|
169 |
|
|
RCEN = 1; // Initiate read sequence
|
170 |
|
|
|
171 |
|
|
while (SSPIF == 0);
|
172 |
|
|
|
173 |
|
|
ReadChar = SSPBUF; // Store byte in buffer
|
174 |
|
|
SSPIF = 0;
|
175 |
|
|
|
176 |
|
|
if(LastRead == 1){
|
177 |
|
|
ACKDT = 1; // indicate we're done by no ack
|
178 |
|
|
}else{
|
179 |
|
|
ACKDT = 0; // else acknowledge the byte
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
ACKEN = 1; // Start acknowledge sequence
|
183 |
|
|
|
184 |
|
|
return ReadChar;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/****************************************************************************
|
188 |
|
|
* Function:WriteI2C *
|
189 |
|
|
* Purpose: Send <I2CBYTES> bytes to I2C slave *
|
190 |
|
|
* Input: char I2CADD: Address of I2C slave to start communication with *
|
191 |
|
|
* char I2CBYTES: Number of bytes to send *
|
192 |
|
|
* char*I2CBUFFER: Buffer containing data to send *
|
193 |
|
|
* Output: 0: Communication did not succeed *
|
194 |
|
|
* 1: Communication succeeded *
|
195 |
|
|
****************************************************************************/
|
196 |
|
|
char WriteI2C(RWStruct *IOStruct) //char DevAddr, char NoBytes, char *WriteBuffer);
|
197 |
|
|
{
|
198 |
|
|
char Count = 0;
|
199 |
|
|
char Res = 1;
|
200 |
|
|
|
201 |
|
|
if(IOStruct->WrInProgress == 1){
|
202 |
|
|
Res = WaitForWrite(IOStruct->DevAddr & 0xfe, &IOStruct->WrInProgress);
|
203 |
|
|
}else{
|
204 |
|
|
Res = StartI2C(IOStruct->DevAddr & 0xfe);
|
205 |
|
|
}
|
206 |
|
|
if(Res == 1){
|
207 |
|
|
if(!WriteI2CByte((char) IOStruct->RWAddress / 0x100))
|
208 |
|
|
Res = 0;
|
209 |
|
|
else if(!WriteI2CByte((char) IOStruct->RWAddress % 0x100))
|
210 |
|
|
Res = 0;
|
211 |
|
|
else{
|
212 |
|
|
for(Count = 0; Count < IOStruct->NoBytes; Count++){
|
213 |
|
|
if(WriteI2CByte(IOStruct->IOBuffer[Count]) == 0){
|
214 |
|
|
Res = 0;
|
215 |
|
|
break;
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
}else{
|
220 |
|
|
Res = 0;
|
221 |
|
|
}
|
222 |
|
|
StopI2C();
|
223 |
|
|
IOStruct->WrInProgress = 1;
|
224 |
|
|
return Res;
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
/****************************************************************************
|
228 |
|
|
* Function:ReadI2C *
|
229 |
|
|
* Purpose: Reads <I2CBYTES> bytes to I2C slave *
|
230 |
|
|
* Input: char I2CADD: Address of I2C slave to start communication with *
|
231 |
|
|
* char I2CBYTES: Number of bytes to send *
|
232 |
|
|
* char*I2CBUFFER: Buffer containing data to send *
|
233 |
|
|
* Output: 0: Communication did not succeed *
|
234 |
|
|
* 1: Communication succeeded *
|
235 |
|
|
****************************************************************************/
|
236 |
|
|
char ReadI2C(RWStruct *IOStruct) //char DevAddr, char NoBytes, char *ReadBuffer);
|
237 |
|
|
{
|
238 |
|
|
char Count = 0;
|
239 |
|
|
char Ptr = 0;
|
240 |
|
|
char Res = 1;
|
241 |
|
|
|
242 |
|
|
if(IOStruct->WrInProgress == 1){
|
243 |
|
|
Res = WaitForWrite(IOStruct->DevAddr & 0xfe, &IOStruct->WrInProgress);
|
244 |
|
|
}else{
|
245 |
|
|
Res = StartI2C( IOStruct->DevAddr & 0xfe);
|
246 |
|
|
}
|
247 |
|
|
if(Res == 1){
|
248 |
|
|
if(!WriteI2CByte((char) IOStruct->RWAddress / 0x100))
|
249 |
|
|
Res = 0;
|
250 |
|
|
else if(!WriteI2CByte((char) IOStruct->RWAddress % 0x100))
|
251 |
|
|
Res = 0;
|
252 |
|
|
else if(!ReStartI2C(IOStruct->DevAddr | 0x1))
|
253 |
|
|
Res = 0;
|
254 |
|
|
else {
|
255 |
|
|
Count = IOStruct->NoBytes - 1;
|
256 |
|
|
for(Ptr = 0; Ptr < Count; Ptr++){
|
257 |
|
|
IOStruct->IOBuffer[Ptr] = ReadI2CByte(0);
|
258 |
|
|
}
|
259 |
|
|
IOStruct->IOBuffer[Ptr] = ReadI2CByte(1);
|
260 |
|
|
}
|
261 |
|
|
StopI2C();
|
262 |
|
|
}
|
263 |
|
|
return Res;
|
264 |
|
|
}
|