-
Notifications
You must be signed in to change notification settings - Fork 28
/
i2c.c
218 lines (186 loc) · 3.5 KB
/
i2c.c
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
#include "i2c.h"
#include "ch554_platform.h"
xdata uint8_t I2C_Buf;
SBIT(sda_pin, 0x90, 5);
SBIT(scl_pin, 0x90, 4);
//sbit sda_pin = P1^5;
//sbit scl_pin = P1^4;
#define I2C_SDA() (sda_pin==1)
#define I2C_SDA_H() {sda_pin = 1;}
#define I2C_SDA_L() {sda_pin = 0;}
#define I2C_SCL_H() {scl_pin = 1;}
#define I2C_SCL_L() {scl_pin = 0;}
void I2C_DELAY(void) {
uint8_t i;
for (i=0; i<7; i++); // Frequency control
}
void I2C_Init(void) {
I2C_SDA_H();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
}
// Generate a I2C start sequence
void I2C_Send_Start(void) {
// I2C_SCL = H, I2C_SDA = H
I2C_SDA_H();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SDA_L();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
// I2C_SCL = L, I2C_SDA = L
}
// Write a byte (stored in I2C_Buf) to I2C bus and return the ACK status in I2C_Buf
// xxxx xxx(ACK bit)
// ACK bit = 0 -> ACK received
// ACK bit = 1 -> NACK received
void I2C_WriteByte(void) {
// I2C_SCL = L
if (I2C_Buf & 0x80) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x40) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x20) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x10) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x08) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x04) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x02) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
if (I2C_Buf & 0x01) {I2C_SDA_H();} else {I2C_SDA_L();}
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
I2C_SDA_H();
I2C_DELAY();
// Check ACK bit
// I2C_SCL = L
I2C_SCL_H();
I2C_DELAY();
I2C_Buf = 0;
if (I2C_SDA())
I2C_Buf = 1;
I2C_SCL_L();
I2C_DELAY();
}
// Read a byte from I2C bus and store the result into I2C_Buf
void I2C_ReadByte(void) {
I2C_Buf = 0;
// I2C_SCL = L
I2C_SDA_H(); //Master release the bus
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x80;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x40;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x20;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x10;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x08;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x04;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x02;
I2C_SCL_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
if (I2C_SDA())
I2C_Buf |= 0x01;
I2C_SCL_L();
I2C_DELAY();
}
// Send a ACK to I2C bus
void I2C_Send_ACK(void) {
// I2C_SCL = L
I2C_SDA_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
}
// Send a NACK to I2C bus
void I2C_Send_NACK(void) {
// I2C_SCL = L
I2C_SDA_H();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SCL_L();
I2C_DELAY();
}
// Generate a I2C stop condition, release the bus
void I2C_Send_Stop(void) {
// I2C_SCL = L, I2C_SDA = H
I2C_SDA_L();
I2C_DELAY();
I2C_SCL_H();
I2C_DELAY();
I2C_SDA_H(); //Master release the bus
I2C_DELAY();
}