Unable to read WhoAmI register from LIS3DSH. I'm using I2C
Hello guys,
The LIS3DSH is on the development board itself, which is STM32F407G (ver MB997D). And I am using CubeMX to generate the code.
Here is the wiring
Function
LIS3DSH
Master
SDA
PA7
PINB7
SCL
PA5
PINB6
SEL/SDO
PA6
3V Pin
At first, I used these two API calls before I realised that HAL_I2C_Mem_Write and HAL_I2C_Mem_Read are the
right ones.
uint8_t slavAddr = 0x3A; //[7-bit adddress is 0001 111x]
uint8_t ID,whoAmI=0x0F;
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
HAL_I2C_Master_Transmit(&hi2c1,slavAddr,&whoAmI,1,100);
HAL_I2C_Master_Receive(&hi2c1,slavAddr,&ID,1,100);
while (1)
{
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
Surprisingly I got the default value of Who_Am_I, 0x3F (on the watch window) with the above program. But it wouldn't get every time and realized the mistake and used theHAL_I2C_Mem_Write
and
HAL_I2C_Mem_Read functions. I still don't get the output.uint8_t slavAddr = 0x3A; //[7-bit adddress is 0001 111x]
uint8_t ID,whoAmI=0x0F;
uint8_t ctrlAddr = 0x20;
uint8_t ctrlVal = 0x17;
uint8_t xAddr[2] = {0x28,0x29};
//uint8_t yAddr[2] = {0x2A,0x2B};
//uint8_t zAddr[2] = {0x2C,0x2D};
uint8_t xVal[2],yVal[2],zVal[2];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
//HAL_I2C_Master_Transmit(&hi2c1,slavAddr,&whoAmI,1,100);
//HAL_I2C_Master_Receive(&hi2c1,slavAddr,&ID,1,100);
HAL_I2C_Mem_Read(&hi2c1,slavAddr,whoAmI,1,&ID,1,100);
HAL_I2C_Mem_Write(&hi2c1,slavAddr,ctrlAddr,1,&ctrlVal,1,100);
HAL_I2C_Mem_Read(&hi2c1,slavAddr,xAddr[0],1,&xVal[0],1,100);
while (1)
{
}
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
This is my post ever on any forum. I might have missed out details
Help is appreciated.
