STM32 simulates IIC reading and writing AT24CXX

Publisher:美好的人生Latest update time:2017-11-02 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have only used 51 to make AT24C02 before. Later, I came into contact with models with larger capacity and found many inconsistencies with the previous ones. I have summarized them as follows.

In the previous blog, the basic driver code for STM32 to simulate IIC with IO port has been written. Now, combined with the data sheet, the functions mentioned in the previous blog are used to write the universal driver code for the entire AT24CXX series.

Let's first look at the Chinese manual of AT24CXX.

This is the write step.

The address information of the slave device mentioned above is shown in the figure below.

 

The slave device address bits of AT24CXX with different capacities are different, which is reflected in the differences in the basic read and write functions of the following devices.

Let's initialize AT24CXX first:

void AT24CXX_Init()

{

      IIC_Init();

}

Here you only need to initialize the IIC bus.

 

The following is the basic read and write function of AT24CXX

/*Read a data at the specified address of AT24CXX*/

u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{      
 u8 temp=0;                          
    IIC_Start();  
 if(EE_TYPE>AT24C16)
 {
  IIC_Send_Byte(0XA0);    
  IIC_Wait_Ack();
  IIC_Send_Byte(ReadAddr>>8); //Send high address
  IIC_Wait_Ack();   
 }else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //This is important to understand

 IIC_Wait_Ack(); 
IIC_Send_Byte(ReadAddr%256); //Send low address
 IIC_Wait_Ack();     
 IIC_Start();        
 IIC_Send_Byte(0XA1); //Enter receiving mode
 IIC_Wait_Ack();  
 temp=IIC_Read_Byte(0);     
 IIC_Stop();     
 return temp;

 

The key point to understand is that the drivers for different models are realized by judging the EE_TYPE macro definition, that is, the selection of the model.

Similarly, the function to write data to the specified address is as follows:

void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{                                
  IIC_Start();  
 if(EE_TYPE>AT24C16)
 {
  IIC_Send_Byte(0XA0);   
  IIC_Wait_Ack();
  IIC_Send_Byte(WriteAddr>>8);
  }else
 {
  IIC_Send_Byte(0XA0+((WriteAddr/256 ) <<1));    
 }  
 IIC_Wait_Ack();    
 IIC_Send_Byte(WriteAddr%256);   
 IIC_Wait_Ack();                    
 IIC_Send_Byte(DataToWrite);            
 IIC_Wait_Ack();            
IIC_Stop();
 delay_ms(10); //Note that there is a 10ms delay
}

At this point, the writing of the basic driver code is completed.


Keywords:STM32 Reference address:STM32 simulates IIC reading and writing AT24CXX

Previous article:Understanding of STM32 analog IIC
Next article:Simple SD card reading and writing for STM32

Recommended ReadingLatest update time:2024-11-16 16:20

Serial communication in STM32
Speaking of communication, we all know that communication is divided into parallel communication and serial communication. Parallel communication has a fast speed but occupies more pins, while serial communication has a slow speed but occupies fewer pins. Today we mainly talk about serial communication Serial commun
[Microcontroller]
STM32 PWM output experiment
First, some necessary declarations #include stm32f10x.h #include "pwm.h"u32 Sys_Clk=1000000;u16 pwm1_2_Freqz;//Frequency of pwm wave 1, 2 output port u16 pwm3_4_Freqz;//Frequency of pwm wave 3, 4 output port u16 TIM2_PERIOD;//Number of timer jump cycles u16 TIM4_PERIOD;u16 CCR_VAL1;//Value of the timer comparison reg
[Microcontroller]
STM32 PWM output experiment
STM32 watchdog
STM32 has two watchdogs , one is an independent watchdog and the other is a window watchdog. Let's talk about the independent watchdog first. The feature of the independent watchdog is that it uses the 40k RC oscillator on the chip as the clock (this RC is quite inaccurate). The advantage is that even if the CPU main
[Microcontroller]
STM32 watchdog
STM32 study notes: serial port IAP
1. Brief Introduction IAP (In-Application-Programming): Application programming is a programming mode applied to Flash program memory. It can read/write another program Flash (User Flash) space by calling a specific IAP program when the application is running normally. It can even control the read/write operation of
[Microcontroller]
STM32 study notes: serial port IAP
A first look at the USB peripherals of STM32
These days I have been considering whether to use USB or Ethernet for the project, but the platform we are using, STM32F103ZET6, does not seem to have the Enternet peripheral, so let's consider USB first. One of the major features of USB peripherals is plug-and-play, which is possible because the USB protocol stipulat
[Microcontroller]
MDK development environment STM32 startup file _main function analysis
======================================================================== ** Section #1 'ER_IROM1' (SHT_PROGBITS)     Size   : 1008 bytes (alignment 4)     Address: 0x08000000     $d.realdata     RESET     __Vectors ; starting address of vector table         0x08000000: 20000460 `.. DCD 536872032; Stack pointer addr
[Microcontroller]
Explanation of the usage of stm32 library files
1. We need to add those files to run a program: First of all, we need to have a certain understanding of the library files. Among them, STM32F10xR.LIB is a packaged .C file, which includes stm32f10x_conf.c stm32f10x_adc.c stm32f10x_gpio.c, etc. To use these C functions, just include the header files stm32f10x_conf.h s
[Microcontroller]
Explanation of the usage of stm32 library files
Design of intelligent control network terminal based on STM32 embedded system
0 Introduction Intelligent network terminal is an embedded terminal device that realizes intelligent management. It usually has the basic functions of security access control system and automatic control. This article focuses on the design and implementation of the network control function of intelligent em
[Security Electronics]
Design of intelligent control network terminal based on STM32 embedded system
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号