i2c Write successfully but read wrong data !!

Amir_El
Posts: 17
Joined: Thu Nov 19, 2020 3:55 pm

i2c Write successfully but read wrong data !!

Postby Amir_El » Thu Apr 01, 2021 1:45 pm

Hi everyone, The ESP32 is communicating with PCF8563 RTC. This is my manual RTC Time set routine

Code: Select all

  
  CMD_RTX_Set = i2c_cmd_link_create();
  i2c_master_start(CMD_RTX_Set);
  i2c_master_write_byte(CMD_RTX_Set, RTC_Write_Add, Want_Ack);
  i2c_master_write_byte(CMD_RTX_Set, VL_secondsReg, Want_Ack);
  // Auto increment applay
  i2c_master_write_byte(CMD_RTX_Set, 0x00, Want_Ack);        // Vl Sec
  i2c_master_write_byte(CMD_RTX_Set, 0x45, Want_Ack);        // Min 25
  i2c_master_write_byte(CMD_RTX_Set, 0x14, Want_Ack);        // Hou 1:00 PM
  i2c_master_write_byte(CMD_RTX_Set, 0x01, Want_Ack);        // Days 1
  i2c_master_write_byte(CMD_RTX_Set, 0x04, Want_Ack);        // Wednesday
  i2c_master_write_byte(CMD_RTX_Set, 0x84, Want_Ack);        // set month to 4 and century bit to 1
  i2c_master_write_byte(CMD_RTX_Set, 0x21, Want_Ack);        // 2021
  i2c_master_stop(CMD_RTX_Set);
  Get_I2C_Response = i2c_master_cmd_begin(I2C_NUM_0, CMD_RTX_Set, 1000 / portTICK_RATE_MS);
  if(Get_I2C_Response == ESP_OK)
  {
    printf("RTC Clock Set success\n");
  }
  else
  {
    printf("RTC Clock Set Failed\n");
  }
using the ESP i2c tool to check the RTC I get a correct time reading (Reading 7 bytes-> starting from seconds up to years). Then I know that there are now Hardware connection issues.

Image

now when I try to read the RTC chip by the code, I don't get the correct results !! My RTC Read routine is

Code: Select all

  uint8_t Data_Reg[7] ;
  uint8_t Data_Reg_Size = 7;
  
  CMD_RTX_Get = i2c_cmd_link_create();
  i2c_master_start(CMD_RTX_Get);
  i2c_master_write_byte(CMD_RTX_Get, (RTC_Add<<1) | Write_Bit, Want_Ack);
  i2c_master_write_byte(CMD_RTX_Get, VL_secondsReg, Want_Ack);

  i2c_master_write_byte(CMD_RTX_Get, (RTC_Add<<1) | Read_Bit, Want_Ack);
  if(Data_Reg_Size > 1) 
  {
    i2c_master_read(CMD_RTX_Get, Data_Reg, Data_Reg_Size - 1, ACK_VAL);
  }
  i2c_master_read_byte(CMD_RTX_Get, Data_Reg + Data_Reg_Size - 1, NACK_VAL);
  i2c_master_stop(CMD_RTX_Get);

  esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, CMD_RTX_Get, 1000 / portTICK_RATE_MS);
  if(ret == ESP_OK)
  {
    printf("RTC Clock Capture Successed\n");
    printf("Date: %02d:%02d:%02d Time: %d:%d:%d\n", Data_Reg[6], Data_Reg[5], Data_Reg[3],
                                                   Data_Reg[2], Data_Reg[1], Data_Reg[0]);
  }
  else
  {
    printf("RTC Clock Captured Failed\n");
  }
  i2c_cmd_link_delete(CMD_RTX_Get);
But the Results comes out like this:

Image

Kindly, inform me about what I'm missing.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 251 guests