1 Overview
The EEPROM chip model on the MiniSTM32 development board is 24C02. The total capacity of the chip is 256 bytes, and the chip is connected to the outside through the IIC bus. Here we directly use the AT24C02 on the Atom board, mainly for learning software programming.
2. Hardware Connection
The three pins A2, A1, and A0 are directly grounded. Power supply: (VCC = 2.7V to 5.5V)
Device address setting:
For AT24C02:Addr—>0xA0(write)/0xA1(read).
Single byte write:
Write by page:
Read data from the current address;
Random Read:
Sequential read:
3. Routine Analysis
(I) IIC partial implementation code
Including IIC initialization (IO port), IIC start, IIC end, ACK, IIC read and write and other functions. In other functions, you only need to call the relevant IIC function to communicate with the external IIC device. This is not limited to 24C02. This code can be used on any IIC device.
IIC_SCL and IIC_SDA are connected to PC12 and PC11 of STM32 respectively.
1.IIC initialization
//IO operation function
#define IIC_SCL PCout(12) //SCL
#define IIC_SDA PCout(11) //SDA
#define READ_SDA PCin(11) // Input SDA
//Initialize IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
IIC_SCL=1;
IIC_SDA=1;
}
2. Generate IIC start signal
//Generate IIC start signal
void IIC_Start(void)
{
SDA_OUT(); //sda line output
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0; //Clamp the I2C bus and prepare to send or receive data
}
3. Generate a stop IIC signal
//Generate IIC stop signal
void IIC_Stop(void)
{
SDA_OUT(); //sda line output
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high, DATA change form low to high
delay_us(4);
IIC_SCL=1;
IIC_SDA=1; //Send I2C bus end signal
delay_us(4);
}
4. Wait for the response signal
//Wait for the response signal to arrive
//Return value: 1, failed to receive response
// 0, receiving the response successfully
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //Set SDA to input
IIC_SDA=1;
delay_us(1);
IIC_SCL=1;
delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0; //Clock output 0
return 0;
}
5. Response signal
//Generate ACK response
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
6. No response is generated
//No ACK response is generated
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
7.IIC sends a byte
//IIC sends a byte
//Return whether the slave has a response
//1, there is a response
//0, no response
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0; //Pull the clock low to start data transmission
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7; //Start from high bit, transmit data one bit at a time
txd<<=1;
delay_us(2); //These three delays are required for TEA5767
IIC_SCL=1; //Keep data stable
delay_us(2);
IIC_SCL=0; //Next bit of data transmission starts
delay_us(2);
}
}
8.IIC reads one byte
//Read 1 byte, when ack=1, send ACK, when ack=0, send nACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN(); //SDA is set as input
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)
receive++;
delay_us(1);
}
if (!ack)
IIC_NAck(); //Send nACK
else
IIC_Ack(); //Send ACK
return receive;
}
2. AT24C02 Operation
Directly set the IO port mode to input or output through register operation. The code is as follows:
#define SDA_IN() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=8<<12;}
#define SDA_OUT() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=3<<12;}
1. Initialize the IIC interface
//Initialize the IIC interface
void AT24CXX_Init(void)
{
IIC_Init();
}
2. Read a data at the specified address of AT24CXX
//Read a data at the specified address of AT24CXX
//ReadAddr: the address to start reading, 24C02 size is 2Kbit; ADDR: 0x00---0x7FF
//Return value: the data read
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
if(EE_TYPE>AT24C16)
/*The macro definition has a chip type selection. If the definition is greater than AT24C16, two 8-bit addresses are sent, otherwise one 8-bit address is sent*/
{
IIC_Send_Byte(0XA0); //Send write command, 0XA0: device address, 7-bit address + write (0)
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr>>8); //Send high address
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //Send device address 0XA0, write data
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //Send low address
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA1); //Enter receiving mode, 7-bit address + read (1)
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop(); //Generate a stop condition
return temp;
}
3. Write a data to the specified address of AT24CXX
//Write a data at the specified address of AT24CXX
//WriteAddr: Destination address for writing data
//DataToWrite: data to be written
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //Send write command
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr>>8); //Send high address
}else IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //Send device address 0XA0, write data
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256); //Send low address
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite); //Send byte
IIC_Wait_Ack();
IIC_Stop(); //Generate a stop condition
delay_ms(10); //For EEPROM devices, you need to wait for a while each time you write, otherwise the write will fail!
}
4. Write data of length Len starting from the specified address in AT24CXX
//Start writing data of length Len at the specified address in AT24CXX
//This function is used to write 16-bit or 32-bit data.
//WriteAddr: start writing address
//DataToWrite: the first address of the data array
//Len: length of data to be written 2,4
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)
{
u8 t;
for(t=0;t AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff); } 5. Start reading data of length Len from the specified address in AT24CXX //Start reading data of length Len from the specified address in AT24CXX //This function is used to read 16-bit or 32-bit data. //ReadAddr: start reading address //Return value: data //Len: length of the data to be read 2,4 u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len) { u8 t; u32 temp=0; for(t=0;t { temp<<=8; temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1); } return temp; } 6. Check whether AT24CXX is normal // Check if AT24CXX is normal //Here the last address (255) of 24XX is used to store the flag word. //If you use other 24C series, this address needs to be modified //Return 1: Detection failed //Return 0: Detection successful u8 AT24CXX_Check(void) { u8 temp; temp=AT24CXX_ReadOneByte(255); //Avoid writing AT24CXX every time you start the computer if(temp==0X55) return 0; else //Exclude the first initialization { AT24CXX_WriteOneByte(255,0X55); temp=AT24CXX_ReadOneByte(255); if(temp==0X55) return 0; } return 1; } 7. Start reading the specified number of data from the specified address in AT24CXX /Start reading the specified number of data from the specified address in AT24CXX //ReadAddr: The address to start reading is 0~255 for 24c02 //pBuffer: the first address of the data array //NumToRead: the number of data to be read void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead) { while(NumToRead) { *pBuffer++=AT24CXX_ReadOneByte(ReadAddr++); NumToRead--; } } 8. Write the specified number of data starting from the specified address in AT24CXX //Start writing the specified number of data at the specified address in AT24CXX //WriteAddr: The address to start writing is 0~255 for 24c02 //pBuffer: the first address of the data array //NumToWrite: the number of data to be written void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite) { while(NumToWrite--) { AT24CXX_WriteOneByte(WriteAddr,*pBuffer); WriteAddr++; pBuffer++; } } refer to: 1. Atomic STM32 development library function version
Previous article:SPI Topic (II)——STM32 Driver FLASH (W25Q64)
Next article:STM32 study notes ADC
- 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
- How to perform non-destructive testing on chip circuit boards
- Please help me find out what type of SOT23 device with silk screen "UB17" and "WB14" is
- I would like to ask if there is any interference noise when measuring the ground line with an oscilloscope
- The microcontroller sends a string to the host
- VirtualBox-5.2.12 Environment Guide
- Help
- MATLAB Design Butterworth Bandpass Filter Parameter Setting
- Op amp circuit PCB design tips
- The benefits of eating noodles for engineers
- 【GD32L233C-START Review】6. Get RTC time and display it through OLED