完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
买的DHT11,但就算用例程代码也测不了,不知怎么回事,请求大神的指点啊,51C代码如下:
#include #include #define LCD_DB P0 ***it DQ = P1^0; ***it LCD_RS = P2^4; ***it LCD_RW = P2^5; ***it LCD_E = P2^6; void initial(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++; } |
|
相关推荐
17个回答
|
|
问题解决了吗?
|
|
|
|
楼主,你说清楚一下你那程序运行的结果?是个什么情况,要不别人帮不了你,是在显示上出问题还是说在读数据时出问题
|
|
|
|
我也不知道是哪里出问题,就是显示屏上没有出现该有的数据;有可能是没测到温湿数据,也有可能是没读到数据。液晶屏显示是没问题的,我换了其他显示程序是行的,现在的问题就只有DHT11这个模块的了,不知是程序哪里有问题。 |
|
|
|
|
|
|
|
读取的程序你再看下,之前我搞过一款温湿度的传感器,也是在读取时出现了错误,或是没有读到,最终发现是在时间的问题上,也就是时序上有点出错,最好你用一下示波器看下,搞到时序的问题最好是有示波器来看,那问题很快就会发现了 |
|
|
|
嗯,非常感谢!我会去搞一下的,我也觉得是时序问题,再搞搞看看吧。 |
|
|
|
嗯,非常感谢!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DHT11对时间的要求很高的吗?
|
|
|
|
楼主,你的dht11弄好了吗,我也遇到了问题,能不能发我一份
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
如何配置Linux操作系统设备树让我的开发板可以将板子上的GPIO接口用作 I2S输出??
1469 浏览 1 评论
1340 浏览 0 评论
2025 浏览 0 评论
2006 浏览 2 评论
1116 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-18 14:22 , Processed in 0.722061 second(s), Total 78, Slave 70 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191