📄 eeprom1.lst
字号:
C51 COMPILER V6.12 EEPROM1 07/07/2007 13:55:08 PAGE 1
C51 COMPILER V6.12, COMPILATION OF MODULE EEPROM1
OBJECT MODULE PLACED IN .\eeprom1.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE .\eeprom1.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 #include<reg52.h>
2 #include"port.h"
3 #include"main.h"
4 #include"eeprom.h"
5 #include<intrins.h>
6 bit SystemError;
7 uChar sendbuf[10];
8 uChar receivebuf[10];
9
10 void iic_start(void)
11 {
12 1 // EA=0;//2006.8.2del
13 1 SDA=1;
14 1 SCL=1;
15 1 // delayNOP();//2006.8.2del
16 1 SDA=0;
17 1 // delayNOP();//2006.8.2del
18 1 SCL=0;
19 1 }
20 void iic_stop(void)
21 {
22 1 // SDA=0;//2006.8.2del
23 1 SCL=1;
24 1 // delayNOP();//2006.8.2del
25 1 //SDA=1;//2006.8.2del
26 1 // delayNOP();//2006.8.2del
27 1 // SCL=0;//2006.8.2del
28 1 SDA=0;//2006.8.2add
29 1 SDA=1;//2006.8.2add
30 1 }
31
32 void slave_ACK(void)
33 {
34 1 uChar i=0;
35 1 SDA=1;
36 1 do
37 1 {
38 2 SCL = 0;
39 2 SCL = 1;
40 2 }
41 1 while((SDA) && (i++ < 100));//EepromScl=1:if EepromSda=0,exit
42 1 // while(SDA);
43 1 //SDA=0; //clear EepromSda and EepromScl
44 1 SCL=0;
45 1 }
46
47 void slave_NOACK(void)
48 {
49 1 SDA=1;
50 1 SCL=1;
51 1 SCL=0;
52 1 }
53
54 void check_ACK(void)
55 {
C51 COMPILER V6.12 EEPROM1 07/07/2007 13:55:08 PAGE 2
56 1 SDA=1;
57 1 SCL=1;
58 1 F0=0;
59 1 if(SDA==1)
60 1 F0=1;
61 1 SCL=0;
62 1 }
63
64 void IICSendByte(uChar ch)//发送一个byte 到eeprom
65 {
66 1 uChar n,j;
67 1 j=ch;
68 1 for(n=0;n<8;n++)
69 1 {
70 2 if((j&0x80)==0x80)
71 2 {
72 3 SDA=1;
73 3
74 3 }
75 2 else
76 2 {
77 3 SDA=0;
78 3
79 3 }
80 2 SCL=1;
81 2 SCL=0;
82 2 j=j<<1;
83 2 }
84 1 slave_ACK();//从机应答
85 1 }
86 uChar IICreceiveByte(void)
87 {
88 1 uChar n;
89 1 uChar tdata;
90 1 for(n=0;n<8;n++)
91 1 //while(n--)//2006.8.2change
92 1 {
93 2 SDA=1;
94 2 SCL=1;
95 2 tdata=tdata<<1;
96 2 if(SDA==1)
97 2 tdata=tdata|0x01;
98 2 else
99 2 tdata=tdata&0xfe;
100 2 SCL=0;
101 2 }
102 1 SDA=0;//2006.8.2add
103 1 return(tdata);
104 1 }
105
106 void writeNByte(uInt slave_add,uChar n)//Write n Byte to eeprom 2006.8.3
107 {
*** WARNING C235 IN LINE 107 OF .\EEPROM1.C: parameter 1: different types
108 1 union u {uInt word; struct{uChar hi; uChar lo;} bytes;};
109 1 union u eepromAdd;
110 1 uChar send_da,i=0;
111 1 eepromAdd.word=slave_add; //注意该处地址连续,对于24C16地址有效范围为0x0000 to
-0x07ff 共2K
112 1 eepromAdd.bytes.hi=(eepromAdd.bytes.hi&0x07)<<1;
113 1 send_da=0;
114 1 iic_start();
115 1 IICSendByte(EEPROMWRCMD|eepromAdd.bytes.hi);
C51 COMPILER V6.12 EEPROM1 07/07/2007 13:55:08 PAGE 3
116 1 IICSendByte(eepromAdd.bytes.lo);//write frist word address
117 1 while(n--)
118 1 {
119 2 // send_da=sendbuf[i++];
120 2 i+=3;
121 2 send_da=i;
122 2 IICSendByte(send_da);
123 2
124 2 }
125 1 iic_stop();
126 1 }
127
128 void receiveNByte(uChar slave_add,uChar n)
129 {
130 1 uChar receive_da,i=0;
131 1 iic_start();
132 1 IICSendByte(slave_add);
133 1 check_ACK();
134 1 if(F0==1)
135 1 {
136 2 SystemError=1;
137 2 return;
138 2 }
139 1 while(n--)
140 1 {
141 2 receive_da=IICreceiveByte();
142 2 receivebuf[i++]=receive_da;
143 2 slave_ACK();
144 2 }
145 1 slave_NOACK();
146 1 iic_stop();
147 1 }
148 uChar EepromRead(uInt rdAddress) //read a byte subprogram (8 bit)
149 {
150 1 union u {uInt word; struct {uChar hi; uChar lo;} bytes;};
151 1 union u eepromAdd;
152 1 uChar rdData;
153 1 eepromAdd.word=rdAddress; //get address
154 1 eepromAdd.bytes.hi=(eepromAdd.bytes.hi&0x07)<<1;//get the low three bits and shift move one bit
155 1 iic_start();//produce start condition
156 1 IICSendByte(EEPROMWRCMD|eepromAdd.bytes.hi);//write eeprom write command
157 1 IICSendByte(eepromAdd.bytes.lo);//write frist word address
158 1 //EepromWrByte(EEPROMWRCMD|eepromAdd.bytes.hi);//write eeprom write command
159 1 //EepromWrByte(eepromAdd.bytes.lo);//write frist word address
160 1 iic_start();//produce start condition
161 1 IICSendByte(EEPROMRDCMD|eepromAdd.bytes.hi);
162 1 //EepromWrByte(EEPROMRDCMD|eepromAdd.bytes.hi); //write eeprom write command
163 1 rdData=IICreceiveByte(); //read a byte from EEPROM
164 1 slave_NOACK();
165 1 //EepromNoAck();
166 1 iic_stop();
167 1 //EepromStop();
168 1 return rdData; //return
169 1 }
170
171 void EepromWrite(uInt wrAddress,uChar wrData)
172 {
173 1 union u {uInt word; struct{uChar hi; uChar lo;} bytes;};
174 1 union u eepromAdd;
175 1 uChar i = 0;
176 1 eepromAdd.word=wrAddress; //注意该处地址连续,对于24C16地址有效范围为0x0000 to
-0x07ff 共2K
C51 COMPILER V6.12 EEPROM1 07/07/2007 13:55:08 PAGE 4
177 1 eepromAdd.bytes.hi=(eepromAdd.bytes.hi&0x07)<<1; //将地址拆分,符合24系列的地址格式
178 1 if(wrData!=(EepromRead(wrAddress))) //比较写入的内容与读出的内容是否一致
179 1 {
180 2 do
181 2 {
182 3 //EepromStart();
183 3 iic_start();
184 3 IICSendByte(EEPROMWRCMD|eepromAdd.bytes.hi); //产生报文,包含页地址,操作方式
185 3 //slave_ACK();
186 3 IICSendByte(eepromAdd.bytes.lo);
187 3 //slave_ACK();
188 3 IICSendByte(wrData);
189 3 // slave_ACK();
190 3 //EepromStop();
191 3 iic_stop();
192 3 EepromAckPolling(EEPROMWRCMD|eepromAdd.bytes.hi); //轮询
193 3 }
194 2 while((wrData!=(EepromRead(wrAddress))) && (i++ < 2)); //效验写入的值
195 2 }
196 1 }
197 void EepromAckPolling(uChar device)
198 {
199 1 uChar i,j,k = 0; //define variable
200 1 do
201 1 {
202 2 iic_start();
203 2 //EepromStart (); //produce start condition
204 2 j=device;
205 2 for(i=0;i<8;i++) //write 8 bit
206 2 {
207 3 if((j&0x80)==0)
208 3 {
209 4 SDA=0; //get the highest bit
210 4 }
211 3 else
212 3 {
213 4 SDA=1;
214 4 }
215 3 SCL=1;
216 3 SCL=0;
217 3 j=j<<1; //move left wrTemp one bit
218 3 }
219 2 SDA=1;
220 2 SCL=1;
221 2 }
222 1 while((SDA) && (k++ < 100)); //EepromSda=0:exit
223 1 SCL=0;
224 1 SDA=0;
225 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 408 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 20 10
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -