2025-06-03 8:19 AM
I am trying to communicate with a MS5837 pressure sensor by I2C with the I2C1 on my STM32H563RGT6, but the I2C communication doesn't seem to see the MS5837. I am pretty sure the connections are fine, and I have 2 pull-up resistors of 4.7kOhms for each lines (SCL and SDA). I think my .ioc config is fine since the MS5837 uses 400kHz clock, so I chose the fast mode. I verified the addresses of the MS5837 but something seems to not work properly and I'm running out of ideas at this point.
2025-06-03 8:21 AM
What slave address are you using? Try left-shifting by one bit. HAL expects the 7-bit address to be left shifted.
2025-06-03 8:26 AM
I'm setting up the MS5837 address as 0x76 but shifting it when I read or write. I attached the main.c, the .h and .c of my semi homemade MS5837 library (it started from this : https://github.com/zyurttas/STM32---MS5837-Library).
#define MS5837_ADDR 0x76
#define MS5837_RESET 0x1E
#define MS5837_ADC_READ 0x00
#define MS5837_PROM_READ 0xA0
#define MS5837_CONVERT_D1_8192 0x4A
#define MS5837_CONVERT_D2_8192 0x5A
static t_MS5837_Status I2C_Write(uint8_t addr) {
if (HAL_I2C_Master_Transmit(&hi2c1, MS5837_ADDR << 1, &addr, 1, 100) != HAL_OK)
return MS5837_ERROR;
return MS5837_OK;
}
static t_MS5837_Status I2C_Read(uint8_t addr, uint8_t *reg_data, uint8_t r_len) {
if (HAL_I2C_Master_Transmit(&hi2c1, MS5837_ADDR << 1, &addr, 1, 100) != HAL_OK)
return MS5837_ERROR;
HAL_Delay(20);
if (HAL_I2C_Master_Receive(&hi2c1, MS5837_ADDR << 1, reg_data, r_len, 100) == HAL_OK)
return MS5837_OK;
return MS5837_ERROR;
}
2025-06-03 8:37 AM
Have you used a scope to see what's actually happening on the wires?
2025-06-03 8:40 AM
Sadly no, I don't have a scope on hand right now.
2025-06-04 5:24 AM
Hi
try a simple "you are here test" on the adresses.
So you see if the sensor is responding.
void i2c_test ( I2C_HandleTypeDef* _i2c )
{
char s_buff[20] ={0};
char s2_buff[]="found 0x\0";
char s1_buff[5]={0};
HAL_StatusTypeDef _hal;
deb_print("i2c-Test\n");
for (uint8_t i = 2; i<253; i=i+2)
{
_hal=HAL_I2C_IsDeviceReady(_i2c,i,2,1000);
if ( _hal == HAL_OK)
{
s_buff[0] = 0;
s1_buff[0] = 0;
s1_buff[1]=0;
s1_buff[2]=0;
s1_buff[3]=0;
s1_buff[4]=0;
strcat(s_buff,s2_buff);
itoa(i,s1_buff,16);
strcat(s_buff,s1_buff);
strcat(s_buff,"\n");
ser_print(s_buff);
}
}
}
ser_print is my "print on Uart."
#define deb_uart &huart2
void ser_print( const char buffer[])
{
HAL_UART_Transmit(deb_uart, (uint8_t*) buffer, strlen(buffer), HAL_MAX_DELAY);
}
so you get a list of the adresses on the I²C-Bus.
If your sensor is in the list you have to dig into software.
Somtimes, in growing systems, there are sensors with the same adress.
padawan