STM32模拟I2C协议获取HDC1080温度和湿度传感器数据
HDC1080是一款温湿度传感器,具有如下特点:
其中温度和湿度经过出厂校准。这里介绍STM32模拟I2C总线协议访问HDC1080的HAL库实现范例。
HDC1080电路连接
HDC1080的内部原理及电路连接如下:
HDC1080具有低功耗特征,每次触发检测转换后进入睡眠状态,另外内部有一个加热电阻,在环境湿度高时,可以出发加热去湿度,从而增强温度检测部分的准确性。
HDC1080可以检测供电电压阀值,判断供电电压是否高于或低于2.8V,对于简单电池供电场合可以进行供电判断处理。
HDC1080寄存器说明
HDC1080有如下的一些寄存器,按作用分为读数,配置,读ID三种:
在触发读取温度寄存器操作时,除了会触发温度转换也会触发湿度转换,从而可以一次性将温度和湿度都读出来。
原始数据和常见格式的转换关系为:
这里采用STM32CUBEIDE开发环境,以STM32F103CBT6为例,介绍 HDC1080的访问和温湿度数据获取。
STM32工程基本配置
首先建立基本工程并初始化时钟系统:
选择两个管脚作为模拟I2C协议的管脚:
并配置USB虚拟串口作为打印输出接口:
保存并生成基本工程代码:
STM32工程代码
代码实现HDC1080访问函数,循环读取并打印出HDC1080的设备码,温度和湿度原始数据,温度和湿度的常见格式。
代码里所用的微秒延时函数,参考:STM32 HAL us delay(微秒延时)的指令延时实现方式及优化
代码里所用的浮点转字符处理函数,参考: STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)
完整的main.c代码如下:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
//Written by Pegasus Yu in 2023
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{
__IO uint32_t firstms, secondms;
__IO 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)
{
__IO uint32_t delayReg;
__IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);
delayReg = 0;
while(delayReg!=usNum) delayReg++;
}
void PY_usDelayOptimize(void)
{
__IO uint32_t firstms, secondms;
__IO 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)
{
__IO uint32_t delayReg;
__IO uint32_t msNum = Delay/1000;
__IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);
if(msNum>0) HAL_Delay(msNum);
delayReg = 0;
while(delayReg!=usNum) delayReg++;
}
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define us_num 10
#define SCL_OUT_H HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET)
#define SCL_OUT_L HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET)
#define SDA_OUT_H HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET)
#define SDA_OUT_L HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET)
#define SDA_IN HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1)
void I2C_Init(void)
{
SDA_OUT_H;
SCL_OUT_H;
PY_Delay_us_t(1000000) ;
}
void I2C_Start(void)
{
PY_Delay_us_t(us_num) ;
SDA_OUT_H;
SCL_OUT_H;
PY_Delay_us_t(us_num/2) ;
SDA_OUT_L;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_L;
}
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) ;
}
void I2C_Write_Ack(void)
{
PY_Delay_us_t(us_num/2) ;
SDA_OUT_L;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
SDA_OUT_H;
}
uint8_t I2C_Read_Ack(void)
{
uint8_t status=0;
SCL_OUT_L;
PY_Delay_us_t(us_num/2) ;
SDA_OUT_H;
PY_Delay_us_t(us_num/2) ;
status = SDA_IN;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
SDA_OUT_L;
return status;
}
void I2C_Send_Byte(uint8_t txd)
{
for(uint8_t i=0;i<8;i++)
{
PY_Delay_us_t(us_num/2) ;
if((txd&0x80)>>7) SDA_OUT_H;
else SDA_OUT_L;
txd<<=1;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_H;
PY_Delay_us_t(us_num) ;
SCL_OUT_L;
}
SDA_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/2) ;
SDA_OUT_H;
PY_Delay_us_t(us_num/2) ;
SCL_OUT_H;
rxd<<=1;
if(SDA_IN) rxd++;
PY_Delay_us_t(us_num) ;
}
SCL_OUT_L;
SDA_OUT_H;
if (rdack) I2C_Write_Ack();
return rxd;
}
void HDC1080_WRITE_REG(uint8_t WrAddr, uint16_t data)
{
uint8_t daddr = 0x80; //HDC1080 device address (0x40<<1)
I2C_Start();
I2C_Send_Byte(daddr);
I2C_Read_Ack();
I2C_Send_Byte(WrAddr);
I2C_Read_Ack();
I2C_Send_Byte(data>>8);
I2C_Read_Ack();
I2C_Send_Byte(data);
I2C_Read_Ack();
I2C_Stop();
}
uint16_t HDC1080_READ_REG(uint8_t RdAddr)
{
uint8_t D[2];
uint8_t daddr = 0x80; //HDC1080 device address (0x40<<1)
I2C_Start();
I2C_Send_Byte(daddr);
I2C_Read_Ack();
I2C_Send_Byte(RdAddr);
I2C_Read_Ack();
I2C_Start();
I2C_Send_Byte(daddr+1);
I2C_Read_Ack();
D[0]=I2C_Read_Byte(1);
D[1]=I2C_Read_Byte(0);
I2C_Stop();
return (D[0]<<8|D[1]);
}
uint16_t HDC1080_READ_REG_DEVICE_ID(void)
{
return HDC1080_READ_REG(0xFF); //0x1050
}
#define Retrieve_Mode
void HDC1080_Heater_Enable(void)
{
uint16_t reg_content;
reg_content = HDC1080_READ_REG(0x02);
reg_content |= 0x2000;
HDC1080_WRITE_REG(0x02, reg_content);
}
void HDC1080_Heater_Disable(void)
{
uint16_t reg_content;
reg_content = HDC1080_READ_REG(0x02);
reg_content &= 0xdfff;
HDC1080_WRITE_REG(0x02, reg_content);
}
uint8_t HDC1080_READ_REG_Battery_Vol(void)
{
uint16_t reg_content;
reg_content = HDC1080_READ_REG(0x02);
if(reg_content&0x0800) return 1; //Battery voltage < 2.8V (read only)
else return 0; //Battery voltage > 2.8V
}
#define OP_Delay_us 20000
void HDC1080_READ_DATA(uint16_t * temperature, uint16_t * moisture)
{
uint8_t daddr = 0x80; //HDC1080 device address (0x40<<1)
uint8_t D[4];
I2C_Start();
I2C_Send_Byte(daddr);
I2C_Read_Ack();
I2C_Send_Byte(0x00);
I2C_Read_Ack();
I2C_Stop();
PY_Delay_us_t(OP_Delay_us); //Conversion Time, RHCT>=6.5ms for 14 bit resolution
I2C_Start();
I2C_Send_Byte(daddr+1);
I2C_Read_Ack();
D[0]=I2C_Read_Byte(1);
D[1]=I2C_Read_Byte(1);
D[2]=I2C_Read_Byte(1);
D[3]=I2C_Read_Byte(0);
I2C_Stop();
*temperature = (D[0]<<8|D[1]);
*moisture = (D[2]<<8|D[3]);
}
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/*
*Convert float to string type
*Written by Pegasus Yu in 2022
*stra: string address as mychar from char mychar[];
*float: float input like 12.345
*flen: fraction length as 3 for 12.345
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void py_f2s4printf(char * stra, float x, uint8_t flen)
{
uint32_t base;
int64_t dn;
char mc[32];
base = pow(10,flen);
dn = x*base;
sprintf(stra, "%d.", (int)(dn/base));
dn = abs(dn);
if(dn%base==0)
{
for(uint8_t j=1;j<=flen;j++)
{
stra = strcat(stra, "0");
}
return;
}
else
{
if(flen==1){
sprintf(mc, "%d", (int)(dn%base));
stra = strcat(stra, mc);
return;
}
for(uint8_t j=1;j<flen;j++)
{
if((dn%base)<pow(10,j))
{
for(uint8_t k=1;k<=(flen-j);k++)
{
stra = strcat(stra, "0");
}
sprintf(mc, "%d", (int)(dn%base));
stra = strcat(stra, mc);
return;
}
}
sprintf(mc, "%d", (int)(dn%base));
stra = strcat(stra, mc);
return;
}
}
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
char * degree = "°";
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t DEVID;
char console[256];
char fconsole[256];
uint16_t D_T;
uint16_t D_M;
float F_T;
float F_M;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
PY_usDelayTest();
PY_usDelayOptimize();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
DEVID = HDC1080_READ_REG_DEVICE_ID();
sprintf(console, "\r\nDevice ID = 0x%x\r\n", DEVID);
while(CDC_Transmit_FS((uint8_t* )console, strlen(console))==USBD_BUSY) PY_Delay_us_t(1);
PY_Delay_us_t(500000);
HDC1080_READ_DATA(&D_T, &D_M);
sprintf(console, "\r\nTemperature Read Value = 0x%x\r\nMoisture Read Value = 0x%x", D_T, D_M);
while(CDC_Transmit_FS((uint8_t* )console, strlen(console))==USBD_BUSY) PY_Delay_us_t(1);
F_T = (float)D_T*165/65536-40;
py_f2s4printf(fconsole, F_T, 2);
sprintf(console, "\r\nTemperature = %s%s", fconsole, degree);
while(CDC_Transmit_FS((uint8_t* )console, strlen(console))==USBD_BUSY) PY_Delay_us_t(1);
F_M = (float)D_M*100/65536;
py_f2s4printf(fconsole, F_M, 2);
sprintf(console, "\r\nMoisture = %s%%\r\n", fconsole);
while(CDC_Transmit_FS((uint8_t* )console, strlen(console))==USBD_BUSY) PY_Delay_us_t(1);
PY_Delay_us_t(500000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_SET);
/*Configure GPIO pins : PB0 PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
测试效果
代码的打印输出:
例程下载
STM32F103模拟I2C协议获取HDC1080温湿度传感器数据例程
–End–