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