STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng).

上傳人:gfy****yf 文檔編號(hào):27415230 上傳時(shí)間:2021-08-18 格式:DOC 頁(yè)數(shù):21 大?。?6.51KB
收藏 版權(quán)申訴 舉報(bào) 下載
STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng)._第1頁(yè)
第1頁(yè) / 共21頁(yè)
STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng)._第2頁(yè)
第2頁(yè) / 共21頁(yè)
STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng)._第3頁(yè)
第3頁(yè) / 共21頁(yè)

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

0 積分

下載資源

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

資源描述:

《STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng).》由會(huì)員分享,可在線閱讀,更多相關(guān)《STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng).(21頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

1、****************************************************************************** * @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 ******************************

2、************************************************ * @attention */ //STM32F051 模擬IIC //#include "boardAPI.h" //#include "stm32f0xx.h" //#include "HTU20.h" //#include "uart.h" //#include "delay.h" #include "stm32f0xx.h" #include "stm32lib.h" #define HTU20_CLK RCC_AHBPeriph_GPIOB #define HTU2

3、0_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

4、_disEN 0x02 //不能啟動(dòng)OPT加載 #define OTP_EN 0x00 //啟動(dòng)OPT加載 #define RESh 16 //濕度解析度 #define RESt 16 //溫度解析度 #define MeasureDelay 12 //解析延時(shí)時(shí)間單位ms typedef unsigned char BYTE; typedef unsigned int WORD; BYTE Delayms; GPIO_InitTypeDef GPIO_InitStructure; voiddelayms(BYTE ms; /** * @brief Start HTU

5、GPIO port CLK * @retvalNone */ voidHTUInit(void { RCC_AHBPeriphClockCmd(HTU20_CLK, ENABLE; } /*================================================================ IIC address ================================================================*/ #define SHT20ADDR 0x80 /*==========================

6、====================================== ACK and NACK defination ================================================================*/ #define ACK 0 #define NACK 1 /*================================================================ HTU20D Command Code =================================================

7、===============*/ /* 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

8、 0xfe */ #define SOFT_RESET 0xfe #define READ_REGISTER 0xe7 #define WRITE_REGISTER 0xe6 #define TRIGGER_TEMP 0xf3 #define TRIGGER_HUMI 0xf5 /*================================================================ HTU20 operate interface ============================================================

9、====*/ #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

10、 = 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

11、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_

12、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 S

13、CL 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

14、_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_Re

15、setBits(HTU20_SCL_PORT,HTU20_SCL_PIN; } /*================================================================ Simulation I2C Function ================================================================*/ /** * @brief A short delay for iic synchronization * @param t us * @retvalNone */ void I2CDe

16、lay (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(;

17、__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(vo

18、id { 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(;

19、 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 {

20、 HTU20_SDA_0(; } I2CDelay(1; HTU20_SCL_1(; //輸出SDA穩(wěn)定后拉高SCL,從機(jī)檢測(cè)到后采樣I2CDelay(5; //保證足夠長(zhǎng)鎖定時(shí)間,確保從機(jī)采樣成功 HTU20_SCL_0(; I2CDelay(1; Write_Byte<<= 1; } I2CDelay(4; HTU20_SDA_1(; //8位發(fā)送完后釋放數(shù)據(jù)線,準(zhǔn)備接受應(yīng)答信號(hào)HTU20_SCL_1(; //再次拉高SCL,告訴從機(jī)發(fā)送完畢,等待應(yīng)答I2CDelay(5; //原來(lái)是5 HTU20_SDA_IN(; i = 100; do { if(HTU20

21、_SDA == 0 break; i--; I2CDelay(4; //原來(lái)是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 * @retvalNo

22、ne */ 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_SCL_1(; //拉高SCL期間,采樣 I2CDelay(2; RDByte<<= 1; if(HTU20_SDA == 1 { RDByte |= 0x

23、01; } 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(; //清時(shí)鐘線 I2CDelay(15; //HTU20_SDA_1(; retur

24、nRDByte; } /** * @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 * @r

25、etvalNone */ 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; //設(shè)置分辨率11bit RH% 測(cè)量時(shí)間: 12ms(typ. } // 11bit T℃測(cè)量時(shí)間: 9ms(typ. } I2CStop(; } /** * @bri

26、ef read sht20s 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 add

27、ress + write + ACK { if(I2C_Write_Byte(TempOrHumiCMD == ACK //command { // delayms(MeasureDelay; /*這兩行由下面屏蔽了的兩行改到上面來(lái)的*/ // I2CStart(; do { delayms(MeasureDelay; I2CStart(; } while(I2C_Write_Byte(SHT20ADDR | 0x01 == NACK; //I2C address + read + NACK MSB = I2C_Read_Byte(ACK; LSB = I2C_Read

28、_Byte(ACK; I2C_Read_Byte(NACK; //Checksum + NACK I2CStop(; LSB &= 0xfc; //Data (LSB 的后兩位在進(jìn)行物理計(jì)算前前須置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; ret

29、urn 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;

展開(kāi)閱讀全文
溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
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),我們立即給予刪除!