?
/****************************************************************** 編??譯??器:Haawking IDE V0.1.2 ??D??S??P:dsc28034湖人板 電?腦?配?置:Windows10 專業版; 64 位操作系統 仿??真??器:HX100v2 使??用??庫:無 作? ?? ?? ?用:在OLED屏上顯示溫濕度值 說? ?? ?? ?明:Debug as程序后在oled屏上顯示溫濕度數值 版? ?? ?? ?本:V0.0.1 ******************************************************************/ |
第一步:創建工程
上篇文章中有具體流程,這里不做過多贅述
第二步:編程
根據官方提供的DHT11使用手冊。流程如下:
DHT11鏈接:https://pan.baidu.com/s/1DxBY9vMtx5BlxtpIx6okKQ?
提取碼:8034
? ? ? ?將GPIO0設為普通IO口,并設置為輸出狀態,拉高主機,拉低主機18-20ms,拉高主機20-40us;設置為輸入狀態,等待從機響應;響應過后判斷輸入的高電平,在高電平段延時40us后仍為高電平則輸入1,為低電平則輸入0。
程序如下:
?
DHT11.c
?
//初始化GPIO0
void gpio0_init(void)
{
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 =0;
GpioCtrlRegs.GPAQSEL1.bit.GPIO0 = 0;
EDIS;
}
//主機輸出設置
void DHT11_Rst(void)
{
uchar i;
EALLOW;
DAT_DIR=1;
EDIS;
// // test
// while(1) {
// DAT= 0;
// DELAY_US(10);
// DAT = 1;
// DELAY_US(10);
// }
DAT= 0;
DELAY_US(300000);
DAT = 1;
DELAY_US(350);
}
(注:程序中的test目的是測試延時函數DELAY_US()的真實值)
?
//等待DHT11的響應
Uint8 Dht11_Check(void){
Uint8 retry=0;
EALLOW;
DAT_DIR=0;
EDIS;
? ? while (DAT&&retry<100){
? ? ? ? retry++;
? ? ? ? DELAY_US(1);
? ? }
? ? if(retry>=100)return 1; else retry=0;
? ? while (!DAT&&retry<100){
? ? ? ? retry++;
? ? ? ? DELAY_US(1);
? ? }
? ? if(retry>=100)return 1;else return 0;
}
?
//初始化DHT11
Uint8 DHT11_Init (void){
DHT11_Rst();
return Dht11_Check();
}
//從DHT11讀取一個位
uchar Dht11_ReadBit(void){
? ? uchar retry=0;
? ? while (DAT&&retry<100){
? ? ? ? ? ?retry++;
? ? ? ? ? ? DELAY_US(1);
? ? ? ? }
? ? ? ?retry=0;
? ? while(!DAT&&retry<100){
? ? ? retry++;
? ? ? DELAY_US(1);
? ? }
? ? DELAY_US(400);
? ? if(DAT)return 1; else return 0;
}
?
//從DHT11讀取一個字節
uchar Dht11_ReadByte(void){
? ? uchar i,dat;
? ? dat=0;
? ? for (i=0;i<8;i++){
? ? ? ? dat<<=1;
? ? ? ? dat|=Dht11_ReadBit();
? ? }
? ? return dat;
}
?
//讀取一次數據
Uint8 DHT11_ReadData(Uint8 *temp,u8 *humi){
uchar buf[5];
uchar i;
DHT11_Rst();
? ? if(Dht11_Check()==0){
? ? ? ? for(i=0;i<5;i++){
? ? ? ? ? ? buf[i]=Dht11_ReadByte();
? ? ? ? }
? ? ? ? if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]){
? ? ? ? *humi=buf[0];
? ? ? ? *temp=buf[2];
? ? ? ? }
? ? }
}
?
DHT11.h
?
#define uchar? ? ? ? ? ? ? ? ? unsigned char
#define DAT_DIR GpioCtrlRegs.GPADIR.bit.GPIO0?
#define DAT GpioDataRegs.GPADAT.bit.GPIO0
?
extern char temp[4];
?
void gpio0_init(void);
void DHT11_Rst(void);
Uint8 Dht11_Check(void);
Uint8 DHT11_Init (void);
uchar Dht11_ReadBit(void);
uchar Dht11_ReadByte(void);
Uint8 DHT11_ReadData(Uint8 *temp,u8 *humi);
?
這里給出OLED屏.h取字模板,OLED模塊例程主頁有,還請自行參考
?
static int Hzk[][32]=
{
{0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
{0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00},/*"溫",0*/
?
{0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
{0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F,0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00},/*"濕",1*/
?
{0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00},
{0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00},/*"度",2*/
};
static int F8X16[]=
{
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",0*/
0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,/*"1",1*/
0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*"2",2*/
0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,/*"3",3*/
0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24,/*"4",4*/
0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00,/*"5",5*/
0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,/*"6",6*/
0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00,/*"7",7*/
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*"8",8*/
0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00,/*"9",9*/
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,/*"C",10*/
0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00,/*"%",11*/
0x00,0x00,0x04,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"^",12*/
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,/*"R",13*/
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,/*"H",14*/
};
?
main.c
?
void delay();
Uint8 temperature;
Uint8 humidity;
void main(void)
{
InitSysCtrl();
DINT;
InitPieCtrl();
IER_DISABLE(0xFFFF);
IFR_DISABLE(0xFFFF);
InitPieVectTable();
? ? gpio0_init();
? ? I2C_GPIO_Config_OLED();
? ? OLED_Init();
OLED_Clear();
OLED_ShowCHinese(0,0,0);
OLED_ShowCHinese(16,0,2);
OLED_ShowCHinese(0,4,1);
OLED_ShowCHinese(16,4,2);
? ? while(1)
? ? {
? ? ? ? ? DHT11_ReadData(&temperature,&humidity);
? ? ? OLED_ShowNum(40,0,temperature,2,16);
? ? ? OLED_ShowNum(40,4,humidity,2,16);
OLED_ShowChar(56,0,12,16);
OLED_ShowChar(64,0,10,16);
OLED_ShowChar(56,4,11,16);
OLED_ShowChar(64,4,13,16);
OLED_ShowChar(72,4,14,16);
? ? ? delay();
? ?}
}
void delay(void)
{
for(int i=0;i<50000;i++);
}
第三步:調試與顯示溫濕度
調試無錯后,全速狀態下顯示的溫濕度值會因環境溫濕度的變化而變化,顯示效果如下:
特別鳴謝 原創作者 博主「晟逸軒」
評論
查看更多