STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过).docx

上传人:豆**** 文档编号:34407866 上传时间:2022-08-16 格式:DOCX 页数:8 大小:32.87KB
返回 下载 相关 举报
STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过).docx_第1页
第1页 / 共8页
STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过).docx_第2页
第2页 / 共8页
点击查看更多>>
资源描述

《STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过).docx》由会员分享,可在线阅读,更多相关《STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过).docx(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、如有侵权,请联系网站删除,仅供学习与交流STM32F030读取温湿度传感器SHT20的C程序源码(经实测实用过)【精品文档】第 8 页 * file HTU20.x * brief HTU20 function * cpu STM32F051 * compiler Keil uVision V5.01 * author Hanney * copyright * version V1.0.1 * date 18-Sept-2015 * modifydate 20-Sept-2015 * attention/STM32F051 模拟IIC/#include boardAPI.h/#include

2、stm32f0xx.h/#include HTU20.h/#include uart.h/#include delay.h#include stm32f0xx.h#include stm32lib.h#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_11b0x81/RH 11 TH 11#de

3、fine RESOLUTION_12b0x01/RH 8 TH 12#define RESOLUTION_13b0x80/RH 10 TH 13#define RESOLUTION_14b0x00/RH 12 TH 14#define OTP_disEN0x02/不能启动OPT加载#define OTP_EN0x00/启动OPT加载#defineRESh16/湿度解析度#defineRESt16/温度解析度#defineMeasureDelay12/解析延时时间 单位mstypedef unsigned charBYTE;typedef unsigned int WORD;BYTE Delay

4、ms;GPIO_InitTypeDef GPIO_InitStructure;void delayms(BYTE ms); * brief Start HTU GPIO port CLK * retval Nonevoid HTUInit(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

5、 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 0x

6、e7#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 * retval Nonevoid HT

7、U20_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

8、(HTU20_SDA_PORT, &GPIO_InitStructure); * brief Set SDA pin dircate as intput * retval Nonevoid 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_InitS

9、tructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SDA_PORT, &GPIO_InitStructure); * brief Set SDA pin as high * retval Nonevoid HTU20_SDA_1() GPIO_SetBits(HTU20_SDA_PORT,HTU20_SDA_PIN); * brief Set SDA pin as low * retval Nonevoid HTU20_SDA_0() GPIO_ResetBits(HTU20_SDA_PORT,HTU20_SDA_PIN); * br

10、ief Set SCL pin dircate as output * retval Nonevoid 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

11、_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SCL_PORT, &GPIO_InitStructure); * brief Set SCL pin as high * retval Nonevoid HTU20_SCL_1() GPIO_SetBits(HTU20_SCL_PORT,HTU20_SCL_PIN); * brief Set SCL pin as low * retval Nonevoid HTU20_SCL_0() GPIO_ResetBits(HTU20_SCL_PORT,HTU20_SCL_PIN)

12、; Simulation I2C Function * brief A short delay for iic synchronization * param t us * retval Nonevoid 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(); _

13、nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); * brief I2C init * retval Nonevoid 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、S

14、DA同为高,SDA跳变成低之后,SCL跳变成低 * retval Nonevoid 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跳变成高 * retval Nonevoid I2CStop(void) HTU20_SDA_OUT();

15、HTU20_SDA_0(); HTU20_SCL_0(); I2CDelay(5); HTU20_SCL_1(); I2CDelay(5); HTU20_SDA_1(); I2CDelay(5); * brief simu IIC write byte * param Write_Byte: data * retval NoneBYTE 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_

16、SDA_0(); I2CDelay(1); HTU20_SCL_1(); /输出SDA稳定后拉高SCL,从机检测到后采样 I2CDelay(5); /保证足够长锁定时间,确保从机采样成功 HTU20_SCL_0(); I2CDelay(1); Write_Byte 0);HTU20_SCL_0();if(i) return ACK;elsereturn NACK;/ if(HTU20_SDA = 1) /SDA为高,收到NACK/ return NACK;/ else /SDA为低,收到ACK/ return ACK;/ HTU20_SCL_0();/ I2CDelay(15); * brie

17、f simu read byte form IIC * param * retval NoneBYTE 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

18、= 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(); return RDByte; * br

19、ief soft reset by transmit reset command * retval Nonevoid SoftReset(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 * retval Nonevoid SET_Resolution(void) I2CStart(

20、); 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 sht20s converted result * param TempOrHumiCMD: operation command fo

21、r temperature or humidity * retval Nonefloat 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)

22、;/*这两行由下面屏蔽了的两行改到上面来的*/ 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 = (

23、MSB 8) + LSB; if (TempOrHumiCMD = (char)TRIGGER_HUMI) /*- calculate relative humidity %RH -*/ /equation: RH% = -6 + 125 * SRH/2RESh Humidity = (temp * 125) / 65536 - 6; return Humidity; else /*- calculate temperature C -*/ /equation:T = -46.85 + 175.72 * ST/2RESt Temperature = (temp * 175.72) / 65536 - 46.85; return Temperature; return 0;void delayms(BYTE ms)BYTE t;for(t=0;t100;t+)I2CDelay(10);/Delayms = ms;/while(Delayms);#define TRIGGER_TEMP 0xf3#define TRIGGER_HUMI 0xf5void I2CInit(void);void SoftReset(void);void SET_Resolution(void);float ReadSht20(char TempOrHumiCMD);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁