买的DHT11,但就算用例程代码也测不了,不知怎么回事,请求大神的指点啊,51C代码如下:
#include <reg52.h>
#include <intrins.h>
#define LCD_DB P0
sbit DQ = P1^0;
sbit LCD_RS = P2^4;
sbit LCD_RW = P2^5;
sbit LCD_E = P2^6;
void ini
tial(void);
void read_DHT11(void);
void LCD_write_command(unsigned char com);
void LCD_display_char(unsigned char x, unsigned char y, unsigned char dat);
unsigned char read_DHT11_char(void);
void control_temperature_humidity(void);
void delay_xms(unsigned int time_xms);
void delay_x10us(unsigned int time_x10us);
unsigned char stop_system = 0;
unsigned char lineOne[] = "TS(0-50): C";
unsigned char lineTwo[] = "HS(20-90): %RH";
unsigned int T0_number = 0, T1_number, PWM_width_H;
unsigned char temperature_ten, temperature_one, humidity_ten, humidity_one;
unsigned char temperature_H, temperature_L, humidity_H, humidity_L, checkData;
void initial(void)
{
unsigned char i, j;
TMOD = 0x11; //定时器0工作方式1,16位计数器;定时器1工作方式1,16位计数器
TH1 = 0xFC; //定时器1溢出周期1ms,延时
TH1 = 0x66;
TH0 = 0xFC; //定时器0中断周期1ms,PWM
TL0 = 0x66;
EA = 1;
ET1 = 1;
ET0 = 1;
EX0 = 1;
IT1 = 1;
TR0 = 1;
LCD_write_command(0x38); //设置8位格式,2行,5x7
LCD_write_command(0x0c); //设置整体显示,关闭光标,且不闪烁
LCD_write_command(0x06); //设置输入方式,增量不移位
LCD_write_command(0x01); //清屏
for (i = 0; i < 16; i++)
{
LCD_display_char(i, 1, lineOne[i]);
}
for (j = 0; j < 16; j++)
{
LCD_display_char(j, 2, lineTwo[j]);
}
LCD_display_char(14, 1, 0xDF); //显示
}
void read_DHT11(void)
{
DQ = 0;
delay_xms(18);
DQ = 1;
delay_x10us(2);
if (DQ == 0)
{
while (DQ == 0);
while (DQ == 1);
humidity_H = read_DHT11_char();
humidity_L = read_DHT11_char();
temperature_H = read_DHT11_char();
temperature_L = read_DHT11_char();
}
}
unsigned char read_DHT11_char(void)
{
unsigned char i, temp_one, temp_two;
for (i = 0; i < 8; i++)
{
while (DQ == 0);
delay_x10us(3);
if (DQ == 0)
{
temp_one = 0;
}
else
{
temp_one = 1;
}
temp_two <<= 1;
temp_two |= temp_one;
while(DQ == 1);
}
return temp_two;
}
void LCD_write_command(unsigned char com)
{
LCD_DB = com;
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 1;
delay_xms(1);
LCD_E = 0;
delay_xms(5);
}
void LCD_display_char(unsigned char x, unsigned char y, unsigned char dat)
{
if (y == 1)
{
LCD_write_command(0x80 + x);
}
else
{
LCD_write_command(0xc0 + x);
}
LCD_DB = dat;
LCD_RS = 1;
LCD_RW = 0;
LCD_E = 1;
delay_xms(1);
LCD_E = 0;
delay_xms(5);
}
void LCD_display_DHT11(void)
{
temperature_ten = temperature_H / 10 + 0x30;
temperature_one = temperature_H % 10 + 0x30;
humidity_ten = humidity_H / 10 + 0x30;
humidity_one = humidity_H % 10 + 0x30;
LCD_display_char(12, 1, temperature_ten);
LCD_display_char(13, 1, temperature_one);
LCD_display_char(11, 2, humidity_ten);
LCD_display_char(12, 2, humidity_one);
}
void delay_xms(unsigned int time_xms)
{
T1_number = 0;
TR1 = 1;
while (1)
{
if (time_xms == T1_number)
{
break;
}
}
TR1 = 0;
}
void delay_x10us(unsigned int time_x10us)
{
while (time_x10us--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void main(void)
{
initial();
while(1)
{
delay_xms(2000);
read_DHT11();
LCD_display_DHT11();
}
}
void INT_0(void) interrupt 0
{
stop_system = 1;
}
void Timer_0(void) interrupt 1
{
TH0 = 0xFC; //定时器0中断周期1ms,PWM
TL0 = 0x66;
}
void Timer_1(void) interrupt 3
{
TH1 = 0xFC; //定时器1溢出周期1ms,延时
TL1 = 0x66;
T1_number++;
}
读取的程序你再看下,之前我搞过一款温湿度的传感器,也是在读取时出现了错误,或是没有读到,最终发现是在时间的问题上,也就是时序上有点出错,最好你用一下示波器看下,搞到时序的问题最好是有示波器来看,那问题很快就会发现了
一周热门 更多>