cancel
Showing results for 
Search instead for 
Did you mean: 

Write in external memory EEPROM AT24C256 using STM32L412KBU6

Kai_Satone
Associate III
Unable to write in EEPROM AT24C256 This is the code 

I2C_HandleTypeDef hi2c1;
#define EEPROM_I2C &hi2c1

// EEPROM ADDRESS (8bits)
#define EEPROM_ADDR 0xA0

// Define the Page Size and number of pages
#define PAGE_SIZE 64     // in Bytes
#define PAGE_NUM  512    // number of pages

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t dataRead[128];
uint8_t dataWrite[100];

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
 // for(int i=0; i<512;i++)
 //{
  //  EEPROM_PageErase(i);
 // }

  for(int i=0;i<100;i++)
  {
    dataWrite[i]=i+10;
  }
  HAL_I2C_Mem_Write(&hi2c1,0x0A,(3<<6),2,dataWrite,64,1000);
  HAL_Delay(5);
  HAL_I2C_Mem_Read(&hi2c1,0x0A,(3<<6),2,dataRead,64,1000);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
2 REPLIES 2

Check for errors being returned by functions.

Use the correct slave address, 0xA0 not 0x0A

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I have changed the 0x0A to 0xA0 but stil it did not make any differance.