技术标签: STM32 温湿度传感器 SHT2x 电子电路 SHT21
#include "stm32f0xx.h"
代替SHT2x.h
#ifndef SHT2x_H
#define SHT2x_H
//---------- Includes ----------------------------------------------------------
#include "I2C_HAL.h"
#include "database.h"
//---------- Defines -----------------------------------------------------------
#define LITTLE_ENDIAN //一定要设置大小端,当设置错误会出现比较大的数据跳动
typedef union {
uint16_t u16; // element specifier for accessing whole u16
int16_t i16; // element specifier for accessing whole i16
struct {
#ifdef LITTLE_ENDIAN // Byte-order is little endian
uint8_t u8L; // element specifier for accessing low u8
uint8_t u8H; // element specifier for accessing high u8
#else // Byte-order is big endian
uint8_t u8H; // element specifier for accessing low u8
uint8_t u8L; // element specifier for accessing high u8
#endif
} s16; // element spec. for acc. struct with low or high u8
} nt16;
// Error codes
typedef enum{
ACK_ERROR = 0x01,
TIME_OUT_ERROR = 0x02,
CHECKSUM_ERROR = 0x04,
UNIT_ERROR = 0x08
}etError;
// CRC
#define SHT2xPOLYNOMIAL 0x131 //P(x)=x^8+x^5+x^4+1 = 100110001
// sensor command
typedef enum{
TRIG_T_MEASUREMENT_HM = 0xE3, // command trig. temp meas. hold master
TRIG_RH_MEASUREMENT_HM = 0xE5, // command trig. humidity meas. hold master
TRIG_T_MEASUREMENT_POLL = 0xF3, // command trig. temp meas. no hold master
TRIG_RH_MEASUREMENT_POLL = 0xF5, // command trig. humidity meas. no hold master
USER_REG_W = 0xE6, // command writing user register
USER_REG_R = 0xE7, // command reading user register
SOFT_RESET = 0xFE // command soft reset
}etSHT2xCommand;
typedef enum {
SHT2x_RES_12_14BIT = 0x00, // RH=12bit, T=14bit
SHT2x_RES_8_12BIT = 0x01, // RH= 8bit, T=12bit
SHT2x_RES_10_13BIT = 0x80, // RH=10bit, T=13bit
SHT2x_RES_11_11BIT = 0x81, // RH=11bit, T=11bit
SHT2x_RES_MASK = 0x81 // Mask for res. bits (7,0) in user reg.
} etSHT2xResolution;
typedef enum {
SHT2x_EOB_ON = 0x40, // end of battery
SHT2x_EOB_MASK = 0x40, // Mask for EOB bit(6) in user reg.
} etSHT2xEob;
typedef enum {
SHT2x_HEATER_ON = 0x04, // heater on
SHT2x_HEATER_OFF = 0x00, // heater off
SHT2x_HEATER_MASK = 0x04, // Mask for Heater bit(2) in user reg.
} etSHT2xHeater;
// measurement signal selection
typedef enum{
HUMIDITY,
TEMP
}etSHT2xMeasureType;
typedef enum{
I2C_ADR_W = 128, // sensor I2C address + write bit
I2C_ADR_R = 129 // sensor I2C address + read bit
}etI2cHeader;
uint8_t SHT2x_ReadUserRegister(uint8_t *pRegisterValue);
uint8_t SHT2x_WriteUserRegister(uint8_t *pRegisterValue);
void SHT2x_InitUserRegister(void);
uint8_t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand);
float SHT2x_CalcRH(uint16_t u16sRH);
float SHT2x_CalcTemperatureC(uint16_t u16sT);
void SHT2x_Process();
#endif
SHT2x.c
//==============================================================================
// SH2x 温湿度采集程序
//==============================================================================
// File : SHT2x.c
// Author : MC
// Compiler : Keil 5
//==============================================================================
//---------- Includes ----------------------------------------------------------
#include "SHT2x.h"
#include <assert.h> // assert functions
/*
校验
data 校验的数据
nbrOfBytes 数据长度
checksum 校验对比数
返回 0 成功 CHECKSUM_ERROR 4 失败
*/
//==============================================================================
uint8_t SHT2x_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
//==============================================================================
{
uint8_t crc = 0;
uint8_t byteCtr;
//calculates 8-Bit checksum with given polynomial
for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
{
crc ^= (data[byteCtr]);
for (uint8_t bit = 8; bit > 0; --bit)
{
if (crc & 0x80) crc = (crc << 1) ^ SHT2xPOLYNOMIAL;
else crc = (crc << 1);
}
}
if (crc != checksum) return CHECKSUM_ERROR;
else return 0;
}
/*
轮询模式采集温湿度
eSHT2xMeasureType 需要采集的项, HUMIDITY湿度 TEMP温度
pMeasurand 采集回来的原始值
*/
//===========================================================================
uint8_t SHT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand)
//===========================================================================
{
uint8_t checksum; //checksum
uint8_t data[2]; //data array for checksum verification
uint8_t error=0; //error variable
uint16_t i=0; //counting variable
//-- write I2C sensor address and command --
I2c_StartCondition();
I2c_WriteByte (I2C_ADR_W); // I2C Adr
IIC_Wait_Ack();
switch(eSHT2xMeasureType)
{
case HUMIDITY: I2c_WriteByte (TRIG_RH_MEASUREMENT_POLL); break;
case TEMP : I2c_WriteByte (TRIG_T_MEASUREMENT_POLL); break;
}
IIC_Wait_Ack();
//-- poll every 10ms for measurement ready. Timeout after 20 retries (200ms)--
do
{
delay_us(10000); //delay 10ms
I2c_StartCondition();
I2c_WriteByte(I2C_ADR_R);
if(i++ >= 20) break;
} while(IIC_Wait_Ack());
if (i>=20) error |= TIME_OUT_ERROR;
//-- read two data bytes and one checksum byte --
pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK);
pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);
checksum=I2c_ReadByte(NO_ACK);
I2c_StopCondition();
//-- verify checksum --
error |= SHT2x_CheckCrc (data,2,checksum);
return error;
}
/*
主机模式采集温度或适度
eSHT2xMeasureType 需要采集的项, HUMIDITY湿度 TEMP温度
pMeasurand 采集回来的原始值
*/
//===========================================================================
uint8_t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand)
//===========================================================================
{
uint8_t checksum; //checksum
uint8_t data[2]; //data array for checksum verification
uint8_t error=0; //error variable
uint16_t i; //counting variable
//-- write I2C sensor address and command --
I2c_StartCondition();
error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
IIC_Wait_Ack();
switch(eSHT2xMeasureType)
{
case HUMIDITY: I2c_WriteByte(TRIG_RH_MEASUREMENT_HM); break;
case TEMP : I2c_WriteByte(TRIG_T_MEASUREMENT_HM); break;
default: assert(0);
}
IIC_Wait_Ack();
I2c_StartCondition();
I2c_WriteByte (I2C_ADR_R);
IIC_Wait_Ack();
//-- read two data bytes and one checksum byte --
pMeasurand->s16.u8H = data[0] = I2c_SHT21WithSCLReadByte(ACK);
pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);
checksum=I2c_ReadByte(NO_ACK);
I2c_StopCondition();
// //-- verify checksum --
error |= SHT2x_CheckCrc (data,2,checksum);
return error;
}
/*
原始值转换为湿度
u16sRH 原始值
返回 湿度
*/
//==============================================================================
float SHT2x_CalcRH(uint16_t u16sRH)
//==============================================================================
{
float humidityRH; // variable for result
u16sRH &= ~0x0003; // clear bits [1..0] (status bits)
//-- calculate relative humidity [%RH] --
humidityRH = -6.0 + (125.0*(float)u16sRH)/65536; // RH= -6 + 125 * SRH/2^16
return humidityRH;
}
/*
原始值转换为温度
u16sRH 原始值
返回 温度
*/
//==============================================================================
float SHT2x_CalcTemperatureC(uint16_t u16sT)
//==============================================================================
{
float temperatureC; // variable for result
u16sT &= ~0x0003; // clear bits [1..0] (status bits)
//-- calculate temperature --
temperatureC= -46.85 + (175.72*(float)u16sT)/65536; //T= -46.85 + 175.72 * ST/2^16
return temperatureC;
}
/*
读取用户寄存器
pRegisterValue 存放数据的指针
返回 0 成功 CHECKSUM_ERROR 4 失败
*/
//===========================================================================
uint8_t SHT2x_ReadUserRegister(uint8_t *pRegisterValue)
//===========================================================================
{
uint8_t checksum; //variable for checksum byte
uint8_t error=0; //variable for error code
I2c_StartCondition();
error |= I2c_WriteByte (I2C_ADR_W);
IIC_Wait_Ack();
error |= I2c_WriteByte (USER_REG_R);
IIC_Wait_Ack();
I2c_StartCondition();
error |= I2c_WriteByte (I2C_ADR_R);
IIC_Wait_Ack();
*pRegisterValue = I2c_ReadByte(NO_ACK);
I2c_StopCondition();
return error;
}
/*
写入用户寄存器
pRegisterValue 待写入的数
返回 0 成功 CHECKSUM_ERROR 4 失败
*/
//===========================================================================
uint8_t SHT2x_WriteUserRegister(uint8_t *pRegisterValue)
//===========================================================================
{
uint8_t error=0; //variable for error code
I2c_StartCondition();
error |= I2c_WriteByte (I2C_ADR_W);
IIC_Wait_Ack();
error |= I2c_WriteByte (USER_REG_W);
IIC_Wait_Ack();
error |= I2c_WriteByte (*pRegisterValue);
IIC_Wait_Ack();
I2c_StopCondition();
return error;
}
/*
初始化用户寄存器
*/
//===========================================================================
void SHT2x_InitUserRegister(void)
//===========================================================================
{
uint8_t userRegister=0;
userRegister = 0x00;//(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
SHT2x_WriteUserRegister(&userRegister); //write changed user reg
}
/*
用户线程
*/
nt16 sRH={
0}; //variable for raw humidity ticks
nt16 sT={
0}; //variable for raw temperature ticks
void SHT2x_Process(){
int ret;
SHT2x_ReadUserRegister(&WorkUnion.input.userRegister); //get actual user reg
ret=SHT2x_MeasureHM(TEMP, &sT);
WorkUnion.input.Tempreture=SHT2x_CalcTemperatureC(sT.u16);
delay_ms(100);
ret=SHT2x_MeasureHM(HUMIDITY, &sRH);
WorkUnion.input.Humidity=SHT2x_CalcRH(sRH.u16);//if(ret==0)
}
I2C_HAL.h
#ifndef I2C_HAL_H
#define I2C_HAL_H
#ifndef I2C_HAL_H
#define I2C_HAL_H
//---------- Includes ----------------------------------------------------------
#include "database.h"
//---------- Defines -----------------------------------------------------------
//IO口操作宏定义
#define GPIOSCL GPIOA
#define PinSCL GPIO_Pin_2
#define GPIOSDA GPIOA
#define PinSDA GPIO_Pin_3
//IO方向设置
#define SDA_IN() {GPIOA->MODER&=0XFFFFFF3F;}
#define SDA_OUT() {uint32_t Temp=GPIOA->MODER;\
Temp&=0XFFFFFF3F;\
Temp|=0x00000040;\
GPIOA->MODER=Temp;}
#define SCL_IN() {GPIOA->MODER&=0XFFFFFFCF;}
#define SCL_OUT() {uint32_t Temp=GPIOA->MODER;\
Temp&=0XFFFFFFCF;\
Temp|=0x00000010;\
GPIOA->MODER=Temp;}
//IO操作函数
#define IIC_SCL1 GPIO_SetBits(GPIOSCL,PinSCL)
#define IIC_SCL0 GPIO_ResetBits(GPIOSCL,PinSCL) //SCL
#define IIC_SDAx(x) GPIO_WriteBit(GPIOSDA,PinSDA,x)
#define IIC_SDA1 GPIO_SetBits(GPIOSDA,PinSDA) //SDA
#define IIC_SDA0 GPIO_ResetBits(GPIOSDA,PinSDA) //SDA
#define READ_SDA GPIO_ReadInputDataBit(GPIOSDA,PinSDA) //输入SDA
#define READ_SCL GPIO_ReadInputDataBit(GPIOSDA,PinSCL) //输入SCL
//---------- Enumerations ------------------------------------------------------
// I2C level
typedef enum{
LOW = 0,
HIGH = 1,
}etI2cLevel;
// I2C acknowledge
typedef enum{
ACK = 0,
NO_ACK = 1,
}etI2cAck;
//==============================================================================
void I2c_Init ();
//==============================================================================
//Initializes the ports for I2C interface
//==============================================================================
void I2c_StartCondition ();
//==============================================================================
// writes a start condition on I2C-bus
// input : -
// output: -
// return: -
// note : timing (delay) may have to be changed for different microcontroller
// _____
// SDA: |_____
// _______
// SCL : |___
//==============================================================================
void I2c_StopCondition ();
//==============================================================================
// writes a stop condition on I2C-bus
// input : -
// output: -
// return: -
// note : timing (delay) may have to be changed for different microcontroller
// _____
// SDA: _____|
// _______
// SCL : ___|
//===========================================================================
uint8_t I2c_WriteByte (uint8_t txByte);
//===========================================================================
// writes a byte to I2C-bus and checks acknowledge
// input: txByte transmit byte
// output: -
// return: error
// note: timing (delay) may have to be changed for different microcontroller
//===========================================================================
uint8_t I2c_ReadByte (etI2cAck ack);
//===========================================================================
// reads a byte on I2C-bus
// input: rxByte receive byte
// output: rxByte
// note: timing (delay) may have to be changed for different microcontroller
u8 IIC_Wait_Ack(void);
uint8_t I2c_SHT21WithSCLReadByte(etI2cAck ack);
#endif
I2C_HAL.c
//---------- Includes ----------------------------------------------------------
#include "I2C_HAL.h"
//==============================================================================
void I2c_Init ()
//==============================================================================
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
IIC_SDA0; // Set port as output for configuration
IIC_SCL0; // Set port as output for configuration
SDA_OUT(); // Set SDA level as low for output mode
SCL_OUT();
IIC_SDA1; // I2C-bus idle mode SDA released (input)
IIC_SCL1; // I2C-bus idle mode SCL released (input)
}
//==============================================================================
void I2c_StartCondition ()
//==============================================================================
{
SDA_OUT();
SCL_OUT();
IIC_SDA1;
IIC_SCL1;
delay_us(20); // hold time start condition (t_HD;STA)
IIC_SDA0;
delay_us(20); // hold time start condition (t_HD;STA)
IIC_SCL0;
delay_us(20);
}
//==============================================================================
void I2c_StopCondition ()
//==============================================================================
{
SDA_OUT();
SCL_OUT();
IIC_SDA0;
IIC_SCL0;
delay_us(20); // set-up time stop condition (t_SU;STO)
IIC_SCL1;
delay_us(20); // set-up time stop condition (t_SU;STO)
IIC_SDA1;
delay_us(20);
}
//不产生ACK应答
void IIC_NAck(void)
{
SCL_OUT();
SDA_OUT();
IIC_SCL0;
IIC_SDA1;
delay_us(10);
IIC_SCL1;
delay_us(10);
IIC_SCL0;
}
//产生ACK应答
void IIC_Ack(void)
{
SCL_OUT();
SDA_OUT();
IIC_SCL0;
IIC_SDA0;
delay_us(10);
IIC_SCL1;
delay_us(10);
IIC_SCL0;
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SCL1;delay_us(5);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
//I2c_StopCondition();
return 1;
}
delay_us(100);
}
IIC_SCL0;//时钟输出0
return 0;
}
//==============================================================================
uint8_t I2c_WriteByte (uint8_t txByte)
//==============================================================================
{
uint8_t mask,error=0;
u8 t;
SCL_OUT();
SDA_OUT();
IIC_SCL0;//拉低时钟开始数据传输
for(t=0;t<8;t++)
{
IIC_SDAx((txByte&0x80)>>7);
txByte<<=1;
delay_us(10); //对TEA5767这三个延时都是必须的
IIC_SCL1;
delay_us(10);
IIC_SCL0;
delay_us(10);
}
return error; //return error code
}
//==============================================================================
uint8_t I2c_ReadByte (etI2cAck ack)
//==============================================================================
{
unsigned char i,receive=0;
SCL_OUT();
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
IIC_SCL0;
delay_us(5);
IIC_SCL1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(5);
}
if (ack==ACK)
IIC_Ack(); //发送ACK
else
IIC_NAck();//发送nACK
return receive;
}
//==============================================================================
uint8_t I2c_SHT21WithSCLReadByte(etI2cAck ack)
//==============================================================================
{
uint16_t i;
unsigned char receive=0;
SCL_IN(); // set SCL I/O port as input
SDA_IN();
for(i=0; i<10000; i++) // wait until master hold is released or
{
delay_us(10); // a timeout (~1s) is reached
if (READ_SCL==1){
if(READ_SDA)receive++;
break;
}
}
SCL_OUT();
SDA_IN();//SDA设置为输入
for(i=0;i<7;i++ )
{
IIC_SCL0;
delay_us(5);
IIC_SCL1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(5);
}
if (ack==ACK)
IIC_Ack(); //发送ACK
else
IIC_NAck();//发送nACK
return receive;
}
文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文
文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作 导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释: cwy_init/init_123..._达梦数据库导入导出
文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js
文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6
文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输
文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...
文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure
文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割
文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答
文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。
文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入
文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf