没有合适的资源?快使用搜索试试~ 我知道了~
首页Sensirion_Humidity_Sensors_SHT3x_Sample_Code_V2
Sensirion_Humidity_Sensors_SHT3x_Sample_Code_V2
5星 · 超过95%的资源 需积分: 15 26 下载量 130 浏览量
更新于2023-03-16
评论
收藏 568KB PDF 举报
盛思睿的温湿度传感器SHT3x的例代码,代码使用I2C,包含软件架构和代码
资源详情
资源评论
资源推荐
www.sensirion.com Version 2 – May 2015 1/34
Sample Code I
2
C
For communication with the SHT3x Humidity and Temperature Sensor through the I
2
C
Interface
1 Structure and Hierarchy of Code
The sample code is structured into various files. The relationship among the different files is given in Figure 1.
Figure 1 Structure of sample code for SHT3x
Table of Contents
1 Structure and Hierarchy of Code 1
2 Sample Code 2
2.1 main.c 2
2.2 sht3x.h 7
2.3 sht3x.c 13
2.4 i2c_hal.h 26
2.5 i2c_hal.c 28
2.6 system.h 31
2.7 system.c 32
2.8 typedefs.h 32
system.h
system.c
i2c_hal.h
i2c_hal.c
typedefs.h
sht3x.h
sht3x.c
main.c
Introduction
This document contains sample code in C for communication with the SHT3x humidity and temperature sensor through the
I
2
C Interface. The purpose of the code is to ease the user’s software programming when implementing SHT3x sensors. Besides
simple measurement of humidity and temperature, the code contains calculation of CRC checksum and calculation of physical
humidity and temperature values. This sample code was written and optimized for the STM32-Discovery board from
STMicroelectronics, but it can easily be applied to other microcontrollers with few changes.
Sample Code for SHT3x
www.sensirion.com Version 2 – May 2015 2/34
2 Sample Code
Below is the C code for the different files. The code was written and optimized for the STM32-Discovery board from
STMicroelectronics (STM32VLDISCOVERY) and can be easily adapted to other microcontrollers. The portions that need to be
adapted for porting to a different microcontroller are indicated in the comments.
2.1 main.c
//=============================================================================
// S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland
//=============================================================================
// Project : SHT3x Sample Code (V1.1)
// File : main.c (V1.1)
// Author : RFU
// Date : 6-Mai-2015
// Controller: STM32F100RB
// IDE : µVision V5.12.0.0
// Compiler : Armcc
// Brief : This code shows how to implement the basic commands for the
// SHT3x sensor chip.
// Due to compatibility reasons the I2C interface is implemented
// as "bit-banging" on normal I/O's. This code is written for an
// easy understanding and is neither optimized for speed nor code
// size.
//
// Porting to a different microcontroller (uC):
// - the definitions of basic types may have to be changed in typedefs.h
// - adapt the button and led functions for your platform in main.c
// - adapt the port functions / definitions for your uC in i2c_hal.h/.c
// - adapt the timing of the delay function for your uC in system.c
// - adapt the SystemInit() in system.c
// - change the uC register definition file <stm32f10x.h> in system.h
//=============================================================================
//-- Includes -----------------------------------------------------------------
#include "system.h"
#include "sht3x.h"
//-- Static function prototypes -----------------------------------------------
static void EvalBoardPower_Init(void);
static void Led_Init(void);
static void UserButton_Init(void);
static void LedBlueOn(void);
static void LedBlueOff(void);
static void LedGreenOn(void);
static void LedGreenOff(void);
static u8t ReadUserButton(void);
//-----------------------------------------------------------------------------
int main(void)
{
etError error; // error code
u32t serialNumber;// serial number
regStatus status; // sensor status
ft temperature; // temperature [°C]
ft humidity; // relative humidity [%RH]
bt heater; // heater, false: off, true: on
SystemInit();
Led_Init();
UserButton_Init();
Sample Code for SHT3x
www.sensirion.com Version 2 – May 2015 3/34
EvalBoardPower_Init();
SHT3X_Init(0x45); // Address: 0x44 = Sensor on EvalBoard connector
// 0x45 = Sensor on EvalBoard
// wait 50ms after power on
DelayMicroSeconds(50000);
error = SHT3x_ReadSerialNumber(&serialNumber);
if(error != NO_ERROR){} // do error handling here
// demonstrate a single shot measurement with clock-stretching
error = SHT3X_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH,
MODE_CLKSTRETCH, 50);
if(error != NO_ERROR){} // do error handling here
// demonstrate a single shot measurement with polling and 50ms timeout
error = SHT3X_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH, MODE_POLLING,
50);
if(error != NO_ERROR){} // do error handling here
// loop forever
while(1)
{
error = NO_ERROR;
// loop while no error
while(error == NO_ERROR)
{
// read status register
error |= SHT3X_ReadStatus(&status.u16);
if(error != NO_ERROR) break;
// check if the reset bit is set after a reset or power-up
if(status.bit.ResetDetected)
{
//override default temperature and humidity alert limits (red LED)
error = SHT3X_SetAlertLimits( 70.0f, 50.0f, // high set: RH [%], T [°C]
68.0f, 48.0f, // high clear: RH [%], T [°C]
32.0f, -2.0f, // low clear: RH [%], T [°C]
30.0f, -4.0f); // low set: RH [%], T [°C]
if(error != NO_ERROR) break;
// clear reset and alert flags
error = SHT3X_ClearAllAlertFlags();
if(error != NO_ERROR) break;
//start periodic measurement, with high repeatability and 1 measurements per
second
error = SHT3X_StartPeriodicMeasurment(REPEATAB_HIGH, FREQUENCY_1HZ);
if(error != NO_ERROR) break;
//switch green LED on
LedGreenOn();
}
// read measurment buffer
error = SHT3X_ReadMeasurementBuffer(&temperature, &humidity);
if(error == NO_ERROR)
{
// flash blue LED to signalise new temperature and humidity values
Sample Code for SHT3x
www.sensirion.com Version 2 – May 2015 4/34
LedBlueOn();
DelayMicroSeconds(10000);
LedBlueOff();
}
else if (error == ACK_ERROR)
{
// there were no new values in the buffer -> ignore this error
error = NO_ERROR;
}
else break;
// read heater status
heater = status.bit.HeaterStatus ? TRUE : FALSE;
// if the user button is not pressed ...
if(ReadUserButton() == 0)
{
// ... and the heater is on
if(heater)
{
// switch off the sensor internal heater
error |= SHT3X_DisableHeater();
if(error != NO_ERROR) break;
}
}
else
// if the user button is pressed ...
{
// ... and the heater is off
if(!heater)
{
// switch on the sensor internal heater
error |= SHT3X_EnableHeater();
if(error != NO_ERROR) break;
}
}
// wait 100ms
DelayMicroSeconds(100000);
}
// in case of an error ...
// ... switch green and blue LED off
LedGreenOff();
LedBlueOff();
// ... try first a soft reset ...
error = SHT3X_SoftReset();
// ... if the soft reset fails, do a hard reset
if(error != NO_ERROR)
{
SHT3X_HardReset();
}
// flash green LED to signalise an error
LedGreenOn();
DelayMicroSeconds(10000);
LedGreenOff();
}
}
Sample Code for SHT3x
www.sensirion.com Version 2 – May 2015 5/34
//-----------------------------------------------------------------------------
static void EvalBoardPower_Init(void) /* -- adapt this code for your platform --
*/
{
RCC->APB2ENR |= 0x00000008; // I/O port B clock enabled
GPIOB->CRH &= 0x0FFF0FFF; // set push-pull output for Vdd & GND pins
GPIOB->CRH |= 0x10001000; //
GPIOB->BSRR = 0x08008000; // set Vdd to High, set GND to Low
}
//-----------------------------------------------------------------------------
static void Led_Init(void) /* -- adapt this code for your platform --
*/
{
RCC->APB2ENR |= 0x00000010; // I/O port C clock enabled
GPIOC->CRH &= 0xFFFFFF00; // set general purpose output mode for LEDs
GPIOC->CRH |= 0x00000011; //
GPIOC->BSRR = 0x03000000; // LEDs off
}
//-----------------------------------------------------------------------------
static void UserButton_Init(void) /* -- adapt this code for your platform --
*/
{
RCC->APB2ENR |= 0x00000004; // I/O port A clock enabled
GPIOA->CRH &= 0xFFFFFFF0; // set general purpose input mode for User Button
GPIOA->CRH |= 0x00000004; //
}
//-----------------------------------------------------------------------------
static void LedBlueOn(void) /* -- adapt this code for your platform --
*/
{
GPIOC->BSRR = 0x00000100;
}
//-----------------------------------------------------------------------------
static void LedBlueOff(void) /* -- adapt this code for your platform --
*/
{
GPIOC->BSRR = 0x01000000;
}
//-----------------------------------------------------------------------------
static void LedGreenOn(void) /* -- adapt this code for your platform --
*/
{
GPIOC->BSRR = 0x00000200;
}
//-----------------------------------------------------------------------------
static void LedGreenOff(void) /* -- adapt this code for your platform --
*/
{
GPIOC->BSRR = 0x02000000;
}
//-----------------------------------------------------------------------------
剩余33页未读,继续阅读
zyclovemoon
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- ExcelVBA中的Range和Cells用法说明.pdf
- 基于单片机的电梯控制模型设计.doc
- 主成分分析和因子分析.pptx
- 共享笔记服务系统论文.doc
- 基于数据治理体系的数据中台实践分享.pptx
- 变压器的铭牌和额定值.pptx
- 计算机网络课程设计报告--用winsock设计Ping应用程序.doc
- 高电压技术课件:第03章 液体和固体介质的电气特性.pdf
- Oracle商务智能精华介绍.pptx
- 基于单片机的输液滴速控制系统设计文档.doc
- dw考试题 5套.pdf
- 学生档案管理系统详细设计说明书.doc
- 操作系统PPT课件.pptx
- 智慧路边停车管理系统方案.pptx
- 【企业内控系列】企业内部控制之人力资源管理控制(17页).doc
- 温度传感器分类与特点.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1