《STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測實(shí)用過)(共11頁)》由會(huì)員分享,可在線閱讀,更多相關(guān)《STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測實(shí)用過)(共11頁)(11頁珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、精選優(yōu)質(zhì)文檔-傾情為你奉上/* * * 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 stm32f0xx.h/#include HTU20.h/#include uart.h/#
2、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#define RESOLUTION_12b0x01/RH 8 TH 12#define RESO
3、LUTION_13b0x80/RH 10 TH 13#define RESOLUTION_14b0x00/RH 12 TH 14#define OTP_disEN0x02/不能啟動(dòng)OPT加載#define OTP_EN0x00/啟動(dòng)OPT加載#defineRESh16/濕度解析度#defineRESt16/溫度解析度#defineMeasureDelay12/解析延時(shí)時(shí)間 單位mstypedef unsigned charBYTE;typedef unsigned int WORD;BYTE Delayms;GPIO_InitTypeDef GPIO_InitStructure;void de
4、layms(BYTE ms);/* * brief Start HTU GPIO port CLK * retval None */void 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 Temperature Measureme
5、nt 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_REGI
6、STER 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 None */void HTU20_SDA_O
7、UT() 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_SD
8、A_PORT, &GPIO_InitStructure);/* * brief Set SDA pin dircate as intput * retval None */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_InitStruc
9、ture.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SDA_PORT, &GPIO_InitStructure);/* * brief Set SDA pin as high * retval None */void HTU20_SDA_1() GPIO_SetBits(HTU20_SDA_PORT,HTU20_SDA_PIN);/* * brief Set SDA pin as low * retval None */void HTU20_SDA_0() GPIO_ResetBits(HTU20_SDA_PORT,HTU20_SDA_PIN)
10、;/* * brief Set SCL pin dircate as output * retval None */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_Le
11、vel_2; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(HTU20_SCL_PORT, &GPIO_InitStructure);/* * brief Set SCL pin as high * retval None */void HTU20_SCL_1() GPIO_SetBits(HTU20_SCL_PORT,HTU20_SCL_PIN);/* * brief Set SCL pin as low * retval None */void HTU20_SCL_0() GPIO_ResetBits(HTU20_SC
12、L_PORT,HTU20_SCL_PIN);/*= Simulation I2C Function=*/* * brief A short delay for iic synchronization * param t us * retval None */void I2CDelay (BYTE t) while(t-) _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _n
13、op(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); /* * brief I2C init * retval None */void I2CInit(void)HTUInit(); HTU20_SDA_OUT(); HTU20_SCL_OUT(); HTU20_SDA_1(); I2CDelay(1); HTU20_SCL_1(); I2CDelay(1);/* *
14、 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跳變成高 * ret
15、val None */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 * param Write_Byte: data * retval None */BYTE I2C_Write_Byte(BYTE Write_Byte) BYTE i; HTU20_SDA_OUT(); for(i = 0; i 8; +i) i
16、f(Write_Byte & SHT20ADDR) HTU20_SDA_1(); else HTU20_SDA_0(); I2CDelay(1); HTU20_SCL_1(); /輸出SDA穩(wěn)定后拉高SCL,從機(jī)檢測到后采樣 I2CDelay(5); /保證足夠長鎖定時(shí)間,確保從機(jī)采樣成功 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為低,收到AC
17、K/ return ACK;/ HTU20_SCL_0();/ I2CDelay(15);/* * brief simu read byte form IIC * param * retval None */BYTE I2C_Read_Byte(BYTE AckValue)/receivebyte BYTE i, RDByte = 0;HTU20_SCL_0();/*后改的程序*/ HTU20_SDA_OUT();/ HTU20_SCL_0(); HTU20_SDA_1(); /釋放總線,并置數(shù)據(jù)線為輸入 HTU20_SDA_IN(); for (i = 0; i 8; +i) HTU20_S
18、CL_1(); /拉高SCL期間,采樣 I2CDelay(2); RDByte = 1; if(HTU20_SDA = 1) RDByte |= 0x01; else RDByte &= 0xfe; I2CDelay(1); HTU20_SCL_0(); /下降沿告知從機(jī)發(fā)送下一位 I2CDelay(6); HTU20_SDA_OUT(); /接受完一個(gè)字節(jié),發(fā)送ACK or NACK if(AckValue = 1) HTU20_SDA_1(); else HTU20_SDA_0(); I2CDelay(3); HTU20_SCL_1(); I2CDelay(5); HTU20_SCL_0(
19、); /清時(shí)鐘線 I2CDelay(15); /HTU20_SDA_1(); return RDByte;/* * brief soft reset by transmit reset command * retval None */void SoftReset(void) I2CInit(); I2CStart(); I2C_Write_Byte(SHT20ADDR & 0xfe); /I2C address + write I2C_Write_Byte(SOFT_RESET); /soft reset I2CStop();/* * brief SET HTU20D resolution b
20、y write register * retval None */void SET_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); /設(shè)置分辨率 11bit RH% 測量時(shí)間:12ms(typ.) / 11bit T 測量時(shí)間: 9ms(typ.) I2CStop();/* * brief re
21、ad sht20s converted result * param TempOrHumiCMD: operation command for temperature or humidity * retval None */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 + A
22、CK 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); /Che
23、cksum + NACK I2CStop(); LSB &= 0xfc; /Data (LSB) 的后兩位在進(jìn)行物理計(jì)算前前須置0 temp = (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 -*/ /eq
24、uation: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);專心-專注-專業(yè)