C51 COMPILER V8.08   DS18B20                                                               12/03/2014 20:29:53 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN ds18b20.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ds18b20.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include<reg52.h>
   2          #include<intrins.h>
   3          #define uchar unsigned char
   4          #define uint unsigned int
   5          sbit ds   = P2^2;
   6          sbit dula = P2^6;
   7          sbit wela = P2^7;
   8          sbit beep = P2^3;
   9          bit flag;
  10          uint temp;
  11          uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
  12          0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};  
  13          
  14          uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
  15          0x87,0xff,0xef}; 
  16          
  17          void TempDelay(uchar us)
  18          {
  19   1              while(us--); //6.51us
  20   1      }
  21          void delay(int z)
  22          {
  23   1              uint x,y;
  24   1              for(x=z;x>0;x--)
  25   1                      for(y=115;y>0;y--);
  26   1      }
  27          void display(uint temp)
  28          {
  29   1              uchar bai,shi,ge;
  30   1              bai=temp/100;
  31   1              shi=temp%100/10;
  32   1              ge=temp%100%10;
  33   1      
  34   1              dula=1;
  35   1              P0=table[bai];
  36   1              dula=0;
  37   1              P0=0xff;
  38   1              wela=1;
  39   1              P0=0xfe;
  40   1              wela=0;
  41   1              delay(3);
  42   1      
  43   1              dula=1;
  44   1              P0=table1[shi];
  45   1              dula=0;
  46   1              P0=0xff;
  47   1              wela=1;
  48   1              P0=0xfd;
  49   1              wela=0;
  50   1              delay(3);
  51   1      
  52   1              dula=1;
  53   1              P0=table[ge];
  54   1              dula=0;
  55   1              P0=0xff;
C51 COMPILER V8.08   DS18B20                                                               12/03/2014 20:29:53 PAGE 2   

  56   1              wela=1;
  57   1              P0=0xfb;
  58   1              wela=0;
  59   1              delay(3);
  60   1      }
  61          void ds_reset()
  62          {
  63   1              ds=1;
  64   1              _nop_();
  65   1              ds=0;
  66   1              TempDelay(80);//520us
  67   1              ds=1;
  68   1              TempDelay(5);
  69   1              if(ds==0)
  70   1                      flag=1;
  71   1              else
  72   1                      flag=0;
  73   1              TempDelay(20);
  74   1              ds=1;
  75   1      }
  76          bit ds_read_bit()
  77          {
  78   1              bit dat;
  79   1              ds=0;
  80   1              _nop_();
  81   1              _nop_();
  82   1              ds=1;
  83   1              _nop_();
  84   1              dat=ds;
  85   1              TempDelay(12);
  86   1              return dat;
  87   1      }
  88          uchar ds_read_byte()
  89          {
  90   1              uchar i,j,value;
  91   1              for(i=0;i<8;i++)
  92   1              {
  93   2                      j=ds_read_bit();
  94   2                      value=(j<<7)|(value>>1);
  95   2              }
  96   1              return value;
  97   1      }
  98          void ds_write_byte(uchar dat)
  99          {
 100   1              uchar i,onebit;
 101   1              for(i=0;i<8;i++)
 102   1              {
 103   2                      onebit=dat&0x01;
 104   2                      if(onebit)         //写1
 105   2                      {
 106   3                              ds=0;
 107   3                              TempDelay(1);
 108   3                              ds=1;
 109   3                              _nop_();
 110   3                      }
 111   2                      else  //写0
 112   2                      {
 113   3                              ds=0;
 114   3                              TempDelay(11);
 115   3                              ds=1;
 116   3                              _nop_();
 117   3                      }
C51 COMPILER V8.08   DS18B20                                                               12/03/2014 20:29:53 PAGE 3   

 118   2                      dat>>=1;
 119   2              }
 120   1      }
 121          void ds_change()
 122          {
 123   1              ds_reset();
 124   1              ds_write_byte(0xcc);
 125   1              ds_write_byte(0x44);
 126   1      }
 127          
 128          uint get_temperature()
 129          {
 130   1              uchar a,b;
 131   1              float temperature;
 132   1              ds_reset();
 133   1              ds_write_byte(0xcc);
 134   1              ds_write_byte(0xbe);
 135   1              a=ds_read_byte();
 136   1              b=ds_read_byte();
 137   1              temp=b;
 138   1              temp<<=8;
 139   1              temp|=a;
 140   1              temperature=temp*0.0625;
 141   1              temp=temperature*10+0.5;
 142   1              return temp;
 143   1      } 
 144          void main()
 145          {
 146   1              ds_change();
 147   1              delay(1000);
 148   1              while(1)
 149   1              {               
 150   2                      display(get_temperature());
 151   2                      ds_change();
 152   2                      if(get_temperature()>350)//35度蜂鸣器发声
 153   2                              beep=0;
 154   2                      else
 155   2                              beep=1;
 156   2              }
 157   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    449    ----
   CONSTANT SIZE    =     26    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      2       6
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      1       1
END OF MODULE INFORMATION.


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