STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測實(shí)用過)(共11頁)

上傳人:2127513****773577... 文檔編號(hào):58668042 上傳時(shí)間:2022-02-28 格式:DOCX 頁數(shù):11 大?。?7.42KB
收藏 版權(quán)申訴 舉報(bào) 下載
STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測實(shí)用過)(共11頁)_第1頁
第1頁 / 共11頁
STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測實(shí)用過)(共11頁)_第2頁
第2頁 / 共11頁
STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測實(shí)用過)(共11頁)_第3頁
第3頁 / 共11頁

下載文檔到電腦,查找使用更方便

0 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《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è)

展開閱讀全文
溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!