2020-04-10 12:16 AM
Hello I am trying to communicate with TI BQ28Z610 by using Nucleo-F746ZG. All I do is trying to read data from TI chip. I am sending 0x08 to slave address 0x16.I2C frequency is 80kHz and pull ups are 10 K. Here is the related part of the trial code:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//READ VOLTAGE FOR TESTING
#define I2C_Slave_Address (0x16) // Receive 0x55+1 0x55
uint8_t I2C_Data[16];
uint8_t I2C_DataToSend;
char statusString[255];
int statusStringLength;
I2C_DataToSend=0x08;
if (HAL_OK != HAL_I2C_Master_Transmit(&hi2c1,(uint8_t)I2C_Slave_Address,&I2C_DataToSend,1,1000)){
statusStringLength = sprintf(statusString,"Transmit failed");
CDC_Transmit_FS(statusString,statusStringLength);
HAL_GPIO_WritePin(LD1_GPIO_Port,LD1_Pin,1);
}
if (HAL_OK != HAL_I2C_Master_Receive(&hi2c1,(uint8_t)I2C_Slave_Address + 1 , I2C_Data,1,1000)){
statusStringLength = sprintf(statusString,"Receive failed");
CDC_Transmit_FS(statusString,statusStringLength);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,1);
}
statusStringLength = sprintf(statusString,"Voltage: %d", (int) I2C_Data[0]);
CDC_Transmit_FS(statusString,statusStringLength);
HAL_Delay(100);
}
Here is the oscilloscope image:
Here is the logic analyzer image:
So what might be the reason behind it? For me everything looks fine. Thank you beforehand.
2020-04-10 01:05 AM
In the logic analyzer trace I see a NACK, so the slave doesn't respond to this address.
If you are sure of the address, perhaps is must be left shifted : 0x16 << 1
By the way I thing that HAL_I2C_Master_Receive() sets the read bit of the slave address, no need of I2C_Slave_Address + 1
2020-04-10 01:08 AM
So if there is a NACK in the first place for the address, there I cannot see data bits in the data line am I correct? Or despite the NACK MCU will still try to send the data? And thank you for your response :)
2020-04-10 01:19 AM
First the master sends the address of the slave to speak to.
If the slave recognizes its address it will maintail the SDA line low, this is a ACK, then le master will continue to send the data.
If the slave doesn't recognizes its address, it will do nothing so the pullup will maintain the SDA line up, this is a NACK. Then the master stop.
You should read some tutorial about I2C before programming.