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;

本文来源:https://www.bwwdw.com/article/n6lg.html

Top