STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng).
《STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng).》由會員分享,可在線閱讀,更多相關(guān)《STM32F030讀取溫濕度傳感器SHT20的C程序源碼經(jīng).(21頁珍藏版)》請?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 //不能啟動OPT加載 #define OTP_EN 0x00 //啟動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ī)檢測到后采樣I2CDelay(5; //保證足夠長鎖定時(shí)間,確保從機(jī)采樣成功 HTU20_SCL_0(; I2CDelay(1; Write_Byte<<= 1; } I2CDelay(4; HTU20_SDA_1(; //8位發(fā)送完后釋放數(shù)據(jù)線,準(zhǔn)備接受應(yīng)答信號HTU20_SCL_1(; //再次拉高SCL,告訴從機(jī)發(fā)送完畢,等待應(yīng)答I2CDelay(5; //原來是5 HTU20_SDA_IN(; i = 100; do { if(HTU20
21、_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 * @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% 測量時(shí)間: 12ms(typ. } // 11bit T℃測量時(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; /*這兩行由下面屏蔽了的兩行改到上面來的*/ // 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;
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 市教育局冬季運(yùn)動會安全工作預(yù)案
- 2024年秋季《思想道德與法治》大作業(yè)及答案3套試卷
- 2024年教師年度考核表個(gè)人工作總結(jié)(可編輯)
- 2024年xx村兩委涉案資金退還保證書
- 2024年憲法宣傳周活動總結(jié)+在機(jī)關(guān)“弘揚(yáng)憲法精神推動發(fā)改工作高質(zhì)量發(fā)展”專題宣講報(bào)告會上的講話
- 2024年XX村合作社年報(bào)總結(jié)
- 2024-2025年秋季第一學(xué)期初中歷史上冊教研組工作總結(jié)
- 2024年小學(xué)高級教師年終工作總結(jié)匯報(bào)
- 2024-2025年秋季第一學(xué)期初中物理上冊教研組工作總結(jié)
- 2024年xx鎮(zhèn)交通年度總結(jié)
- 2024-2025年秋季第一學(xué)期小學(xué)語文教師工作總結(jié)
- 2024年XX村陳規(guī)陋習(xí)整治報(bào)告
- 2025年學(xué)校元旦迎新盛典活動策劃方案
- 2024年學(xué)校周邊安全隱患自查報(bào)告
- 2024年XX鎮(zhèn)農(nóng)村規(guī)劃管控述職報(bào)告