找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 13079|回复: 18
打印 上一主题 下一主题
收起左侧

51单片机读取DH11温湿度模块程序

  [复制链接]
跳转到指定楼层
楼主

  1. /******************************************************
  2.         功   能:DH11温湿度1602(6线驱动)显示
  3.         单片机:STC12C5608AD
  4.         晶  振:11?0592M
  5.         作  者:苏义江
  6.         时  间:2016-10-22
  7. ***************************************************/

  8. #include <reg52.h>
  9. #include <intrins.h>  
  10. #define uint unsigned int  
  11. #define uchar unsigned char   
  12. sbit DATA=P1^1;//dht11data端接单片机的P1^0口//  

  13. uchar RHL,RHH,CL,CH;  
  14. uint m,n;
  15. //很正要的延时
  16. void delayus(uchar  n)
  17. {   
  18.         while(n--);
  19. }
  20. //很正要的延时
  21. void Delay10ms(void)//12M晶振下延时
  22. {   
  23.         uchar a,b;
  24.     for(b=249;b>0;b--)
  25.     for(a=17;a>0;a--);
  26. /*

  27.         uchar i,j,k;
  28.         for(i=5;i>0;i--)
  29.         for(j=4;j>0;j--)   
  30.         {   
  31.                 for(k=248;k>0;k--);   
  32.         }
  33. */
  34. }
  35. //粗延时
  36. void delay_ms(uint z)
  37. {
  38. uint x,y;
  39. for(x=z;x>0;x--)
  40.   for(y=614;y>0;y--);
  41. }
  42. //起始
  43. void Start(void)
  44. {
  45.     DATA=1;   
  46.         delay_ms(10);   
  47.         DATA=0;            
  48.         Delay10ms(); //主机把总线拉低必须大于
  49.         //18msDHT11能检测到起始信号   
  50.         Delay10ms();
  51.         Delay10ms();   
  52.         DATA=1;
  53.         delay_ms(1);        
  54. }
  55. uint integer(uchar x)
  56. {  
  57.         uchar h,l;  
  58.         uint n;  
  59.         l=x&0x0f;  
  60.         h=(x&0xf0)>>4;  
  61.         n=h*16+l;  
  62.         return n;   
  63. }


  64. uchar readdht11_byte()
  65. {   
  66.         uchar i,dht;  
  67.         for(i=8;i>0;i--)  
  68.         {   
  69.                 dht=dht<<1;   
  70.                 while(!DATA); //等待50ms   
  71.                 delayus(30); //延时29us   
  72.                 if(DATA==1)   
  73.                 {
  74.                         dht=dht|0x01;   
  75.                         delayus(60);
  76.                 }   
  77.                 else     
  78.                 delayus(30); //延时29us              
  79.         }
  80.         return(dht);   
  81. }
  82. void read_dht11()
  83. {   
  84.         Start();
  85.         DATA=0;   //数据线拉低>18ms  
  86.         delay_ms(20); //延时>18ms
  87.          DATA=1;   //拉高等待  
  88.         delayus(30);  //延时29us  
  89.         DATA=1;  
  90.         if(!DATA)  
  91.         {   
  92.                 while(!DATA);//  
  93.                 while(DATA);   
  94.                 RHH=readdht11_byte();//湿度整数数据  
  95.                 RHL=readdht11_byte();//湿度小数数据  
  96.                 CH=readdht11_byte();//温度整数数据  
  97.                 CL=readdht11_byte();//温度小数数据   
  98.         }  
  99. }
  100. //////////////////////////////////////////////////
  101. //LCD1602接口定义    RW接地         
  102. #define LCD_DB P2 // - - P2 = DB4~DB7
  103. sbit LCD_RS=P2^2; // - - p2.2 = RS
  104. sbit LCD_E=P2^3; // - - p2.3 = E
  105. void LCD_delay_10us(uint n) // - - 10微秒的延时子程序
  106. {
  107. uint i,j;
  108. for(i=n*10;i>0;i--) // - - 晶振及单片机修改设置
  109. for(j=2;j>0;j--);
  110. }

  111. void LCD_delay_50us(uint n) // - - 50微秒的延时子程序
  112. {
  113.         uint i,j;
  114.         for(i=n*10;i>0;i--) // - - 晶振及单片机修改设置
  115.         for(j=22;j>0;j--);
  116. }
  117. // - - 向LCD1602写指令
  118. void LCD_write_4bit_command(uchar dat);
  119. // - - 向LCD1602写高四位指令
  120. void LCD_write_H4bit_command(uchar dat);
  121. // - - 向LCD1602写低四位指令
  122. void LCD_write_L4bit_command(uchar dat);
  123. // - - 初始化LCD1602
  124. void LCD_init(void)
  125. {
  126.         LCD_delay_50us(10);
  127.         LCD_RS=0; // - - 指令

  128.         LCD_E=0; // - - 使能
  129.         LCD_write_L4bit_command(0x03); // - - 设置4位格式,2行,5x7
  130.         LCD_delay_50us(10);        
  131.         LCD_write_L4bit_command(0x03); // - - 设置4位格式,2行,5x7
  132.         LCD_delay_50us(10);
  133.         LCD_write_L4bit_command(0x02); // - - 设置4位格式,2行,5x7
  134.         LCD_delay_50us(10);
  135.         LCD_write_4bit_command(0x28); // - - 设置4位格式,2行,5x7
  136.         LCD_delay_10us(10);
  137.         LCD_write_4bit_command(0x28); // - - 设置4位格式,2行,5x7
  138.         LCD_delay_10us(5);
  139.         LCD_write_4bit_command(0x0c); // - - 整体显示,关光标,不闪烁
  140.         LCD_delay_10us(5);
  141.         LCD_write_4bit_command(0x06); // - - 设定输入方式,增量不移位
  142.         LCD_delay_10us(5);
  143.         LCD_write_4bit_command(0x01); // - - 清除屏幕显示
  144.         LCD_delay_50us(10);
  145. }
  146. //********************************

  147. // - - 向LCD1602写指令
  148. void LCD_write_4bit_command(uchar dat)
  149. {
  150.         LCD_delay_10us(10);
  151.         LCD_RS=0; // - - 指令

  152.         LCD_write_H4bit_command(dat);
  153.         LCD_write_L4bit_command(dat);
  154. }

  155. // - - 向LCD1602写高四位指令
  156. void LCD_write_H4bit_command(uchar dat)
  157. {
  158.         LCD_delay_10us(100);
  159.         LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
  160.         LCD_delay_10us(10);
  161.         LCD_E=1; // - - 允许
  162.         LCD_delay_10us(100);
  163.         LCD_E=0;
  164. }

  165. // - - 向LCD1602写低四位指令
  166. void LCD_write_L4bit_command(uchar dat)
  167. {
  168.         dat<<=4;
  169.         LCD_delay_10us(10);
  170.         LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
  171.         LCD_delay_10us(10);
  172.         LCD_E=1; // - - 允许
  173.         LCD_delay_10us(100);
  174.         LCD_E=0;
  175. }

  176. // - - 向LCD1602写数据
  177. void LCD_write_4bit_data(uchar dat)
  178. {
  179.         LCD_delay_10us(10);
  180.         LCD_RS=1; // - - 数据

  181.         LCD_write_H4bit_command(dat);
  182.         LCD_write_L4bit_command(dat);
  183. }

  184. // - - 设置显示位置
  185. void LCD_set_xy(uchar x,uchar y)
  186. {
  187.         uchar address;
  188.         if(y==1)
  189.         {
  190.                 address=0x80+x; // - - 第一行位置
  191.         }
  192.         else
  193.         {
  194.                 address=0xc0+x; // - - 第二行位置
  195.         }
  196.         LCD_delay_10us(10);
  197.         LCD_write_4bit_command(address);
  198. }

  199. // - - 显示一个字符函数
  200. void LCD_disp_char(uchar x,uchar y,uchar dat)
  201. // - - LCD_disp_char(0,1,0x38); // - - 显示8
  202. {
  203.         LCD_set_xy(x,y);
  204.         LCD_delay_10us(15);
  205.         LCD_write_4bit_data(dat);
  206. }


  207. // - - 显示一个字符串函数
  208. void LCD_disp_string(uchar x,uchar y,uchar *s)
  209. {
  210.         LCD_set_xy(x,y);
  211.         LCD_delay_10us(5);
  212.         while(*s!='\0')
  213.         {
  214.                 LCD_write_4bit_data(*s);
  215.                 s++;
  216.         }
  217. }
  218. void main()//主函数模块//   
  219. {      
  220.         
  221. //        lcd_init();//初始化LCD
  222.         LCD_init();
  223.         delay_ms(1500);
  224.         while(1)     
  225.         {     
  226.                 delay_ms(1500);  
  227.                 read_dht11();
  228.                 m=integer(RHH);   
  229.                 n=integer(CH);
  230.         
  231.                 LCD_disp_string(0,0,"R:");
  232.                 LCD_disp_char(3,0,m/10+0x30) ;
  233.                 LCD_disp_char(4,0,m%10+0x30) ;
  234.                 LCD_disp_char(6,0,'%') ;

  235.                 LCD_disp_string(0,1,"T:");
  236.                 LCD_disp_char(3,1,n/10+0x30) ;
  237.                 LCD_disp_char(4,1,n%10+0x30) ;
  238.                
  239.                 LCD_disp_char(6,1,0xdf) ;
  240.                 LCD_disp_char(7,1,0x43) ;         
  241.         }  
  242. }




复制代码


评分

参与人数 7黑币 +138 收起 理由
long123~~ + 5 很给力!
tbjgdtcn + 3 很给力!
twomore9 + 4 共享资料的黑币奖励!
ssfc + 5
davg1 + 11 共享资料的黑币奖励!
lyMarvin + 10 谢谢分享!值得学习
admin + 100 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:96682 发表于 2016-10-22 16:42 | 只看该作者
LZ,为何不用温度标识符 “℃” 而用 “°” ?
回复

使用道具 举报

板凳
ID:79544 发表于 2016-10-23 10:06 | 只看该作者
wc86110 发表于 2016-10-22 16:42
LZ,为何不用温度标识符 “℃” 而用 “°” ?

地址不够啦。我是加在智能小车上的,前面显示超声波距离
回复

使用道具 举报

地板
ID:152571 发表于 2016-12-6 13:59 | 只看该作者
你好请问这个程序是在什么上面编译的呢
回复

使用道具 举报

5#
ID:152574 发表于 2016-12-6 14:19 | 只看该作者
赞一个
回复

使用道具 举报

6#
ID:150203 发表于 2016-12-6 22:42 | 只看该作者
赞一个
回复

使用道具 举报

7#
ID:146910 发表于 2016-12-11 17:24 | 只看该作者
能在上面加时间和日期吗
回复

使用道具 举报

8#
ID:79544 发表于 2016-12-24 12:50 | 只看该作者
存存不调皮 发表于 2016-12-6 13:59
你好请问这个程序是在什么上面编译的呢

keil-uv2
回复

使用道具 举报

9#
ID:79544 发表于 2016-12-24 12:51 | 只看该作者
wangjianb01978 发表于 2016-12-11 17:24
能在上面加时间和日期吗

加上时间的函数就可以的
回复

使用道具 举报

10#
ID:157262 发表于 2016-12-25 16:04 | 只看该作者
谢谢楼主的贡献
回复

使用道具 举报

11#
ID:153777 发表于 2016-12-27 17:10 | 只看该作者
谢谢楼主的贡献
回复

使用道具 举报

12#
ID:200368 发表于 2017-5-13 17:35 | 只看该作者
楼主能分享一下电路图吗我邮箱2292718639@qq.com
回复

使用道具 举报

13#
ID:200368 发表于 2017-5-13 17:36 | 只看该作者
温湿度不是4个脚麻还有一个clk接哪
回复

使用道具 举报

14#
ID:205572 发表于 2017-5-28 10:40 | 只看该作者
楼主,可以把你做这个智能车的全部资料发我一份么?谢谢楼主了!我的邮箱577848721@qq.com
回复

使用道具 举报

15#
ID:264754 发表于 2017-12-21 13:02 | 只看该作者
请问1602接线,怎么接的,谢谢
回复

使用道具 举报

16#
ID:280132 发表于 2018-4-29 08:41 | 只看该作者
为什么没有附件啊?找不到。

回复

使用道具 举报

17#
ID:381976 发表于 2018-8-2 09:24 | 只看该作者
请问,这个代码是完整的吗?
回复

使用道具 举报

18#
ID:400557 发表于 2018-11-15 19:05 | 只看该作者
这个是4线接线法吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表