/****************************************************************** 编 译 器: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++);
}
第三步:调试与显示温湿度
调试无错后,全速状态下显示的温湿度值会因环境温湿度的变化而变化,显示效果如下:
特别鸣谢 原创作者 博主「晟逸轩」
评论
查看更多