找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7027|回复: 16
收起左侧

51单片机18b20 dht11温湿度程序代码

  [复制链接]
ID:102721 发表于 2016-1-11 11:29 | 显示全部楼层 |阅读模式
18b20 dht11的51单片机驱动程序
00000.png

dht11单片机源程序:
  1. #include"DHT11.h"
  2. #include"delay.h"

  3. #define uchar unsigned char
  4. #define uint unsigned uint
  5. uchar onedata;
  6. uchar rh,rl,th,tl;

  7. sbit JS=P1^4;

  8. void start(void)//起始信号
  9. {
  10.         JS=1;
  11.         delay_us(8);
  12.         JS=0;
  13.         delay_ms(20);//主机总线拉低大于18毫秒
  14.         JS=1;               
  15.         delay_us(25);//发送起始信号结束后,拉高电平25US
  16. }

  17. uchar Receive_Byte()//接收一个字节
  18. {
  19.         uchar i,temp;
  20.         for(i=0;i<8;i++) //接收八位数据
  21.         {
  22.                 while(!JS);        //等待50us的低电平开始信号结束
  23.                 delay_us(25);//开始信号结束之后延时25us
  24.                 temp=0;                //时间在26us—28us 接收数据为‘0’
  25.                 if(JS==1)
  26.                 temp=1;                //时间在26us—28us之后还为高电平 接收数据为‘1’
  27.                 while(JS);        //等待数据信号高电平26us—28us 接收数据为‘0’,70us为‘1’
  28.                 onedata<<=1;
  29.                 onedata|=temp;
  30.         }
  31.         return onedata;
  32. }

  33. void receive(void)//接收数据
  34. {
  35.         uchar t_h,t_l,r_h,r_l;
  36.         uchar check,num_check;
  37.         start();  //开始信号
  38.         JS=1;          //主机何为输入,判断从机 DUL11 相应信号
  39.         if(!JS)        //判断从机是否有低电平响应信号
  40.         {
  41.                 while(!JS);//判断从机发出80us的低电平响应信号是否结束
  42.                 while(JS); //判断从机80us的高电平是否结束 结束则主机进入接收状态

  43.                 r_h=Receive_Byte();
  44.                 r_l=Receive_Byte();
  45.                 t_h=Receive_Byte();
  46.                 t_l=Receive_Byte();

  47.                 check=Receive_Byte(); //校验位
  48.                 JS=0;
  49.                 delay_us(50);//当最后一位数据接收完毕时从机拉低50us的低电平
  50.                 JS=1;                //主机有上啦电阻拉高进入空闲状态
  51.                 num_check=r_h+r_l+t_h+t_l;
  52.                 if(num_check==check)
  53.                 {
  54.                         rh=r_h;
  55.                         rl=r_l;
  56.                         th=t_h;
  57.                         tl=t_l;
  58.                         check=num_check;
  59.                 }       
  60.         }
  61. }

  62. void Get_Air_Humidity()        //获取空气湿度
  63. {
  64.         start();  //DHT11起始信号
  65.         receive();//获取DHT11温湿度
  66.         //SendData_Uart1(rh);//串口输出湿度值               
  67. }

复制代码

ds18b20单片机源程序:
  1. #include"DS18B20.h"

  2. #define uchar unsigned char
  3. #define uint  unsigned int

  4. //18b20测温
  5. sbit DQ=P3^6;
  6. sbit boom=P3^5;

  7. uchar fuhao; //判断温度的正负
  8. uchar temp_int=0;//定义温度的整数部分
  9. uint  temp_point=0;//定义温度的小数部分
  10. uint  temp,tempflag=261;


  11. //char a,b,c,d,e,f,g,h;

  12. //void delay(uchar i)
  13. //{
  14. //        while(i--);
  15. //}

  16. /*初始化18b20*/
  17. void Init_18b20(void)
  18. {
  19.         P3M1=0X00;
  20.         P3M0=0X40;
  21.         DQ=1; //复位18b20
  22.         delay_us(16);
  23.         DQ=0; //拉低DQ
  24.         delay_us(160);
  25.         DQ=1; //拉高DQ
  26.         delay_us(100);
  27. }

  28. /*读一个字节*/
  29. ReadOneChar(void)
  30. {
  31.         uchar i=0;
  32.         uchar dat=0;
  33.         P3M1=0X00;
  34.         P3M0=0X00;
  35.         for(i=8;i>0;i--)
  36.         {
  37.                 DQ=0;                 //给脉冲信号
  38.                 dat>>=1;
  39.                 delay_us(1);
  40.                 DQ=1;                 //给脉冲信号
  41.                 if(DQ)
  42.                 {
  43.                         dat|=0x80;
  44.                 }
  45.                 delay_us(20);
  46.         }
  47.         return(dat);
  48. }

  49. /*写一个字节*/
  50. void WriteOneChar(uchar dat)
  51. {
  52.         uchar i=0;
  53.         P3M1=0X00;
  54.         P3M0=0X40;
  55.         for(i=8;i>0;i--)
  56.         {
  57.                 DQ=0;
  58.                 DQ=dat&0x01;
  59.                 delay_us(20);
  60.                 DQ=1;
  61.                 dat>>=1;
  62.         }
  63.         delay_us(8);
  64. }


  65. /*写RAm中的三四字节的内容*/
  66. void Write_Calm(void)
  67. {
  68.         Init_18b20();
  69.         WriteOneChar(0xCC);//跳过读序列号操作
  70.         WriteOneChar(0x4E);//写温度上下限及配置寄存器
  71.         WriteOneChar(50);        //写温度上限
  72.         WriteOneChar(0);        //写温度下限
  73.         WriteOneChar(0x7f);//默认十二位精度
  74. }

  75. /*读取温度*/
  76. void ReadTemperature(void)
  77. {
  78.         uchar low=0;
  79.         uchar high=0;
  80.         uint  t=0;
  81.         Init_18b20();
  82.         WriteOneChar(0xCC);  //跳过读序列号操作
  83.         WriteOneChar(0x44);  //启动温度转换

  84.         LCD_write_Num(36,1,temp_int);

  85.         Init_18b20();
  86.         WriteOneChar(0xCC);  //跳过读序列号操作
  87.         WriteOneChar(0xBE);  //读取温度寄存器等(共9个可读寄存器)前两个就是温度

  88.         low=ReadOneChar();          //读取温度最低值
  89.         high=ReadOneChar();  //读取温度最高值
  90.         fuhao=high&0xf0;          //取得温度的符号,fuhao==0温度为正或0,否则为负
  91.         if(fuhao!=0)
  92.         {
  93.                 t=65536-(high<<8|low)+1;
  94.                 high=t/256;
  95.                 low=t%256;
  96.         }
  97.         temp_point=(low&0x0f)*625;
  98.         low=low>>4;                  //低位右移4位,舍弃小数部分
  99.         high=high<<4;                  //高位左移4为,舍弃符号位
  100.         temp_int=high|low;       
  101.    
  102.         temp=temp_int%100*10+temp_point/1000;
  103. }

复制代码

完整代码(包含头文件)下载: 温湿度.rar (2.64 KB, 下载次数: 138)

评分

参与人数 3黑币 +55 收起 理由
66飛宇 + 4 很给力!
genuinehell + 1 很给力!
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:92231 发表于 2016-1-11 11:37 来自手机 | 显示全部楼层
下载来学习一下楼主编程技巧
回复

使用道具 举报

ID:102721 发表于 2016-1-13 12:28 | 显示全部楼层
可以的,其实我很菜的。
回复

使用道具 举报

ID:136144 发表于 2016-8-7 05:51 | 显示全部楼层
喜欢。收下了。
回复

使用道具 举报

ID:149790 发表于 2016-12-13 14:03 | 显示全部楼层
感谢分享,十分有用
回复

使用道具 举报

ID:144022 发表于 2017-2-1 11:07 | 显示全部楼层
顶楼主,赞一个
回复

使用道具 举报

ID:43186 发表于 2017-2-18 11:40 | 显示全部楼层
下载来学习一下楼主编程技巧,顶一下
回复

使用道具 举报

ID:47933 发表于 2017-4-3 15:18 | 显示全部楼层
这个很有用
回复

使用道具 举报

ID:190743 发表于 2017-4-18 11:11 | 显示全部楼层
51黑有你更精彩
回复

使用道具 举报

ID:191259 发表于 2017-4-21 17:25 | 显示全部楼层
都需要黑币   好无奈
回复

使用道具 举报

ID:196248 发表于 2017-7-27 16:29 | 显示全部楼层
delay.h 文件怎么没有
回复

使用道具 举报

ID:323396 发表于 2018-5-6 13:14 | 显示全部楼层
学习学习
回复

使用道具 举报

ID:358458 发表于 2018-6-26 02:19 | 显示全部楼层
编译不过
回复

使用道具 举报

ID:418842 发表于 2018-10-31 23:13 | 显示全部楼层
很给力哈
回复

使用道具 举报

ID:433198 发表于 2018-11-26 09:17 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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