[Blue Bridge Cup Embedded] [HAL Library] Seven, AT24C02 data storage – i2c verification

Schematic Analysis

It can be seen from the schematic diagram that SCL and SDA correspond to PB6 and PB7. This method is a software simulation of iic operation. This experiment will realize the storage and reading of data at the specified address of AT24C02, and display the data with lcd. The iic file provided by the official routine used directly by this project will not be described here.

STM32CubeMX configuration

Clock, the clock tree configuration is no longer described.
In fact, in this experiment, you can directly generate the project without configuring GPIO, because there are functions to configure related pins in the iic file, but it doesn’t matter if we configure it here, you don’t need to worry about the mode, students who want to know can go to i2c The start function of the .c file.
Just generate the project.

code

We need to add user code. This project involves two folders: lcd and iic:
link: https://pan.baidu.com/s/1XBkoEnNFpfE1Y-dUVCRW5Q
Extraction code: 6666
Put the user folder in the project directory, and Add a path to the project. (Introduced in previous sections)
The header file added by main.h:

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "i2c.h"
#include "stdio.h"
/* USER CODE END Includes */

main.c defines variables:

/* Private typedef ---------------------------------------------- -------------*/ 
/* USER CODE BEGIN PTD */ 
uint8_t val = 0 ;   // initial value of data stored 
char buf[ 20 ];      // used to display a line of lcd String 
/* USER CODE END PTD */

AT24C02 read and write code:

/* Private user code --------------------------------------------- ------------*/ 
/* USER CODE BEGIN 0 */ 
// AT24C02 reads the specified address data 
uint8_t AT24C02_Read( uint8_t address)
{
    unsigned char val;

    I2CStart(); 
    I2CSendByte(0xa0);
    I2CWaitAck(); 

    I2CSendByte(address);
    I2CWaitAck();

    I2CStart();
    I2CSendByte(0xa1); 
    I2CWaitAck();
    val = I2CReceiveByte(); 
    I2CWaitAck();
    I2CStop();

    return(val);
}
// AT24C02 writes data at the specified address 
void  AT24C02_Write ( unsigned  char address, unsigned  char data)
 {
    I2CStart();
    I2CSendByte(0xa0); 
    I2CWaitAck();

    I2CSendByte(address);   
    I2CWaitAck();
    I2CSendByte(data); 
    I2CWaitAck();
    I2CStop();
}
/* USER CODE END 0 */

The code in the main function:

/* USER CODE BEGIN 2 */
    I2CInit();              // -----------
    LCD_Init();
                            //Initialization of i2c and lcd - clear screen on white background, red font
    LCD_Clear(White);
    LCD_SetBackColor(White);
    LCD_SetTextColor(Red);  // -----------

    AT24C02_Write( 0 , val);   // First write the initial data to the specified address 
    HAL_Delay( 100 );          // It takes a certain amount of time to write, and the delay of 1ms is also acceptable for my test 
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */ 
      val = AT24C02_Read( 0 ); // Read data from specified address 
      sprintf (buf, " AT24C02_DATA:%d " , val); // Put val value into buf string 
      LCD_DisplayStringLine( Line4, ( uint8_t *)(buf)); // Display the contents of buf on the fourth line
      val++; 
      AT24C02_Write( 0 , val); // Write the val value after adding one to the specified address 
      HAL_Delay( 1000 );
     /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

operation result

It is incremented by one every second. When it exceeds 255, it overflows and will be reset to zero.

Leave a Comment

Your email address will not be published. Required fields are marked *