STM32F030读取温湿度传感器SHT20的C程序源码经
更新时间:2024-05-01 01:50:01 阅读量: 综合文库 文档下载
****************************************************************************** * @file HTU20.x
* @brief HTU20 function * @cpu STM32F051
* @compiler KeiluVision V5.01 * @author Hanney * @copyright * @version V1.0.1 * @date 18-Sept-2015 * @modifydate20-Sept-2015
****************************************************************************** * @attention
*/
//STM32F051 模拟IIC //#include \//#include \//#include \//#include \//#include \
#include \#include \
#define HTU20_CLK RCC_AHBPeriph_GPIOB #define HTU20_SDA_PORT GPIOB #define HTU20_SCL_PORT GPIOB #define HTU20_SDA_PIN GPIO_Pin_11 #define HTU20_SCL_PIN GPIO_Pin_10 #define RESOLUTION_11b 0x81 //RH 11 TH 11 #define RESOLUTION_12b 0x01 //RH 8 TH 12 #define RESOLUTION_13b 0x80 //RH 10 TH 13 #define RESOLUTION_14b 0x00 //RH 12 TH 14 #define OTP_disEN 0x02 //不能启动OPT加载 #define OTP_EN 0x00 //启动OPT加载 #define RESh 16 //湿度解析度 #define RESt 16 //温度解析度
#define MeasureDelay 12 //解析延时时间单位ms typedef unsigned char BYTE; typedef unsigned int WORD; BYTE Delayms;
GPIO_InitTypeDef GPIO_InitStructure; voiddelayms(BYTE ms; /**
* @brief Start HTU GPIO port CLK * @retvalNone */
voidHTUInit(void {
RCC_AHBPeriphClockCmd(HTU20_CLK, ENABLE; }
/*================================================================
IIC address
================================================================*/ #define SHT20ADDR 0x80
/*================================================================ ACK and NACK defination
================================================================*/ #define ACK 0
#define NACK 1
/*================================================================
HTU20D Command Code
================================================================*/ /*
Command Code Comment Trigger Temperature Measurement 0xe3 Hold master Trigger Humidity Measurement 0xe5 Hold master Trigger Temperature Measuremeng 0xf3 No Hold master Trigger Humidity Measurement 0xf5 No Hold master Write user register 0xe6
Read user register 0xe7 Soft Reset 0xfe */
#define SOFT_RESET 0xfe #define READ_REGISTER 0xe7 #define WRITE_REGISTER 0xe6 #define TRIGGER_TEMP 0xf3 #define TRIGGER_HUMI 0xf5
/*================================================================
HTU20 operate interface
================================================================*/
#define HTU20_SCL
GPIO_ReadInputDataBit(HTU20_SCL_PORT,HTU20_SCL_PIN #define HTU20_SDA GPIO_ReadInputDataBit(HTU20_SDA_PORT,HTU20_SDA_PIN
/**
* @brief Set SDA pin dircate as output * @retvalNone */
void HTU20_SDA_OUT( {
GPIO_StructInit(&GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = HTU20_SDA_PIN; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2; GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SDA_PORT, &GPIO_InitStructure; } /**
* @brief Set SDA pin dircate as intput * @retvalNone */
void HTU20_SDA_IN( {
GPIO_StructInit(&GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = HTU20_SDA_PIN; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2; GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SDA_PORT, &GPIO_InitStructure; } /**
* @brief Set SDA pin as high * @retvalNone */
void HTU20_SDA_1( {
GPIO_SetBits(HTU20_SDA_PORT,HTU20_SDA_PIN;
} /**
* @brief Set SDA pin as low * @retvalNone */
void HTU20_SDA_0( {
GPIO_ResetBits(HTU20_SDA_PORT,HTU20_SDA_PIN; } /**
* @brief Set SCL pin dircate as output * @retvalNone */
void HTU20_SCL_OUT( {
GPIO_StructInit(&GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = HTU20_SCL_PIN; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SCL_PORT, &GPIO_InitStructure;
} /**
* @brief Set SCL pin as high * @retvalNone */
void HTU20_SCL_1( {
GPIO_SetBits(HTU20_SCL_PORT,HTU20_SCL_PIN; } /**
* @brief Set SCL pin as low * @retvalNone */
void HTU20_SCL_0( {
GPIO_ResetBits(HTU20_SCL_PORT,HTU20_SCL_PIN; }
/*================================================================
Simulation I2C Function
================================================================*/
/**
* @brief A short delay for iic synchronization * @param t us * @retvalNone */
void I2CDelay (BYTE t { while(t-- {
__nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(;
__nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; __nop(; } } /**
* @brief I2Cinit * @retvalNone */
void I2CInit(void { HTUInit(;
HTU20_SDA_OUT(; HTU20_SCL_OUT(; HTU20_SDA_1(; I2CDelay(1; HTU20_SCL_1(; I2CDelay(1; } /**
* @brief IIC start signal
* @comment SCL、SDA同为高,SDA跳变成低之后,SCL跳变成低 * @retval None */
void I2CStart(void {
HTU20_SDA_OUT(; HTU20_SDA_1(; HTU20_SCL_1(; I2CDelay(5; //大于4.7us HTU20_SDA_0(; I2CDelay(5; //大于4us HTU20_SCL_0(; I2CDelay(5; } /**
* @brief IIC Stop Signal
* @commet SCL、SDA同为低,SCL跳变成高之后,SDA跳变成高* @retvalNone
*/
void I2CStop(void {
HTU20_SDA_OUT(; HTU20_SDA_0(; HTU20_SCL_0(; I2CDelay(5; HTU20_SCL_1(; I2CDelay(5; HTU20_SDA_1(; I2CDelay(5; } /**
* @brief simu IIC write byte * @paramWrite_Byte: data * @retvalNone */
BYTE I2C_Write_Byte(BYTE Write_Byte {
BYTE i;
HTU20_SDA_OUT(; for(i = 0; i < 8; ++i {
if(Write_Byte& SHT20ADDR {
HTU20_SDA_1(; } else {
HTU20_SDA_0(; }
I2CDelay(1;
HTU20_SCL_1(; //输出SDA稳定后拉高SCL,从机检测到后采样I2CDelay(5; //保证足够长锁定时间,确保从机采样成功
HTU20_SCL_0(; I2CDelay(1; Write_Byte<<= 1; }
I2CDelay(4;
HTU20_SDA_1(; //8位发送完后释放数据线,准备接受应答信号HTU20_SCL_1(; //再次拉高SCL,告诉从机发送完毕,等待应答I2CDelay(5; //原来是5
HTU20_SDA_IN(; i = 100; do {
if(HTU20_SDA == 0 break; i--;
I2CDelay(4; //原来是5 }while(i>0; HTU20_SCL_0(; if(i return ACK; else return NACK;
// if(HTU20_SDA == 1 //SDA为高,收到NACK // return NACK;
// else //SDA为低,收到ACK // return ACK; // HTU20_SCL_0(;
// I2CDelay(15; } /**
* @brief simu read byte form IIC * @param * @retvalNone */
BYTE I2C_Read_Byte(BYTE AckValue//receivebyte {
BYTE i, RDByte = 0;
HTU20_SCL_0(; /*后改的程序*/ HTU20_SDA_OUT(; // HTU20_SCL_0(;
HTU20_SDA_1(; //释放总线,并置数据线为输入HTU20_SDA_IN(; for (i = 0; i < 8; ++i {
HTU20_SCL_1(; //拉高SCL期间,采样 I2CDelay(2; RDByte<<= 1;
if(HTU20_SDA == 1 {
RDByte |= 0x01; } else {
RDByte&= 0xfe; }
I2CDelay(1;
HTU20_SCL_0(; //下降沿告知从机发送下一位 I2CDelay(6; }
HTU20_SDA_OUT(; //接受完一个字节,发送ACK or NACK if(AckValue == 1 {
HTU20_SDA_1(; } else {
HTU20_SDA_0(;
}
I2CDelay(3; HTU20_SCL_1(; I2CDelay(5;
HTU20_SCL_0(; //清时钟线 I2CDelay(15; //HTU20_SDA_1(; returnRDByte; } /**
* @brief soft reset by transmit reset command * @retvalNone */
voidSoftReset(void { I2CInit(; I2CStart(;
I2C_Write_Byte(SHT20ADDR & 0xfe; //I2C address + write I2C_Write_Byte(SOFT_RESET; //soft reset
I2CStop(; } /**
* @brief SET HTU20D resolution by write register * @retvalNone */
voidSET_Resolution(void { I2CStart(;
if(I2C_Write_Byte(SHT20ADDR & 0xfe == ACK //I2C address + write + ACK {
if(I2C_Write_Byte(WRITE_REGISTER==ACK //写用户寄存器 {
if(I2C_Write_Byte(0x83==ACK; //设置分辨率11bit RH% 测量时间: 12ms(typ. } // 11bit T℃测量时间: 9ms(typ. } I2CStop(; } /**
* @brief read sht20's converted result
* @paramTempOrHumiCMD: operation command for temperature or humidity * @retvalNone */
float ReadSht20(char TempOrHumiCMD { float temp; BYTE MSB,LSB;
float Humidity, Temperature; SET_Resolution(; I2CStart(;
if(I2C_Write_Byte(SHT20ADDR & 0xfe == ACK //I2C address + write + ACK {
if(I2C_Write_Byte(TempOrHumiCMD == ACK //command {
// delayms(MeasureDelay; /*这两行由下面屏蔽了的两行改到上面来的*/ // I2CStart(; do {
delayms(MeasureDelay; I2CStart(;
} while(I2C_Write_Byte(SHT20ADDR | 0x01 == NACK; //I2C address + read + NACK
MSB = I2C_Read_Byte(ACK; LSB = I2C_Read_Byte(ACK;
I2C_Read_Byte(NACK; //Checksum + NACK I2CStop(;
LSB &= 0xfc; //Data (LSB 的后两位在进行物理计算前前须置0 temp = (MSB << 8 + LSB;
if (TempOrHumiCMD == ((charTRIGGER_HUMI {
/*-- calculate relative humidity [%RH] --*/ //equation: RH% = -6 + 125 * SRH/2^RESh Humidity = (temp * 125 / 65536 - 6; return Humidity; } else {
/*-- calculate temperature [°C] --*/ //equation:T = -46.85 + 175.72 * ST/2^RESt Temperature = (temp * 175.72 / 65536 - 46.85; return Temperature; } } } return 0; }
voiddelayms(BYTE ms { BYTE t; for(t=0;t<100;t++
{ I2CDelay(10; // // } } Delayms = ms; while(Delayms;
//////////////////////////////////////////////////////////////////////////////////////////////////// #define TRIGGER_TEMP #define TRIGGER_HUMI 0xf3 0xf5 void I2CInit(void; voidSoftReset(void; voidSET_Resolution(void; float ReadSht20(char TempOrHumiCMD;
/*-- calculate temperature [°C] --*/ //equation:T = -46.85 + 175.72 * ST/2^RESt Temperature = (temp * 175.72 / 65536 - 46.85; return Temperature; } } } return 0; }
voiddelayms(BYTE ms { BYTE t; for(t=0;t<100;t++
{ I2CDelay(10; // // } } Delayms = ms; while(Delayms;
//////////////////////////////////////////////////////////////////////////////////////////////////// #define TRIGGER_TEMP #define TRIGGER_HUMI 0xf3 0xf5 void I2CInit(void; voidSoftReset(void; voidSET_Resolution(void; float ReadSht20(char TempOrHumiCMD;
正在阅读:
STM32F030读取温湿度传感器SHT20的C程序源码经05-01
数据结构复习04-19
鼓励运动员的话100字02-07
仪器分析 试题及答案10-25
2011年二级市政考试用书增值服务三06-24
藏在我心里的一件事作文550字06-23
参加市领导会见人员名单05-01
项目十 激励同步练习题04-11
冰蓄冷研究的现状与展望01-02
第2章 水静力学12-23
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 温湿度
- 程序源码
- 传感器
- STM32F030
- 读取
- SHT20
- (2015年秋)人教版高中化学必修一第四章《非金属及其化合物》单
- 简爱试题
- 湖北省黄冈市黄冈中学2016-2017学年高一上学期期末考试模拟(3)生
- 基于 NET的超市管理系统的设计与实现本科生毕业论文
- 雅思阅读教师25小时授课大纲-雅思阅读剑桥4-8
- 配位化合物
- 常用机关公文写作
- 车牌自动识别系统的设计 开题报告 - 图文
- 安全评价报告
- 单宁
- 卫星族谱 - 图文
- 工笔画 - 图文
- 2015年高考真题(课标卷I)物理试题详尽解析(Word版)20150810
- 华南理工 - 材料力学随堂练习
- 乡级土地规划数据库标准(送审稿0306)
- 读《论持久战》有感
- 看云识天1说课稿
- 课堂新坐标新高考化学人教版一轮复习课时分层练:第3章 金属及其
- 蜜豆馅饼的做法食谱-你要的家常菜
- 五年级学生评语41