📄 i2c.lst
字号:
C51 COMPILER V6.20c I2C 06/21/2007 21:37:54 PAGE 1
C51 COMPILER V6.20c, COMPILATION OF MODULE I2C
OBJECT MODULE PLACED IN I2C.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE I2C.C BROWSE DEBUG OBJECTEXTEND
stmt level source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <absacc.h>
6 #include <float.h>
7 #include <INTRINS.h>
8 #include"my.h"
9 #define uchar unsigned char
10 #define uint unsigned int
11 #define OK 0 //0 - respond
12 #define NOK 1 //1 - no respond
13
14 sbit SDA =P0^0;// sda p00
15 sbit SCL =P0^1;// scl p01
16 sbit WP =P0^2;// /wc p02
17
18 #define SCL_HIGH; SCL=1;\
19 _nop_();\
20 _nop_();
21 #define SCL_LOW; SCL=0;\
22 _nop_();\
23 _nop_();
24 #define SDA_HIGH; SDA=1;\
25 _nop_();\
26 _nop_();
27 #define SDA_LOW; SDA=0;\
28 _nop_();\
29 _nop_();
30
31 /**************************************************
32 串口0队列 pc
33 //使用w77e58 片内1k SRAM, 要定义为xdata ,
34 //为确保使用的是片内1k RAM, 最好和 _at_ 0x0000;联合使用
35 **************************************************/
36
37 extern unsigned char xdata xBuf_0[130];
38 extern unsigned char xdata *Seri0_ReadSendPoint;
39 extern unsigned char xdata *Seri0_WriteSendPoint;
40 extern unsigned char data Seri0_SendNum;
41 extern unsigned char xdata *Seri0_ReadReciPoint;
42 extern unsigned char xdata *Seri0_WriteReciPoint;
43 extern unsigned char data Seri0_ReciNum;
44
45 _Delay(unsigned char a){
46 1 unsigned int bb;
47 1 for(;a>0;a--){
48 2 bb=275;
49 2 for(;bb>0;bb--);
50 2 }
51 1 }
52
53 /**********************************************************
54 Function : I2CStart
55 Description : I2C bus start signal
C51 COMPILER V6.20c I2C 06/21/2007 21:37:54 PAGE 2
56 Input : none
57 Output : none
58 ***********************************************************/
59 void I2CStart(void)
60 {
61 1 SDA_HIGH;
62 1 SCL_HIGH;
63 1 SDA_LOW;
64 1 SCL_LOW;
65 1 }
66
67 /**********************************************************
68 Function : I2CStop
69 Description : I2C bus stop signal
70 Input : none
71 Output : none
72 ***********************************************************/
73 void I2CStop(void)
74 {
75 1 SDA_LOW;
76 1 SCL_HIGH;
77 1 SDA_HIGH;
78 1 SCL_LOW;
79 1 }
80 /**********************************************************
81 Function : WaitAck for check ack
82 Description : wait I2C slave to respond ack
83 Input : none
84 Output : 0 - respond; 1 - no respond
85 ***********************************************************/
86 unsigned char WaitAck(void)
87 {
88 1 unsigned char bAck;
89 1
90 1 SDA_HIGH;
91 1 SCL_HIGH;
92 1 if(SDA==0)
93 1 bAck=OK;
94 1 else
95 1 bAck=NOK;
96 1 SCL_LOW;
97 1 return bAck;
98 1 }
99 /**********************************************************
100 Function : SendAck
101 Description : send ack to I2C slave
102 Input : none
103 Output : none
104 ***********************************************************/
105 void SendAck(void)
106 {
107 1 SDA_LOW;
108 1 SCL_HIGH;
109 1 SCL_LOW;
110 1 SDA_HIGH;
111 1 }
112 /**********************************************************
113 Function : SendNak
114 Description : send not ack signal to I2C slave
115 Input : none
116 Output : none
117 ***********************************************************/
C51 COMPILER V6.20c I2C 06/21/2007 21:37:54 PAGE 3
118 void SendNak(void)
119 {
120 1 SDA_HIGH;
121 1 SCL_HIGH;
122 1 SCL_LOW;
123 1 SDA_LOW;
124 1 }
125
126 /**********************************************************
127 Function : I2CSendByte
128 Description : send a byte to I2C slave
129 Input : data - the byte to be sent
130 Output : none
131 ***********************************************************/
132 void I2CSendByte(unsigned char bChar)
133 {
134 1 unsigned char i;
135 1 for(i=0;i<8;i++)
136 1 {
137 2 bChar<<=1;
138 2 if(CY){SDA_HIGH}
139 2 else {SDA_LOW}
140 2 SCL_HIGH;
141 2 SCL_LOW;
142 2 }
143 1 }
144
145 /**********************************************************
146 Function : I2CReceiveByte
147 Description : Receive a byte from I2C slave
148 Input : none
149 Output : the byte received
150 ***********************************************************/
151
152 unsigned char I2CReceiveByte(void)
153 {
154 1 unsigned char i,bChar;
155 1
156 1 bChar=0;
157 1 SDA=1; //input init
158 1 for(i=0;i<8;i++)
159 1 {
160 2
161 2 SCL_HIGH;
162 2 bChar<<=1;
163 2 if(SDA) bChar++;
164 2 SCL_LOW;
165 2 }
166 1 return bChar;
167 1 }
168 /**/
169 /*
170 功能:I2C芯片初始化
171 输入:none
172 输出: none
173 注意:
174 */
175 void vInitI2C(void)
176 {
177 1 WP=0;
178 1 SCL_LOW;
179 1 SDA_LOW;
C51 COMPILER V6.20c I2C 06/21/2007 21:37:54 PAGE 4
180 1 }
181
182 /*************************************************
183 功能: 读I2C
184 输入:
185 Sla: 芯片地址(A0:EEPROM1;A4:EEPROM2)
186 Address: 片内首地址
187 Send_Data: 数据地址
188 len: 长度(<=128)
189 输出: 0:正确; 1: 错误
190 注意:
191 unsigned char IRcvStr( unsigned char Sla,unsigned int Address ,
192 unsigned char *RcvData ,unsigned char len)
193 **************************************************/
194 unsigned char IRcvStr( unsigned char Sla,unsigned int Address ,
195 unsigned char *RcvData ,unsigned char len)
196 {
197 1 unsigned char i;
198 1
199 1 I2CStart();
200 1 I2CSendByte(Sla); //SEND DEVICE ADDRESS
201 1 if(WaitAck())
202 1 goto I2CReadError;
203 1 // return (1);
204 1 I2CSendByte( (uchar)(Address>>8) ); //SEND ADDRESS HIGH
205 1 if(WaitAck())
206 1 goto I2CReadError;
207 1 // return (2);
208 1 I2CSendByte( (uchar)Address ); //SEND ADDRESS LOW
209 1 if(WaitAck())
210 1 goto I2CReadError;
211 1 // return (3);
212 1
213 1 I2CStart();
214 1 Sla|=0x05; //read bit=1 Sla|=0x01
215 1 I2CSendByte(Sla); //SEND DEVICE ADDRESS
216 1 if(WaitAck())
217 1 goto I2CReadError;
218 1 // return (4);
219 1 for(i=0;i<len;i++)
220 1 {
221 2 RcvData[i]=I2CReceiveByte(); //SEND DATA
222 2 if(i==len-1)
223 2 SendNak(); //发送完最后字节,发送NAK
224 2 else
225 2 SendAck();
226 2 }
227 1 I2CStop();
228 1 return OK;
229 1 I2CReadError:
230 1 I2CStop();
231 1 return NOK;
232 1 }
233
234 /********************************************************
235 功能: 页写I2C
236 输入:
237 Sla: 芯片地址(A0:EEPROM1;A4:EEPROM2)
238 Address: 片内首地址
239 Send_Data: 数据源地址
240 len: 数据长度(<=128)
241 输出: 0:正确; 1: 时序错误; 2:写入的数据读出后不对
C51 COMPILER V6.20c I2C 06/21/2007 21:37:54 PAGE 5
242 注意: 不能跨页
243 *********************************************************/
244 unsigned char ISendStr( unsigned char Sla,unsigned int Address ,
245 unsigned char *Send_Data ,unsigned char len )
246 {
247 1 unsigned char i;
248 1 WP=0;
249 1 I2CStart();
250 1 I2CSendByte(Sla); //SEND DEVICE ADDRESS
251 1 if(WaitAck())
252 1 goto ErrorI2CWrite;
253 1 I2CSendByte( (uchar)(Address>>8) ); //SEND ADDRESS HIGH
254 1 if(WaitAck())
255 1 goto ErrorI2CWrite;
256 1 I2CSendByte( (uchar)Address ); //SEND ADDRESS LOW
257 1 if(WaitAck())
258 1 goto ErrorI2CWrite;
259 1 for(i=0;i<len;i++)
260 1 {
261 2 I2CSendByte(Send_Data[i]); //SEND DATA
262 2 if(WaitAck())
263 2 goto ErrorI2CWrite;
264 2 }
265 1 I2CStop();
266 1
267 1 for(;;) //查询是否写完
268 1 {
269 2 I2CStart();
270 2 I2CSendByte(Sla);
271 2 if(!WaitAck())
272 2 break;
273 2 }
274 1 WP=0;
275 1 _Delay(10);
276 1 /*
277 1 for(i=0;i<len;i++)
278 1 {
279 1 IRcvStr(Sla, Address ,&bI2CReadData ,1); //写后读
280 1 Address++;
281 1 if( bI2CReadData!=Send_Data[i] )
282 1 goto ErrorI2CWrite;
283 1 }
284 1 */
285 1 _Delay(100);
286 1 return OK;// return 0;
287 1
288 1 ErrorI2CWrite:
289 1 I2CStop();
290 1 WP=0;
291 1 _Delay(100);
292 1 return NOK;// return 1;
293 1 }
294
295 extern union d{
296 INT16U u16;
297 INT8U u8[2];
298 }m2;
299 #define pageaddr m2.u16
300 #define pageaddrH m2.u8[0]
301 #define pageaddrL m2.u8[1]
302
303 extern unsigned char tmp;
C51 COMPILER V6.20c I2C 06/21/2007 21:37:54 PAGE 6
304 unsigned char Sla;
305
306 /*
307 (my页)=0页~2047页(0页~0x7FF页)
308 AT24c512 Device Select Code
309 -------------------------------------------------------------------------------------
310 | Device Type Identifier | Chip Enable Address | R/W |
311 | b7 b6 b5 b4 | b3 b2 b1 | b0 |
312 -------------------------------------------------------------------------------------
313 Device Select Code| 1 0 1 0 | 0 E2 E1 | R/W |
314 -------------------------------------------------------------------------------------
315 Note: 1. The most significant bit, b7, is sent first.
316
317 */
318 Set_Sla(){
319 1 Sla=(pageaddrH & 0x6);
320 1 //if((Sla=0)||(Sla=2))Sla=4;
321 1 Sla|=0xA0;
322 1 pageaddr<<=7;
323 1 }
324
325 Pc2eep2pc()
326 {
327 1 pageaddrH=xBuf_0[0];//页号
328 1 pageaddrL=xBuf_0[1];
329 1 Set_Sla();
330 1 tmp=ISendStr( Sla, pageaddr, xBuf_0+2, 128);//写入
331 1 //xBuf_0[0]=tmp;
332 1 //读eep操作:
333 1 //if(tmp!=0){显示"Pc2eep2pc"}
334 1 for(tmp=2;tmp<130;tmp++)xBuf_0[tmp]=0;
335 1 //Set_Sla();
336 1 tmp=IRcvStr(Sla, pageaddr ,xBuf_0+2 ,128); //读出
337 1 //xBuf_0[1]=tmp;
338 1 }
339
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 520 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 1 14
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -