将DHT11数据显示在 LCD1602上

LCD1602.c文件

#include "LCD1602.h"
#include "sys.h"
#include "delay.h"
// D0<--> B11
// D1<--> B10
// D2<--> B1
// D3<--> B0
// D4<--> A7
// D5<--> A6
// D6<--> A5
// D7<--> A4


// RS<-->A3
// E<--> A2
// RW<-->A1


void  LCD1602_Init(void)
{


GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | 
                           GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7  ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11 ; 
GPIO_Init(GPIOB, &GPIO_InitStructure);
}




void  LCD1602_Write_Cmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
delay_ms(1);
LCD_E = 1;
LCD_D0 = cmd>>0;
LCD_D1 = cmd>>1;
LCD_D2 = cmd>>2;
LCD_D3 = cmd>>3;
LCD_D4 = cmd>>4;
LCD_D5 = cmd>>5;
LCD_D6 = cmd>>6;
LCD_D7 = cmd>>7;
delay_ms(1);
LCD_E  = 0;
delay_ms(1);


}




void  LCD1602_Write_Data(unsigned char data)
{
LCD_RS = 1;
LCD_RW = 0;
delay_ms(1);
LCD_E  = 1;
LCD_D0 = data>>0;
LCD_D1 = data>>1;
LCD_D2 = data>>2;
LCD_D3 = data>>3;
LCD_D4 = data>>4;
LCD_D5 = data>>5;
LCD_D6 = data>>6;
LCD_D7 = data>>7;
delay_ms(1);
LCD_E  = 0;
delay_ms(1);

}


DHT11.c文件

#include "DHT11.h"
#include "delay.h"


unsigned char T_int = 0;
unsigned char T_dec = 0;
unsigned char H_int = 0;
unsigned char H_dec = 0;
unsigned char check_sum = 0;
unsigned int count = 0;//重试次数




void DHT11_PIN_Init(unsigned char mode)

GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
if(mode == READ)//浮空输入
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
}
if(mode == WRITE)//推挽式输出
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
}
GPIO_Init(GPIOA, &GPIO_InitStructure);
}


unsigned char DHT11_Get_Data(void)
{




//单片机发送起始信号

DHT11_PIN_Init(WRITE);
DHT11_DATA_WRITE = 0;
delay_ms(20);
DHT11_DATA_WRITE = 1;
DHT11_PIN_Init(READ);

//等待DHT11应答
count = 0;
while(DHT11_DATA_READ != 0)
{
count++;


if(count >= 1000)
{
return 2;
}
}
delay_us(40);
count = 0;
while(DHT11_DATA_READ == 0)
{
count++;


if(count >= 1000)
{
return 2;
}
}
delay_us(40);
count = 0;
while(DHT11_DATA_READ != 0)
{
count++;


if(count >= 1000)
{
return 2;
}
}


//接收数据
H_int = DHT11_READ_BYTE();
H_dec = DHT11_READ_BYTE();
T_int = DHT11_READ_BYTE();
T_dec = DHT11_READ_BYTE();
check_sum = DHT11_READ_BYTE();


if(check_sum == H_int+H_dec+T_int+T_dec)
{
return 1;
}
else
{
return 0;
}

}


unsigned char DHT11_READ_BYTE(void)
{
unsigned char i = 0;
unsigned char buf = 0;
for(i = 0;i<8;i++)
{
                  buf = buf << 1;
count = 0;
while(DHT11_DATA_READ == 0)
{
count++;

if(count >= 1000)
{
return 2;
}
}


delay_us(40);
if(DHT11_DATA_READ == 0)
{
buf = buf | 0x00;
}
else
{
buf = buf | 0x01;
count = 0;
while(DHT11_DATA_READ != 0)
{
count++;
if(count >= 1000)
{
return 2;
}
}


}
}
return buf;
        
}

实证照片


将DHT11数据显示在 LCD1602上_第1张图片

将DHT11数据显示在 LCD1602上_第2张图片

你可能感兴趣的:(stm32实战)