Drive TMOS module STHS34PF80(6) based on STM32CUBEMX----get status data

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.
This section will continue to configure the code in the previous section, and detect whether there is a human body by obtaining the status flag of the module.
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

video teaching

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

Drive TMOS module STHS34PF80(6) based on STM32CUBEMX----get status data

sample application

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

Reference Demo

hthttps://github.com/kriswiner/STHS34PF80/tree/main

Complete code download

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

Refer to the program to obtain data

insert image description here

get data flag

Use the status register (STATUS, address 23h) to check when a new data set is available. The dataset here may include multiple data outputs: object temperature, ambient temperature, and possibly several other outputs generated by embedded intelligent algorithms (such as presence detection, motion detection, and ambient temperature shock detection).

insert image description here

The reference document settings are as follows.

insert image description here

Modify as follows.

/**
  * @brief  获取DRDY
  *
  * @param  add      设备地址
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */	
	
uint8_t STHS34PF80_getDataReadyStatus(uint8_t add)
{
    
    
	uint8_t temp = 0;//STHS34PF80_STATUS->0x23
	sths34pf80_read_reg(add, STHS34PF80_STATUS, (uint8_t *)&temp, 1);

   return temp;
  }	

Using the data ready signal

The FUNC_STATUS register (address 25h) is a read-only register that contains the status flags of the device's embedded smart algorithm.

insert image description here

PRES_FLAG: This is a presence detection flag. This bit changes to 1 when presence is detected and returns to 0 when no presence is detected. The default value is 0.
(0: Presence not detected; 1: Presence detected)
MOT_FLAG: This is a motion detection flag. This bit goes to 1 when motion is detected and goes back to 0 when no motion is detected. The default value is 0.
(0: no motion detected; 1: motion detected)
TAMB_SHOCK_FLAG: This is an ambient temperature shock detection flag. This bit changes to 1 when an ambient temperature shock is detected and returns to 0 when no ambient temperature shock is detected. The default value is 0.
(0: no ambient temperature shock detected; 1: ambient temperature shock detected)

insert image description here

The reference document settings are as follows.

insert image description here

The fetch code is as follows.

/**
  * @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;
  }

Output data from embedded smart digital algorithms

In Section 4.5.4, the output data of the embedded intelligent digital algorithm is described. These data include the output data of presence detection, motion detection, and environmental shock detection, sent to the TPRESENCE_H (3Bh) and TPRESENCE_L (3Ah) registers, TMOTION_H (3Dh) and TMOTION_L (3Ch) registers, and TAMB_SHOCK_H (3Fh) ) register.
These data have no intrinsic physical meaning, but are simply the signs that embedded intelligent digital algorithms use to assert the corresponding signs.

insert image description here

insert image description here
insert image description here
The fetch code is as follows.

/**
  * @brief  					嵌入式智能数字算法产生的存在检测输出数据
  *
  * @param  add      设备地址
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */	
	

int16_t STHS34PF80_readPresence(uint8_t add)
{
    
    
	uint8_t rawData[2];  //STHS34PF80_TPRESENCE_L->0x3A
	sths34pf80_read_reg(add, STHS34PF80_TPRESENCE_L, (uint8_t *)&rawData[0], 2);	

	return (int16_t)(((int16_t)rawData[1]) << 8 | rawData[0]);			
  }

/**
  * @brief  					嵌入式智能数字算法产生的运动检测输出数据
  *
  * @param  add      设备地址
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */	
int16_t STHS34PF80_readMotion(uint8_t add)
{
    
    
	uint8_t rawData[2];  //STHS34PF80_TMOTION_L->0x3C
	sths34pf80_read_reg(add, STHS34PF80_TMOTION_L, (uint8_t *)&rawData[0], 2);	

	return (int16_t)(((int16_t)rawData[1]) << 8 | rawData[0]);				

  }

/**
  * @brief  					嵌入式智能数字算法产生的环境冲击输出数据
  *
  * @param  add      设备地址
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */	
int16_t STHS34PF80_readAmbShock(uint8_t add)
{
    
    
	uint8_t rawData[2]; //STHS34PF80_TAMB_SHOCK_L->0x3E
	sths34pf80_read_reg(add, STHS34PF80_TAMB_SHOCK_L, (uint8_t *)&rawData[0], 2);	

	return (int16_t)(((int16_t)rawData[1]) << 8 | rawData[0]);			

  }	

A simple example is provided in Section 4.5.3, showing how to take object temperature data (in Least Significant Bit LSB) from a sensor and convert it to degrees Celsius °C.
The procedure for this example is also applicable to obtaining ambient temperature data.

insert image description here

The code is shown below.

/**
  * @brief  
  *
  * @param  add      视场内对象发出的红外辐射量(未补偿)
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */	
int16_t STHS34PF80_readObjTemp(uint8_t add)
{
    
    
	uint8_t rawData[2];  //STHS34PF80_TOBJECT_L->0x26
	sths34pf80_read_reg(add, STHS34PF80_TOBJECT_L, (uint8_t *)&rawData[0], 2);	

	return (int16_t)(((int16_t)rawData[1]) << 8 | rawData[0]);	
  }

Section 4.5.2 describes how to obtain ambient temperature data and in which registers this data can be found.
Register Storage: Ambient temperature data is sent into the TAMBIENT_H (29h) and TAMBIENT_L (28h) registers. Among them, TAMBIENT_H stores the most significant byte (MSB) of the ambient temperature value, and TAMBIENT_L stores the least significant byte (LSB).
Data format: By splicing TAMBIENT_H and TAMBIENT_L, the complete ambient temperature output data can be obtained. The value is represented as a 16-bit signed two's complement number.
Unit Conversion: Each ambient temperature output sample can be converted to degrees Celsius by a sensitivity value (100 LSB/°C in this example). The formula is as follows:

insert image description here

Application scenario: The ambient temperature data represents the actual temperature of the environment thermally coupled with the device. These data can be used to correct for changes in the object temperature output data due to ambient temperature changes thermally coupled to the sensor (see Section 4.6 for details).

insert image description here

The code is shown below.

/**
  * @brief  
  *
  * @param  add      环境温度数据
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */		
	
	
int16_t STHS34PF80_readAmbTemp(uint8_t add)
{
    
    
	uint8_t rawData[2];//STHS34PF80_TAMBIENT_L->0x28
	sths34pf80_read_reg(add, STHS34PF80_TAMBIENT_L, (uint8_t *)&rawData[0], 2);	

   return (int16_t)(((int16_t)rawData[1]) << 8 | rawData[0]);
}

The compensated object temperature data is stored in the TOBJ_COMP_L (38h) and TOBJ_COMP_H (39h) registers. Among them, TOBJ_COMP_H contains the most significant byte (MSB), and TOBJ_COMP_L contains the least significant byte (LSB).
It is important to note that even when the compensation algorithm is active, the 26h and 27h registers still contain raw object temperature data. If compensation is not active, registers 38h and 39h are aligned with registers 26h and 27h.
The compensated object temperature data (in LSB) is proportional to the transmittance of the optical system used. You can transform these compensated data by the actual sensitivity value to get the corresponding value in degrees Celsius. However, these compensated data do not actually represent the temperature of objects within the field of view.
If the gain reduction feature is enabled (see Section 4.7), the embedded compensation algorithm cannot be enabled.

insert image description here

The code is shown below.

/**
  * @brief  					嵌入式智能数字算法产生的视场内物体发射的红外辐射量输出数据(已经经过环境温度补偿)
  *
  * @param  add      设备地址
  * @param  val      设备ID.
  * @retval ret   		正常返回HAL_OK
  *
  */		
int16_t STHS34PF80_readCompObjTemp(uint8_t add)
{
    
    
	uint8_t rawData[2];//STHS34PF80_TOBJ_COMP_L->0x38
	sths34pf80_read_reg(add, STHS34PF80_TOBJ_COMP_L, (uint8_t *)&rawData[0], 2);	

	return (int16_t)(((int16_t)rawData[1]) << 8 | rawData[0]);		
  }	
	

main program

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
//		printf("PA7=%d",HAL_GPIO_ReadPin  ( GPIOA, GPIO_PIN_7));
//		if(HAL_GPIO_ReadPin  ( GPIOA, GPIO_PIN_7))
//		{
    
    
		
			status = STHS34PF80_getDataReadyStatus(STHS34PF80_ADDRESS);		
		
			if(FUNCTIONS) 
			{
    
    		
				funcstatus = STHS34PF80_getFuncStatus(STHS34PF80_ADDRESS);				
				if(funcstatus & 0x04) printf("(存在)Presence detected !\n");
				if(funcstatus & 0x02) printf("(运动)Motion detected !\n");
				if(funcstatus & 0x01) printf("(环境温度冲击)T Shock detected !\n");				
			}
		
			if(status & 0x04)   // when data ready		
			{
    
    
				Presence = STHS34PF80_readPresence(STHS34PF80_ADDRESS);//嵌入式智能数字算法产生的存在检测输出数据
				Motion =   STHS34PF80_readMotion(STHS34PF80_ADDRESS); // 嵌入式智能数字算法产生的运动检测输出数据
				AmbTemp =  STHS34PF80_readAmbTemp(STHS34PF80_ADDRESS);//环境温度数据
				ObjTemp =  STHS34PF80_readObjTemp(STHS34PF80_ADDRESS);//视场内对象发出的红外辐射量(未补偿)
				CompObjTemp =  STHS34PF80_readCompObjTemp(STHS34PF80_ADDRESS);//嵌入式智能数字算法产生的视场内物体发射的红外辐射量输出数据(已经经过环境温度补偿)				
				AmbShock = STHS34PF80_readAmbShock(STHS34PF80_ADDRESS);//嵌入式智能数字算法产生的环境冲击输出数据

				printf("(原始数据)Raw counts\n");
				printf("(视场内对象发出的红外辐射量(未补偿))STHS34PF80 ObjTemp = %d\n",ObjTemp);  
				printf("(视场内物体发射的红外辐射量输出(经过环境温度补偿))STHS34PF80 Compensated ObjTemp = %d\n",CompObjTemp);  				
				printf("(环境温度)STHS34PF80 AmbTemp = %d\n",AmbTemp);  
				printf("(存在检测)STHS34PF80 Presence = %d\n",Presence);  
				printf("(运动检测输出)STHS34PF80 Motion = %d\n",Motion);  
				printf("(环境冲击输出)STHS34PF80 AmbShock = %d\n",AmbShock);  

				printf("(环境温度)STHS34PF80 Ambient Temperature = %.2f\n",(float)(AmbTemp) / 100.0f); //环境温度数据
				printf("(对象温度)STHS34PF80 Object Temperature = %.2f\n",((float)(ObjTemp) / (float)(ObjSense) ) ); 
	
			}
		
//		}			
		
		
		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/132517425