Detecting the end of the self-write cycle of the AT24CXX

Publisher:yanfeng00Latest update time:2016-12-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

stm32_i2c timing diagram part


Reference address:Detecting the end of the self-write cycle of the AT24CXX

Previous article:Share some experience in STM32 hardware design
Next article:When does the STM32 APB2 AFIO clock need to be turned on?

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号