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
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 }
Previous article:EEPROM 24c02 [I2C code package-save to realize running lights]
Next article:EEPROM AT24c02 [Store\Read one byte]
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- Several key performance characteristics of the MSP432P401R MCU 14-bit ADC
- 【BearPi-HM Micro】Development of smart street light module based on E53 interface
- Using Ultra-Wideband Technology to Display the Bible
- How is "u8 Dat2:7;" written in the structure? What does it mean?
- Modeling and Optimization Design of Finite State Machines
- Help! Can micropython do image processing or video processing?
- Pin-regulated 8mA output amplifier
- Is this the most comprehensive collection of RF project design tools?
- A pitfall in buying goods on a certain fish - BMW night vision camera
- Big Brother, please help