找回密码
 立即注册

QQ登录

只需一步,快速开始

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

大佬们,我写了个程序不知道为什么小数部分不根据18b20改变其他位数都没问题,大佬...

[复制链接]
跳转到指定楼层
楼主
  1. #include<reg52.h>                                                
  2. #include<math.h>                                       
  3. #define uint unsigned int                                
  4. #define uchar unsigned char                                
  5. sbit lcden=P2^0;                                       
  6. sbit lcdrw=P2^1;
  7. sbit lcdrs=P2^2;                                       
  8. sbit DQ = P2^3;                        
  9. sbit s1=P1^0;                                                
  10. sbit s2=P1^1;                                                        
  11. sbit s3=P1^2;                                                        
  12. sbit D1=P3^4;            
  13. sbit D2=P3^5;            
  14. uchar num,fig,s1num,figh,figl,t,temp_value;
  15. char h,l;                                                        
  16. void delay(uint z);                                       
  17. void init();                                                
  18. void write_com(uchar com);                        
  19. void write_data(uchar date);                        
  20. void keyscan();                                                  
  21. void write_hl(uchar add,char date);           
  22. void write_temp_value(uchar add,char date);        
  23. void delay_18B20(unsigned int i);        
  24. void Init_DS18B20(void);                        
  25. unsigned char ReadOneChar(void);        
  26. void WriteOneChar(uchar dat);               
  27. void ReadTemp(void);                                
  28. void comp();
  29. void string(uchar ad,uchar *s);
  30. void main()
  31. {
  32.         init();                                                        
  33.         Init_DS18B20();                                       
  34.         while(1)                                                
  35.         { if(s1==0||s1num>=1)
  36.                 {        keyscan();                                
  37.                 }
  38.         else{        ReadTemp();                                
  39.                         write_temp_value(0,temp_value);
  40.                 }
  41.                 comp();               
  42.         }
  43. }

  44. void delay(uint z)                                       
  45. {
  46.         uint x,y;
  47.         for(x=z;x>0;x--)
  48.         for(y=110;y>0;y--);
  49. }
  50. void string(uchar ad,uchar *s)
  51. {
  52.    write_com(ad);
  53.    while(*s>0)
  54.    {
  55.              write_data(*s++);
  56.           delay(100);
  57.    }
  58. }


  59. void init()                                                //LCD初始化程序
  60. {   lcden=0;
  61.           lcdrw=0;
  62.         write_com(0x38);                         //设置16x2 显示5x7 点阵,8 位数据接口
  63.         write_com(0x0c);                 //设置开始显示不显示光标
  64.         write_com(0x06);                         //写一个字符后地址指针加1
  65.         write_com(0x01);                         //显示清零数据指针清零
  66.         write_com(0x80);                         //将数据指针第一行第一个字处,
  67.         
  68.         write_com(0x89);                         //定义当前指针位置
  69.         string(0x89,"H:+28.6");                                 
  70.         write_com(0x80+9+0x40);                  //定义当前指针位置
  71.         string(0x80+9+0x40,"L:+15.3");                                
  72.         h=28.6;                  //h=+20
  73.         l=15.3;                                                     //l=+10
  74.         D1=1;
  75.         D2=1;
  76.         figh=0;                                                         //给正确初始值,便于上电比较
  77.         figl=1;                                                         //给正确初始值,便于上电比较
  78. }
  79. void write_com(uchar com)                //写命令程序
  80. {        lcdrs=0;                                         
  81.         P0=com;                                         
  82.         delay(5);                                         
  83.         lcden=1;                                         
  84.         delay(5);                                       
  85.         lcden=0;                                 
  86. }
  87. void write_data(uchar date)                //写数据程序
  88. {        lcdrs=1;                                         //设置为写入数据
  89.         P0=date;                                         //将数据赋给P0 口
  90.         delay(5);                                         //延时
  91.         lcden=1;                                         //打开LCD使能,输入一个高脉冲
  92.         delay(5);                                         //延时
  93.         lcden=0;                                         //置低完成高脉冲
  94. }

  95. void write_temp_value(uchar add,char date)         //读取的温度值在LCD上显示程序
  96. {        uchar bai,shi,ge,min;
  97.         bai=date*10/1000;
  98.         shi=date*10/100%10;                                 
  99.         ge=date*10/10%10;
  100.         min=date*10%10;                                                 //分离出百,十,个位数
  101.         if((bai|shi|ge)==0)                                 //如果百十都等于0
  102.         shi=0x70;                                                 //十赋值0x70,以便十位写入空字符,write_data(0x30+shi),参考1602字符图形码
  103.         if(bai==0)
  104.         bai=0x70;
  105.         write_com(0x80+add);                     //设置当前光标位置,准备写数据
  106.         
  107.         if(fig==0&&shi!=0x70)                         //如果温度为负且十位不等于0(如果十位等于0,shi=0x70)
  108.         { write_data('-');                                 //在百位写入负号
  109.           write_data(0x40+shi);                        //写入十位数据
  110.         }
  111.         if(fig==0&&shi==0x70)                         //如果温度为负且百十位都等于0
  112.         { write_data(0x30+0x70);                 //在百位写入一个空字符
  113.           write_data('-');                                 //在十位写入负号
  114.         }
  115.         if(fig==1)write_data(0x30+bai);  //温度正,写入百位数据
  116.         if(fig==1)write_data(0x30+shi);         //温度正,写入十位数据
  117.         if(fig==1)write_data(0x30+ge);         //写入个位数据
  118.         write_data('.');
  119.         write_data(0x30+min);                                  //写小数点        
  120.         write_data(0xdf);                                 //写入°
  121.         write_data('C');                                 //写入C
  122.         
  123. }
  124. void write_hl(uchar add,char date)         //设置的高低温度值在LCD上显示程序
  125. {        uchar bai,shi,ge,min;
  126.         bai=date/1000;
  127.         shi=date/100%10;                                 
  128.         ge=date/10%10;
  129.         min=date%10;                                                 //分离出百,十,个位数
  130.         write_com(0x80+add);                     //设置当前光标位置,准备写数据
  131.         
  132.         if(t==1&&shi!=0)                             //如果温度为负且十位不等于0(如果十位等于0,shi=0x70)            负数:百位第1种情况,写-
  133.                 { write_data('-');}                         //在百位写入负号
  134.         if(t==1&&shi==0)                             //如果温度为负且百十位都等于0                                                    负数:百位第2种情况,写空
  135.                 { write_data(0x30+0x70);         //在百位写入一个空字符
  136.                     write_data('-');                         //在十位写入负号                                                                            负数:十位第1种情况,写-
  137.                 }
  138.         if(t==1&&shi!=0)                             //如果温度为负且十不等于0                                                                负数:十位第2种情况,写数                                                
  139.             {write_data(0x30+shi);}                 //在十位写入数据
  140.         if(t==0&&bai==0&&shi!=0)                 //否则温度为正,如果bai=0且shi!=0                                                  正数:百位第1种情况,写+
  141.                 {write_data('+');}                         //在百位写入+
  142.         if(t==0&&bai==0&&shi==0)                 //否则温度为正,如果bai=0且shi=0                                                           正数:百位第2种情况,写空
  143.                 {write_data(0x30+0x70);}         //在百位写入一个空字符
  144.         if(t==0&&bai!=0)        
  145.                 {write_data(0x30+bai);}      //否则百位为正不等于0,写入百位数                                                           正数:百位第3种情况,写数
  146.         if(t==0&&bai==0&&shi==0&&ge!=0)         //如果百十等于0,个不等于0                                                                           正数:十位第1种情况,写+
  147.                 {write_data('+');}                         //在十位写入+
  148.         if(t==0&&bai==0&&shi==0&&ge==0)         //如果百十个都等于0
  149.             {write_data(' ');}                     //在十位写入空字符                                                                                           正数:十位第2种情况,写空
  150.         if(t==0&&shi!=0)        
  151.                 {write_data(0x30+shi);}        
  152.                 write_data(0x30+min);     //在十位写入数                                                                                                   正数:十位第3种情况,写数                    
  153.         write_data(0x30+ge);                         //写入个位数据
  154. }

  155. /***********ds18b20子程序*************************/

  156. /***********ds18b20延迟子函数(晶振12MHz )*******/

  157. void delay_18B20(unsigned int i)
  158. {
  159.         while(i--);
  160. }



  161. void Init_DS18B20(void)                          //ds18b20初始化函数
  162. {
  163.          unsigned char x=0;
  164.          DQ = 1;         
  165.          delay_18B20(8);  
  166.          DQ = 0;         
  167.          delay_18B20(80);
  168.          DQ = 1;         
  169.          delay_18B20(14);
  170.          x=DQ;            
  171.          delay_18B20(20);
  172. }



  173. unsigned char ReadOneChar(void)                  // ds18b20读一个字节
  174. {
  175.         uchar i=0;
  176.         uchar dat = 0;
  177.         for (i=8;i>0;i--)
  178.          {
  179.                   DQ = 0; // 给脉冲信号
  180.                   dat>>=1;
  181.                   DQ = 1; // 给脉冲信号
  182.                   if(DQ)
  183.                   dat|=0x80;
  184.                   delay_18B20(4);
  185.          }
  186.          return(dat);
  187. }



  188. void WriteOneChar(uchar dat)                          //ds18b20写一个字节  
  189. {                                                                          
  190.          unsigned char i=0;
  191.          for (i=8; i>0; i--)
  192.          {
  193.                   DQ = 0;
  194.                  DQ = dat&0x01;
  195.             delay_18B20(5);
  196.                  DQ = 1;
  197.             dat>>=1;
  198. }
  199. }



  200. void ReadTemp(void)                                                         //读取ds18b20当前温度
  201. {
  202.         unsigned char a=0;
  203.         unsigned char b=0;
  204.         unsigned char t=0;

  205.         Init_DS18B20();
  206.         WriteOneChar(0xCC);            
  207.         WriteOneChar(0x44);

  208.         delay_18B20(100);      

  209.         Init_DS18B20();
  210.         WriteOneChar(0xCC);         
  211.         WriteOneChar(0xBE);

  212.         delay_18B20(100);

  213.         a=ReadOneChar();   
  214.         b=ReadOneChar();           
  215. //        a=0xff;                                
  216. //        b=0xfe;
  217.         temp_value=b<<4;                  
  218.         temp_value+=(a&0xf0)>>4;
  219.         fig=0x01;                                 
  220.         if(temp_value>0x7f)                 
  221.         {fig=0;                                       
  222.          a=~a+1;                                 
  223.          b=~b;
  224.          temp_value=b<<4;                 
  225.          temp_value+=(a&0xf0)>>4;
  226.          temp_value*=0.625;
  227.         }            
  228. }

  229. void keyscan()                                                 //按键扫描
  230. {        if(s1==0)                                                 //如果s1按下,执行下面程序
  231.         {   delay(5);                                         //        延时一会
  232.                 if(s1==0)                                         //如果s1确实按下,执行下面程序
  233.                 {        s1num++;                                 //s1num自加1
  234.                         while(!s1);                                 //如果s1松开,执行下面程序,否则一直停在这
  235.                         if(s1num==1)                         //如果s1只按一次,执行下面
  236.                         {
  237.                                 write_com(0x80+13);       //设置当前指针位置
  238.                                 write_com(0x0f);                  //写入光标闪烁命令
  239.                         }
  240.                         if(s1num==2)                                  //如果s1按2次,执行下面
  241.                         {
  242.                                 write_com(0x80+0x40+13);  //设置当前指针位置,光标闪
  243.                         }
  244.                         if(s1num==3)                                  //如果s1按3次,执行下面
  245.                         {        s1num=0;                                  //s1num清0
  246.                                 write_com(0x0c);              //写入光标不闪烁命令
  247.                         }
  248.                 }
  249.         }
  250.         
  251.         if(s2==0)                                                          //如果s2按下,执行下面程序
  252.         {        delay(5);                                                  //延时
  253.                 if(s2==0);                                                  //如果s2按下,执行下面程序
  254.                 {               
  255.                                 while(!s2);                                  //如果s2松开,执行下面程序,否则一直停在这
  256.                                 if(s1num==1)                          //如果光标在h的位置闪烁,执行下面程序
  257.                                 {        h++;                                  //h加1
  258.                                         if(h==127)                            //=127清0
  259.                                         h=0;                                 
  260.                                         figh=0;                                  //设置figh=0,符号为正
  261.                                         t=figh;                                  //赋值给t,以便液晶显示
  262.                                         if(h<=-1)                          //如果h为负数
  263.                                         {figh=1;                          //设置figh=0
  264.                                          t=figh;                          //赋值给t,以便液晶显示
  265.                                          h=abs(h);                          //h取绝对值
  266.                                          write_hl(11,h);          //h写入LCD
  267.                                          h=0-h;                                  //把h变回原来的负数
  268.                                         }
  269.                                         else{write_hl(11,h);} //否则h写入LCD
  270.                                         write_com(0x80+13);          //光标在温度值位置闪烁
  271.                                 }
  272.                                 if(s1num==2)                          //如果光标在l的位置闪烁,执行下面程序
  273.                                 {        l++;                                 
  274.                                         if(l==127)                           
  275.                                         l=0;
  276.                                         figl=0;
  277.                                         t=figl;
  278.                                         if(l<=-1)
  279.                                         {figl=1;
  280.                                          t=figl;
  281.                                          l=abs(l);
  282.                                          write_hl(0x40+11,l);         
  283.                                          l=0-l;
  284.                                         }
  285.                                         else{write_hl(0x40+11,l);}         
  286.                                         write_com(0x80+0x40+13);        
  287.                                 }
  288.                 }
  289.         }
  290.         if(s3==0)                                                          //如果s3按下,执行下面程序
  291.         {        delay(5);                                                  //延时
  292.                 if(s3==0);
  293.                 {
  294.                                 while(!s3);                                  //s3松开后,执行下面程序
  295.                                 if(s1num==1)                          //如果光标在h的位置闪烁,执行下面程序
  296.                                 {        h--;                                  //h自减1
  297.                                         if(h==-55)                          //如果h=-55清0
  298.                                         h=0;
  299.                                         figh=0;                                  //设置figh=0,温度值为正
  300.                                         t=figh;                                  //赋值给t,以便液晶显示
  301.                                         if(h<=-1)                          //如果h温度值为负数
  302.                                         {figh=1;                          //设置figh=1
  303.                                          t=figh;                          //赋值给t,以便液晶显示
  304.                                          h=abs(h);                          //h取绝对值
  305.                                          write_hl(11,h);          //h写入LCD
  306.                                          h=0-h;                                  //把h变回原来的负数
  307.                                         }
  308.                                         else{write_hl(11,h);} //否则h写入LCD         
  309.                                         write_com(0x80+13);          //光标在温度值位置闪烁
  310.                                 }                 
  311.                                 if(s1num==2)
  312.                                 {        l--;
  313.                                         if(l==-55)
  314.                                         h=0;
  315.                                         figl=0;
  316.                                         t=figl;
  317.                                         if(l<=-1)
  318.                                         {figl=1;
  319.                                          t=figl;
  320.                                          l=abs(l);
  321.                                          write_hl(0x40+11,l);         
  322.                                          l=0-l;
  323.                                         }
  324.                                         else{write_hl(0x40+11,l);}         
  325.                                         write_com(0x80+0x40+13);
  326.                                 }
  327.                 }
  328.         }               
  329. }        
  330. void comp()                                                                //温度值报警程序
  331. {if(fig==1&&temp_value>=h)                                
  332. D1=0;
  333. if(fig==1&&temp_value<=h)                        
  334. D1=1;
  335.          if(fig==0)                                                        //如果读取温度值为负
  336.          {  if(figh==0)                                                //如果设置最高值温度值为正
  337.                  D1=1;  
  338.                  if(figh==1)                                                //如果设置最高值温度值为负
  339.                   {h=abs(h);
  340.                    if(temp_value<=h)
  341.                    D1=0;
  342.                    if(temp_value>=h)
  343.                    D1=1;
  344.                    h=-h;         
  345.                   }
  346.          }
  347. if(fig==1&&temp_value<=l)
  348. D2=0;
  349. if(fig==1&&temp_value>=l)
  350. D2=1;            
  351. if(fig==0)
  352.                 {         if(figl==0)
  353.                          D2=0;
  354.                          if(figl==1)
  355.                          {l=abs(l);
  356.                           if(temp_value>=l)
  357.                           D2=0;
  358.                           if(temp_value<=l)
  359.                           D2=1;
  360.                           l=-l;
  361.                          }
  362.                 }               
  363. }                     
复制代码

G3%`Q)A{U3V1P49J1}ZVMW8.png (20.93 KB, 下载次数: 22)

G3%`Q)A{U3V1P49J1}ZVMW8.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:657149 发表于 2019-12-11 00:09 | 只看该作者
小数位始终是0不跟随18b20的改变而改变
回复

使用道具 举报

板凳
ID:396245 发表于 2019-12-11 11:16 | 只看该作者
  • void write_temp_value(uchar add,char date)//读取的温度值在LCD上显示程序
  • {uchar bai,shi,ge,min;
  •         bai =日期* 10/1000;
  •         shi =日期* 10/100%10;
  •         ge = date * 10/10%10;
  •         min = 2; //分离出百,十,个位数            这里,你把一个常量赋值给了min,当然不会变咯
  •         if((bai | shi | ge)== 0)//如果百十都等于0

回复

使用道具 举报

地板
ID:470332 发表于 2019-12-11 11:29 | 只看该作者
你的temp_value是uchar类型,你说temp_value*=0.625是多少
回复

使用道具 举报

5#
ID:213173 发表于 2019-12-11 17:53 | 只看该作者
sunxuu 发表于 2019-12-11 00:09
小数位始终是0不跟随18b20的改变而改变

temp_value的数据类型定义和使用不恰当
uchar num,fig,s1num,figh,figl,t,temp_value;
void write_temp_value(uchar add,char date);
write_temp_value(0,temp_value);
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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