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

📄 ds18b20.lst

📁 51单片机用c语言实例 包括ad
💻 LST
字号:
C51 COMPILER V7.09   DS18B20                                                               05/23/2006 11:28:32 PAGE 1   


C51 COMPILER V7.09, 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          /************************************************************
   2          *文件名称: DS18B20.C
   3          *功能描述: 18B20驱动程序,DQ为数据口,上拉4.7k电阻,接于P3.4
   4          *          11.0592M晶振,
   5          *************************************************************/
   6          #include <reg51.h>
   7          #include <intrins.h>
   8          #include <Absacc.h>
   9          
  10          typedef unsigned char uchar;
  11          typedef unsigned int  uint;
  12          
  13          #define C8255_A         XBYTE[0x7f00]
  14          #define C8255_B         XBYTE[0x7f01]
  15          #define C8255_CON       XBYTE[0x7f03]
  16          
  17          sbit dq = P3^4;
  18          bit  flag;
  19          uint Temperature;
  20          uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节
  21          uchar id_buff[8];       //read rom ID为8字节
  22          uchar *p;
  23          
  24          unsigned char code Led[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  25          unsigned char data Dispbuff[] = {0x00,0x00,0x00};
  26          
  27          /************************************************************
  28          *Function:延时处理
  29          *************************************************************/
  30          void TempDelay (uint us)
  31          {
  32   1              while(us--);
  33   1      }
  34          
  35          /************************************************************
  36           *Function:显示程序
  37           ************************************************************/
  38          void Display()
  39          {
  40   1              uchar i, j=0xfe;
  41   1              Dispbuff[0] = Temperature/100;                  // 百位
  42   1              Dispbuff[1] = (Temperature%100)/10;             // 十位
  43   1              Dispbuff[2] = (Temperature%100)%10;             // 个位
  44   1      
  45   1              for(i=0; i<3; i++)
  46   1              {               
  47   2                      C8255_B = 0x00;
  48   2                      C8255_A = j;
  49   2                      C8255_B = Led[Dispbuff[i]];
  50   2                      TempDelay(60);
  51   2                      j = (j<<1)|(j>>7);
  52   2              }
  53   1              C8255_B = 0x00;
  54   1      }
  55          
C51 COMPILER V7.09   DS18B20                                                               05/23/2006 11:28:32 PAGE 2   

  56          /************************************************************
  57          *Function:18B20初始化
  58          *************************************************************/
  59          void Init18b20 (void)
  60          {
  61   1              dq=1;
  62   1              _nop_();
  63   1              dq=0;
  64   1              TempDelay(86);          //delay 530 uS
  65   1              _nop_();
  66   1              dq=1;
  67   1              TempDelay(14);          //delay 100 uS
  68   1              _nop_();
  69   1              _nop_();
  70   1              _nop_();
  71   1              
  72   1              if(dq==0)
  73   1                      flag = 1;               //detect 1820 success!
  74   1              else
  75   1                      flag = 0;               //detect 1820 fail!
  76   1              TempDelay(20);          //20
  77   1              _nop_();
  78   1              _nop_();
  79   1              dq = 1;
  80   1      }
  81          /************************************************************
  82          *Function:向18B20写入一个字节
  83          *************************************************************/
  84          void WriteByte (uchar wr)  //单字节写入
  85          {
  86   1              uchar i;
  87   1              for (i=0;i<8;i++)
  88   1              {
  89   2                      dq = 0;
  90   2                      _nop_();
  91   2                      dq=wr&0x01;
  92   2                      TempDelay(5);   //delay 45 uS //5
  93   2                      _nop_();
  94   2                      _nop_();
  95   2                      dq=1;
  96   2                      wr >>= 1;
  97   2              }
  98   1      }
  99          /************************************************************
 100          *Function:读18B20的一个字节
 101          *************************************************************/
 102          uchar ReadByte (void)     //读取单字节
 103          {
 104   1              uchar i,u=0;
 105   1              for(i=0;i<8;i++)
 106   1              {
 107   2                      dq = 0;
 108   2                      u >>= 1;
 109   2                      dq = 1;
 110   2                      if(dq==1)
 111   2                      u |= 0x80;
 112   2                      TempDelay (4);
 113   2                      _nop_();
 114   2              }
 115   1              return(u);
 116   1      }
 117          /************************************************************
C51 COMPILER V7.09   DS18B20                                                               05/23/2006 11:28:32 PAGE 3   

 118          *Function:读18B20 多字节读
 119          *************************************************************/
 120          void read_bytes (uchar j)
 121          {
 122   1              uchar i;
 123   1              for(i=0;i<j;i++)
 124   1              {
 125   2                      *p = ReadByte();
 126   2                      p++;
 127   2              }
 128   1      }
 129          
 130          /************************************************************
 131          *Function:读取温度
 132          *************************************************************/
 133          void GemTemp (void)
 134          {
 135   1              unsigned char temp1; 
 136   1              read_bytes (9);                                         // 读取scratchpad中的值
 137   1              temp1 = (temp_buff[0]>>4)&0x0f;         // 舍去小数点
 138   1              if((temp_buff[0]&0x08)==0x08)           // 四舍五入
 139   1                      temp1 += 1;
 140   1              temp1 = ((temp_buff[1]<<4)&0x70)|temp1;
 141   1              Temperature = temp1;
 142   1              TempDelay(1);
 143   1      }
 144          /************************************************************
 145          *Function:内部配置
 146          *************************************************************/
 147          void Config18b20 (void)  //重新配置报警限定值和分辨率
 148          {
 149   1              Init18b20();
 150   1              WriteByte(0xcc);  //skip rom
 151   1              WriteByte(0x4e);  //写 scratchpad, 后跟3个字节数据
 152   1              WriteByte(0x1E);  //上限: 30(TH)
 153   1              WriteByte(0x0A);  //下限: 10(TL)
 154   1              WriteByte(0x7f);  //设置分辨率: 12 bit
 155   1              Init18b20();
 156   1              WriteByte(0xcc);  //skip rom
 157   1              WriteByte(0x48);  //保存设定值, 写EERAM
 158   1              Init18b20();
 159   1              WriteByte(0xcc);  //skip rom
 160   1              WriteByte(0xb8);  //回调设定值, 读EERAM
 161   1      }
 162          /************************************************************
 163          *Function:读18B20ID
 164          *************************************************************/
 165          void ReadID (void)//读取器件 id
 166          {
 167   1              Init18b20();
 168   1              WriteByte(0x33);  //read rom
 169   1              read_bytes(8);
 170   1      }
 171          /************************************************************
 172          *Function:18B20 测温处理
 173          *************************************************************/
 174          void TemperatuerResult(void)
 175          {
 176   1              Init18b20 ();
 177   1              WriteByte(0xcc);   //skip rom
 178   1              WriteByte(0x44);   //温度转换指令
 179   1              TempDelay(300);
C51 COMPILER V7.09   DS18B20                                                               05/23/2006 11:28:32 PAGE 4   

 180   1              Init18b20 ();
 181   1              WriteByte(0xcc);   //skip rom
 182   1              WriteByte(0xbe);   //读取温度指令, 即读 scratchpad
 183   1              p = temp_buff;
 184   1              GemTemp();
 185   1      }
 186          
 187          void main(void)
 188          {
 189   1              p = id_buff;
 190   1              ReadID();
 191   1              Config18b20();
 192   1              C8255_CON = 0x81;                       // 初始化8255
 193   1              Display();
 194   1              while(1)
 195   1              {
 196   2                      TemperatuerResult();    // 测温
 197   2                      Display();                              // 显示
 198   2              }
 199   1      }
 200          
 201          
 202          
 203          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    440    ----
   CONSTANT SIZE    =     10    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     25       2
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      1    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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