EEPROM 24c02 [Read storage multiple bytes]

Publisher:蓝天飞行Latest update time:2017-01-15 Source: eefocusKeywords:EEPROM  24c02 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First store the data, then read it out and display it on the digital tube.

In addition to the connections defined in the code, p0 must also be connected to pin 8 of the 8-bit digital tube.

  1 /*-----------------------------------------------

  2 Name: IIC protocol EEPROM24c02

  3 Content: This program is used to test the performance of EEPROM. The test method is as follows: write some data to 24c02, then clear the data in the memory.

  4 After power failure, the main memory will lose this information, and then load this data from 24c02 to see if it is the same as written.

  5 The function uses software delay to generate SCL pulses, so some modifications must be made to the high crystal frequency.... (This example is a 1us machine

  6 cycles, that is, the crystal frequency must be less than 12MHZ)

  7------------------------------------------------* /  

  8 #include //Include header file

  9 #include

 10 

 11 #define _Nop() _nop_() //Define null instruction

 12 

 13 // Constant, variable definition area

 14 unsigned char code dofly_DuanMa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,

 15 0x77,0x7c,0x39,0x5e,0x79,0x71}; // Display segment code value 0~F

 16 unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // respectively correspond to the corresponding digital tube light up, that is, the bit code

 17 

 18 sbit LATCH1=P2^2;

 19 sbit LATCH2=P2^3;

 20 

 21 sbit SDA=P2^1; //Simulate I2C data transmission bit

 22 sbit SCL=P2^0; //Simulated I2C clock control bit

 twenty three 

 24 bit ack; //Response flag

 25 

 26 void DelayUs2x(unsigned char t); //Function declaration 

 27 void DelayMs(unsigned char t);

 28 /*-------------------------------------------------- -

 29 uS delay function, with input parameter unsigned char t, no return value

 30 unsigned char is defined as an unsigned character variable, and its value range is

 31 0~255 Here we use a 12M crystal oscillator. Please use assembly for accurate delay. Approximate delay

 32 The length is as follows: T=tx2+5 uS 

 33------------------------------------------------* /

 34 void DelayUs2x(unsigned char t)

 35 {   

 36 while(--t);

 37 }

 38 /*-------------------------------------------------- -

 39 mS delay function, with input parameter unsigned char t, no return value

 40 unsigned char is defined as an unsigned character variable, and its value range is

 41 0~255 Here we use a 12M crystal oscillator. Please use assembly for accurate delay.

 42------------------------------------------------* /

 43 void DelayMs(unsigned char t)

 44 {

 45     

 46 while(t--)

 47 {

 48 //About 1mS delay

 49 DelayUs2x(245);

 50 DelayUs2x(245);

 51 }

 52 }

 53 /*-------------------------------------------------- -

 54 Start bus

 55------------------------------------------------* /

 56 void Start_I2c()

 57 {

 58 SDA=1; //Send the data signal of the start condition

 59 _Nop();

 60 SCL=1;

 61 _Nop(); //Start condition establishment time is greater than 4.7us, delay

 62 _Nop();

 63 _Nop();

 64 _Nop();

 65 _Nop();    

 66 SDA=0; //Send start signal

 67 _Nop(); //Start condition lock time is greater than 4μ

 68 _Nop();

 69 _Nop();

 70 _Nop();

 71 _Nop();       

 72 SCL=0; //Clamp the I2C bus and prepare to send or receive data

 73 _Nop();

 74 _Nop();

 75 }

 76 /*-------------------------------------------------- -

 77 End bus

 78------------------------------------------------* /

 79 void Stop_I2c()

 80 {

 81 SDA=0; //Send the data signal of the end condition

 82 _Nop(); //Send the clock signal of the end condition

 83 SCL=1; //End condition establishment time is greater than 4μ

 84 _Nop();

 85 _Nop();

 86 _Nop();

 87 _Nop();

 88 _Nop();

 89 SDA=1; //Send I2C bus end signal

 90 _Nop();

 91 _Nop();

 92 _Nop();

 93 _Nop();

 94 }

 95 /*-------------------------------------------------- ------------------

 96 Bytes Data Transfer Function               

 97 Function prototype: void SendByte(unsigned char c);

 98 Function: Send data c, which can be an address or data, wait for a response after sending, and

 99 This status bit operates. (No response or non-response makes ack=0 false)     

100 Data transmission is normal, ack=1; ack=0 means the controlled device has no response or is damaged.

101 -------------------------------------------------- ------------------*/

102 void SendByte(unsigned char c)

103 {

104 unsigned char BitCnt;

105     

106 for(BitCnt=0;BitCnt<8;BitCnt++) //The length of the data to be transmitted is 8 bits

107 {

108 if((c<

109 else SDA=0;                

110 _Nop();

111 SCL=1; //Set the clock line high to notify the controlled device to start receiving data bits

112 _Nop(); 

113 _Nop(); // Ensure that the clock high level period is greater than 4μ

114 _Nop();

115 _Nop();

116 _Nop();         

117 SCL=0; 

118 }

119 _Nop();

120 _Nop();

121 SDA=1; //Release the data line after sending 8 bits and prepare to receive the response bit

122 _Nop();

123 _Nop();   

124 SCL=1;

125 _Nop();

126 _Nop();

127 _Nop();

128 if(SDA==1)ack=0;     

129 else ack=1; //Judge whether the response signal is received

130 SCL=0;

131 _Nop();

132 _Nop();

133 }

134 /*-------------------------------------------------- ------------------

135 Byte data transfer function               

136 Function prototype: unsigned char RcvByte();

137 Function: Used to receive data from the device and determine bus errors (no response signal is sent).

138 Please use the response function after sending.  

139 -------------------------------------------------- ------------------*/    

140 unsigned char RcvByte()

141 {

142 unsigned char retc;

143 unsigned char BitCnt;

144     

145 retc=0; 

146 SDA=1; //Set the data line to input mode

147 for(BitCnt=0;BitCnt<8;BitCnt++)

148 {

149 _Nop();           

150 SCL=0; //Set the clock line to low, ready to receive data bits

151 _Nop();

152 _Nop(); //Clock low level period is greater than 4.7us

153 _Nop();

154 _Nop();

155 _Nop();

156 SCL=1; //Set the clock line high to make the data on the data line valid

157 _Nop();

158 _Nop();

159 retc=retc<<1;

160 if(SDA==1)retc=retc+1; //Read data bit, and put the received data bit into retc

161 _Nop();

162 _Nop(); 

163 }

164 SCL=0;    

165 _Nop();

166 _Nop();

167 return(retc);

168 }

169 /*-------------------------------------------------- ------------------

170 Response subfunction

171 Prototype: void Ack_I2c(void);

172 -------------------------------------------------- ---------------*/

173 void Ack_I2c(void)

174 {

175 SDA=0;     

176 _Nop();

177 _Nop();

178 _Nop();      

179 SCL=1;

180 _Nop();

181 _Nop(); //Clock low level period is greater than 4μ

182 _Nop();

183 _Nop();

184 _Nop();  

185 SCL=0; //Clear the clock line and clamp the I2C bus to continue receiving

186 _Nop();

187 _Nop();    

188 }

189 /*-------------------------------------------------- ------------------

190 Non-response subfunction

191 Prototype: void NoAck_I2c(void);

192 -------------------------------------------------- ---------------*/

193 void NoAck_I2c(void)

194 {

195 SDA=1;

196 _Nop();

197 _Nop();

198 _Nop();      

199 SCL=1;

200 _Nop();

201 _Nop(); //Clock low level period is greater than 4μ

202 _Nop();

203 _Nop();

204 _Nop();  

205 SCL=0; //Clear the clock line and clamp the I2C bus to continue receiving

206 _Nop();

207 _Nop();    

208 }

209 /*-------------------------------------------------- ------------------

210 Send byte data function to a device without subaddress               

211 Function prototype: bit ISendByte(unsigned char sla,ucahr c);  

212 Function: The whole process from starting the bus to sending address, data, and ending the bus, slave device address sla.

213 If 1 is returned, the operation is successful, otherwise the operation is incorrect.

214 Note: The bus must be terminated before use.

215 -------------------------------------------------- ---------------*/

216 /*bit ISendByte(unsigned char sla, unsigned char c)

217 {

218 Start_I2c(); //Start the bus

219 SendByte(sla); //Send device address

220 if(ack==0)return(0);

221 SendByte(c); //Send data

222 if(ack==0)return(0);

223 Stop_I2c(); //End the bus

224 return(1);

225 }

226 */

227 

228 /*-------------------------------------------------- ------------------

229 Send multi-byte data function to a sub-address device               

230 Function prototype: bit ISendStr(unsigned char sla, unsigned char suba, ucahr *s, unsigned char no);  

231 Function: From starting the bus to sending address, sub-address, data, and ending the bus, the slave device

232 Address sla, subaddress suba, the content to be sent is the content pointed to by s, and no bytes are sent.

233 If 1 is returned, the operation is successful, otherwise the operation is wrong.

234 Note: The bus must be terminated before use.

235 -------------------------------------------------- ---------------*/

236 bit ISendStr(unsigned char sla, unsigned char suba, unsigned char *s, unsigned char no)

237 {

238 unsigned char i;

239     

240 Start_I2c(); //Start the bus

241 SendByte(sla); //Send device address

242 if(ack==0)return(0);

243 SendByte(suba); //Send device subaddress

244 if(ack==0)return(0);

245     

246 for(i=0;i

247 {   

248 SendByte(*s); //Send data

249 DelayMs(1);

250 if(ack==0)return(0);

251 s++;

252 } 

253 Stop_I2c(); //End the bus

254 return(1);

255 }

256 

257 /*-------------------------------------------------- ------------------

258 Read byte data function to a device without subaddress               

259 Function prototype: bit IRcvByte(unsigned char sla,ucahr *c);  

260 Function: From starting the bus to sending the address, reading the data, and ending the bus, the whole process is from the device ground

261 Address sla, return value in c.

262 If 1 is returned, the operation is successful, otherwise the operation is wrong.

263 Note: The bus must be terminated before use.

264 -------------------------------------------------- ---------------*/

265 /*bit IRcvByte(unsigned char sla, unsigned char *c)

266 {

267 Start_I2c(); //Start the bus

268 SendByte(sla+1); //Send device address

269 ​​if(ack==0)return(0);

270 *c=RcvByte(); //Read data

271 NoAck_I2c(); //Send a non-acknowledge bit

272 Stop_I2c(); //End the bus

273 return(1);

274 }

275 

276 */

277 /*-------------------------------------------------- ------------------

278 Read multi-byte data function from a sub-address device               

279 Function prototype: bit ISendStr(unsigned char sla, unsigned char suba, ucahr *s, unsigned char no);  

280 Function: From starting the bus to sending address, sub-address, reading data, and ending the bus, the slave device

281 Address sla, subaddress suba, the read content is placed in the storage area pointed to by s, and no bytes are read.

282 If 1 is returned, the operation is successful, otherwise the operation is wrong.

283 Note: The bus must be terminated before use.

284 -------------------------------------------------- ---------------*/

285 bit IRcvStr(unsigned char sla, unsigned char suba, unsigned char *s, unsigned char no)

286 {

287 unsigned char i;

288     

289 Start_I2c(); //Start the bus

290 SendByte(sla); //Send device address

291 if(ack==0)return(0);

292 SendByte(suba); //Send device subaddress

293 if(ack==0)return(0);

294     

295 Start_I2c();

296 SendByte(sla+1);

297 if(ack==0)return(0);

298     

299 for(i=0;i

300 {   

301 *s=RcvByte(); //Send data

302 Ack_I2c(); //Send the answer bit 

303 s++;

304 } 

305 *s=RcvByte();

306 NoAck_I2c(); //Send no acknowledge bit

307 Stop_I2c(); //End the bus

308 return(1);

309 }

310 /*-------------------------------------------------- -

311 Main function

312------------------------------------------------* /

313 void main()

314 {

315 unsigned char dofly[4]={1,2,3,4}; // Display code value 1234

316 unsigned char i;

317     

318 ISendStr(0xae,4,dofly,4); //Write to 24c02

319 DelayMs(200);

320 dofly[0]=0; //Clear current data

321 dofly[1]=0;

322 dofly[2]=0;

323 dofly[3]=0;

324 IRcvStr(0xae,4,dofly,4); //Call storage data

325     

326 while(1)

327 { 

328 P0=dofly_DuanMa[dofly[i]]; //Display storage data

329 LATCH1=1; //Latch

330 LATCH1=0;

331        

332 P0=dofly_WeiMa[i]; //Get bit code

333 LATCH2=1; // latch

334 LATCH2=0;

335 DelayMs(200); //Delay is used to demonstrate the display data

336 DelayMs(200);

337 i++;

338 if(i==4)

339 i=0;

340 }

341 }


Keywords:EEPROM  24c02 Reference address:EEPROM 24c02 [Read storage multiple bytes]

Previous article:EEPROM 24c02 [I2C code package-save to realize running lights]
Next article:EEPROM AT24c02 [Store\Read one byte]

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号