概述用于配置和设置 sths34pf80 传感器的一些参数,以便进行存在检测和运动检测。
最近在弄st和瑞萨ra的课程,需要样片的可以加群申请:615061293 。
样品申请https://www.wjx.top/vm/ohckxjk.aspx#
视频教程https://www.bilibili.com/video/bv1qm4y1p79x/
完整代码下载https://download.csdn.net/download/qq_24312945/88216813
参考程序初始化相对于驱动人体检测demo,新的案例设置了传感器的存在和运动相关的阈值、滞后和中断配置,以实现存在检测和运动检测的功能,并在相关事件发生时触发中断。
省电模式下面文本描述了关机模式的使用和功能。关机模式是一种休眠模式,用于将传感器设备置于休眠状态,从而节省功耗。在关机模式下,设备停止数据采集,并且大部分内部模块都被关闭,以最小化电流消耗。这使得传感器在供电的情况下能够实现最低的功耗水平。
尽管设备处于关机模式,但它仍保持 i²c / spi 通信串口处于活动状态,以便能够与设备进行通信和配置设置。关机模式下,配置寄存器的内容被保留,而输出数据寄存器不会更新,这意味着在进入关机模式前,最后一次采样的数据将保留在内存中。
为了进入关机模式并避免在重新进入连续模式时读取错误的输出数据,文本提供了正确的步骤。然而,这些步骤在你的问题中并未提供,因此无法给出完整的步骤。
上面文档主要对0x25,0x23,0x20寄存器进行操作,其中读取0x25多次,主要功能是对status (23h)的drdy进行清0。
查看下面表格也可以得知,有多种操作可以对status (23h)的drdy 清零。
其中0x20是配置速率寄存器。
设置存在阈值存在检测如下所示。
以设置存在阈值为例,探索设置的步骤。
这里使用的函数为 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;}最开始读取ctrl1(20h)的数据,同时进行保存到odr变量中。
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)这行代码用于在设置存在阈值之前,通过调用 sths34pf80_tmos_odr_check_safe_set 函数来确保在设置新的参数之前传感器的状态是稳定且安全的。该函数如下所示。
/** * @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;}其中下电模式在else中执行。
先是执行sths34pf80_read_reg(add, sths34pf80_func_status, (uint8_t *)&func_status, 1),读取0x25主要功能是对status (23h)的drdy进行清0,之后执行如下代码。
/* wait drdy bit go to '1' */ do { ret += sths34pf80_tmos_drdy_status_get(add, &status); } while (status.drdy != 0u);sths34pf80_tmos_drdy_status_get代码主要是对status (23h)的drdy进行读取,当读取为0,跳出上面的循环。
/** * @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;}之后重新回到sths34pf80_tmos_odr_check_safe_set函数中,执行如下操作。
主要功能就是将ctrl1(20h)里面的odr数据清零,同时读取0x25,主要功能是对status (23h)的drdy进行清0
/* 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然后执行如下代码将odr数据重新写入ctrl1(20h)寄存器中。
ctrl1.odr = (odr_new & 0xfu); ret += sths34pf80_write_reg(add, sths34pf80_ctrl1, (uint8_t *)&ctrl1, 1);//sths34pf80_ctrl1- >0x20紧接着返回sths34pf80_presence_threshold_set函数中,执行如下操作。
将传入的数据保存到buff数组中,然后执行sths34pf80_func_cfg_write将buff数据传入到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查看sths34pf80_func_cfg_write函数,如下所示。
/** * @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 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);
继续返回sths34pf80_func_cfg_write函数,然后执行如下函数,该函数为写入或者读取嵌入式函数的地址,具体要看page_rw (11h)设置为读或者写,同时写入固定的data指令,当写完之后进入page_rw (11h)关闭读或写操作。
/* select register address (it will autoincrement when writing) */ ret += sths34pf80_write_reg(add, sths34pf80_func_cfg_addr, &addr, 1); for (i = 0; i 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;}该函数对reset_algo (2ah)的algo_enable_reset 置为位1,执行算法重置操作。默认情况下,这个位的值为 0,表示不进行算法重置。当用户修改了与算法相关的参数时(例如阈值、滞后等),需要将 algo_enable_reset 位设置为 1。重置完成后,algo_enable_reset 位应返回到默认值 0。
接着执行sths34pf80_tmos_odr_check_safe_set,将刚刚保存的odr数据重新导入进入。
/* set saved odr back */ ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);到此,sths34pf80_presence_threshold_set函数就已经执行完毕。
设置存在滞后这里使用的函数为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;}最开始读取ctrl1(20h)的数据,同时进行保存到odr变量中。
/* 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)这行代码用于在设置存在阈值之前,通过调用 sths34pf80_tmos_odr_check_safe_set 函数来确保在设置新的参数之前传感器的状态是稳定且安全的。
紧接着将传入的数据保存到val数组中,然后执行sths34pf80_func_cfg_write将val数据传入到hyst_presence (27h)中。
ret += sths34pf80_func_cfg_write(add, sths34pf80_hyst_presence, &val, 1);这个滞后值有助于避免在边界值附近产生频繁的状态切换。
默认值: 预设的滞后值默认值是 32(0x32)。
例如,假设 presence_ths 设置为 200,而 hyst_presence 设置为 32。当测量值超过 200 时,传感器会认为存在某种状态。然后,要从存在状态切换到非存在状态,传感器必须降低测量值至少到达 200 - 32 = 168。只有当测量值降低到 168 以下时,传感器才会触发状态切换。
通过设置适当的滞后值,可以防止在测量值在阈值附近波动时出现不必要的状态切换,从而提高存在检测的稳定性。
接着执行sths34pf80_algo_reset函数,该函数主要对reset_algo (2ah)的algo_enable_reset 置为位1,执行算法重置操作。
ret += sths34pf80_algo_reset(add);接着执行sths34pf80_tmos_odr_check_safe_set,将刚刚保存的odr数据重新导入进入。
/* set saved odr back */ ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);到此,sths34pf80_presence_hysteresis_set函数就已经执行完毕。
设置动作阈值这里使用的函数为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;}最开始读取ctrl1(20h)的数据,同时进行保存到odr变量中。
/* 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)这行代码用于在设置存在阈值之前,通过调用 sths34pf80_tmos_odr_check_safe_set 函数来确保在设置新的参数之前传感器的状态是稳定且安全的。
ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, 0);接着将传入的数据保存到buff数组中,然后执行sths34pf80_func_cfg_write将buff数据传入到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);motion_ths 寄存器(地址范围为 0x22 到 0x23)用于设置运动检测算法的阈值。运动检测算法用于检测是否发生了物体的运动或动作。
以下是这个寄存器的作用:
运动阈值(motion threshold): 这是一个用于运动检测算法的阈值。阈值定义了在测量数据中,何时认为发生了物体的运动或动作。阈值是一个 15 位的无符号整数,其取值范围在 0 到 32767 之间。
默认值: 预设的运动阈值默认值是 200(0x00c8)。
例如,如果 motion_ths 设置为 300,这意味着当传感器测量的某个特定值超过了 300,传感器会认为发生了运动或动作状态。
通过设置适当的运动阈值,可以根据应用需求来调整运动检测的敏感度。更高的阈值会导致较大的变化才能被视为运动,而较低的阈值则会使传感器更敏感,甚至可能在较小的变化时触发运动检测。
接着执行sths34pf80_algo_reset函数,该函数主要对reset_algo (2ah)的algo_enable_reset 置为位1,执行算法重置操作。
ret += sths34pf80_algo_reset(add);接着执行sths34pf80_tmos_odr_check_safe_set,将刚刚保存的odr数据重新导入进入。
/* set saved odr back */ ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);到此,sths34pf80_motion_threshold_set函数就已经执行完毕。
设置动作滞后这里使用的函数为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;}最开始读取ctrl1(20h)的数据,同时进行保存到odr变量中。
/* 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)这行代码用于在设置存在阈值之前,通过调用 sths34pf80_tmos_odr_check_safe_set 函数来确保在设置新的参数之前传感器的状态是稳定且安全的。
紧接着将传入的数据保存到val数组中,然后执行sths34pf80_func_cfg_write将val数据传入到hyst_motion (26h)中。
ret += sths34pf80_func_cfg_write(add, sths34pf80_hyst_motion, &val, 1);在运动检测算法中,滞后指的是当从运动状态切换到静止状态时,需要等待的时间或条件,以防止在短时间内频繁地切换状态。
默认值: 预设的滞后值默认值是 32(0x32)。
例如,如果 motion_ths 设置为 300,而 hyst_motion 设置为 32。当测量值超过 300 时,传感器会认为发生了运动。然后,要从运动状态切换到非运动状态,传感器必须降低测量值至少到达 300 - 32 = 268。只有当测量值降低到 268 以下时,传感器才会触发状态切换。
通过设置适当的运动滞后值,可以避免在测量值在运动状态附近波动时产生不必要的状态切换,从而提高运动检测的稳定性。这类似于存在检测中的概念,但针对的是运动状态。
接着执行sths34pf80_algo_reset函数,该函数主要对reset_algo (2ah)的algo_enable_reset 置为位1,执行算法重置操作。
ret += sths34pf80_algo_reset(add);接着执行sths34pf80_tmos_odr_check_safe_set,将刚刚保存的odr数据重新导入进入。
/* set saved odr back */ ret += sths34pf80_tmos_odr_check_safe_set(add, ctrl1, odr);到此,sths34pf80_motion_hysteresis_set函数就已经执行完毕。
主程序初始化如下。
/* 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%xn,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: %02drn, 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 */main函数如下所示。
三星DRAM稳固,国产DRAM未来要走的路还有很长
2020年全球手机销量TOP 10公布
工信部:国家无人机系统质量检验中心开始筹建,推动产业发展
光子学将独树一帜 进入发展的曲线阶段
小米接手宝沃?雷军造车路上再得一宝
基于STM32CUBEMX驱动TMOS模块STHS34PF80(3)----修改检测阈值
中国电信天翼云携手欧拉共同完善CTyunOS操作系统
2017款路虎发现车行驶中发动机抖动且加速无力
任正非:不能与谷歌合作的话,华为有其他选择
什么是模拟量,它的概念是怎样的
制造业数字化转型五大困境
2016手机充电器品牌排行榜前十
关于线束连接器端子的故障分析
UPS旁路超盯梢报警的解决方法
AI人工智能是否能完全取代医生呢?
在开发平台上使用VITIS AI加速AI应用
如何设计一个简单的12V锂离子电池组
娃哈哈集团成立一家新公司——浙江娃哈哈智能机器人有限公司
神经元芯片(neuron chip)
手机无线耳机什么牌子好?适合苹果手机的蓝牙耳机推荐