Based on STM32CUBEMX driving TMOS module STHS34PF80----4. Interrupt acquisition signal
- overview
- video teaching
- sample application
- video tutorial
- Reference Demo
- Reference Demo
- Complete code download
- Embedded function address
- Serial port configuration
- IIC configuration
- IO port settings
- Serial port redirection
- Reference program initialization
- IIC write function
- IIC read function
- get id
- Autoboot process and shutdown mode configuration for devices
- power saving mode
- Sensitivity value for temperature data
- set low pass filter
- Temperature measurement filter method
- write embedded function
- read embedded function
- reset module
- Clear DRDY
overview
The STHS34PF80 sensor project modifies the Arduino script and re-ports it to the STM32 MCU.
This project is based on the STHS34PF80 IR Temperature Sensor which is capable of detecting ambient and object temperature and detects presence and motion within a maximum range of 4 meters. There is an Arduino script that shows how to configure the sensor for basic ambient and object temperature measurements, and how to configure the embedded function algorithms and use them to detect presence and motion. The script allows continuous or one-shot mode, allowing changes to the low-pass filter and detection threshold for various detection behaviors. The script utilizes the embedded interrupt engine to detect the readiness of temperature data and to notify presence and motion events.
Overall, this is an interesting project using the STHS34PF80 IR temperature sensor for presence and motion detection for a variety of applications, from environmental and object temperature monitoring to people and animal counting. This project already has a functional Arduino script as a starting point for further development and testing by anyone interested in using this sensor.
The sensor's built-in intelligent digital algorithm. The sensor has three different detection modes: presence detection, motion detection, and ambient temperature impact detection. By configuring the embedded function registers, the parameters of the embedded functions can be adjusted.
Recently, I am taking courses of ST and Renesas RA. If you need samples, you can add a group application: 6_15061293.
video teaching
https://www.bilibili.com/video/BV1kF411C71S/
Based on STM32CUBEMX driver TMOS module STHS34PF80 (5) ---- configure embedded functions
sample application
https://www.wjx.top/vm/OhcKxJk.aspx#
video tutorial
Reference Demo
Reference Demo
hthttps://github.com/kriswiner/STHS34PF80/tree/main
Complete code download
https://download.csdn.net/download/qq_24312945/88257572
Embedded function address
Embedded function registers in the STHS34PF80 sensor. These registers can be used to configure the embedded algorithms for compensating for ambient temperature variations in object temperature, as well as the embedded smart digital algorithms.
Serial port configuration
Check the schematic diagram, PA9 and PA10 are set as the serial ports of the development board.
Configure the serial port.
IIC configuration
In this application, the STS34PF80 module communicates with the main controller through the I2C (IIC) interface. Specifically, the I2C pins of the STS34PF80 module are connected to two IO ports of the main controller, PB6 (pin B6) and PB7 (pin B7).
Configure the IIC as normal mode, and the speed is 100k.
IO port settings
The STS34PF80IO settings are shown below.
In IIC mode, CS needs to give a high level.
The official IIC wiring is as follows.
PA8 needs to be configured as output mode, the default is high level, and PA7 is configured as input mode.
Serial port redirection
Open the magic wand, check MicroLIB
In main.c, add the header file. If you don’t add it, an identifier “FILE” is undefined error will appear.
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
Function declaration and serial port redirection:
/* USER CODE BEGIN PFP */
int fputc(int ch, FILE *f){
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END PFP */
Reference program initialization
IIC write function
The address of STHS34PF80 is 101 1010 (0x5A). If it is a write operation, the specific address is 1011 0100 (0xB4).
/**
* @brief 读取数据
*
* @param add 模块地址
* @param reg 寄存器地址
* @param data buffer 缓冲区
* @param len 读取长度
* @retval ret 正常返回HAL_OK
*
*/
uint8_t sths34pf80_read_reg(uint8_t add,uint8_t reg, uint8_t * data, uint8_t len)
{
uint8_t ret;
ret=HAL_I2C_Mem_Read(&hi2c1 ,(add<<1)|1,reg,I2C_MEMADD_SIZE_8BIT,data,len,0xffff);
return ret;
}
IIC read function
The address of STHS34PF80 is 101 1010 (0x5A). If it is a read operation, the specific address is 1011 0101 (0xB5).
/**
* @brief 写入数据
*
* @param add 模块地址
* @param reg 寄存器地址
* @param data buffer 缓冲区
* @param len 写入长度
* @retval ret 正常返回HAL_OK
*
*/
uint8_t sths34pf80_write_reg(uint8_t add,uint8_t reg, uint8_t * data, uint8_t len)
{
uint8_t ret;
HAL_I2C_Mem_Write(&hi2c1 ,(add<<1)|0,reg,I2C_MEMADD_SIZE_8BIT,data,len,0xffff);
return ret;
}
get id
Refer to the corresponding ID acquisition driver program in the sample program, as shown below.
You can view 0x0F to obtain the ID, and the read value should be 0xD3.
The read function is as follows.
/**
* @brief 获取设备ID
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
uint8_t STHS34PF80_getChipID(uint8_t add)
{
uint8_t temp[1]={0};
sths34pf80_read_reg(add,STHS34PF80_WHO_AM_I,temp,1);
return temp[0];
}
The driver is shown below.
uint8_t STHS34PF80_ID = STHS34PF80_getChipID(STHS34PF80_ADDRESS); // Read CHIP_ID register for STHS34PF80
printf("STHS34PF80_ID=0x%x\n",STHS34PF80_ID);
Autoboot process and shutdown mode configuration for devices
After the device is powered up, it performs a 2.5 ms boot process to download calibration coefficients from the embedded flash to internal registers and load the default contents of the AVG_TRIM (10h), CTRL0 (17h) and SENS_DATA (1Dh) registers. During the boot process, the device's registers are not accessible.
After booting is complete, the device is automatically configured in shutdown mode. After power-on, the boot process can be restarted by setting the BOOT bit of the CTRL2 (21h) register to 1 to reload the default values of the above registers. There is no need to switch the power cord of the device.
During a reboot, the device's registers are not accessible. After the reboot is complete, the device is automatically configured to the previous working mode and the BOOT bit is automatically cleared to 0.
After modification, it looks like this.
/**
* @brief 传感器重置
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
uint8_t STHS34PF80_reset(uint8_t add)
{
uint8_t temp = 0;
int32_t ret;
//STHS34PF80_CTRL2->0x21
ret=sths34pf80_read_reg(add,STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
if (ret == HAL_OK)
{
temp=temp | 0x80;
ret = sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);// set bit 7 to force device reset, wait 2.5 ms for reboot
}
HAL_Delay(3);
return ret;
}
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.
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 DRDY of STATUS (23h), among which reading FUNC_STATUS (25h) can clear DRDY.
Where 0x20 is the configuration rate register.
The power off code provided by the reference documentation is as follows.
After modification, it is as follows.
void STHS34PF80_powerDown(uint8_t add)
{
uint8_t stat = 0;
//STHS34PF80_FUNC_STATUS->0x25
STHS34PF80_ReadByte(add,STHS34PF80_FUNC_STATUS); // This is done to reset the DRDY bit of the STATUS (23h) register to 0
//STHS34PF80_STATUS->0x23
stat=STHS34PF80_ReadByte(add,STHS34PF80_STATUS);
while( (stat & 0x04) )
{
stat=STHS34PF80_ReadByte(add,STHS34PF80_STATUS);
HAL_Delay(1);
} // wait for STATUS DR bit --> 0
uint8_t temp=0;
//STHS34PF80_CTRL1->0x20
temp=STHS34PF80_ReadByte(add,STHS34PF80_CTRL1);
STHS34PF80_WriteByte(add,STHS34PF80_CTRL1, temp & ~(0x07));// set bits 0 - 3 to 0 to power downt
//STHS34PF80_FUNC_STATUS->0x25
STHS34PF80_ReadByte(add,STHS34PF80_FUNC_STATUS); // reset DR bit of STATUS to
}
Sensitivity value for temperature data
τ represents the transmittance of the optical system over the operating wavelength range (values range from 0 to 1), and SENSITIVITY DEFAULT is the default sensitivity value for object temperature data (typically around 2000 LSB/°C). The default sensitivity values are obtained by performing a per-unit calibration using a blackbody covering the full field of view of the sensor without optics (i.e., τ = 1).
Changes in transmittance may affect sensor readings, so sensitivity may need to be adjusted or accounted for by transmittance when using the sensor with optics.
It can be obtained by reading the SENS_DATA (1Dh) register.
The reference code is as follows.
After modification, it is as follows.
/**
* @brief 获取设备灵敏度
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
int16_t STHS34PF80_readSenseData(uint8_t add)
{
uint8_t temp = 0;
int32_t ret;
//STHS34PF80_SENS_DATA->0x1D
ret = sths34pf80_read_reg(add, STHS34PF80_SENS_DATA, (uint8_t *)&temp, 1);
return (int16_t)((int16_t)temp << 8 | 0x00);
}
The main program prints the saved values.
// 获取设备灵敏度
ObjSense = (STHS34PF80_readSenseData(STHS34PF80_ADDRESS) / 16) + 2048;
printf("Object Sense Data (LSB/oC) =%d \n",ObjSense)
set low pass filter
Configuration The low-pass filter can be configured through LPF1 (0Ch) and LPF2 (0Dh).
STHS34PF80 has built-in embedded algorithms, and the device has three embedded detection modes, including presence detection, motion detection and ambient temperature shock detection.
Each algorithm uses a different low-pass filter (LPF_P, LPF_M and LPF_A_T). The presence and motion detection algorithms also share another low-pass filter (LPF_P_M). These filters are used to generate intermediate signals (TPRESENCE, TMOTION and TAMB_SHOCK) that can be used to fine-tune the algorithm itself.
Below are the block diagrams of presence detection and motion detection respectively.
The reference document settings are as follows.
Modify as follows.
/**
* @brief 设置低通滤波器
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
uint8_t STHS34PF80_setLowpassFilters(uint8_t add,uint8_t lpf_P, uint8_t lpf_M, uint8_t lpf_PM, uint8_t lpf_Tshock)
{
int32_t ret;
uint8_t LPF1,LPF2;
LPF1=lpf_PM << 3 | lpf_M;
LPF2=lpf_P << 3 | lpf_Tshock;
//STHS34PF80_LPF1->0x0C
ret = sths34pf80_write_reg(add, STHS34PF80_LPF1, (uint8_t *)&LPF1, 1);
if(ret==HAL_OK)
{
//STHS34PF80_LPF2->0x0D
ret=sths34pf80_write_reg(add, STHS34PF80_LPF2, (uint8_t *)&LPF2, 1);
}
return ret;
}
Temperature measurement filter method
First you need to set the AVG_TRIM (10h) register to configure the parameters of the temperature average filter to control the smoothness and stability of the ambient temperature and target temperature measurement. In temperature measurement, it may be affected by noise and interference, which may lead to short-term fluctuations in the measured value. By applying mean filtering, these fluctuations can be smoothed out, resulting in more stable temperature data. The setting in the AVG_TRIM register determines the level of averaging filtering, which affects the time constant and smoothness of the filtering.
The default parameters here are as follows
- Write 02h in the AVG_TRIM (10h) register // AVG_T = 8, AVG_TMOS = 32
- Write 07h in CTRL1 (20h) register // ODR = 15 Hz
AVG_T[1:0]: This setting bit is used to select the average filter sample number of ambient temperature.
AVG_TMOS[2:0]: This setting bit is used to select the number of averaging filter samples for the target temperature and the noise level associated with it.
Then you need to set the temperature range, CTRL0 (17h) is mainly used to configure the gain of the device to adapt to different operating temperature ranges. The specific gain setting will determine the temperature range over which the device can operate.
A value of 000 indicates wide mode.
A value of 111 indicates the default gain mode.
Looking at the table below, the default gain mode can be set when there is not much difference between room and sensor temperatures.
Set interrupt can be configured by CTRL3 (22h) register.
This register provides the system with key information about the current state of the device, which can be used to drive other logic or trigger corresponding operations, such as interrupt service routines. For example, PRES_FLAG could be used to determine if someone has entered an area, MOT_FLAG could be used to detect human motion, and TAMB_SHOCK_FLAG might be used in an environmental monitoring system to catch sudden temperature changes.
By checking FUNC_STATUS (25h), it can be known that PRES_FLAG is presence detection, MOT_FLAG is motion detection, and TAMB_SHOCK_FLAG is the ambient temperature shock detection flag.
The output mode can be set by configuring IEN[1:0] of CTRL3 (22h) register.
The IEN[1:0] bits are in the CTRL3 register, which defines which signal should be routed to the INT pin (i.e. interrupt output):
00: INT pin is in a high-impedance state.
01: Route DRDY (data ready signal) to INT pin.
10: Route the INT_OR signal to the INT pin.
The reference program configuration is as follows.
After modification, it looks like this.
/**
* @brief 设置温度测量滤波方式
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
uint8_t STHS34PF80_config(uint8_t add,uint8_t avgt, uint8_t avgtmos, uint8_t gain, bool functions)
{
int32_t ret;
uint8_t temp;
uint8_t AVG_TRIM;
AVG_TRIM = avgt << 4 | avgtmos;
//STHS34PF80_AVG_TRIM->0x10//设置滤波样本数
ret = sths34pf80_write_reg(add, STHS34PF80_AVG_TRIM, (uint8_t *)&AVG_TRIM, 1);
if(ret==HAL_OK)
{
//STHS34PF80_CTRL0->0x17//设置温度增益
sths34pf80_read_reg(add, STHS34PF80_CTRL0, (uint8_t *)&temp, 1);
temp=temp | gain << 4;
ret = sths34pf80_write_reg(add, STHS34PF80_CTRL0, (uint8_t *)&temp, 1);
// Configure interrupt behavior
// select active HIGH (bit 7 == 0), push-pull (bit 6 == 0), pulsed mode (bit 2 == 0)
temp=0x01;//STHS34PF80_CTRL3->0x22
ret = sths34pf80_write_reg(add, STHS34PF80_CTRL3, (uint8_t *)&temp, 1);
temp=0x3A;//0011 1010
// if(functions) _i2c_bus->writeByte(STHS34PF80_ADDRESS, STHS34PF80_CTRL3, 0x32 ); // configure INT_OR for either Presence or Motion detection
if(functions)
ret = sths34pf80_write_reg(add, STHS34PF80_CTRL3, (uint8_t *)&temp, 1);
}
return ret;
}
write embedded function
The embedded function write operation is as follows.
The case implementation is as follows.
CTRL2 (21h) is shown below, setting FUNC_CFG_ACCESS to 1 is mainly to enable access to embedded function registers.
The FUNC_CFG_WRITE flag bit of the register PAGE_RW (11h) is set to 1 to enable the write process of the embedded function.
/************开启嵌入式函数读写************/
uint8_t temp ; //STHS34PF80_CTRL2->0x21
sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
temp = temp | 0x10;//STHS34PF80_CTRL2->0x21 //开启嵌入式函数访问
sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);//允许访问嵌入式函数寄存器
temp = 0x40; //STHS34PF80_FUNC_PAGE_RW->0x11 //开启嵌入式函数写操作
sths34pf80_write_reg(add, STHS34PF80_FUNC_PAGE_RW, (uint8_t *)&temp, 1); //允许写入嵌入函数寄存器
The register FUNC_CFG_ADDR (08h) is the address for configuring the embedded function, and FUNC_CFG_DATA (09h) is the incoming data to the embedded function.
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).
/************设置存在阈值************/
temp = STHS34PF80_PRESENCE_THS_L;//STHS34PF80_FUNC_CFG_ADDR->0x08 嵌入式地址PRESENCE_THS (20h - 21h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为PRESENCE_THS_L
temp =presence_ths ;//STHS34PF80_FUNC_CFG_DATA->0x09
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入低字节
temp = STHS34PF80_PRESENCE_THS_H;//STHS34PF80_FUNC_CFG_ADDR->0x08 嵌入式地址PRESENCE_THS (20h - 21h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为PRESENCE_THS_H
temp = presence_ths>>8;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入高字节
MOTION_THS (22h - 23h) This register is used to set the threshold of the motion detection algorithm. In practical applications, you may need to read or set this register to adjust or view the threshold of the motion detection algorithm. A value that is too high may result in insensitive motion detection; a value that is too low may result in false positives. This value ranges from 0 to 32767. The default value is 200.
/************设置运动阈值************/
temp = STHS34PF80_MOTION_THS_L;//嵌入式地址MOTION_THS (22h - 23h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为MOTION_THS_L
temp = motion_ths ;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入低字节
temp = STHS34PF80_MOTION_THS_H;//嵌入式地址MOTION_THS (22h - 23h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为MOTION_THS_H
temp = motion_ths >>8;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入高字节
TAMB_SHOCK_THS (24h - 25h) This register is used to set the threshold of the ambient temperature shock detection algorithm. In short, when the ambient temperature suddenly changes to a certain extent (beyond this set threshold), the system may consider that a "temperature shock" event has occurred. This is a 15-bit unsigned integer. That means it has a range of 0 to 32767.
/************设置环境温度冲击阈值************/
temp = STHS34PF80_TAMB_SHOCK_THS_L;//嵌入式地址TAMB_SHOCK_THS (24h - 25h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为TAMB_SHOCK_THS_L
temp = tamb_shock_ths ;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入低字节
temp = STHS34PF80_TAMB_SHOCK_THS_H;//嵌入式地址TAMB_SHOCK_THS (24h - 25h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为TAMB_SHOCK_THS_H
temp = tamb_shock_ths >>8;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入高字节
HYST_PRESENCE (27h) This register is used to set the hysteresis configuration value of the presence detection algorithm. Hysteresis is an important concept in many sensor applications to help reduce frequent output switching due to small or temporary input changes. In short, hysteresis provides a certain "tolerance" to the sensor's reading, and only when the input value exceeds this tolerance range does the output change. This is an 8-bit unsigned integer, so its range is 0 to 255, and the default value is 32.
/************设置存在滞后值************/
temp = STHS34PF80_HYST_PRESENCE;//嵌入式地址HYST_PRESENCE (27h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为HYST_PRESNCE
temp = presence_hyst;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入1字节
HYST_MOTION (26h) This register is used to set the hysteresis configuration value for the motion detection algorithm. This is an 8-bit unsigned integer, so its range is 0 to 255, and the default value is 32.
/************设置运动滞后值************/
temp = STHS34PF80_HYST_MOTION;//嵌入式地址HYST_MOTION (26h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为HYST_MOTION
temp = motion_hyst;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入1字节
The ALGO_CONFIG (28h) register is used to configure and tune the behavior of the algorithm, including interrupt pulse mode, temperature compensation, and absolute value selection for the presence detection algorithm. Appropriate configuration can make the algorithm more suitable for the needs of specific applications.
INT_PULSED: When set to 1, the flag generated by the algorithm will be pulsed on the INT pin (high level for the time defined by ODR). The default value is 0.
0: Latch mode
1: Pulse mode
COMP_TYPE: Enable built-in linear algorithm for compensating ambient temperature variation in object temperature. The default value is 0.
0: Disabled
1: Enabled
SEL_ABS: Select absolute value in presence detection algorithm. The default value is 0.
0: Absolute value not applied
1: Absolute value applied
Here set to pulse output mode, set algorithm interrupt to pulse mode (bit 3 = 1).
// set algorithm interrupt to pulsed mode (bit 3 = 1) or latch mode (bit 3 = 0)
/************设置脉冲模式************/
temp = STHS34PF80_ALGO_CONFIG;//嵌入式地址ALGO_CONFIG (28h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为ALGO_CONFIG
temp = 0x08;
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//写入0x08(位3)使能脉冲模式
Finally close the embedded write operation.
/************关闭嵌入式函数读写************/
temp = 0x00;//STHS34PF80_FUNC_PAGE_RW->0x11 //关闭嵌入式函数写
sths34pf80_write_reg(add, STHS34PF80_FUNC_PAGE_RW, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为STHS34PF80_FUNC_PAGE_RW
//STHS34PF80_CTRL2->0x21 //读取CTRL2 (21h)
sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
temp = temp & ~(0x10); //STHS34PF80_CTRL2->0x21 //清除FUNC_CFG_ACCESS
sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
read embedded function
The embedded function read operation is as follows.
The case implementation is as follows.
CTRL2 (21h) is shown below, setting FUNC_CFG_ACCESS to 1 is mainly to enable access to embedded function registers.
The FUNC_CFG_READ flag bit of the register PAGE_RW (11h) is set to 1 to enable the read process of the embedded function.
uint8_t rawData[2] = {
0, 0}; // register data stored here
uint8_t temp ; //STHS34PF80_CTRL2->0x21
sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
temp = temp | 0x10;//STHS34PF80_CTRL2->0x21 //开启嵌入式函数访问
sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);//允许访问嵌入式函数寄存器
temp = 0x20; //STHS34PF80_FUNC_PAGE_RW->0x11 //开启嵌入式函数读操作
sths34pf80_write_reg(add, STHS34PF80_FUNC_PAGE_RW, (uint8_t *)&temp, 1); //允许写入嵌入函数寄存器
The register FUNC_CFG_ADDR (08h) is the address for configuring the embedded function, and FUNC_CFG_DATA (09h) is the incoming data to the embedded function.
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.
/************读取存在阈值************/
temp = STHS34PF80_PRESENCE_THS_L;//嵌入式地址PRESENCE_THS (20h - 21h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为PRESENCE_THS_L
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[0], 1);
temp = STHS34PF80_PRESENCE_THS_H;//嵌入式地址PRESENCE_THS (20h - 21h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为PRESENCE_THS_H
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[1], 1);
dest[0] = (uint16_t)(((uint16_t)rawData[1]) << 8 | rawData[0]);
MOTION_THS (22h - 23h) This register is used to set the threshold of the motion detection algorithm. In practical applications, you may need to read or set this register to adjust or view the threshold of the motion detection algorithm. A value that is too high may result in insensitive motion detection; a value that is too low may result in false positives. This value ranges from 0 to 32767. The default value is 200.
/************读取运动阈值************/
temp = STHS34PF80_MOTION_THS_L;//嵌入式地址MOTION_THS (22h - 23h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为MOTION_THS_L
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[0], 1);
temp = STHS34PF80_MOTION_THS_H;//嵌入式地址MOTION_THS (22h - 23h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为MOTION_THS_H
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[1], 1);
dest[1] = (uint16_t)(((uint16_t)rawData[1]) << 8 | rawData[0]);
TAMB_SHOCK_THS (24h - 25h) This register is used to set the threshold of the ambient temperature shock detection algorithm. In short, when the ambient temperature suddenly changes to a certain extent (beyond this set threshold), the system may consider that a "temperature shock" event has occurred. This is a 15-bit unsigned integer. That means it has a range of 0 to 32767.
/************读取环境温度冲击阈值************/
temp = STHS34PF80_TAMB_SHOCK_THS_L;//嵌入式地址TAMB_SHOCK_THS (24h - 25h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为TAMB_SHOCK_THS_L
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[0], 1);
temp = STHS34PF80_TAMB_SHOCK_THS_H; //嵌入式地址TAMB_SHOCK_THS (24h - 25h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为TAMB_SHOCK_THS_H
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[1], 1);
dest[2] = (uint16_t)(((uint16_t)rawData[1]) << 8 | rawData[0]);
HYST_MOTION (26h) This register is used to set the hysteresis configuration value for the motion detection algorithm. This is an 8-bit unsigned integer, so its range is 0 to 255, and the default value is 32.
/************读取运动滞后值************/
temp = STHS34PF80_HYST_MOTION;//嵌入式地址HYST_MOTION (26h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为HYST_MOTION
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[0], 1);
HYST_PRESENCE (27h) This register is used to set the hysteresis configuration value of the presence detection algorithm. Hysteresis is an important concept in many sensor applications to help reduce frequent output switching due to small or temporary input changes. In short, hysteresis provides a certain "tolerance" to the sensor's reading, and only when the input value exceeds this tolerance range does the output change. This is an 8-bit unsigned integer, so its range is 0 to 255, and the default value is 32.
/************读取存在滞后值************/
temp = STHS34PF80_HYST_PRESENCE;//嵌入式地址HYST_PRESENCE (27h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为HYST_PRESNCE
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[1], 1);
dest[3] = (uint16_t)(((uint16_t)rawData[1]) << 8 | rawData[0]);
The ALGO_CONFIG (28h) register is used to configure and tune the behavior of the algorithm, including interrupt pulse mode, temperature compensation, and absolute value selection for the presence detection algorithm. Appropriate configuration can make the algorithm more suitable for the needs of specific applications.
INT_PULSED: When set to 1, the flag generated by the algorithm will be pulsed on the INT pin (high level for the time defined by ODR). The default value is 0.
0: Latch mode
1: Pulse mode
COMP_TYPE: Enable built-in linear algorithm for compensating ambient temperature variation in object temperature. The default value is 0.
0: Disabled
1: Enabled
SEL_ABS: Select absolute value in presence detection algorithm. The default value is 0.
0: Absolute value not applied
1: Absolute value applied
Here set to pulse output mode, set algorithm interrupt to pulse mode (bit 3 = 1).
// set algorithm interrupt to pulsed mode (bit 3 = 1) or latch mode (bit 3 = 0)
/************读取脉冲模式************/
temp = STHS34PF80_ALGO_CONFIG;//嵌入式地址ALGO_CONFIG (28h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为ALGO_CONFIG
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[0], 1);
The HYST_TAMB_SHOCK (29h) register is used to set the hysteresis configuration value for the sudden ambient temperature detection algorithm. Hysteresis is common in sensor applications to prevent frequent switching of the output due to small, temporary signal changes. By providing a "tolerance" to the sensor's reading, hysteresis ensures that the output changes only when the input value falls outside this tolerance. This is an 8-bit unsigned integer, meaning it has a range of 0 to 255.
/************读取环境温度滞后值************/
temp = STHS34PF80_HYST_TAMB_SHOCK;//嵌入式地址HYST_TAMB_SHOCK (29h)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为HYST_TAMB_SHOCK
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&rawData[1], 1);
dest[4] = (uint16_t)(((uint16_t)rawData[1]) << 8 | rawData[0]);
Finally close the embedded write operation.
/************设置中断模式************/
temp = 0x00;//STHS34PF80_FUNC_PAGE_RW->0x11 //关闭嵌入式函数写
sths34pf80_write_reg(add, STHS34PF80_FUNC_PAGE_RW, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为STHS34PF80_FUNC_PAGE_RW
//STHS34PF80_CTRL2->0x21 //读取CTRL2 (21h)
sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
temp = temp & ~(0x10); //STHS34PF80_CTRL2->0x21 //清除BOOT和ONE_SHOT
sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
reset module
When changing parameters related to the algorithm or the cutoff frequency of the filter, or when enabling/disabling the embedded compensation algorithm, it is necessary to reset the algorithm following the steps above. This process involves entering power-down mode, changing the algorithm configuration, enabling and disabling embedded function register access and writing, and setting the desired output data rate or triggering a one-shot acquisition in the final steps.
/**
* @brief 重置STHS34PF80传感器内置的算法
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
void STHS34PF80_resetAlgo(uint8_t add)
{
uint8_t temp ; //STHS34PF80_CTRL2->0x21 //读取CTRL2 (21h)
sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
temp= temp | 0x10;//STHS34PF80_CTRL2->0x21 //开启嵌入式函数访问
sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);//允许访问嵌入式函数寄存器
temp = 0x40; //STHS34PF80_FUNC_PAGE_RW->0x11 //开启嵌入式函数写操作
sths34pf80_write_reg(add, STHS34PF80_FUNC_PAGE_RW, (uint8_t *)&temp, 1); //允许写入嵌入函数寄存器
temp = STHS34PF80_RESET_ALGO;//嵌入式地址STHS34PF80_RESET_ALGO (2Ah)
sths34pf80_write_reg(add, STHS34PF80_FUNC_CFG_ADDR, (uint8_t *)&temp, 1); //设置嵌入式寄存器地址为STHS34PF80_RESET_ALGO
temp = 0x01;
sths34pf80_read_reg(add, STHS34PF80_FUNC_CFG_DATA, (uint8_t *)&temp, 1);//设置ALGO_ENABLE_RESET位
temp = 0x00;//STHS34PF80_FUNC_PAGE_RW->0x11 //关闭嵌入式函数写
sths34pf80_write_reg(add, STHS34PF80_FUNC_PAGE_RW, (uint8_t *)&temp, 1);//设置嵌入式寄存器地址为STHS34PF80_FUNC_PAGE_RW
//STHS34PF80_CTRL2->0x21 //读取CTRL2 (21h)
sths34pf80_read_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
temp = temp & ~(0x10); //STHS34PF80_CTRL2->0x21 //清除FUNC_CFG_ACCESS
sths34pf80_write_reg(add, STHS34PF80_CTRL2, (uint8_t *)&temp, 1);
}
The last ninth step needs to set the BDU rate.
/**
* @brief 关闭低功耗,进入正常模式,设置ODR
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
uint8_t STHS34PF80_powerUp(uint8_t add,uint8_t odr)
{
uint8_t temp = 0;
int32_t ret; //STHS34PF80_CTRL1->0x20
ret = sths34pf80_read_reg(add, STHS34PF80_CTRL1, (uint8_t *)&temp, 1);
temp=temp | 0x10 | odr;// set bdu = 1 (bit 4 == 1) and odr
if(ret==HAL_OK)
ret = sths34pf80_write_reg(add, STHS34PF80_CTRL1, (uint8_t *)&temp, 1);
return ret;
}
Clear DRDY
By reading 0x25, 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).
/**
* @brief 清零DRDY
*
* @param add 设备地址
* @param val 设备ID.
* @retval ret 正常返回HAL_OK
*
*/
uint8_t STHS34PF80_getFuncStatus(uint8_t add)
{
uint8_t temp = 0;//STHS34PF80_FUNC_STATUS->0x25
sths34pf80_read_reg(add, STHS34PF80_FUNC_STATUS, (uint8_t *)&temp, 1);
return temp;
}