找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3755|回复: 1
收起左侧

单片机数字温度计proteus仿真设计 带源码

[复制链接]
ID:137190 发表于 2016-10-8 23:27 | 显示全部楼层 |阅读模式
0.png 0.png
仿真工程文件及所有完整程序等资料下载地址:
http://www.51hei.com/bbs/dpj-56304-1.html


  1. #include "reg51.h"

  2. char disp[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
  3. char disp_dot[11]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xc0};

  4. sbit DQ = P1^3;   //定义通信端口

  5. //晶振12MHz  
  6. void delay_18B20(unsigned int i)
  7. {
  8.         while(i--);
  9. }

  10. //初始化函数
  11. Init_DS18B20(void)
  12. {
  13.          unsigned char x=0;
  14.          DQ = 1;          //DQ复位
  15.          delay_18B20(8);  //稍做延时
  16.          DQ = 0;          //单片机将DQ拉低
  17.          delay_18B20(80); //精确延时 大于 480us
  18.          DQ = 1;          //拉高总线
  19.          delay_18B20(14);
  20.          x=DQ;            //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
  21.          delay_18B20(20);
  22. }


  23. //读一个字节  
  24. ReadOneChar(void)
  25. {
  26.         unsigned char i=0;
  27.         unsigned char dat = 0;
  28.         for (i=8;i>0;i--)
  29.          {
  30.                   DQ = 0; // 给脉冲信号
  31.                   dat>>=1;
  32.                   DQ = 1; // 给脉冲信号
  33.                   if(DQ)
  34.                   dat|=0x80;
  35.                   delay_18B20(4);
  36.          }
  37.         return(dat);
  38. }

  39. //写一个字节  
  40. WriteOneChar(unsigned char dat)
  41. {
  42. unsigned char i=0;
  43. for (i=8; i>0; i--)
  44. {
  45.   DQ = 0;
  46.   DQ = dat&0x01;
  47.   delay_18B20(5);
  48.   DQ = 1;
  49.   dat>>=1;
  50. }
  51. }

  52. //读取温度
  53. ReadTemperature(void)
  54. {
  55.         unsigned char a=0;
  56.         unsigned char b=0;
  57.         unsigned int  t=0;
  58.        
  59.         Init_DS18B20();
  60.         WriteOneChar(0xCC); // 跳过读序号列号的操作
  61.         WriteOneChar(0x44); // 启动温度转换
  62.         delay_18B20(100);
  63.         Init_DS18B20();
  64.         WriteOneChar(0xCC); //跳过读序号列号的操作
  65.         WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
  66.         a=ReadOneChar();
  67.         b=ReadOneChar();
  68.                
  69.         //传感器返回值除16得实际温度值
  70.         //为了得到2位小数位,先乘100,再除16,考虑整型数据长度,
  71.         //技巧处理后先乘25,再除4,除4用右移实现

  72.         t = (b*256+a)*25;       
  73.         return( t >> 2  );
  74. }

  75. main()
  76. {
  77.         unsigned int tmp;
  78.         unsigned char counter;
  79.         while(1)
  80.         {
  81.                 //温度测量频率没有必要太高,太高反而影响数码显示
  82.                 //所以用计数器加以控制
  83.                 if(counter-- == 0)
  84.                 {
  85.                         tmp = ReadTemperature();                        
  86.                         counter = 20;
  87.                 }

  88.                 P2 = 0xff;
  89.                 P0 = disp[tmp%10];
  90.                 P2 = 0xfb;
  91.                 delay_18B20(1000);
  92.                 P2 = 0xff;
  93.                 P0 = disp[tmp/10%10];
  94.                 P2 = 0xf7;
  95.                 delay_18B20(1000);
  96.                 P2 = 0xff;
  97.                 P0 = disp_dot[tmp/100%10];
  98.                 P2 = 0xef;
  99.                 delay_18B20(1000);
  100.                 P2 = 0xff;
  101.                 P0 = disp[tmp/1000%10];
  102.                 P2 = 0xdf;
  103.                 delay_18B20(1000);
  104.         }
  105. }
复制代码


回复

使用道具 举报

ID:786004 发表于 2020-6-21 18:53 | 显示全部楼层
想问一下楼主,要是湿度计的话,该怎么改呢
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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