Skip to main content
Visitor II
June 28, 2020
Question

Hi, I'm trying to program my STN32G431RB in order to work with the sensor HTS221, I write the code but I don't know if it's correct, I would be really grateful if someone could tell me.

  • June 28, 2020
  • 1 reply
  • 857 views

 while (1)

 {

  /* USER CODE END WHILE */

 for (int i=0; i<11; i++)

 {

 buf1[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf1[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf2[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf3[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf4[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf5[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf6[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf7[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf8[i]=0x0;

 }

 for (int i=0; i<11; i++)

 {

 buf9[i]=0x0;

 }

 buf9[0] = WIA;

 ret9 = HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf9, 1, HAL_MAX_DELAY);

 if (ret9 != HAL_OK) {

 strcpy((char*)buf9, "Error Tx\r\n");

 } else{

 ret9 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf9, 2, HAL_MAX_DELAY);

 if (ret9 != HAL_OK ) {

 strcpy((char*)buf9, "Error Rx\r\n");

 }else {

 }

 }

 //Lettura umidità

 buf1[0] = REGH_MSB;

 ret1 = HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf1, 1, HAL_MAX_DELAY);

 if (ret1 != HAL_OK) {

 strcpy((char*)buf1, "Error Tx\r\n");

 } else{

 ret1 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf1, 2, HAL_MAX_DELAY);

 if (ret1 != HAL_OK ) {

 strcpy((char*)buf1, "Error Rx\r\n");

 }else {

 buf2[0] = REGH_LSB;

 ret2 = HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf2, 1, HAL_MAX_DELAY);

 if (ret2 != HAL_OK) {

 strcpy((char*)buf2, "Error Tx\r\n");

 } else{

 ret2 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf2, 2, HAL_MAX_DELAY);

 if (ret2 != HAL_OK ) {

 strcpy((char*)buf2, "Error Rx\r\n");

 }else {

 H_OUT= buf1[0]<<8 | buf2[0];

 }

 }

 }

 }

 //Lettura valori per conversione umidità

 //1-H0_T0_OUT

 buf3[0] = REG_H0_T0_OUT_LSB ;

 ret3= HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf3, 1, HAL_MAX_DELAY);

 if (ret3 != HAL_OK) {

 strcpy((char*)buf3, "Error Tx\r\n");

 } else{

 ret3 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf3, 2, HAL_MAX_DELAY);

 if (ret1 != HAL_OK ) {

 strcpy((char*)buf3, "Error Rx\r\n");

 }else {

 buf4[0] = REG_H0_T0_OUT_MSB;

 ret4 = HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf4, 1, HAL_MAX_DELAY);

 if (ret4 != HAL_OK) {

 strcpy((char*)buf4, "Error Tx\r\n");

 } else{

 ret4 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf4, 2, HAL_MAX_DELAY);

 if (ret4 != HAL_OK ) {

 strcpy((char*)buf4, "Error Rx\r\n");

 }else {

 H0_T0_OUT= buf4[0]<<8 | buf3[0];

 }

 }

 }

 }

 //2-H1_T0_OUT

 buf5[0] = REG_H1_T0_OUT_LSB ;

 ret5= HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf5, 1, HAL_MAX_DELAY);

 if (ret5 != HAL_OK) {

 strcpy((char*)buf5, "Error Tx\r\n");

 } else{

 ret5 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf5, 2, HAL_MAX_DELAY);

 if (ret5 != HAL_OK ) {

 strcpy((char*)buf5, "Error Rx\r\n");

 }else {

 buf6[0] = REG_H1_T0_OUT_MSB;

 ret6 = HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf6, 1, HAL_MAX_DELAY);

 if (ret6 != HAL_OK) {

 strcpy((char*)buf6, "Error Tx\r\n");

 } else {

 ret6 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf6, 2, HAL_MAX_DELAY);

 if (ret6 != HAL_OK ) {

 strcpy((char*)buf6, "Error Rx\r\n");

 }else {

 H1_T0_OUT= buf6[0]<<8 | buf5[0];

 }

 }

 }

 }

 //3-H0_rHx2

 buf7[0] = REG_H0_rH;

 ret7= HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf7, 1, HAL_MAX_DELAY);

 if (ret7 != HAL_OK) {

 strcpy((char*)buf7, "Error Tx\r\n");

 } else{

 ret7 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf7, 2, HAL_MAX_DELAY);

 if (ret7 != HAL_OK ) {

 strcpy((char*)buf7, "Error Rx\r\n");

 }else {

 H0_rHx2= buf7[0];

 H0_rH= H0_rHx2 / 2;

 }

 }

 //4-H1_rHx2

 buf8[0] = REG_H1_rH;

  ret8= HAL_I2C_Master_Transmit(&hi2c1, ADDR, buf8, 1, HAL_MAX_DELAY);

  if (ret8 != HAL_OK) {

  strcpy((char*)buf8, "Error Tx\r\n");

  } else{

  ret8 = HAL_I2C_Master_Receive(&hi2c1, ADDR, buf8, 2, HAL_MAX_DELAY);

  if (ret8 != HAL_OK ) {

  strcpy((char*)buf8, "Error Rx\r\n");

  }else {

  H1_rHx2= buf8[0] ;

  H1_rH= H1_rHx2 /2;

  }

  }

 C1= H1_rH-H0_rH;

 C2=H_OUT-H0_T0_OUT;

 C3=C1*C2;

 C4=H1_T0_OUT-H0_T0_OUT;

 C5=C3/C4;

 HUM=C5+H0_rH;

    This topic has been closed for replies.

    1 reply

    ST Employee
    June 29, 2020

    Hi @MBalu.1​ , can you please be more specific on the error (code) you are getting? Just to understand if you are missing the I2C communication with the HTS221 sensor (you can check it reading the WHO_AM_I register), or you are reading wrong raw data, or it is a matter of code computation. I can suggest you the C libraries on github for the HTS221 part number (LINK), especially the hts221_read_data_polling.c example. I added STM32G4 tag for broader support from STM32 products' experts. Regards

    MBalu.1Author
    Visitor II
    June 30, 2020
    Hi, thank you for the answer, the code doesn't give me any error, I can read the humidity value but I can't understand if this value is correct. Il lun 29 giu 2020, 15:27 ST Community ha scritto:
    ST Employee
    June 30, 2020

    Hi @MBalu.1​ , and which values do you get? You should get something like 50% rH or similar value, I guess... In this technical note TN1218 you can check how to interpret humidity and temperature readings in the HTS221 digital humidity sensor. Regards