找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机两路测温 Keil4工程+Proteus仿真

[复制链接]
ID:210035 发表于 2017-6-11 11:13 | 显示全部楼层 |阅读模式
单片机两路测温 Keil4工程+Proteus仿真,有需要的自行下载

Proteus仿真图:
0.png 0.png

单片机源程序如下:
  1. #include<reg52.h>
  2. #define uchar unsigned char                        
  3. #define uint unsigned int
  4. uchar i;
  5. char csh=0;
  6. sbit lcdrs=P2^0;
  7. sbit lcdrw=P2^1;
  8. sbit lcden=P2^2;
  9. uchar code t0[]="the temperature ";
  10. uchar code t1[]=" is             ";
  11. uchar code wendu[]="0123456789";        //利用一个温度表解决温度显示乱码
  12. sbit DQ_1= P3^0;//定义ds18B20总线IO
  13. sbit DQ_2= P3^1;//定义ds18B20总线IO
  14. void delay(uint z);
  15. void write_com(uchar com);
  16. void write_date(unsigned char date);
  17. void init_lcd();
  18. void tmpDelay(int num);//延时函数
  19. void Init_DS18B20();//初始化ds1820
  20. unsigned char ReadOneChar_1();//读一个字节
  21. void WriteOneChar_1(unsigned char dat);//写一个字节
  22. unsigned int Readtemp_1();//读取温度
  23. unsigned char ReadOneChar_2();//读一个字节
  24. void WriteOneChar_2(unsigned char dat);//写一个字节
  25. unsigned int Readtemp_2();//读取温度
  26. void display();
  27. void delay(uint z)
  28. {
  29.         uint x,y;
  30.         for(x=100;x>1;x--)
  31.                 for(y=z;y>1;y--);
  32. }

  33. void write_com(uchar com)
  34. {
  35.         lcdrs=0;
  36.         P0=com;       
  37.         delay(5);
  38.         lcden=1;
  39.         delay(5);
  40.         lcden=0;
  41. }
  42. void write_date(unsigned char date)
  43. {
  44.         lcdrs=1;
  45.         P0=date;
  46.         delay(5);
  47.         lcden=1;
  48.         delay(5);
  49.         lcden=0;
  50. }
  51. void init_lcd()
  52. {
  53.         lcden=0;
  54.         lcdrw=0;
  55.         write_com(0x38);
  56.         write_com(0x01);
  57.         write_com(0x0c);
  58.         write_com(0x06);
  59.         write_com(0x80);       
  60.         for(i=0;i<16;i++)
  61.                 {
  62.                         write_date(t0[i]);
  63.                         delay(0);
  64.                 }
  65.         write_com(0x80+0x40);
  66.         for(i=0;i<16;i++)
  67.                 {
  68.                         write_date(t1[i]);
  69.                         delay(0);
  70.                 }
  71. }

  72. //温度采集模块
  73. void tmpDelay(int num)//延时函数
  74. {
  75.         while(num--) ;
  76. }       
  77. /******************************************************************************/
  78. void Init_DS18B20()//初始化ds1820
  79. {
  80.         unsigned char x1=0;
  81.         unsigned char x2=0;
  82.         DQ_1 = 1;    //DQ复位
  83.         tmpDelay(8);  //稍做延时
  84.         DQ_1 = 0;    //单片机将DQ拉低
  85.         tmpDelay(80); //精确延时 大于 480us
  86.         DQ_1 = 1;    //拉高总线
  87.         tmpDelay(14);
  88.         x1=DQ_1;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
  89.         tmpDelay(20);

  90.         ///////////////////////////////////////////
  91.        
  92.         DQ_2 = 1;    //DQ复位
  93.         tmpDelay(8);  //稍做延时
  94.         DQ_2 = 0;    //单片机将DQ拉低
  95.         tmpDelay(80); //精确延时 大于 480us
  96.         DQ_2 = 1;    //拉高总线
  97.         tmpDelay(14);
  98.         x2=DQ_2;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
  99.         tmpDelay(20);
  100. }
  101. unsigned char ReadOneChar_1()//读一个字节
  102. {
  103.         unsigned char i=0;
  104.         unsigned char dat_1 = 0;
  105.         for (i=8;i>0;i--)
  106.         {
  107.                 DQ_1 = 0; // 给脉冲信号
  108.                 dat_1>>=1;
  109.                 DQ_1 = 1; // 给脉冲信号
  110.                 if(DQ_1)
  111.                 dat_1|=0x80;
  112.                 tmpDelay(4);
  113.         }
  114.         return(dat_1);
  115. }
  116. unsigned char ReadOneChar_2()//读一个字节
  117. {
  118.         unsigned char i=0;
  119.         unsigned char dat_2 = 0;
  120.         for (i=8;i>0;i--)
  121.         {
  122.                 DQ_2 = 0; // 给脉冲信号
  123.                 dat_2>>=1;
  124.                 DQ_2 = 1; // 给脉冲信号
  125.                 if(DQ_2)
  126.                 dat_2|=0x80;
  127.                 tmpDelay(4);
  128.         }
  129.         return(dat_2);
  130. }



  131. void WriteOneChar_1(unsigned char dat_1)//写一个字节
  132. {
  133.         unsigned char i=0;
  134.         for (i=8; i>0; i--)
  135.         {
  136.                 DQ_1= 0;
  137.                 DQ_1= dat_1&0x01;
  138.                 tmpDelay(5);
  139.                 DQ_1= 1;
  140.                 dat_1>>=1;
  141.         }
  142. }

  143. void WriteOneChar_2(unsigned char dat_2)//写一个字节
  144. {
  145.         unsigned char i=0;
  146.         for (i=8; i>0; i--)
  147.         {
  148.                 DQ_2= 0;
  149.                 DQ_2= dat_2&0x01;
  150.                 tmpDelay(5);
  151.                 DQ_2= 1;
  152.                 dat_2>>=1;
  153.         }
  154. }

  155. unsigned int Readtemp_1()//读取温度
  156. {
  157.         unsigned char a=0;
  158.         unsigned char b=0;
  159.         unsigned int t=0;
  160.         float tt=0;       
  161.         Init_DS18B20();
  162.         WriteOneChar_1(0xCC); // 跳过读序号列号的操作
  163.         WriteOneChar_1(0x44); // 启动温度转换
  164.         Init_DS18B20();
  165.         WriteOneChar_1(0xCC); //跳过读序号列号的操作
  166.         WriteOneChar_1(0xBE); //读取温度寄存器
  167.         a=ReadOneChar_1();   //连续读两个字节数据   //读低8位
  168.         b=ReadOneChar_1();                          //读高8位
  169.         t=b;
  170.         t<<=8;
  171.         t=t|a; //两字节合成一个整型变量。
  172.         tt=t*0.0625;  //得到真实十进制温度值,因为DS18B20可以精确到0.0625度,所以读回数据的最低位代表的是0.0625度
  173.         t= tt*10+0.5; //放大十倍,这样做的目的将小数点后第一位也转换为可显示数字,同时进行一个四舍五入操作。
  174.         return(t);
  175. }

  176. unsigned int Readtemp_2()//读取温度
  177. {
  178.         unsigned char a=0;
  179.         unsigned char b=0;
  180.         unsigned int t=0;
  181.         float tt=0;       
  182.         Init_DS18B20();
  183.         WriteOneChar_2(0xCC); // 跳过读序号列号的操作
  184.         WriteOneChar_2(0x44); // 启动温度转换
  185.         Init_DS18B20();
  186.         WriteOneChar_2(0xCC); //跳过读序号列号的操作
  187.         WriteOneChar_2(0xBE); //读取温度寄存器
  188.         a=ReadOneChar_2();   //连续读两个字节数据   //读低8位
  189.         b=ReadOneChar_2();                          //读高8位
  190.         t=b;
  191.         t<<=8;
  192.         t=t|a; //两字节合成一个整型变量。
  193.         tt=t*0.0625;  //得到真实十进制温度值,因为DS18B20可以精确到0.0625度,所以读回数据的最低位代表的是0.0625度
  194.         t= tt*10+0.5; //放大十倍,这样做的目的将小数点后第一位也转换为可显示数字,同时进行一个四舍五入操作。
  195.         return(t);
  196. }

  197. void display()
  198. {
  199.         unsigned int num;                   //定义的时候用uchar宏定义就会出错
  200.         unsigned int shi,ge,xiaoshu;            //这里的num,shi,ge,xiaoshu 必须用unsigned int无符号整数来表示,用unshigned char  字符型则显示错误
  201.         num=Readtemp_1();
  202.         if(csh<=8){num=0;csh++;}
  203.         shi=num/100;
  204.         ge=num/10%10;
  205.         xiaoshu=num%10;

  206.     write_com(0x80+0x40+5);
  207.         write_date(wendu[shi]);

  208.         write_com(0x80+0x40+6);
  209.         write_date(wendu[ge]);

  210.         write_com(0x80+0x40+7);
  211.         write_date(0x2e);

  212.         write_com(0x80+0x40+8);
  213.         write_date(wendu[xiaoshu]);

  214. /////////////////////////////////////////////////////

  215.         num=Readtemp_2();
  216.         if(csh<=8){num=0;csh++;}
  217.         shi=num/100;
  218.         ge=num/10%10;
  219.         xiaoshu=num%10;

  220.     write_com(0x80+0x40+11);
  221.         write_date(wendu[shi]);

  222.         write_com(0x80+0x40+12);
  223.         write_date(wendu[ge]);

  224.         write_com(0x80+0x40+13);
  225.         write_date(0x2e);

  226.         write_com(0x80+0x40+14);
  227.         write_date(wendu[xiaoshu]);
  228. }
复制代码

所有资料51hei提供下载:
单片机,两路测温.rar (71.52 KB, 下载次数: 32)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:711517 发表于 2020-3-19 15:48 | 显示全部楼层
有器件名单吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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