The self-write cycle of AT24CXX is "less than 5ms", so it can be solved by using a delay function to delay 5ms.
Pain point: When writing a delay function, you can use software delay and timer delay. For software delay, you need an oscilloscope to see the exact time. It is very troublesome to use an oscilloscope or not. Moreover, if the main frequency of the microcontroller changes, the value of the software delay needs to be changed. The timer delay takes up a timer and wastes microcontroller resources. In addition, if the AT24CXX suddenly extends the writing time due to its own problems, and this is not taken into account when writing the program, the whole system will be ruined.
Solution: Look at the write timing. After the microcontroller sends a start signal to the AT24CXX and then sends an address data, the AT24CXX will return an ACK signal. If the self-write cycle is not over, it will not return an ACK signal. Therefore, we can observe whether there is this ACK to determine whether the self-write cycle is over. In this way, no matter how long the self-write cycle of the 24CXX is, you can immediately write another data or perform a read operation to improve the efficiency of the program.
The following is a program written using the STM32 library function. The implementation steps are the same as those described in the above solution.
void WaitWriteCycleOver(void) { do { /* Send start signal */ I2C_GenerateSTART(I2C1, ENABLE); /* Read SR1 register to clear the start flag */ I2C_ReadRegister(I2C1, I2C_Register_SR1); /* Send device address */ I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter); /* Check whether the device address is sent successfully */ }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & I2C_FLAG_ADDR)); /* Clear ACK flag */ I2C_ClearFlag(I2C1, I2C_FLAG_AF); /* Send a stop signal */ I2C_GenerateSTOP(I2C1, ENABLE); }
After reading this, you may have some questions about the program:
Question 1: Why detect ADDR instead of ACK?
Because we are using hardware I2C, the detection of ACK is done by hardware. If ADDR in SR1 is set, it means that the hardware has detected ACK, otherwise, ACK is not detected.
Question 2: Why do we need to clear ACK alone and not other flags?
Because the ACK bit needs to be cleared by software, the other bits are automatically cleared after reading.
Previous article:Share some experience in STM32 hardware design
Next article:When does the STM32 APB2 AFIO clock need to be turned on?
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University ---- ISO 7637
- EEWORLD University Hall----Basics of Motors and Traction (Petroleum University)
- ZigBee transmission based on self-powered switch
- TMS320DM642 Bootmode
- Single chip microcomputer drives 9013 transistor to realize infrared emission circuit diagram
- What are the characteristics of such a zero-pole system?
- SDR-Hackrf one
- SHT31 Review + Unboxing Test
- Matlab and Modelsim co-simulation error
- ST NUCLEO-G071RB evaluation application - Come and experience the new generation of entry-level 32-bit STM32, giving you the best cost-effective experience