STM32模拟I2C协议获取MLX90614红外温度传感器测温数据(Open Drain管脚配置)
STM32的GPIO管脚可以配置为Open Drain输出模式,并且有两个功能:
- 可以设置内部上拉,因此对于I2C访问速度不是特别高的情况,可以不用外部I2C上拉电阻;
- 虽然是Open Drain输出管脚,可以直接读取管脚电平状态,如同读取输入管脚而不必将输出管脚先切换成输入管脚。
MLX90614是无接触红外温度传感器,有DAA医疗级别高精度的型号,也有针对不同测温距离的型号,适合不同场景产品的应用。MLX90614可以采用PWM方式或者I2C方式进行数据获取,这里是模拟I2C的实现方式。
硬件连接
这里用低成本的STM32F030F4P6开发板作为控制器,读取MLX90614的温度数据。连接关系如下:
软件工程配置
这里采用STM32CUBEIDE开发环境和HAL库。首先建立STM32F030F4P6工程和配置时钟。测试中发现,如果HCLK高于4MHz(无论外部时钟源或内部时钟源),则代码里模拟I2C时序功能读到错误数据,即时序不能保证。推测是低端芯片的锁相环电路性能比较差,HCLK频率高了抖动反而比较大。因此将HCLK频率配置为4MHz。另外在STM32F3和STM32F4系列做同样代码测试,则没有HCLK频率配置高低引起问题,也说明了是STM32F0系列的性能问题。
然后配置PA5和PA6作为Open Drain输出带上拉,默认为高电平输出:
然后配置USART1用于串口数据输出:
保存并生成初始工程代码。 FLASH比较小的MCU需要设置“size”优化的编译模式,避免编译后的代码占用空间超过FLASH最大空间。参见 STM32 region `FLASH‘ overflowed by xxx bytes 问题解决 。
软件工程代码
代码里需要用到HAL工程微秒级延时,HAL库工程微秒延时的实现原理参考STM32 HAL us delay(微秒延时)的指令延时实现方式及优化。
代码里I2C_Init()初始化函数用于保证MLX90614进入I2C控制模式,然后在while循环里不断的读取温度并串口输出。
MLX90614的读时序如下图所示: PEC是MLX90614发出的CRC-8校验字节,MCU侧可以将前面5个字节内容做CRC-8的计算,得到CRC-8的计算校验字节,和MLX90614发出的CRC-8校验字节比较,以判断传输和接收是否正确。因此设计了针对MLX90614读操作的CRC-8校验函数如下:
uint8_t PY_CRC_MLX90614_READ(uint8_t daddr, uint8_t Raddr, uint8_t dl, uint8_t dh)
{
uint64_t cdata = 0;
uint16_t data_t = 0;
uint16_t crc_poly = 0x0107;
uint16_t index_t = 47;
uint16_t index = 47;
uint8_t rec = 0;
cdata |= (((uint64_t)daddr)<<40);
cdata |= (((uint64_t)Raddr)<<32);
cdata |= (((uint64_t)(daddr+1))<<24);
cdata |= (((uint64_t)dl)<<16);
cdata |= (((uint64_t)dh)<<8);
while(index_t>0)
{
if( (cdata>>index_t)&1 )
{
index = index_t;
index_t = 0;
data_t |= (cdata>>(index-8));
{
data_t = data_t ^ crc_poly;
}
while(index!=0xffff)
{
if ((data_t>>7)&1) rec = 1;
else if ((data_t>>6)&1) rec = 2;
else if ((data_t>>5)&1) rec = 3;
else if ((data_t>>4)&1) rec = 4;
else if ((data_t>>3)&1) rec = 5;
else if ((data_t>>2)&1) rec = 6;
else if ((data_t>>1)&1) rec = 7;
else if ((data_t>>0)&1) rec = 8;
else rec = 9;
if((index-8)<rec)
{
data_t = data_t<<(index-8);
index = 0xffff;
}
else
{
for(uint8_t i=1;i<=rec;i++)
{
data_t = (data_t<<1)|((cdata>>(index-8-i))&1) ;
}
if(rec!= 9)
{
data_t = data_t ^ crc_poly;
index -= rec;
}
else
{
data_t = 0;
index_t = index-8-1;
index = 0xffff;
}
}
}
}
else
{
index_t--;
if(index_t<8) break;
}
}
return (uint8_t)data_t;
}
代码设计上,通过串口将温度数据的高字节和低字节输出,可以对高字节和低字节按照公式计算,得到浮点格式的温度数据。主要的实现代码如下:
#include "main.h"
float usDelayBase;
void PY_usDelayTest(void)
{
uint32_t firstms, secondms;
uint32_t counter = 0;
firstms = HAL_GetTick()+1;
secondms = firstms+1;
while(uwTick!=firstms) ;
while(uwTick!=secondms) counter++;
usDelayBase = ((float)counter)/1000;
}
void PY_Delay_us_t(uint32_t Delay)
{
uint32_t delayReg;
uint32_t usNum = (uint32_t)(Delay*usDelayBase);
delayReg = 0;
while(delayReg!=usNum) delayReg++;
}
void PY_usDelayOptimize(void)
{
uint32_t firstms, secondms;
float coe = 1.0;
firstms = HAL_GetTick();
PY_Delay_us_t(1000000) ;
secondms = HAL_GetTick();
coe = ((float)1000)/(secondms-firstms);
usDelayBase = coe*usDelayBase;
}
void PY_Delay_us(uint32_t Delay)
{
uint32_t delayReg;
uint32_t msNum = Delay/1000;
uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);
if(msNum>0) HAL_Delay(msNum);
delayReg = 0;
while(delayReg!=usNum) delayReg++;
}
#define us_num 5
#define SCL_OUT_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)
#define SCL_OUT_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)
#define SDA_OUT_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET)
#define SDA_OUT_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET)
#define SDA_IN HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6)
void I2C_Init(void)
{
SDA_OUT_H;
SCL_OUT_L;
PY_Delay_us_t(2000) ;
SCL_OUT_H;
SDA_OUT_H;
PY_Delay_us_t(2000) ;
}
void I2C_Start(void)
{
PY_Delay_us_t(us_num) ;
SDA_OUT_H;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SDA_OUT_L;
PY_Delay_us_t(us_num) ;
}
void I2C_Stop(void)
{
SCL_OUT_L;
PY_Delay_us_t(us_num) ;
SDA_OUT_L;
PY_Delay_us_t(us_num) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SDA_OUT_H;
PY_Delay_us_t(us_num) ;
}
uint8_t I2C_Write_Ack(void)
{
uint8_t status=0;
SDA_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_H;
status = SDA_IN;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
return status;
}
uint8_t I2C_Read_Ack(void)
{
uint8_t status=0;
SCL_OUT_L;
SDA_OUT_H;
PY_Delay_us_t(us_num) ;
status = SDA_IN;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
return status;
}
void I2C_Send_Byte(uint8_t txd)
{
SCL_OUT_L;
for(uint8_t i=0;i<8;i++)
{
if((txd&0x80)>>7) SDA_OUT_H;
else SDA_OUT_L;
txd<<=1;
PY_Delay_us_t(us_num) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
}
}
uint8_t I2C_Read_Byte(unsigned char rdack)
{
uint8_t rxd=0;
for(uint8_t i=0;i<8;i++ )
{
SCL_OUT_L;
PY_Delay_us_t(us_num) ;
SCL_OUT_H;
rxd<<=1;
if(SDA_IN) rxd++;
PY_Delay_us_t(us_num) ;
}
if (rdack) I2C_Read_Ack();
return rxd;
}
uint8_t PY_CRC_MLX90614_READ(uint8_t daddr, uint8_t Raddr, uint8_t dl, uint8_t dh)
{
uint64_t cdata = 0;
uint16_t data_t = 0;
uint16_t crc_poly = 0x0107;
uint16_t index_t = 47;
uint16_t index = 47;
uint8_t rec = 0;
cdata |= (((uint64_t)daddr)<<40);
cdata |= (((uint64_t)Raddr)<<32);
cdata |= (((uint64_t)(daddr+1))<<24);
cdata |= (((uint64_t)dl)<<16);
cdata |= (((uint64_t)dh)<<8);
while(index_t>0)
{
if( (cdata>>index_t)&1 )
{
index = index_t;
index_t = 0;
data_t |= (cdata>>(index-8));
{
data_t = data_t ^ crc_poly;
}
while(index!=0xffff)
{
if ((data_t>>7)&1) rec = 1;
else if ((data_t>>6)&1) rec = 2;
else if ((data_t>>5)&1) rec = 3;
else if ((data_t>>4)&1) rec = 4;
else if ((data_t>>3)&1) rec = 5;
else if ((data_t>>2)&1) rec = 6;
else if ((data_t>>1)&1) rec = 7;
else if ((data_t>>0)&1) rec = 8;
else rec = 9;
if((index-8)<rec)
{
data_t = data_t<<(index-8);
index = 0xffff;
}
else
{
for(uint8_t i=1;i<=rec;i++)
{
data_t = (data_t<<1)|((cdata>>(index-8-i))&1) ;
}
if(rec!= 9)
{
data_t = data_t ^ crc_poly;
index -= rec;
}
else
{
data_t = 0;
index_t = index-8-1;
index = 0xffff;
}
}
}
}
else
{
index_t--;
if(index_t<8) break;
}
}
return (uint8_t)data_t;
}
uint32_t Get_Temp_DATA( uint8_t ReaAd)
{
uint8_t Pecreg = 0;
uint8_t DataL = 0 ,DataH = 0;
uint32_t Result = 0;
uint8_t daddr = 0x00;
I2C_Start();
I2C_Send_Byte(daddr);
I2C_Write_Ack();
I2C_Send_Byte(ReaAd);
I2C_Write_Ack();
I2C_Start();
I2C_Send_Byte(daddr+1);
I2C_Write_Ack();
DataL=I2C_Read_Byte(1);
DataH=I2C_Read_Byte(1);
Pecreg=I2C_Read_Byte(1);
I2C_Stop();
Result |= (((uint32_t)DataH)<<24);
Result |= (((uint32_t)DataL)<<16);
Result |= (((uint32_t)Pecreg)<<8);
Result |= PY_CRC_MLX90614_READ(daddr, ReaAd, DataL, DataH);
return Result;
}
UART_HandleTypeDef huart1;
float temperature_f;
uint32_t temperature_d;
uint8_t temprst[4];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
PY_usDelayTest();
PY_usDelayOptimize();
I2C_Init();
PY_Delay_us(1000000);
while (1)
{
temperature_d=Get_Temp_DATA(0x07);
temprst[0]= (temperature_d>>24)&0xff;
temprst[1]= (temperature_d>>16)&0xff;
temprst[2]= (temperature_d>>8)&0xff;
temprst[3]= (temperature_d>>0)&0xff;
if(temprst[2]==temprst[3]) HAL_UART_Transmit(&huart1, temprst, 2, 2700);
PY_Delay_us(2000000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_SET);
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
完整STM32CUBEIDE工程下载: https://download.csdn.net/download/hwytree/82030600
温度数据
可以通过PC串口工具获得温度数据,如: 16进制39C8,对应十进制14792,按照公式计算(14792*2-27315)/100=22.69℃。
–End–
|