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

📄 ds18b20.lst

📁 18B20的单片机实现程序
💻 LST
字号:
C51 COMPILER V6.12  DS18B20                                                                05/07/2008 23:22:36 PAGE 1   


C51 COMPILER V6.12, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN .\DS18B20.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE .\DS18B20.C DEBUG OBJECTEXTEND

stmt level    source

   1          //安装目录下的EXE文件打开后可在电脑上显示当前温度值
   2          #include <reg52.h>
   3          #define uchar unsigned char
   4          #define uint unsigned int
   5          sbit DS=P2^2;           //define interface
   6          uint temp;             // variable of temperature
   7          uchar flag1;            // sign of the result positive or negative
   8          sbit dula=P2^6;
   9          sbit wela=P2^7;
  10          /*unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
  11                                  0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
  12          unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
  13                                  0x87,0xff,0xef};*/
  14          unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
  15                                  0xf8,0x80,0x90,0x88,0x83,0xc6,0xa2,0x86,0x8e};
  16          unsigned char code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,
  17                                  0x78,0x00,0x10};
  18          
  19          
  20          void delay(uint count)      //delay
  21          {
  22   1        uint i;
  23   1        while(count)
  24   1        {
  25   2          i=200;
  26   2          while(i>0)
  27   2          i--;
  28   2          count--;
  29   2        }
  30   1      }
  31          ///////功能:串口初始化,波特率9600,方式1///////
  32          void Init_Com(void)
  33          {
  34   1           TMOD = 0x20;
  35   1           PCON = 0x00;
  36   1           SCON = 0x50;
  37   1           TH1 = 0xFd;
  38   1           TL1 = 0xFd;
  39   1           TR1 = 1;
  40   1      }
  41          
  42          void dsreset(void)       //send reset and initialization command
  43          {
  44   1        uint i;
  45   1        DS=0;
  46   1        i=103;
  47   1        while(i>0)i--;
  48   1        DS=1;
  49   1        i=4;
  50   1        while(i>0)i--;
  51   1      }
  52          
  53          bit tmpreadbit(void)       //read a bit
  54          {
  55   1         uint i;
C51 COMPILER V6.12  DS18B20                                                                05/07/2008 23:22:36 PAGE 2   

  56   1         bit dat;
  57   1         DS=0;i++;          //i++ for delay
  58   1         DS=1;i++;i++;
  59   1         dat=DS;
  60   1         i=8;while(i>0)i--;
  61   1         return (dat);
  62   1      }
  63          
  64          uchar tmpread(void)   //read a byte date
  65          {
  66   1        uchar i,j,dat;
  67   1        dat=0;
  68   1        for(i=1;i<=8;i++)
  69   1        {
  70   2          j=tmpreadbit();
  71   2          dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好一个字节在DAT里
  72   2        }
  73   1        return(dat);
  74   1      }
  75          
  76          void tmpwritebyte(uchar dat)   //write a byte to ds18b20
  77          {
  78   1        uint i;
  79   1        uchar j;
  80   1        bit testb;
  81   1        for(j=1;j<=8;j++)
  82   1        {
  83   2          testb=dat&0x01;
  84   2          dat=dat>>1;
  85   2          if(testb)     //write 1
  86   2          {
  87   3            DS=0;
  88   3            i++;i++;
  89   3            DS=1;
  90   3            i=8;while(i>0)i--;
  91   3          }
  92   2          else
  93   2          {
  94   3            DS=0;       //write 0
  95   3            i=8;while(i>0)i--;
  96   3            DS=1;
  97   3            i++;i++;
  98   3          }
  99   2      
 100   2        }
 101   1      }
 102          
 103          void tmpchange(void)  //DS18B20 begin change
 104          {
 105   1        dsreset();
 106   1        delay(1);
 107   1        tmpwritebyte(0xcc);  // address all drivers on bus
 108   1        tmpwritebyte(0x44);  //  initiates a single temperature conversion
 109   1      }
 110          
 111          uint tmp()               //get the temperature
 112          {
 113   1        float tt;
 114   1        uchar a,b;
 115   1        dsreset();
 116   1        delay(1);
 117   1        tmpwritebyte(0xcc);
C51 COMPILER V6.12  DS18B20                                                                05/07/2008 23:22:36 PAGE 3   

 118   1        tmpwritebyte(0xbe);
 119   1        a=tmpread();
 120   1        b=tmpread();
 121   1        temp=b;
 122   1        temp<<=8;             //two byte  compose a int variable
 123   1        temp=temp|a;
 124   1        tt=temp*0.0625;
 125   1        temp=tt*10+0.5;
 126   1        return temp;
 127   1      }
 128          
 129          void readrom()          //read the serial
 130          {
 131   1        uchar sn1,sn2;
 132   1        dsreset();
 133   1        delay(1);
 134   1        tmpwritebyte(0x33);
 135   1        sn1=tmpread();
 136   1        sn2=tmpread();
 137   1      }
 138          
 139          
 140          void delay10ms()            //delay
 141            {
 142   1          uchar a,b;
 143   1          for(a=10;a>0;a--)
 144   1            for(b=60;b>0;b--);
 145   1         }
 146          
 147          void display(uint temp)			//显示程序
 148          {
 149   1         uchar A1,A2,A2t,A3,ser;
 150   1         ser=temp/10;
 151   1         SBUF=ser;
 152   1         A1=temp/100;
 153   1         A2t=temp%100;
 154   1         A2=A2t/10;
 155   1         A3=A2t%10;
 156   1         dula=0;
 157   1         P0=table[A1];		//显示百位
 158   1         dula=1;
 159   1         dula=0;
 160   1      
 161   1         wela=0;
 162   1         //P0=0x7e;
 163   1      	P0=0x81;
 164   1         wela=1;
 165   1         wela=0;
 166   1         delay(1);
 167   1      
 168   1         dula=0;
 169   1         P0=table1[A2];		//显示十位
 170   1         dula=1;
 171   1         dula=0;
 172   1      
 173   1         wela=0;
 174   1         //P0=0x7d;
 175   1      	P0=0x82;
 176   1         wela=1;
 177   1         wela=0;
 178   1         delay(1);
 179   1      
C51 COMPILER V6.12  DS18B20                                                                05/07/2008 23:22:36 PAGE 4   

 180   1         P0=table[A3];		//显示个位
 181   1         dula=1;
 182   1         dula=0;
 183   1      
 184   1         //P0=0x7b;
 185   1      	P0=0x84;
 186   1         wela=1;
 187   1         wela=0;
 188   1         delay(1);
 189   1      }
 190          
 191          
 192          void main()
 193          {
 194   1       uchar a;
 195   1       Init_Com();
 196   1        do
 197   1        {
 198   2          tmpchange();
 199   2         // delay(200);
 200   2      for(a=10;a>0;a--)
 201   2        {   display(tmp());
 202   3        }
 203   2        }                while(1);
 204   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    515    ----
   CONSTANT SIZE    =     26    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      3       8
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       2
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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