⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18b20_1602.lst

📁 基于c51单片机的18b20实时温度检测
💻 LST
📖 第 1 页 / 共 2 页
字号:
 226   1       bit b = 0;
 227   1      
 228   1       DO = 0; //产生读时隙  
 229   1       i++;    //维持低电平至少1us
 230   1      
 231   1       DO = 1; //1us以上后拉高
 232   1       Delayus(2); //延时8us,DO下降沿15内ds18b20输出的数据才有效
 233   1      
 234   1      
 235   1       b = DO; //读取数据
 236   1       Delayus(40);    //每个读时隙至少持续60us
 237   1      
 238   1       return(b);
 239   1      } 
 240          
C51 COMPILER V8.12   18B20_1602                                                            07/30/2008 10:40:15 PAGE 5   

 241          /*读取一个字节*/
 242          uchar Readbyte()
 243          {
 244   1       uchar byte_read = 0;
 245   1       uchar i, j;
 246   1      
 247   1       for(i=0; i<8; i++)
 248   1        {
 249   2         j = Readbit();
 250   2         byte_read = (j<<i) | byte_read; //低位读起
 251   2        }
 252   1      
 253   1       return(byte_read);
 254   1      } 
 255          
 256          /*写一个字节*/
 257          void Writebyte(uchar byte_to_write)
 258          {
 259   1       uchar i = 0;
 260   1       uchar j = 0;
 261   1       bit write_bit = 0;
 262   1      
 263   1       for(j=0; j<8; j++)
 264   1        {
 265   2         write_bit =  (byte_to_write & 0x01);
 266   2          if(write_bit == 1) //写1
 267   2               {
 268   3                DO = 0;  //产生写时隙
 269   3                Delayus(3);      //延时15us
 270   3                
 271   3                DO = 1;  //写1
 272   3                Delayus(40);   //延时,写时隙不得低于60us
 273   3               }
 274   2              else
 275   2               {
 276   3                DO = 0;  //产生写时隙
 277   3                Delayus(50);   //延时,保持低约60us~120us 
 278   3                DO = 1;  
 279   3                i++;
 280   3               }
 281   2         byte_to_write = byte_to_write >> 1;
 282   2        }
 283   1      }
 284          
 285          
 286          /*启动温度转换*/
 287          void StartConvert()
 288          {
 289   1       Resetpaulse();   // 发出复位脉冲,每次操作都从复位开始
 290   1       Delay(1);
 291   1       EA = 0;
 292   1       Writebyte(0xcc); //skip room命令
 293   1       Writebyte(0x44); //启动温度转换命令
 294   1       EA = 1;
 295   1      }
 296          
 297          /*读取温度值*/
 298          void ReadTempreture()
 299          {
 300   1       EA = 0;
 301   1       Resetpaulse();   // 发出复位脉冲,每次操作都从复位开始
 302   1       Delay(1);
C51 COMPILER V8.12   18B20_1602                                                            07/30/2008 10:40:15 PAGE 6   

 303   1       Writebyte(0xcc); //skip room命令
 304   1       Writebyte(0xbe); //读取暂存器命令
 305   1       temp_l = Readbyte(); //存储温度低字节值 (整数部分低四位和小数部分)
 306   1       temp_h = Readbyte(); //存储温度高字节值 (其中高五位为符号位)
 307   1       EA = 1;
 308   1      }
 309          
 310          /*数据处理程序*/
 311          void Digital_process()
 312          {
 313   1       uchar total = 0;
 314   1       uchar low = 0;
 315   1       uint  dicimal = 0;
 316   1      
 317   1       tempsign = (temp_h >> 7) & 0x01; //得出符号位
 318   1       total = ((temp_h << 4)&0xf0) | ((temp_l >> 4)&0x0f); //取整数位
 319   1       low =  temp_l & 0x0f; //取小数位
 320   1      
 321   1       if(tempsign == 0)
 322   1        {
 323   2         temp_integer[0] = total / 100 + '0'; //计算百、十、个位                
 324   2         temp_integer[1] = (total%100)/10 + '0';
 325   2         temp_integer[2] = (total%100)%10 + '0'; 
 326   2         temp_integer[3] = '\0';
 327   2          if(temp_integer[0] == '0')
 328   2               {
 329   3                 if(temp_integer[1] != '0')
 330   3                      {
 331   4                   temp_integer[0] = '\0'; //百位零消隐
 332   4                   
 333   4                   }
 334   3                 else if(temp_integer[1] == '0')
 335   3                  {
 336   4                       temp_integer[0] = '\0';  //百位,十位零都消隐
 337   4                       temp_integer[1] = '\0';
 338   4                       }
 339   3               }
 340   2         dicimal = low * 625;          //计算小数
 341   2         temp_dicimal[0] = dicimal / 1000 + '0';      //十分位
 342   2         temp_dicimal[1] = dicimal % 1000 /100 + '0';  //百分位
 343   2         temp_dicimal[2] = dicimal % 100 / 10 + '0';   //千分位
 344   2         temp_dicimal[3] = dicimal % 10 + '0';                //万分位
 345   2         temp_dicimal[4] = '\0';        //数组加一个空字符(好像系统也会自动加上的?)
 346   2        }
 347   1      
 348   1        else if(tempsign == 1)          //负数处理
 349   1        {
 350   2         if(low == 0x00)              //负数要取反加一再乘以0.0625就是实际温度值了,我这里没有设那么多int型变量,
 351   2           {
 352   3                total = ~total + 1;  //所以就用了这么一个计算方法
 353   3                low &= 0x0f;
 354   3                }                                /*具体一点讲,小树低四位为全零时取反加一要有进位,此时只要整数位取反加一即可,
 355   2                               小数位不用理会,其余情况整数位取反,小数位取反加一*/
 356   2         else
 357   2           {
 358   3                total = ~total ;
 359   3                low = (~low) + 1;
 360   3                low &= 0x0f;    //注意高四位要变成零
 361   3                }               
 362   2         temp_integer[1] = (total%100)/10 + '0'; //计算十、个位
 363   2         temp_integer[2] = (total%100)%10 + '0'; 
 364   2         temp_integer[3] = '\0';
C51 COMPILER V8.12   18B20_1602                                                            07/30/2008 10:40:15 PAGE 7   

 365   2      
 366   2      
 367   2         if(temp_integer[1] == '0')
 368   2          {
 369   3               temp_integer[1] = '\0';
 370   3          }
 371   2         dicimal = low * 625;
 372   2         temp_dicimal[0] = dicimal / 1000 + '0';
 373   2         temp_dicimal[1] = dicimal % 1000 /100 + '0';
 374   2         temp_dicimal[2] = dicimal % 100 / 10 + '0';
 375   2         temp_dicimal[3] = dicimal % 10 + '0';
 376   2         temp_dicimal[4] = '\0';
 377   2        }
 378   1       
 379   1      
 380   1      }
 381          
 382          
 383          
 384          void main()
 385          {
 386   1       bit palse = 0;
 387   1       Initial_LCD();
 388   1      
 389   1       GotoXY(0,0);
 390   1       Print("CHECKING...",12);
 391   1       Delay(3000);
 392   1       
 393   1      
 394   1       palse = Resetpaulse();   //检测DS18B20是否响应
 395   1       if(palse)
 396   1        {
 397   2         Initial_LCD();
 398   2         GotoXY(0,0);
 399   2         Print("DS18B20 OK",11);
 400   2         }
 401   1       else
 402   1         {
 403   2         Initial_LCD();
 404   2         GotoXY(0,0);
 405   2         Print("DS18B20 ERROR",13);
 406   2         while(1);
 407   2      
 408   2         } 
 409   1       
 410   1       do{
 411   2         Delay(1);
 412   2         StartConvert();
 413   2         Delay(1020);
 414   2         ReadTempreture();
 415   2         Digital_process();
 416   2      
 417   2         if(tempsign == 0)     //显示正值温度
 418   2         {
 419   3           GotoXY(0,1);
 420   3               Print("TEMP:",5);
 421   3               GotoXY(5,1);
 422   3           Print(temp_integer,3);
 423   3           GotoXY(8,1);
 424   3           Print(".",1);
 425   3           GotoXY(9,1);
 426   3           Print(temp_dicimal,4);
C51 COMPILER V8.12   18B20_1602                                                            07/30/2008 10:40:15 PAGE 8   

 427   3              }
 428   2         else                              //显示负值温度
 429   2          {
 430   3               GotoXY(0,1);
 431   3               Print("TEMP:",5);
 432   3               GotoXY(5,1);
 433   3               Print("-",1);
 434   3               GotoXY(6,1);
 435   3           Print(temp_integer + 1,2);
 436   3           GotoXY(8,1);
 437   3           Print(".",1);
 438   3           GotoXY(9,1);
 439   3           Print(temp_dicimal,4);
 440   3              }
 441   2         }
 442   1       while(1); 
 443   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    991    ----
   CONSTANT SIZE    =     47    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     12       3
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       5
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -