STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測(cè)實(shí)用過)(共11頁)
-
資源ID:58668042
資源大小:17.42KB
全文頁數(shù):11頁
- 資源格式: DOCX
下載積分:0積分
快捷下載
會(huì)員登錄下載
微信登錄下載
微信掃一掃登錄
友情提示
2、PDF文件下載后,可能會(huì)被瀏覽器默認(rèn)打開,此種情況可以點(diǎn)擊瀏覽器菜單,保存網(wǎng)頁到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請(qǐng)使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無水印,預(yù)覽文檔經(jīng)過壓縮,下載后原文更清晰。
5、試題試卷類文檔,如果標(biāo)題沒有明確說明有答案則都視為沒有答案,請(qǐng)知曉。
|
STM32F030讀取溫濕度傳感器SHT20的C程序源碼(經(jīng)實(shí)測(cè)實(shí)用過)(共11頁)
精選優(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"/#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 RESOLUTION_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 delayms(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 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 * retval None */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 * 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_InitStructure.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);/* * 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_Level_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_SCL_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(); _nop(); _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);/* * 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跳變成高 * retval 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) if(Write_Byte & SHT20ADDR) HTU20_SDA_1(); else 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); /原來是5 HTU20_SDA_IN();i = 100;doif(HTU20_SDA = 0) break;i-;I2CDelay(4); /原來是5while(i>0);HTU20_SCL_0();if(i) return ACK;elsereturn 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 * 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_SCL_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(); /清時(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 by 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% 測(cè)量時(shí)間:12ms(typ.) / 11bit T 測(cè)量時(shí)間: 9ms(typ.) I2CStop();/* * brief read sht20's 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 + 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) 的后兩位在進(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 -*/ /equation: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;t<100;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è)