📄 24c04.lst
字号:
C51 COMPILER V8.05a 24C04 06/08/2008 20:18:03 PAGE 1
C51 COMPILER V8.05a, COMPILATION OF MODULE 24C04
OBJECT MODULE PLACED IN 24C04.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE 24C04.c LARGE BROWSE DEBUG OBJECTEXTEND
line level source
1 #include"AT24C04.h"
2
3 bit flag3;
4
5 unsigned int ucReceData;
6
7 void delay(void);
8
9 void ACK();
10 void NoACK();
11
12
13
14 /**************************************************************/
15 void delay(void)
16 {
17 1 unsigned int i;
18 1 for(i=100;i>0;i--)
19 1 _nop_();
20 1 }
21
22
23 /*********************************************************
24 **名称:I2C_Start
25 **功能:启动I2C
26 **输入:无
27 **返回:无
28 *********************************************************/
29 void I2C_Start()
30 {
31 1 SDA=1;
32 1 delay();
33 1 SCL=1;
34 1 delay();
35 1 SDA=0;
36 1 delay();
37 1 SCL=0; //钳位I2C总线,准备发送数据
38 1 }
39
40
41
42 /**********************************************************
43 **名称:I2C_Stop
44 **功能:停止I2C
45 **输入:无
46 **返回:无
47 **********************************************************/
48 void I2C_Stop()
49 {
50 1 SDA=0;
51 1 delay();
52 1 SCL=1;
53 1 delay();
54 1 SDA=1;
55 1 delay();
C51 COMPILER V8.05a 24C04 06/08/2008 20:18:03 PAGE 2
56 1 }
57
58
59
60
61 /**********************************************************
62 **名称:Ack
63 **功能:应答信号
64 **输入:无
65 **返回:无
66 **********************************************************/
67 void Ack()
68 {
69 1 SDA=0;
70 1 delay();
71 1 SCL=1;
72 1 delay();
73 1 SCL=0;
74 1 delay();
75 1 SDA=1;
76 1 delay();
77 1 }
78
79
80
81 /********************************************************
82 **名称:NoAck
83 **功能:发送非应答信号
84 **输入:无
85 **返回:无
86 ********************************************************/
87 void NoAck()
88 {
89 1 SDA=1;
90 1 delay();
91 1 SCL=1;
92 1 delay();
93 1 SCL=0;
94 1 delay();
95 1 SDA=0;
96 1 delay();
97 1 }
98
99
100
101
102 /********************************************************
103 **名称:Test_Ack()
104 **功能:检测应答位
105 **输入:无
106 **返回:flag3,有应答时flag3为0,无应答时flag3为1
107 *********************************************************/
108 bit Test_Ack()
109 {
110 1 SCL=0;
111 1 SDA=1;//读入数据
112 1 _nop_();_nop_();_nop_();_nop_();
113 1 SCL=1;
114 1 _nop_();_nop_();_nop_();_nop_();
115 1 if(SDA==0)
116 1 flag3=1;
117 1 else flag3=0;
C51 COMPILER V8.05a 24C04 06/08/2008 20:18:03 PAGE 3
118 1 SCL=0;
119 1 return(flag3);
120 1 }
121
122
123
124 /********************************************************
125 **名称:SendData()
126 **功能:发送一字节数据
127 **输入:buffer
128 **返回:
129 *******************************************************/
130 void SendData(unsigned int buffer)
131 {
132 1 unsigned int BitCnt=8;//一字节8位
133 1 unsigned int temp=0;
134 1 do
135 1 {
136 2 temp=buffer;
137 2 SCL=0;
138 2 delay();
139 2 if((temp&0x80)==0) //判断最高位是0还是1
140 2 SDA=0;
141 2 else
142 2 SDA=1;
143 2 delay();
144 2 SCL=1;
145 2 temp=_crol_(buffer,1);//将buffer中的数据左移一位
146 2 buffer=temp;
147 2 BitCnt--;
148 2 }
149 1 while(BitCnt);
150 1 SCL=0;
151 1 }
152
153 /**************************************************************
154 **名称:unsigned intReceiveData()
155 **功能:接收一字节数据
156 **输入:
157 **返回:ucReceData
158 **说明:将接收的数据存放在ucReceData中
159 **************************************************************/
160 unsigned int ReceiveData()
161 {
162 1 unsigned int BitCnt=8;
163 1 unsigned int temp=0;
164 1 SDA=1;//读入数据
165 1 do
166 1 {
167 2 SCL=0;
168 2 delay();
169 2 SCL=1;
170 2 delay();
171 2 if(SDA==1)
172 2 ucReceData=ucReceData|0x01; //低位置1
173 2 else
174 2 ucReceData=ucReceData&0x0fe; //低位清0
175 2 if(BitCnt-1)
176 2 {
177 3 temp=_crol_(ucReceData,1); //数据左移一位
178 3 ucReceData=temp;
179 3 }
C51 COMPILER V8.05a 24C04 06/08/2008 20:18:03 PAGE 4
180 2 BitCnt--;
181 2 }
182 1 while(BitCnt);
183 1 SCL=0;
184 1 return(ucReceData);
185 1 }
186
187 /*************************************************************
188 **名称:bit WriteNByte()
189 **功能:主机向24C02中写入多字节数据
190 **输入:
191 **返回:
192 **说明:sla-器件地址 suba-数据地址,*s-写入的数据,n-写入的字节数(n<=8)
193 **************************************************************/
194 bit WriteNByte(unsigned int sla,unsigned int suba,unsigned char *s,unsigned int n)
195 {
196 1 unsigned int i;
197 1 I2C_Start();//启动I2C
198 1 SendData(sla);//发送器件地址
199 1 Test_Ack();
200 1 if(flag3==0) return(0);
201 1 SendData(suba);
202 1 Test_Ack();
203 1 if(flag3==0) return(0);
204 1 for(i=0;i<n;i++)//写入8字节数据
205 1 {
206 2 SendData(*(s+i));
207 2 Test_Ack();
208 2 if(flag3==0) return(0);
209 2 }
210 1 I2C_Stop();
211 1 return(1);
212 1 }
213 /*************************************************************
214 **名称:bit ReadNByte()
215 **功能:主机从24C02中读出N字节数据(n<=8)
216 **输入:
217 **返回:
218 **说明:随机地址读操作
219 **************************************************************/
220 bit ReadNByte(unsigned int sla,unsigned int suba,unsigned char *p,unsigned int n)
221 {
222 1 unsigned int i;
223 1 I2C_Start();//启动I2C
224 1 SendData(sla);//发送器件地址
225 1 Test_Ack();
226 1 if(flag3==0) return(0);
227 1 SendData(suba);//发送器件内部地址
228 1 Test_Ack();
229 1 if(flag3==0) return(0);
230 1
231 1 I2C_Start();
232 1 SendData(sla+1);
233 1 Test_Ack();
234 1 if(flag3==0) return(0);
235 1 for(i=0;i<n-1;i++)//读取字节数据
236 1 {
237 2 *(p+i)=ReceiveData();//读取数据
238 2 ACK();
239 2 }
240 1 *(p+n-1)=ReceiveData();
241 1 //*(p+n)='\0';
C51 COMPILER V8.05a 24C04 06/08/2008 20:18:03 PAGE 5
242 1 NoACK();
243 1 I2C_Stop();
244 1 return(1);
245 1 }
246
247
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 592 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 2 24
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
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 + -