通过上一篇文章我们已经读取到了HPTA 32X32d热堆阵列传感器的EEPROM中的参数,有了这些参数我们就可以对传感器进行初始化配置,并利用这些参数去参与像素数据的计算过程。
1、软件控制流程图
HPTA 32X32d热堆阵列传感器的软件控制流程如上图所示,系统初始化和读EEPROM之前的文章都有部分或全部说明了,所以下文直接从初始化Sensor开始继续往下写。
2、Sensor接口函数
根据数据手册,控制传感器需要写配置寄存器、读状态寄存器和读数据寄存器,我们先把这3个接口函数的代码写一下。
#define SLAVE_ADDR (0x1A<<1)
int sensor_writeReg(const char *regName, uint8_t reg, uint8_t data)
{
int ret = 0;
if(bsp_IIC_SendByteToSensor(SLAVE_ADDR, reg, data) == 0)
{
ret = 0; // succeed
}
else
{
ret = -1; // failed
}
return ret;
}
int sensor_readStatus(uint8_t reg, uint8_t *rxData)
{
int ret = 0;
if(reg != SENSOR_REG_Status)
{
return -1;
}
if(bsp_IIC_ReadByteFromSensor(SLAVE_ADDR, reg, rxData) == 0)
{
ret = 0; // succeed
}
else
{
ret = -1; // failed
}
return ret;
}
int sensor_readBlockData(const char *regName,
uint8_t dataType,
uint8_t blockNum,
uint8_t reg,
void (*pCallback)(uint8_t, uint8_t, uint8_t, uint8_t*))
{
int ret = 0;
uint8_t rxData[258] = {0};
if((reg != SENSOR_REG_DataTop) && (reg != SENSOR_REG_DataBot))
{
return -1;
}
if(bsp_IIC_ReadBytesFromSensor(SLAVE_ADDR, reg, rxData, 258) == 0)
{
if(pCallback > 0)
{
(*pCallback)(dataType, blockNum, reg, rxData);
}
}
else
{
ret = -1; // failed
}
return ret;
}
3、初始化Sensor
以下是初始化Sensor的代码,先唤醒Sensor,再按照数据手册的描述,把从EEPROM中读到的MBIT(calib)、BIAS(calib)、CLK(calib)、BPA(calib)、PU(calib)几个参数写入TRIM_REG1 ~TRIM_REG7寄存器,然后读一遍数据寄存器,最后进入休眠。
void initSensor(void)
{
uint8_t status = 0;
sensor_writeReg(" WAKEUP", SENSOR_REG_Config, 0x01);
HAL_Delay(5);
sensor_writeReg(" MBIT_TRIM", SENSOR_REG_Trim1, g_tEEParam.u8MBIT_Calib);
HAL_Delay(5);
sensor_writeReg("BIAS_TRIML", SENSOR_REG_Trim2, g_tEEParam.u8BIAS_Calib);
HAL_Delay(5);
sensor_writeReg("BIAS_TRIMR", SENSOR_REG_Trim3, g_tEEParam.u8BIAS_Calib);
HAL_Delay(5);
sensor_writeReg(" CLK_TRIM", SENSOR_REG_Trim4, g_tEEParam.u8CLK_Calib);
HAL_Delay(5);
sensor_writeReg(" BPA_TRIML", SENSOR_REG_Trim5, g_tEEParam.u8BPA_Calib);
HAL_Delay(5);
sensor_writeReg(" BPA_TRIMR", SENSOR_REG_Trim6, g_tEEParam.u8BPA_Calib);
HAL_Delay(5);
sensor_writeReg(" PU_TRIM", SENSOR_REG_Trim7, g_tEEParam.u8PU_Calib);
HAL_Delay(5);
sensor_writeReg(" START", SENSOR_REG_Config, 0x09);
sensor_readStatus(SENSOR_REG_Status, &status);
HAL_Delay(30);
sensor_readStatus(SENSOR_REG_Status, &status);
if(status & 1)
{
sensor_readBlockData(" PIX_TOP", dataType_PTAT, 0, SENSOR_REG_DataTop, (void*)0);
sensor_readBlockData(" PIX_BOT", dataType_PTAT, 0, SENSOR_REG_DataBot, (void*)0);
}
sensor_writeReg(" SLEEP", SENSOR_REG_Config, 0x00);
HAL_Delay(5);
}
4、读原始像素数据
原始像素数据要分4次读出,每次读原始像素数据前都要写配置寄存器(Configuration register),目的是选择数据转换类型和指定要转换的数据块,配置寄存器定义如下:
示例:配置寄存器写入0x19,表示对block1进行PTAT转换,具体涵义如下:
BLOCK=01b:表示选择block1;
VDD_MEAS=0b:表示不进行VDD转换,即进行PTAT转换;
START=1b:表示开始转换;
WAKEUP=1b:表示唤醒传感器。
注1:每次转换时,START和WAKEUP必须同时置1;
注2:转换类型分为PTAT转换和VDD转换两种,这两种转换类型读到的像素数据没有区别,不同的是进行PTAT转换时读到的数据前两个字节表示PTAT值,进行VDD转换时前两个字节表示VDD值,读取block数据时一般交替进行PTAT和VDD转换,并对多个PTAT/VDD值计算均值,PTAT和VDD的均值在后续的计算中有重要作用;
每次启动数据转换后,要读取Sensor的状态寄存器(Status Register)的值,当状态寄存器的EOC=1表示转换完成,然后才可以读取block数据,状态寄存器的定义如下:
读原始像素数据的代码如下:
static void app_readBlockData(void)
{
uint8_t status = 0;
sensor_writeReg("Config Block0 PTAT", SENSOR_REG_Config, CONFIG_CMD_Block0_PTAT);
do
{
sensor_readStatus(SENSOR_REG_Status, &status);
}while(!(status&1));
sensor_readBlockData(" PIX_TOP", dataType_PTAT, 0, SENSOR_REG_DataTop, app_readBlockData_callback);
sensor_readBlockData(" PIX_BOT", dataType_PTAT, 0, SENSOR_REG_DataBot, app_readBlockData_callback);
sensor_writeReg("Config Block1 VDD", SENSOR_REG_Config, CONFIG_CMD_Block1_VDD);
do
{
sensor_readStatus(SENSOR_REG_Status, &status);
}while(!(status&1));
sensor_readBlockData(" PIX_TOP", dataType_VDD, 1, SENSOR_REG_DataTop, app_readBlockData_callback);
sensor_readBlockData(" PIX_BOT", dataType_VDD, 1, SENSOR_REG_DataBot, app_readBlockData_callback);
sensor_writeReg("Config Block2 PTAT", SENSOR_REG_Config, CONFIG_CMD_Block2_PTAT);
do
{
sensor_readStatus(SENSOR_REG_Status, &status);
}while(!(status&1));
sensor_readBlockData(" PIX_TOP", dataType_PTAT, 2, SENSOR_REG_DataTop, app_readBlockData_callback);
sensor_readBlockData(" PIX_BOT", dataType_PTAT, 2, SENSOR_REG_DataBot, app_readB