i2c_24c02.lst
来自「Keil C51单片机基础实验源程序」· LST 代码 · 共 339 行
LST
339 行
C51 COMPILER V7.06 I2C_24C02 01/11/2007 20:52:06 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE I2C_24C02
OBJECT MODULE PLACED IN I2C_24C02.OBJ
COMPILER INVOKED BY: D:\keil\C51\BIN\C51.EXE I2C_24C02.C BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /***********************************************************
2 **模块名称:24C02的读写
3 **编写人:bradley 日期 2005-5-1
4 **修改人:psp 日期 2006-10-27
5 **功能描述:24C02储存开机次数实验
6 **该试验功能是单片机复位一次, 自动从24C02中读取数据
7 **然后加1,最终数码管中的数据就是开机的次数,具有一定的实用意义
8 **烧写后用手按复位键可以看到数码管每按一下加一,也可以断电再开机
9 **********************************************************/
10
11 #include <reg52.h>
12 #include <intrins.h>
13 #define uchar unsigned char
14 #define uint unsigned int
15
16 code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
17 0x82,0xf8,0x80,0x90,0xff}; //共阳数码管段码
18 sbit SDA=P3^5; //定义数据线
19 sbit SCL=P3^4; //定义时钟线
20 bit flag;
21
22 uint idata ucSendBuffer[1]=0;
23 uint idata ucReceData;
24
25 void delay(void);
26 void delay_10ms(void);
27
28 void ACK();
29 void NoACK();
30
31
32
33 /**************************************************************/
34 void delay(void)
35 {
36 1 uint i;
37 1 for(i=100;i>0;i--)
38 1 _nop_();
39 1 }
40
41 void delay1ms()
42 {
43 1 uchar i;
44 1 for(i=124;i>0;i--); //延时124*8+10=1002us
45 1 }
46
47 /*********************************************************
48 **名称:I2C_Start
49 **功能:启动I2C
50 **输入:无
51 **返回:无
52 *********************************************************/
53 void I2C_Start()
54 {
55 1 SDA=1;
C51 COMPILER V7.06 I2C_24C02 01/11/2007 20:52:06 PAGE 2
56 1 delay();
57 1 SCL=1;
58 1 delay();
59 1 SDA=0;
60 1 delay();
61 1 SCL=0; //钳位I2C总线,准备发送数据
62 1 }
63
64
65
66 /**********************************************************
67 **名称:I2C_Stop
68 **功能:停止I2C
69 **输入:无
70 **返回:无
71 **********************************************************/
72 void I2C_Stop()
73 {
74 1 SDA=0;
75 1 delay();
76 1 SCL=1;
77 1 delay();
78 1 SDA=1;
79 1 delay();
80 1 }
81
82
83
84
85 /**********************************************************
86 **名称:Ack
87 **功能:应答信号
88 **输入:无
89 **返回:无
90 **********************************************************/
91 void Ack()
92 {
93 1 SDA=0;
94 1 delay();
95 1 SCL=1;
96 1 delay();
97 1 SCL=0;
98 1 delay();
99 1 SDA=1;
100 1 delay();
101 1 }
102
103
104
105 /********************************************************
106 **名称:NoAck
107 **功能:发送非应答信号
108 **输入:无
109 **返回:无
110 ********************************************************/
111 void NoAck()
112 {
113 1 SDA=1;
114 1 delay();
115 1 SCL=1;
116 1 delay();
117 1 SCL=0;
C51 COMPILER V7.06 I2C_24C02 01/11/2007 20:52:06 PAGE 3
118 1 delay();
119 1 SDA=0;
120 1 delay();
121 1 }
122
123
124
125
126 /********************************************************
127 **名称:Test_Ack()
128 **功能:检测应答位
129 **输入:无
130 **返回:flag,有应答时flag为0,无应答时flag为1
131 *********************************************************/
132 bit Test_Ack()
133 {
134 1 SCL=0;
135 1 SDA=1;//读入数据
136 1 _nop_();_nop_();_nop_();_nop_();
137 1 SCL=1;
138 1 _nop_();_nop_();_nop_();_nop_();
139 1 if(SDA==0)
140 1 flag=1;
141 1 else flag=0;
142 1 SCL=0;
143 1 return(flag);
144 1 }
145
146
147
148 /********************************************************
149 **名称:SendData()
150 **功能:发送一字节数据
151 **输入:buffer
152 **返回:
153 *******************************************************/
154 void SendData(uint buffer)
155 {
156 1 uint BitCnt=8;//一字节8位
157 1 uint temp=0;
158 1 do
159 1 {
160 2 temp=buffer;
161 2 SCL=0;
162 2 delay();
163 2 if((temp&0x80)==0) //判断最高位是0还是1
164 2 SDA=0;
165 2 else
166 2 SDA=1;
167 2 delay();
168 2 SCL=1;
169 2 temp=_crol_(buffer,1);//将buffer中的数据左移一位
170 2 buffer=temp;
171 2 BitCnt--;
172 2 }
173 1 while(BitCnt);
174 1 SCL=0;
175 1 }
176
177 /**************************************************************
178 **名称:uint ReceiveData()
179 **功能:接收一字节数据
C51 COMPILER V7.06 I2C_24C02 01/11/2007 20:52:06 PAGE 4
180 **输入:
181 **返回:ucReceData
182 **说明:将接收的数据存放在ucReceData中
183 **************************************************************/
184 uint ReceiveData()
185 {
186 1 uint BitCnt=8;
187 1 uint temp=0;
188 1 SDA=1;//读入数据
189 1 do
190 1 {
191 2 SCL=0;
192 2 delay();
193 2 SCL=1;
194 2 delay();
195 2 if(SDA==1)
196 2 ucReceData=ucReceData|0x01; //低位置1
197 2 else
198 2 ucReceData=ucReceData&0x0fe; //低位清0
199 2 if(BitCnt-1)
200 2 {
201 3 temp=_crol_(ucReceData,1); //数据左移一位
202 3 ucReceData=temp;
203 3 }
204 2 BitCnt--;
205 2 }
206 1 while(BitCnt);
207 1 SCL=0;
208 1 return(ucReceData);
209 1 }
210
211 /*************************************************************
212 **名称:bit WriteNByte()
213 **功能:主机向24C02中写入多字节数据
214 **输入:
215 **返回:
216 **说明:sla-器件地址 suba-数据地址,*s-写入的数据,n-写入的字节数(n<=8)
217 **************************************************************/
218 bit WriteNByte(uint sla,uint suba,uint *s,uint n)
219 {
220 1 uint i;
221 1 I2C_Start();//启动I2C
222 1 SendData(sla);//发送器件地址
223 1 Test_Ack();
224 1 if(flag==0) return(0);
225 1 SendData(suba);
226 1 Test_Ack();
227 1 if(flag==0) return(0);
228 1 for(i=0;i<n;i++)//写入8字节数据
229 1 {
230 2 SendData(*(s+i));
231 2 Test_Ack();
232 2 if(flag==0) return(0);
233 2 }
234 1 I2C_Stop();
235 1 return(1);
236 1 }
237 /*************************************************************
238 **名称:bit ReadNByte()
239 **功能:主机从24C02中读出N字节数据(n<=8)
240 **输入:
241 **返回:
C51 COMPILER V7.06 I2C_24C02 01/11/2007 20:52:06 PAGE 5
242 **说明:随机地址读操作
243 **************************************************************/
244 bit ReadNByte(uint sla,uint suba,uint *p,uint n)
245 {
246 1 uint i;
247 1 I2C_Start();//启动I2C
248 1 SendData(sla);//发送器件地址
249 1 Test_Ack();
250 1 if(flag==0) return(0);
251 1 SendData(suba);//发送器件内部地址
252 1 Test_Ack();
253 1 if(flag==0) return(0);
254 1
255 1 I2C_Start();
256 1 SendData(sla+1);
257 1 Test_Ack();
258 1 if(flag==0) return(0);
259 1 for(i=0;i<n-1;i++)//读取字节数据
260 1 {
261 2 *(p+i)=ReceiveData();//读取数据
262 2 ACK();
263 2 }
264 1 *(p+n-1)=ReceiveData();
265 1
266 1 NoACK();
267 1 I2C_Stop();
268 1 return(1);
269 1 }
270 /***************************************************************
271 **名称:main()
272 **功能:
273 **输入:
274 **返回:
275 **说明:
276 ****************************************************************/
277 void main()
278 {
279 1 ReadNByte(0xa0,0x00,ucSendBuffer,1);
280 1 ucSendBuffer[0]++;
281 1
282 1 WriteNByte(0xa0,0x00,ucSendBuffer,1);
283 1
284 1 while(1)
285 1 {
286 2 P1=0xfd; //P1.1=0,选通第二位
287 2 P2=seg7code[ucSendBuffer[0]%1000/100]; //百位
288 2 delay1ms();
289 2 P2=0xff; //消隐
290 2
291 2 P1=0xfb; //P1.3=0,选通第三位
292 2 P2=seg7code[ucSendBuffer[0]%100/10]; //十位
293 2 delay1ms();
294 2 P2=0xff; //消隐
295 2
296 2 P1=0xf7; //P1.3=0,选通第四位
297 2 P2=seg7code[ucSendBuffer[0]%10]; //个位
298 2 delay1ms();
299 2 P2=0xff; //消隐
300 2
301 2 }
302 1
303 1
C51 COMPILER V7.06 I2C_24C02 01/11/2007 20:52:06 PAGE 6
304 1
305 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 700 ----
CONSTANT SIZE = 11 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 22
IDATA SIZE = 4 ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?