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.
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
- 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 ---- Using lithium-ion batteries more safely and efficiently - Battery Management System (BMS) Solution
- Talk: Small capital investment
- Where does the EG8010 dead time go?
- Downloads|Selected Courseware & Latest Materials from Tektronix 2019 Semiconductor Seminar
- GaN Power HEMT > 650 V: Parametric Analysis and Comparison with SiC MOSFET
- Voltage controlled current source composed of opa547
- [GUIX①, one of threadX components] guix text button
- Qorvo Technology Video: GaN Solutions for Sub-6 GHz 5G Base Station Applications
- [Jihai M3 core APM32E103VET6S MINI development board] 02. USART online interaction function
- PCB design of RF devices