-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
236 lines (205 loc) · 4.36 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
219
220
221
222
223
224
225
226
227
228
/***************************************************************************************
* FileName : main.c
* CopyRight :
* ModuleName :
*
* CPU :
* RTOS :
*
* Create Data : 2017.47.10
* Author/Corportation : yezhihuo
*
* Abstract Description :
*
*--------------------------------Revision History--------------------------------------
* No version Data Revised By Item Description
*
*
***************************************************************************************/
/**************************************************************
* Debug switch Section
**************************************************************/
/**************************************************************
* Include File Section
**************************************************************/
#include"i2c.h"
/**************************************************************
* Macro Define Section
**************************************************************/
/**************************************************************
* Struct Define Section
**************************************************************/
/**************************************************************
* Prototype Declare Section ????
**************************************************************/
/**************************************************************
* Global Variable Declare Section????
**************************************************************/
sbit sda = P3^0;
sbit scl = P3^1;
#define I2CAddr 0xd0 //设备地址
/**************************************************************
* Function Define Section??????
**************************************************************/
/* @name :void delay5us(uchar i)
* @description: 延时 5us
* @param : uchar i
* @return :none
* @notice : none
*/
void Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/* @name : void start()
* @description : 开始信号
* @param : none
* @return : none
* @notice : None
*/
void start()
{
sda = 1;
Delay5us();
scl = 1;
Delay5us();
sda = 0;
Delay5us();
}
/* @name : void stop()
* @description : 停止信号
* @param : none
* @return : none
* @notice : None
*/
void stop()
{
sda = 0;
Delay5us();
scl = 1;
Delay5us();
sda = 1;
Delay5us();
}
/* @name : void respons()
* @description : 应答信号
* @param : none
* @return : none
* @notice : None
*/
void respons()
{
uchar i = 0;
scl = 1;
Delay5us();
while( (sda == 1) && (i < 250) ) //若一段时间内未接受到应答则默认已接收
{
i++;
}
scl = 0;
Delay5us();
}
/* @name : void init()
* @description : 总线初始化
* @param : none
* @return : none
* @notice : None
*/
void initI2C()
{
sda = 1;
Delay5us();
scl = 1;
Delay5us();
}
/* @name : void writeByte(uchar date)
* @description : 写一个字节
* @param : date 要写入的字节
* @return : none
* @notice : None
*/
void writeByte(uchar date)
{
uchar i,temp;
temp = date;
for( i = 0; i < 8; i++ )
{
temp <<= 1; //temp左移一位,将最高位移入PSW寄存器的CY位
scl = 0;
Delay5us();
sda = CY; // 将CY位发送出去
Delay5us();
scl = 1;
Delay5us();
}
scl = 0;
Delay5us();
sda = 1;
Delay5us();
}
/* @name : uchar readByte()
* @description : 读取一个字节
* @param : none
* @return : 读取的字节
* @notice : None
*/
uchar readByte()
{
uchar i,k = 0;
scl = 0;
Delay5us();
sda = 1;
Delay5us();
for( i = 0; i < 8; i++ )
{
scl = 1;
Delay5us();
k = (k <<1) | sda;
scl = 0;
Delay5us();
}
return k;
}
/* @name : void writeAdd(uchar adress, uchar date)
* @description : 向从机发送数据
* @param : address 从机
* @return : none
* @notice : None
*/
void writeAdd(uchar address, uchar date)
{
start();
writeByte(I2CAddr);
respons();
writeByte(address);
respons();
writeByte(date);
respons();
stop();
}
/* @name : uchar readAdd()
* @description :
* @param : none
* @return : none
* @notice : None
*/
uchar readAdd(uchar address)
{
uchar date;
start();
writeByte(I2CAddr);
respons();
writeByte(address);
respons();
start();
writeByte(I2CAddr + 1);
respons();
date = readByte();
stop();
return date;
}