Docs

Micro-controllers, wireless transmission and database

Head

EEPROM AT24C Series with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Addressing

The module I am using is AT24C256. Datasheet is available at http://ww1.microchip.com/downloads/en/devicedoc/doc0670.pdf

You can use any AT24C Series with the same wiring connection and code below, it will work

Wiring Diagram

Diagram

Image1

Image2

STM32CubeIDE Settings

Set PA1 to GPIO_Output

Set PB1 to GPIO_EXTI1

Click NVIC → EXTI line1 interrupt → Enabled (Tick)

Click connectivity --> Click I2C1

For I2C select I2C

Configuration --> Parameter Settings

For I2C speed select Fast Mode

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN PV */
#define EEPROM_ADDR 0xA0
uint8_t toWriteN[] = "N";
uint8_t toWriteY[] = "Y";
uint32_t previousMillis = 0;
uint8_t StatusLED[1];
/* USER CODE END PV */

  /* USER CODE BEGIN 2 */
  HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDR, 0, 2, StatusLED, 1, 1000);
  if( StatusLED[0] == 78) // Ascii value of 'N' is 78 (N for NO)
  {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
  }
  else if ( StatusLED[0] == 89) // Ascii value of 'Y' is 89 (Y for YES)
  {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1);
  }
  else
  {
    HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDR, 0, 2, toWriteN, 1, 1000);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
  }
  /* USER CODE END 2 */

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if (GPIO_Pin == GPIO_PIN_1 && (HAL_GetTick() - previousMillis > 500))
  {
    HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDR, 0, 2, StatusLED, 1, 1000);
    if( StatusLED[0] == 78) // Ascii value of 'N' is 78 (N for NO)
    {
      HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDR, 0, 2, toWriteY, 1, 1000);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1);
    }
    else if ( StatusLED[0] == 89) // Ascii value of 'Y' is 89 (Y for YES)
    {
      HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDR, 0, 2, toWriteN, 1, 1000);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0);
    }
    previousMillis = HAL_GetTick();
  }
}
/* USER CODE END 4 */