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

📄 lesson8.lst

📁 DS18B20测量温度 1602做为液晶显示温度 c语言编写 有注释
💻 LST
字号:
C51 COMPILER V8.02   LESSON8                                                               05/07/2008 23:43:57 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE LESSON8
OBJECT MODULE PLACED IN lesson8.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE lesson8.c DEBUG OBJECTEXTEND

line level    source

   1          #include<reg52.h>
   2          #define uchar unsigned char
   3          #define uint unsigned int
   4          #define T_cont 0.0625;
   5          uchar code welcome[]={"High Sensor Temp"};
   6          uchar code welcome1[]={"Design by:ljh"};
   7          uchar code temp[]={"temp: "};
   8          uchar code ds18b20[]={"DS18B20 OK"};
   9          
  10          bit T_sign;
  11          sbit DS=P3^3;
  12          float temp1=0;
  13          uchar t_bai,t_shi,t_ge,t_feng,t_miao;
  14          
  15          sbit lcden=P2^2;
  16          sbit lcdrs=P2^0;
  17          sbit lcdrw=P2^1;
  18          
  19          
  20          
  21          
  22            ///////////////////////////////////////////////////////////////////////
  23           
  24           void delay(uint z)
  25          {
  26   1              uint x,y;
  27   1              for(x=z;x>0;x--)
  28   1                      for(y=110;y>0;y--);
  29   1      }
  30           
  31           
  32           void dsreset(void)       //send reset and initialization command
  33          
  34          {
  35   1      
  36   1        uint i;                //DS18B20初始化
  37   1      
  38   1        DS=0;
  39   1      
  40   1        i=103;
  41   1      
  42   1        while(i>0)i--;
  43   1      
  44   1        DS=1;
  45   1      
  46   1        i=4;
  47   1      
  48   1        while(i>0)i--;
  49   1      
  50   1      }
  51          
  52           
  53          
  54          bit tmpreadbit(void)       //read a bit 读一位
  55          
C51 COMPILER V8.02   LESSON8                                                               05/07/2008 23:43:57 PAGE 2   

  56          {
  57   1      
  58   1         uint i;
  59   1      
  60   1         bit dat;
  61   1      
  62   1         DS=0;i++;          //i++ for delay  小延时一下
  63   1      
  64   1         DS=1;i++;i++;
  65   1      
  66   1         dat=DS;
  67   1      
  68   1         i=8;
  69   1         while(i>0)i--;
  70   1      
  71   1         return (dat);
  72   1      
  73   1      }
  74          
  75           
  76          
  77          uchar tmpread(void)   //read a byte date 读一个字节
  78          
  79          {
  80   1      
  81   1        uchar i,j,dat;
  82   1      
  83   1        dat=0;
  84   1      
  85   1        for(i=1;i<=8;i++)
  86   1      
  87   1        {
  88   2      
  89   2          j=tmpreadbit();
  90   2      
  91   2          dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好//一个字节在DAT里
  92   2      
  93   2        }
  94   1      
  95   1        return(dat);             //将一个字节数据返回
  96   1      
  97   1      }
  98          
  99           
 100          
 101          void tmpwritebyte(uchar dat)   //write a byte to ds18b20
 102          
 103          {                           //写一个字节到DS18B20里
 104   1      
 105   1        uint i;
 106   1      
 107   1        uchar j;
 108   1      
 109   1        bit testb;
 110   1      
 111   1        for(j=1;j<=8;j++)
 112   1      
 113   1        {
 114   2      
 115   2          testb=dat&0x01;
 116   2      
 117   2          dat=dat>>1;
C51 COMPILER V8.02   LESSON8                                                               05/07/2008 23:43:57 PAGE 3   

 118   2      
 119   2          if(testb)     //write 1    写1部分
 120   2      
 121   2          {
 122   3      
 123   3            DS=0;
 124   3      
 125   3            i++;i++;
 126   3      
 127   3            DS=1;
 128   3      
 129   3            i=8;while(i>0)i--;
 130   3      
 131   3          }
 132   2      
 133   2          else
 134   2      
 135   2          {
 136   3      
 137   3            DS=0;       //write 0   写0部分
 138   3      
 139   3            i=8;while(i>0)i--;
 140   3      
 141   3            DS=1;
 142   3      
 143   3            i++;i++;
 144   3      
 145   3          }
 146   2      
 147   2        }
 148   1      
 149   1      }
 150          
 151           
 152          
 153          
 154          
 155          
 156          
 157          void readtemperature()
 158          {
 159   1              uint y;
 160   1              uchar T_L=0;
 161   1              uchar T_H=0;
 162   1              uchar k;
 163   1              dsreset();
 164   1              delay(1);
 165   1              tmpwritebyte(0xcc);
 166   1              tmpwritebyte(0x44);
 167   1              dsreset();
 168   1              delay(1);
 169   1              tmpwritebyte(0xcc);
 170   1              tmpwritebyte(0xbe);
 171   1              T_L=tmpread();
 172   1              T_H=tmpread();
 173   1              k=T_H&0x08;
 174   1              if(k==0x08)
 175   1                      T_sign=1;
 176   1              else
 177   1                      T_sign=0;
 178   1      
 179   1              T_H=T_H&0x07;
C51 COMPILER V8.02   LESSON8                                                               05/07/2008 23:43:57 PAGE 4   

 180   1              temp1=(T_H*256+T_L)*T_cont;
 181   1              temp1=temp1*100;
 182   1              t_bai=(uint)temp1/10000;
 183   1              y=(uint)temp1%10000;
 184   1              t_shi=y/1000;
 185   1              y=(uint)y%1000;
 186   1              t_ge=y/100;
 187   1              y=(uint)y%100;
 188   1              t_feng=y/10;
 189   1              t_miao=(uint)y%10;
 190   1      
 191   1      
 192   1      }       
 193           
 194          
 195          ///////////////////////////////////////////////////////////
 196          
 197          
 198          void write_com(uchar com)
 199          {
 200   1              
 201   1              lcdrs=0;
 202   1              P0=com;
 203   1              delay(5); //延时
 204   1              lcden=1;   //下三行表示E高脉冲到来就开始转换
 205   1              delay(5);
 206   1              lcden=0;
 207   1      }
 208          
 209          void write_data(uchar date)
 210          {
 211   1              lcdrs=1;
 212   1              P0=date;
 213   1              delay(5);
 214   1              lcden=1;
 215   1              delay(5);
 216   1              lcden=0;
 217   1      }
 218          void init()
 219          {
 220   1              lcdrw=0;        
 221   1              lcden=0;
 222   1              write_com(0x38);
 223   1              write_com(0x0e);
 224   1              write_com(0x06);
 225   1              write_com(0x01);
 226   1              
 227   1      }
 228          void print(uchar a,uchar *str)
 229          {
 230   1              write_com(a);
 231   1              while(*str!='\0') {write_data(*str++);}
 232   1              *str=0;
 233   1                              
 234   1      }
 235          
 236          void print1(uchar a,uchar t)
 237          {
 238   1              write_com(a);
 239   1              write_data(t);
 240   1      }
 241          
C51 COMPILER V8.02   LESSON8                                                               05/07/2008 23:43:57 PAGE 5   

 242          
 243          
 244          
 245          
 246          
 247          
 248           void main()
 249          {
 250   1      
 251   1              
 252   1              init();
 253   1              
 254   1              
 255   1              print(0x80,welcome);
 256   1              print(0xc0,welcome1);
 257   1              
 258   1              delay(1000);
 259   1       
 260   1              init();
 261   1              while(1)
 262   1              {
 263   2                      readtemperature();
 264   2                      print(0x80,ds18b20);
 265   2                      print(0xc0,temp);
 266   2                      if(T_sign==1)
 267   2                              print1(0xc7,0x2d);
 268   2                      else
 269   2                              print1(0xc7,0x2b);
 270   2      
 271   2                      if(t_bai!=0)
 272   2                              print1(0xc8,t_bai+0x30);
 273   2                      else
 274   2                              print1(0xc8,0x20);
 275   2                      print1(0xc9,t_shi+0x30);
 276   2                      print1(0xca,t_ge+0x30);
 277   2                      print1(0xcb,0x2e);
 278   2                      print1(0xcc,t_feng+0x30);
 279   2                      print1(0xcd,t_miao+0x30);
 280   2                      print1(0xce,0x43);
 281   2      
 282   2              } 
 283   1      
 284   1              
 285   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    674    ----
   CONSTANT SIZE    =     49    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      9       3
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      1       2
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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