STM32CubeMX configuration IIC bus read and write EEPROM learning records (can't learn to find me)

One, STM32CubeMX configuration

First, open STM32CubeMX and select the chip model to build the project. Here I choose STM32F03ZET6 (Punctual Atomic Warship V3). The specific process of building a new project will not be repeated, it is too basic.
First, configure the SYS and RCC tabs in the System Core directory as shown below:

Configure Debug under SYS as Serial Wire

Configuration

Configure HSE (High Speed ​​Clock) under RCC

I configured here as an external crystal Crystal/Ceramic Resonator, which is set according to the hardware configuration of the board

Insert picture description here

IIC bus interface configuration

STM32F103ZET6 has two hardware IICs. I2C1 under the Connectivity tab is configured as I2C basic parameters (parameter Setting) to keep the default unchanged. DMA and NVIC are not configured for the time being, they will be discussed separately later

Insert picture description here

Serial port configuration

In order to facilitate the test code effect to see the phenomenon, add a serial port configuration output related information.
Serial port configuration is a commonplace talk. The last article has detailed procedures, including serial port interruption and DMA. Let me see the link below. I won’t talk about it here.

Previous article Portal

Clock tree

Clock tree configuration
This test is relatively simple, only using IIC and USART two peripherals, so the configuration of STM32CubeMX is now over. Set the project name, file path, IDE, and code style to generate code. Click here to see STM32CubeMX configuration for specific settings There are detailed instructions at the end of the section.

2. Code realization (on talent)

EEPROM address

#define ADDR_AT24C02_Write 0xA0
#define ADDR_AT24C02_Read 0xA1

The first is to define the read and write address of the EEPROM chip AT24C02. Why are they 0xA0 and 0xA1? Why are they not other types of silicon? Listen to me carefully. This is related to the circuit connection of the chip, as shown in the following figure:

Insert picture description here
The write address of EEPROM is defined as: 0 1 0 1 A0 A1 A2 0
The read address of EEPROM is defined as: 0 1 0 1 A0 A1 A2 1.
Among them, A0~A2 represent the level status of the three pins A0 to A2 of the chip. , I have connected to the ground here, so A0, A1, A2 are all 0, so they are 0xA0 and 0xA1 respectively. For details, you can see the datasheet of AT24C02.

Code

(1) First, I defined two arrays, which are used to write data into EEPROM and store data read from EEPROM respectively

uint8_t I2C_Buffer_Write[256];
uint8_t I2C_Buffer_Read[256];

(2) The following is the key code. I will post my code for your reference. Please correct me if there is any error:

void Fill()
{
    
    
	uint16_t i;

	sprintf((char *)test,"\r\n\r\n I2C AT24C02 Example\r\n");
	HAL_UART_Transmit(&huart1,test, 39, 100); 
	sprintf((char *)test,"\r\n I2C Write Buffer:\r\n");
	HAL_UART_Transmit(&huart1,test, 22, 100);
	for(i=0; i<255; i++)
	{
    
    
	  I2C_Buffer_Write[i]=i;     /* WriteBuffer Initialization */
	}
	for(i=0; i<256; i=i+8)
	{
    
    
		if (HAL_I2C_Mem_Write(&hi2c1, ADDR_AT24C02_Write, i, I2C_MEMADD_SIZE_8BIT,I2C_Buffer_Write+i,8, 10000) == HAL_OK)
		{
    
    
			sprintf((char *)test,"Success\r\n");
			HAL_UART_Transmit(&huart1,test, 9, 100);
			HAL_Delay(5);
		}
		else
		{
    
    
			sprintf((char *)test,"False\r\n");
			HAL_UART_Transmit(&huart1,test, 7, 100);
			HAL_Delay(5);
		}
	}
	HAL_I2C_Mem_Read(&hi2c1, ADDR_AT24C02_Read, 0, I2C_MEMADD_SIZE_8BIT,I2C_Buffer_Read,256, 1000);
	for(i=0; i<255; i++)
	{
    
    
		sprintf((char *)test,"%d\r\n",I2C_Buffer_Read[i]);
		HAL_UART_Transmit(&huart1,test,4, 100);
	}

}

The function of this function is also relatively simple. Fill the EEPROM with numbers 1~256 through a 256-dimensional array, then read them out one by one according to the address, put them in the I2C_Buffer_Read array and print them out through the serial port. In addition, the capacity of AT24C02 is 256Byte, which is 256*8=2Kbit, here is also related to the concept of paging storage, every eight bytes is a page, data larger than eight bytes, one page is saved, the code is here for your own research.

result

Insert picture description here
Insert picture description here

It can be seen that the program executes normally. Every time a byte of data is successfully written, a Sucess will be printed, otherwise it will print False. The data read after writing is also the data we wrote.
In addition, EEPROM is a storage device that is not lost after power failure. After writing, you can comment out the written part of the code, and only keep the read part, burn it in and re-power on, and you can still read the data normally. , Which proves the correctness of the experiment.

The ability is not high and the level is limited. Please criticize and correct if there are mistakes. Thank you everyone. If it is helpful to you, please comment and tell me haha

Guess you like

Origin blog.csdn.net/Haders_0610/article/details/109332182