📄 iic.lst
字号:
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE IIC
OBJECT MODULE PLACED IN iic.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE iic.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <reg52.h>
2 #include "delay.h"
3 #include "iic.h"
4
5 /*启动IIC设备*/
6 void start(void)
7 {
8 1 DelayMs(5);
9 1 SDA = 1 ;
10 1 Delay(5);
11 1 SCL = 1 ;
12 1 Delay(5);
13 1 SDA = 0 ;
14 1 Delay(5);
15 1 SCL = 0 ;
16 1 Delay(5);
17 1 }
18
19
20 /*停止IIC设备*/
21 void stop(void)
22 {
23 1 Delay(5);
24 1 SCL = 0 ;
25 1 Delay(5);
26 1 SDA = 0 ;
27 1 Delay(5);
28 1 SCL = 1 ;
29 1 Delay(5);
30 1 SDA = 1 ;
31 1 Delay(5);
32 1 DelayMs(15);
33 1 }
34
35 /*检测ack*/
36 bit testack(void)
37 {
38 1 bit AckFlag;
39 1 Delay(5);
40 1 SDA = 1;
41 1 Delay(5);
42 1 SCL = 1;
43 1 Delay(5);
44 1 AckFlag = SDA;
45 1 SCL = 0;
46 1 Delay(5);
47 1 return(AckFlag);
48 1 }
49
50 /*发送应答信号*/
51 void sendack(void)
52 {
53 1 Delay(5);
54 1 SDA = 0;
55 1 Delay(5);
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 2
56 1 SCL = 1;
57 1 Delay(5);
58 1 SCL = 0;
59 1 Delay(5);
60 1 SDA = 1;
61 1 Delay(5);
62 1 }
63 /*发送非应答信号*/
64 void sendNack(void)
65 {
66 1 Delay(5);
67 1 SDA = 1 ;
68 1 Delay(5);
69 1 SCL = 1 ;
70 1 Delay(5);
71 1 SCL = 0 ;
72 1 Delay(5);
73 1 }
74
75 /*写1个字节的数据*/
76 void write8bit(unsigned char T_Data)
77 {
78 1 unsigned char i ;
79 1 for(i = 8; i != 0; i--)
80 1 {
81 2 SDA = (bit)(T_Data & 0x80);
82 2 Delay(5);
83 2 SCL = 1 ;
84 2 Delay(5);
85 2 SCL = 0 ;
86 2 Delay(5);
87 2 T_Data = T_Data << 1 ;
88 2 Delay(5);
89 2 }
90 1 }
91
92 /*读1个字节的数据*/
93 unsigned char read8bit(void)
94 {
95 1 unsigned char i = 0 ;
96 1 unsigned char R_Data = 0x00 ;
97 1
98 1 for(i = 8; i != 0; i--)
99 1 {
100 2 SDA = 1;
101 2 Delay(5);
102 2 SCL = 0 ;
103 2 Delay(5);
104 2 Delay(5);
105 2 SCL = 1 ;
106 2 R_Data = R_Data << 1 ;
107 2 R_Data = R_Data |((unsigned char)(SDA)) ;
108 2 SCL = 0 ;
109 2 Delay(5);
110 2 }
111 1 return(R_Data);
112 1 }
113
114 /*写1个字节的数据到指定的地址*/
115 void write_I2C(unsigned char RomAddress,unsigned char T_Data)
116 {
117 1 start() ;
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 3
118 1 write8bit(WrDevAddr) ;
119 1 testack() ;
120 1 write8bit(RomAddress) ;
121 1 testack() ;
122 1 write8bit(T_Data) ;
123 1 testack() ;
124 1 stop() ;
125 1 }
126
127
128 /*读1个字节的数据*/
129 unsigned char read_I2C(unsigned char RomAddress)
130 {
131 1 unsigned char R_Data = 0x00 ;
132 1 start() ;
133 1 write8bit(WrDevAddr) ;
134 1 testack() ;
135 1 write8bit(RomAddress) ;
136 1 testack() ;
137 1 start() ;
138 1 write8bit(RdDevAddr) ;
139 1 testack() ;
140 1 R_Data = read8bit() ;
141 1 sendNack() ;
142 1 stop() ;
143 1 return(R_Data) ;
144 1 }
145
146 /*指定的地址写页-8字节数据*/
147 void WriteOnePage (unsigned char addr,unsigned char *thedata) //写入一页8个字节到指定开始地址,可以自动
-翻页
148 {
149 1 unsigned char i; //计数器
150 1 start(); //开始总线
151 1 write8bit(WrDevAddr); //发送控制数据
152 1 testack(); //检查应答信息
153 1 write8bit(addr); //写入地址
154 1 testack();
155 1 for(i=0;i<8;i++) //循环写入第一页的数据
156 1 {
157 2 write8bit(*thedata); //写入数据
158 2 testack();
159 2 thedata++; //数据指针加1
160 2 }
161 1 stop(); //停止总线
162 1 }
163
164 /*指定的地址写N(N<=8)字节数据*/
165 void WriteNByte (unsigned char addr,unsigned char *thedata,unsigned char n) //写入N个字到指定开始地址
166 {
167 1 unsigned char i; //计数器
168 1 start(); //开始总线
169 1 write8bit(WrDevAddr); //发送控制数据
170 1 testack(); //检查应答信息
171 1 write8bit(addr); //写入地址
172 1 testack();
173 1 for(i=0;i<n;i++) //循环写入第一页的数据
174 1 {
175 2 write8bit(*thedata); //写入数据
176 2 testack();
177 2 thedata++; //数据指针加1
178 2 }
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 4
179 1 stop(); //停止总线
180 1 }
181
182 /*指定地址,页写n个数据*/
183 void WritePages(unsigned char addr,unsigned char *thedata,unsigned char n)
184 {
185 1 unsigned char page=n/8;
186 1 unsigned char leave=n%8;
187 1 unsigned char i;
188 1 for(i=0;i<page;i++)
189 1 {
190 2 WriteOnePage((addr+8*i),thedata);
191 2 thedata=thedata+8;
192 2 }
193 1 WriteNByte((addr+8*page),thedata,leave);
194 1 }
195
196 /*指定地址,读取8字节,页读*/
197 void ReadoOnePage(unsigned char addr,unsigned char *thedata) //连续读取8个字节 页
198 {
199 1 unsigned char i;
200 1 start();
201 1 write8bit(WrDevAddr);
202 1 testack();
203 1 write8bit(addr);
204 1 testack();
205 1 start();
206 1 write8bit(RdDevAddr) ;
207 1 testack();
208 1 for(i=0;i<7;i++) //8字节
209 1 {
210 2 *thedata=read8bit(); //读取数据
211 2 sendack(); //发送应答,表示继续读取
212 2 thedata++; //数据指针加1
213 2 }
214 1 *thedata=read8bit(); //读取最后一个数据
215 1 sendNack(); //发送非应答,结束读取
216 1 stop(); //停止总线
217 1 }
218
219 /*连续读取N个字节*/
220 void ReadNByte (unsigned char addr,unsigned char *thedata,unsigned char n)
221 {
222 1 unsigned char i;
223 1 start();
224 1 write8bit(WrDevAddr);
225 1 testack();
226 1 write8bit(addr);
227 1 testack();
228 1 start();
229 1 write8bit(RdDevAddr) ;
230 1 testack();
231 1 for(i=0;i<n-1;i++) //n字节
232 1 {
233 2 *thedata=read8bit(); //读取数据
234 2 sendack(); //发送应答,表示继续读取
235 2 thedata++; //数据指针加1
236 2 }
237 1 *thedata=read8bit(); //读取最后一个数据
238 1 sendNack(); //发送非应答,结束读取
239 1 stop();
240 1 }
C51 COMPILER V8.08 IIC 09/30/2008 14:58:07 PAGE 5
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 768 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 38
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -