-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiic.c
More file actions
executable file
·300 lines (259 loc) · 8.06 KB
/
Copy pathiic.c
File metadata and controls
executable file
·300 lines (259 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "main.h"
#include "incfile.h"
#include "iic.h"
//#define MS_DEBUG
#define IIC_USE_51
#define IIC_USE_TENX
#if 1
sbit MS_SENSOR_SCK=P1^1;
sbit MS_SENSOR_SDA=P1^0;
#define SET_I2C_CLK_OUTPUT MS_SENSOR_SCK = 1
#define SET_I2C_CLK_HIGH MS_SENSOR_SCK = 1
#define SET_I2C_CLK_LOW MS_SENSOR_SCK = 0
#define SET_I2C_DATA_OUTPUT MS_SENSOR_SDA = 1
#define SET_I2C_DATA_INPUT MS_SENSOR_SDA = 1
#define SET_I2C_DATA_HIGH MS_SENSOR_SDA = 1
#define SET_I2C_DATA_LOW MS_SENSOR_SDA = 0
#define GET_I2C_DATA_BIT GPIO_READ()
#endif
#if 0
#define SET_I2C_CLK_OUTPUT SET_GPIO_PA5
#define SET_I2C_CLK_HIGH SET_PA5
#define SET_I2C_CLK_LOW CLR_PA5
#define SET_I2C_DATA_OUTPUT SET_GPIO_PA4
#define SET_I2C_DATA_INPUT SET_PA4
#define SET_I2C_DATA_HIGH SET_PA4
#define SET_I2C_DATA_LOW CLR_PA4
#define GET_I2C_DATA_BIT GPIO_READ()
#endif
#if 0
#define SET_I2C_CLK_OUTPUT SET_GPIO_PB0
#define SET_I2C_CLK_HIGH SET_PB0
#define SET_I2C_CLK_LOW CLR_PB0
#define SET_I2C_DATA_OUTPUT SET_GPIO_PB1
#define SET_I2C_DATA_INPUT SET_PB1
#define SET_I2C_DATA_HIGH SET_PB1
#define SET_I2C_DATA_LOW CLR_PB1
#define GET_I2C_DATA_BIT GPIO_READ()
#endif
#define Delay20us 0//95
#define Delay10us 0// 45
#define Delay5us 0// 21
#define Delay2p5us 0//10
static unsigned char GPIO_READ(void)
{
#if 0
SET_PA4;//very important
#endif
#if 0
SET_PB1;//very important
if (REG1630_PB_GPIO_9B & 0x02) {
return 1;
}
return 0;
#endif
#if 1
// MS_SENSOR_SDA = 1;
if (MS_SENSOR_SDA == 1) {
return 1;
}
return 0;
#endif
}
#if 0
static void GPIO_ModeSetup(bit gpio, char mode)
{
;
}
#endif
static void mma_delay(unsigned char time)
{
unsigned char i;
for (i=0; i<time; i++); //Software loop for time delay
}
void IIC_Init(void)
{
//GPIO_ModeSetup(MS_SENSOR_SCK, 0x00);
SET_I2C_CLK_OUTPUT;
SET_I2C_CLK_HIGH;
//GPIO_ModeSetup(MS_SENSOR_SDA, 0x00); //Set I2C DATA pin as GPIO
SET_I2C_DATA_OUTPUT;
SET_I2C_DATA_HIGH;
}
/**
* describe: I2C Start signal generation:
* Data pin falls down when clock is high
*/
void IIC_Start(void)
{
//GPIO_ModeSetup(MS_SENSOR_SCK, 0x00); //Set I2C CLK pin as GPIO
SET_I2C_CLK_OUTPUT; //Set I2C CLK pin as output
//GPIO_ModeSetup(MS_SENSOR_SDA, 0x00); //Set I2C DATA pin as GPIO
SET_I2C_DATA_OUTPUT; //Set I2C DATA pin as output
SET_I2C_DATA_HIGH; //I2C DATA pin output high(1)
SET_I2C_CLK_HIGH; //I2C CLK pin output high(1)
mma_delay(Delay20us); //Delay 20uS
SET_I2C_DATA_LOW; //I2C DATA pin output low(0)
mma_delay(Delay10us); //Delay 10uS
SET_I2C_CLK_LOW; //I2C CLK pin output low(0)
mma_delay(Delay10us); //Delay 10uS
}
void IIC_Stop (void)
{
mma_delay(Delay10us); //Delay 10uS
SET_I2C_CLK_HIGH; //I2C CLK pin output high(1)
mma_delay(Delay10us); //Delay 10uS
SET_I2C_DATA_HIGH; //I2C DATA pin output high(1)
}
/**
* describe: I2C Repeat Start signal generation:
* Data pin falls down when clock is high
*
*
*_________||||||||||||||____ SLK
*
*______|||||||||||||________ SDA
*
**/
void IIC_RepeatedStart(void)
{
mma_delay(Delay20us); //Delay 20uS
mma_delay(Delay20us); //Delay 20uS
SET_I2C_DATA_HIGH; //I2C DATA pin output high(1)
mma_delay(Delay10us); //Delay 10uS
SET_I2C_CLK_HIGH; //I2C CLK pin output high(1)
mma_delay(Delay20us); //Delay 20uS
mma_delay(Delay20us); //Delay 20uS
SET_I2C_DATA_LOW; //I2C DATA pin output low(0)
mma_delay(Delay10us); //Delay 10uS
SET_I2C_CLK_LOW; //I2C CLK pin output low(0)
mma_delay(Delay10us); //Delay 10uS
}
/**
*
* ____||||||____ SLK
*/
void IIC_OneClk(void)
{
mma_delay(Delay5us);
SET_I2C_CLK_HIGH; //I2C CLK pin output high(1)
mma_delay(Delay10us); //Delay 10uS
SET_I2C_CLK_LOW; //I2C CLK pin output low(0)
mma_delay(Delay5us);
}
/**
* Subroutine: Check I2C Acknowledgement signal
* return: 1 - Return 1 if read 1 from I2C DATA pin
* 0 - Return 0 if read 0 from I2C DATA pin
*
*
* _________---------______________ sck
*
* ---------_______________________sda
*
*
**/
unsigned char IIC_ChkAck(void)
{
SET_I2C_DATA_INPUT; //Set I2C DATA pin as input
mma_delay(Delay5us); //Delay 5uS
SET_I2C_CLK_HIGH; //I2C CLK pin output high(1)
mma_delay(Delay5us); //Delay 5uS again
if (GET_I2C_DATA_BIT) { //Read I2C DATA pin
mma_delay(Delay5us); //Delay 5uS
SET_I2C_CLK_LOW; //I2C CLK pin output low(0)
mma_delay(Delay5us); //Delay 5us again
SET_I2C_DATA_OUTPUT; //Set I2C DATA pin as output
SET_I2C_DATA_LOW; //I2C DATA pin output low(0)
return 1; //Return 1 if read 1 from I2C DATA pin
} else { //If I2C DATA pin is invalid for acknowledgement signal
mma_delay(Delay5us); //Delay 5uS
SET_I2C_CLK_LOW; //I2C CLK pin output low(0)
mma_delay(Delay5us); //Delay 5uS again
SET_I2C_DATA_OUTPUT; //Set I2C DATA pin as output
SET_I2C_DATA_LOW; //I2C DATA pin output low(0)
return 0; //Return 0 if read 0 from I2C DATA pin
}
}
/**
*
* Subroutine: Read one byte and send an acknowledgement signal
*
**/
unsigned char IIC_ReadByteACK(void)
{
char i;
unsigned char dat;
SET_I2C_DATA_INPUT; //Set I2C DATA pin as input
dat = 0; //Prepare to receive data
for (i=7; i>=0; i--) { //Loop 8 times to receive 8 bits
if (GET_I2C_DATA_BIT)
dat |= (0x01<<i); //If read a 1, set to data bit
IIC_OneClk();
} //Output one clock pulse after a bit is read
SET_I2C_DATA_OUTPUT; //Set I2C DATA pin as output
SET_I2C_DATA_LOW; //I2C DATA pin output low(0): the acknowledgement signal
IIC_OneClk(); //Output one clock pulse after data pin is ready
return dat; //Return eceived data
}
/*
* Subroutine: Read one byte but do not send acknowledgement signal
*
**/
unsigned char IIC_ReadByteNCK(void)
{
char i;
unsigned char dat;
SET_I2C_DATA_INPUT; //Set I2C DATA pin as input
dat = 0; //Prepare to receive data
for (i=7; i>=0; i--) { //Loop 8 times to receive 8 bits
if (GET_I2C_DATA_BIT) dat |= (0x01<<i); //If read a 1, set to data bit
IIC_OneClk();
} //Output one clock pulse after a bit is read
SET_I2C_DATA_OUTPUT; //Set I2C DATA pin as output
SET_I2C_DATA_HIGH; //I2C DATA pin output high(1): no acknowledge
IIC_OneClk(); //Output one clock pulse after data pin is ready
SET_I2C_DATA_LOW; //I2C DATA pin output low(0)
return dat; //Return received data
}
/**
* Subroutine: I2C send one byte out
*
*
*
* _____|||||||______|||||||||_____ ....
*
*--<===========>--<==========> ........
*/
void IIC_SendByte(unsigned char sData)
{
char i;
for (i=7; i>=0; i--) {
if ((sData>>i)&0x01) {
SET_I2C_DATA_HIGH;
} else {
SET_I2C_DATA_LOW;
}
IIC_OneClk();
}
}
unsigned char IIC_SendBytes(kal_uint8 *pData, unsigned char len)
{
unsigned char i;
for (i = 0; i < len; i++) {
IIC_SendByte(pData[i]);
if (IIC_ChkAck()) {
IIC_Stop();
return EEPROM_ACK_ERR;
}
}
return EEPROM_RDWR_OK;
}
unsigned char IIC_ReadBytes(kal_uint8 *pData, unsigned char len)
{
unsigned char i;
for(i=0; i<(len-1); i++)
pData[i] = IIC_ReadByteACK();
pData[len-1] = IIC_ReadByteNCK();
return 1;
}