Drive TMOS module STHS34PF80(3) based on STM32CUBEMX----Modify the detection threshold

overview

Used to configure and set some parameters of the STHS34PF80 sensor for presence detection and motion detection.

Recently, I am taking courses of ST and Renesas RA. If you need samples, you can add a group application: 6_15061293.
insert image description here

sample application

https://www.wjx.top/vm/OhcKxJk.aspx#

video tutorial

https://www.bilibili.com/video/BV1Qm4y1p79X/

Drive TMOS module STHS34PF80(3) based on STM32CUBEMX----Modify the detection threshold

Complete code download

https://download.csdn.net/download/qq_24312945/88216813

Reference program initialization

Compared with driving the human detection demo, the new case sets the threshold, hysteresis and interrupt configuration related to the presence and motion of the sensor to realize the functions of presence detection and motion detection, and triggers an interrupt when related events occur.
insert image description here

power saving mode

The following text describes the use and function of Shutdown Mode. Shutdown mode is a sleep mode used to put the sensor device to sleep, saving power. In shutdown mode, the device stops data acquisition and most internal blocks are turned off to minimize current consumption. This enables the sensor to achieve the lowest power consumption level when powered.

Although the device is in shutdown mode, it keeps the I²C/SPI communication serial port active to be able to communicate with the device and configure settings. In shutdown mode, the contents of the configuration registers are retained, but the output data registers are not updated, which means that the last sampled data will remain in memory before entering shutdown mode.
The text provides the correct steps to enter shutdown mode and avoid reading wrong output data when re-entering continuous mode. However, these steps are not provided in your question, so the complete steps cannot be given.
insert image description here
The above document mainly operates on registers 0x25, 0x23, and 0x20, among which 0x25 is read multiple times, and the main function is to clear DRDY of STATUS (23h).
You can also know from the table below that there are many operations that can clear the DRDY of STATUS (23h).
insert image description here

Where 0x20 is the configuration rate register.

insert image description here

Set presence threshold

Presence detection is shown below.
insert image description here

Take setting the existence threshold as an example to explore the steps of setting.

insert image description here
The function used here is sths34pf80_presence_threshold_set(STHS34PF80_ADDRESS, 200).

/**
  * @brief  Presence threshold.[set]
  *
  * @param  ctx      read / write interface definitions
  * @param  val      presence threshold level
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_presence_threshold_set(uint8_t add, uint16_t val)
{
    
    
  sths34pf80_ctrl1_t ctrl1;
  uint8_t odr;
  uint8_t buff[2];
  int32_t ret;

  if ((val & 0x8000U) != 0x0U) {
    
    
    /* threshold values are on 15 bits */
    return -1;
  }

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20
  odr = ctrl1.odr;
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);

  buff[1] = (uint8_t)(val / 256U);
  buff[0] = (uint8_t)(val - (buff[1] * 256U));
  ret += sths34pf80_func_cfg_write(add, STHS34PF80_PRESENCE_THS, &buff[0], 2);//STHS34PF80_PRESENCE_THS->0x20U

  ret += sths34pf80_algo_reset(add);

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

  return ret;
}

At the beginning, the data of CTRL1 (20h) is read and saved to the odr variable at the same time.

  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20
  odr = ctrl1.odr;

ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0) This line of code is used to ensure that the state of the sensor is stable and safe before setting new parameters by calling the sths34pf80_tmos_odr_check_safe_set function before setting the presence threshold. The function is shown below.

/**
  * @brief  Enter to/Exit from power-down in a safe way
  *
  * @param  ctx      read / write interface definitions
  * @param  ctrl1    Value of CTRL1 register
  * @param  odr_new  Value of new odr to be set
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
static uint8_t sths34pf80_tmos_odr_check_safe_set(uint8_t add,sths34pf80_ctrl1_t ctrl1, uint8_t odr_new)
{
    
    
  sths34pf80_func_status_t func_status;
  sths34pf80_tmos_drdy_status_t status;
  int32_t ret = 0;

  if (odr_new > HAL_OK) {
    
    
    /*
     * Do a clean reset algo procedure everytime odr is changed to an
     * operative state.
     */
    ctrl1.odr = 0;
    ret = sths34pf80_write_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20

    ret += sths34pf80_algo_reset(add);
  } else {
    
    
    /* if we need to go to power-down from an operative state
     * perform the safe power-down.
     */
    if ((uint8_t)ctrl1.odr > 0U) {
    
    
      /* reset the DRDY bit */
      ret = sths34pf80_read_reg(add, STHS34PF80_FUNC_STATUS, (uint8_t *)&func_status, 1);//STHS34PF80_FUNC_STATUS->0x25

      /* wait DRDY bit go to '1' */
      do {
    
    
        ret += sths34pf80_tmos_drdy_status_get(add, &status);

      } while (status.drdy != 0U);

      /* set ODR to 0 */
      ctrl1.odr = 0;
      ret += sths34pf80_write_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);

      /* reset the DRDY bit */
      ret += sths34pf80_read_reg(add, STHS34PF80_FUNC_STATUS, (uint8_t *)&func_status, 1);
    }
  }

  ctrl1.odr = (odr_new & 0xfU);
  ret += sths34pf80_write_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);

  return ret;
}

The power-off mode is executed in else.
First execute sths34pf80_read_reg(add, STHS34PF80_FUNC_STATUS, (uint8_t *)&func_status, 1), the main function of reading 0x25 is to clear the DRDY of STATUS (23h), and then execute the following code.

      /* wait DRDY bit go to '1' */
      do {
    
    
        ret += sths34pf80_tmos_drdy_status_get(add, &status);

      } while (status.drdy != 0U);

The sths34pf80_tmos_drdy_status_get code mainly reads the DRDY of STATUS (23h). When the read is 0, it jumps out of the above loop.

/**
  * @brief  status of drdy.[get]
  *
  * @param  ctx      read / write interface definitions
  * @param  val      status of drdy bit (TAMB, TOBJ, TAMB_SHOCK, TPRESENCE, TMOTION).
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_tmos_drdy_status_get(uint8_t add, sths34pf80_tmos_drdy_status_t *val)
{
    
    
  sths34pf80_status_t status;
  int32_t ret;

  ret = sths34pf80_read_reg(add, STHS34PF80_STATUS, (uint8_t *)&status, 1);//STHS34PF80_STATUS->0x23

  val->drdy = status.drdy;

  return ret;
}

Then return to the sths34pf80_tmos_odr_check_safe_set function and perform the following operations.
The main function is to clear the ODR data in CTRL1 (20h) and read 0x25 at the same time. The main function is to clear the DRDY of STATUS (23h)

      /* set ODR to 0 */
      ctrl1.odr = 0;
      ret += sths34pf80_write_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20

      /* reset the DRDY bit */
      ret += sths34pf80_read_reg(add, STHS34PF80_FUNC_STATUS, (uint8_t *)&func_status, 1);//STHS34PF80_FUNC_STATUS->0x25

Then execute the following code to rewrite the ODR data into the CTRL1 (20h) register.

  ctrl1.odr = (odr_new & 0xfU);
  ret += sths34pf80_write_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20

Then return to the sths34pf80_presence_threshold_set function and perform the following operations.
Save the incoming data into the buff array, and then execute sths34pf80_func_cfg_write to pass the buff data into PRESENCE_THS (20h - 21h).

 buff[1] = (uint8_t)(val / 256U);
  buff[0] = (uint8_t)(val - (buff[1] * 256U));
  ret += sths34pf80_func_cfg_write(add, STHS34PF80_PRESENCE_THS, &buff[0], 2);//STHS34PF80_PRESENCE_THS->0x20U

Check out the sths34pf80_func_cfg_write function as shown below.

/**
  * @brief  Function Configuration write
  *
  * @param  ctx      read / write interface definitions
  * @param  addr     embedded register address
  * @param  data     embedded register data
  * @param  len      embedded register data len
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_func_cfg_write(uint8_t add, uint8_t addr, uint8_t *data, uint8_t len)
{
    
    
  sths34pf80_ctrl1_t ctrl1;
  uint8_t odr;
  sths34pf80_page_rw_t page_rw = {
    
    0};
  int32_t ret;
  uint8_t i;

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20
  odr = ctrl1.odr;
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);

  /* Enable access to embedded functions register */
  ret += sths34pf80_mem_bank_set(add, STHS34PF80_EMBED_FUNC_MEM_BANK);

  /* Enable write mode */
  page_rw.func_cfg_write = 1;
  ret += sths34pf80_write_reg(add, STHS34PF80_PAGE_RW, (uint8_t *)&page_rw, 1);

  /* Select register address (it will autoincrement when writing) */
  ret += sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, &addr, 1);

  for (i = 0; i < len; i++)
  {
    
    
    /* Write data */
    ret += sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, &data[i], 1);
  }

  /* Disable write mode */
  page_rw.func_cfg_write = 0;
  ret += sths34pf80_write_reg(add, STHS34PF80_PAGE_RW, (uint8_t *)&page_rw, 1);

  /* Disable access to embedded functions register */
  ret += sths34pf80_mem_bank_set(add, STHS34PF80_MAIN_MEM_BANK);

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

  return ret;
}

Analyze the above code, where sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1) is to read the ODR data, and then execute sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0), this function has been explained above, mainly to enter a power-down mode to ensure that the state of the sensor is stable and safe before setting new parameters to prevent abnormal conditions when setting parameters. Then execute sths34pf80_mem_bank_set(add, STHS34PF80_EMBED_FUNC_MEM_BANK), the specific operation of this function is as follows.

/**
  * @brief  Change memory bank.[set]
  *
  * @param  ctx      read / write interface definitions
  * @param  val      MAIN_MEM_BANK, EMBED_FUNC_MEM_BANK, SENSOR_HUB_MEM_BANK, STRED_MEM_BANK,
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_mem_bank_set(uint8_t add, sths34pf80_mem_bank_t val)
{
    
    
  sths34pf80_ctrl2_t ctrl2;
  int32_t ret;

  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&ctrl2, 1);//STHS34PF80_CTRL2->0x21

  if (ret == HAL_OK)
  {
    
    
    ctrl2.func_cfg_access = ((uint8_t)val & 0x1U);
    ret = sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&ctrl2, 1);//STHS34PF80_CTRL2->0x21
  }

  return ret;
}
CTRL2 (21h)如下所示,对FUNC_CFG_ACCESS设置为1主要是开启访问内嵌函数寄存器。


继续返回sths34pf80_func_cfg_write函数,
之后执行如下函数,主要为向寄存器PAGE_RW (11h)的FUNC_CFG_WRITE标志位置为为1,启用嵌入式函数的写过程。
  /* Enable write mode */
  page_rw.func_cfg_write = 1;
  ret += sths34pf80_write_reg(add, STHS34PF80_PAGE_RW, (uint8_t *)&page_rw, 1);

insert image description here
Continue to return to the sths34pf80_func_cfg_write function, and then execute the following function, which is to write or read the address of the embedded function, depending on whether PAGE_RW (11h) is set to read or write, and write a fixed data instruction at the same time, enter after writing PAGE_RW (11h) Closes read or write operations.

  /* Select register address (it will autoincrement when writing) */
  ret += sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, &addr, 1);
  for (i = 0; i < len; i++)
  {
    
    
    /* Write data */
    ret += sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, &data[i], 1);
  }
  /* Disable write mode */
  page_rw.func_cfg_write = 0;
  ret += sths34pf80_write_reg(add, STHS34PF80_PAGE_RW, (uint8_t *)&page_rw, 1);

insert image description here
The address addr passed here is STHS34PF80_PRESENCE_THS, which is a 15-bit register. The PRESENCE_THS register (address range from 0x20 to 0x21) is mainly used to set the presence threshold of the presence detection algorithm. Presence detection algorithms are used to determine the presence or absence of a certain state or condition, usually in relation to sensor measurement data.
The specific explanation is as follows:
Presence Threshold (Presence Threshold): This is a threshold for the presence detection algorithm. Thresholds define when a condition is considered to exist in the measurement data. Here, the threshold is a 15-bit unsigned integer (range 0 to 32767).
Default value: The preset presence threshold default value is 200 (0x00C8).

insert image description here
Then execute sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr) to rewrite odr.

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

At this point, sths34pf80_func_cfg_write has been set up, return to the sths34pf80_presence_threshold_set function, and execute the sths34pf80_algo_reset operation.

  ret += sths34pf80_algo_reset(add);

The specific operation of this function is as follows.

/**
  * @brief  Reset algo
  *
  * @param  ctx      read / write interface definitions
  * @param  val      reset algo structure
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_algo_reset(uint8_t add)
{
    
    
  uint8_t tmp;
  int32_t ret;

  tmp = 1;
  ret = sths34pf80_func_cfg_write(add, STHS34PF80_RESET_ALGO, &tmp, 1);//STHS34PF80_RESET_ALGO->0x2A

  return ret;
}

This function sets bit 1 of ALGO_ENABLE_RESET of RESET_ALGO (2Ah) to perform algorithm reset operation. By default, this bit has a value of 0, indicating no algorithm reset. When the user modifies the parameters related to the algorithm (such as threshold, hysteresis, etc.), the ALGO_ENABLE_RESET bit needs to be set to 1. After the reset is complete, the ALGO_ENABLE_RESET bit should return to the default value of 0.
insert image description here

Then execute sths34pf80_tmos_odr_check_safe_set to re-import the ODR data just saved.

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

So far, the sths34pf80_presence_threshold_set function has been executed.

There is a lag in the settings

The function used here is sths34pf80_presence_hysteresis_set(STHS34PF80_ADDRESS, 20).

/**
  * @brief  Presence hysteresis.[set]
  *
  * @param  ctx      read / write interface definitions
  * @param  val      Presence hysteresis value
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_presence_hysteresis_set(uint8_t add, uint8_t val)
{
    
    
  sths34pf80_ctrl1_t ctrl1;
  uint8_t odr;
  int32_t ret;

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);
  odr = ctrl1.odr;
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);

  ret += sths34pf80_func_cfg_write(add, STHS34PF80_HYST_PRESENCE, &val, 1);

  ret += sths34pf80_algo_reset(add);

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

  return ret;
}

At the beginning, the data of CTRL1 (20h) is read and saved to the odr variable at the same time.

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20
  odr = ctrl1.odr;

ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0) This line of code is used to ensure that the state of the sensor is stable and safe before setting new parameters by calling the sths34pf80_tmos_odr_check_safe_set function before setting the presence threshold.
Then save the incoming data into the val array, and then execute sths34pf80_func_cfg_write to pass the val data into HYST_PRESENCE (27h).

  ret += sths34pf80_func_cfg_write(add, STHS34PF80_HYST_PRESENCE, &val, 1);

This hysteresis value helps avoid frequent state transitions around boundary values.
Default: The default hysteresis value is 32 (0x32).
insert image description here
For example, suppose PRESENCE_THS is set to 200 and HYST_PRESENCE is set to 32. When the measured value exceeds 200, the sensor assumes that a certain state exists. Then, to switch from the presence state to the non-presence state, the sensor must decrease the measured value by at least 200 - 32 = 168. Only when the measured value drops below 168 does the sensor trigger a state transition.
By setting an appropriate hysteresis value, it is possible to prevent unnecessary state switching when the measured value fluctuates around the threshold, thus improving the stability of presence detection.
Then execute the sths34pf80_algo_reset function, which mainly sets the ALGO_ENABLE_RESET bit of RESET_ALGO (2Ah) to 1, and performs the algorithm reset operation.

 ret += sths34pf80_algo_reset(add);

Then execute sths34pf80_tmos_odr_check_safe_set to re-import the ODR data just saved.

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

So far, the sths34pf80_presence_hysteresis_set function has been executed.

Set Action Threshold

The function used here is sths34pf80_motion_threshold_set(STHS34PF80_ADDRESS, 300).

/**
  * @brief  Motion threshold.[set]
  *
  * @param  ctx      read / write interface definitions
  * @param  val      motion threshold level
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_motion_threshold_set(uint8_t add,  uint16_t val)
{
    
    
  sths34pf80_ctrl1_t ctrl1;
  uint8_t odr;
  uint8_t buff[2];
  int32_t ret;

  if ((val & 0x8000U) != 0x0U) {
    
    
    /* threshold values are on 15 bits */
    return -1;
  }

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);
  odr = ctrl1.odr;
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);

  buff[1] = (uint8_t)(val / 256U);
  buff[0] = (uint8_t)(val - (buff[1] * 256U));
  ret += sths34pf80_func_cfg_write(add, STHS34PF80_MOTION_THS, &buff[0], 2);

  ret += sths34pf80_algo_reset(add);

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

  return ret;
}

At the beginning, the data of CTRL1 (20h) is read and saved to the odr variable at the same time.

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);
  odr = ctrl1.odr;

ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0) This line of code is used to ensure that the state of the sensor is stable and safe before setting new parameters by calling the sths34pf80_tmos_odr_check_safe_set function before setting the presence threshold.

  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);

Then save the incoming data into the buff array, and then execute sths34pf80_func_cfg_write to pass the buff data into MOTION_THS (22h - 23h).

  buff[1] = (uint8_t)(val / 256U);
  buff[0] = (uint8_t)(val - (buff[1] * 256U));
  ret += sths34pf80_func_cfg_write(add, STHS34PF80_MOTION_THS, &buff[0], 2);

The MOTION_THS register (address range 0x22 to 0x23) is used to set the threshold for the motion detection algorithm. Motion detection algorithms are used to detect whether motion or motion of an object has occurred.
Here is what this register does:
Motion Threshold: This is a threshold for the motion detection algorithm. Thresholds define when motion or motion of an object is considered to have occurred in the measurement data. Threshold is a 15-bit unsigned integer ranging from 0 to 32767.
Default value: The preset motion threshold default value is 200 (0x00C8).
For example, if MOTION_THS is set to 300, it means that when a certain value measured by the sensor exceeds 300, the sensor will consider a motion or action state to occur.
By setting an appropriate motion threshold, the sensitivity of motion detection can be adjusted according to application requirements. Higher thresholds cause large changes to be considered motion, while lower thresholds make the sensor more sensitive and may even trigger motion detection on small changes.
insert image description here

Then execute the sths34pf80_algo_reset function, which mainly sets the ALGO_ENABLE_RESET bit of RESET_ALGO (2Ah) to 1, and performs the algorithm reset operation.

 ret += sths34pf80_algo_reset(add);

Then execute sths34pf80_tmos_odr_check_safe_set to re-import the ODR data just saved.

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

At this point, the sths34pf80_motion_threshold_set function has been executed.

set action lag

The function used here is sths34pf80_motion_hysteresis_set(STHS34PF80_ADDRESS, 30).

/**
  * @brief  Motion hysteresis threshold.[set]
  *
  * @param  ctx      read / write interface definitions
  * @param  val      Motion hysteresis value
  * @retval          interface status (MANDATORY: return 0 -> no Error)
  *
  */
uint8_t sths34pf80_motion_hysteresis_set(uint8_t add, uint8_t val)
{
    
    
  sths34pf80_ctrl1_t ctrl1;
  uint8_t odr;
  int32_t ret;

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);
  odr = ctrl1.odr;
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);

  ret += sths34pf80_func_cfg_write(add, STHS34PF80_HYST_MOTION, &val, 1);

  ret += sths34pf80_algo_reset(add);

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

  return ret;
}

At the beginning, the data of CTRL1 (20h) is read and saved to the odr variable at the same time.

  /* Save current odr and enter PD mode */
  ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&ctrl1, 1);//STHS34PF80_CTRL1->0x20
  odr = ctrl1.odr;

ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0) This line of code is used to ensure that the state of the sensor is stable and safe before setting new parameters by calling the sths34pf80_tmos_odr_check_safe_set function before setting the presence threshold.
Then save the incoming data into the val array, and then execute sths34pf80_func_cfg_write to pass the val data into HYST_MOTION (26h).

  ret += sths34pf80_func_cfg_write(add, STHS34PF80_HYST_MOTION, &val, 1);

In motion detection algorithms, hysteresis refers to the time or condition that needs to be waited when switching from a moving state to a stationary state, in order to prevent frequent state switching in a short period of time.
Default: The default hysteresis value is 32 (0x32).
insert image description here

For example, if MOTION_THS is set to 300 and HYST_MOTION is set to 32. When the measurement exceeds 300, the sensor assumes that motion has occurred. Then, to switch from a motion state to a non-motion state, the sensor has to reduce the measured value by at least 300 - 32 = 268. Only when the measured value drops below 268 does the sensor trigger a state transition.
By setting an appropriate motion hysteresis value, unnecessary state switching can be avoided when the measured value fluctuates around the motion state, thereby improving the stability of motion detection. This is similar to the concept in presence detection, but for motion states.
Then execute the sths34pf80_algo_reset function, which mainly sets the ALGO_ENABLE_RESET bit of RESET_ALGO (2Ah) to 1, and performs the algorithm reset operation.

 ret += sths34pf80_algo_reset(add);

Then execute sths34pf80_tmos_odr_check_safe_set to re-import the ODR data just saved.

  /* Set saved odr back */
  ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);

At this point, the sths34pf80_motion_hysteresis_set function has been executed.

main program

Initialization is as follows.

  /* USER CODE BEGIN 2 */
  sths34pf80_lpf_bandwidth_t lpf_m, lpf_p, lpf_p_m, lpf_a_t;	
  sths34pf80_tmos_drdy_status_t status;	
  sths34pf80_tmos_func_status_t func_status;		
	
	HAL_Delay(200);	
	printf("123");
	uint8_t STHS34PF80_ID =STHS34PF80_getChipID(STHS34PF80_ADDRESS);
	printf("STHS34PF80_ID=0x%x\n",STHS34PF80_ID);	
	if (STHS34PF80_ID != 0xD3)
    while(1);
/* Set averages (AVG_TAMB = 8, AVG_TMOS = 32) */
  sths34pf80_avg_tobject_num_set(STHS34PF80_ADDRESS, STHS34PF80_AVG_TMOS_32);
  sths34pf80_avg_tambient_num_set(STHS34PF80_ADDRESS, STHS34PF80_AVG_T_8);

  /* read filters */
  sths34pf80_lpf_m_bandwidth_get(STHS34PF80_ADDRESS, &lpf_m);
  sths34pf80_lpf_p_bandwidth_get(STHS34PF80_ADDRESS, &lpf_p);
  sths34pf80_lpf_p_m_bandwidth_get(STHS34PF80_ADDRESS, &lpf_p_m);
  sths34pf80_lpf_a_t_bandwidth_get(STHS34PF80_ADDRESS, &lpf_a_t);

printf("lpf_m: %02d, lpf_p: %02d, lpf_p_m: %02d, lpf_a_t: %02d\r\n", lpf_m, lpf_p, lpf_p_m, lpf_a_t);
	
	  /* Set BDU */
  sths34pf80_block_data_update_set(STHS34PF80_ADDRESS, 1);
	
	sths34pf80_presence_threshold_set(STHS34PF80_ADDRESS, 300);	//设置存在阈值。
  sths34pf80_presence_hysteresis_set(STHS34PF80_ADDRESS, 20);//“存在滞后”(Presence Hysteresis)的函数
  sths34pf80_motion_threshold_set(STHS34PF80_ADDRESS, 300);//设置动作阈值
  sths34pf80_motion_hysteresis_set(STHS34PF80_ADDRESS, 30);	动作滞后”(Motion Hysteresis)的函数
  /* Set ODR */
  sths34pf80_tmos_odr_set(STHS34PF80_ADDRESS, STHS34PF80_TMOS_ODR_AT_30Hz);

    int32_t cnt = 0;
	
  /* USER CODE END 2 */

The main function is shown below.

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    sths34pf80_tmos_drdy_status_get(STHS34PF80_ADDRESS, &status);
    if (status.drdy)
    {
    
    
      sths34pf80_tmos_func_status_get(STHS34PF80_ADDRESS, &func_status);
			printf("-->环境温度冲击检测标志位 %d - 存在检测标志位 %d - 运动检测标志位 %d\r\n",func_status.tamb_shock_flag, func_status.pres_flag, func_status.mot_flag);
   }
			HAL_Delay(1000);

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132269300