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

📄 ds18b20.lst

📁 温度传感器ds18b20的驱动程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.02   DS18B20                                                               06/12/2008 17:28:54 PAGE 1   


C51 COMPILER V8.02, 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 "at89x52.h"
   2          #include <intrins.h>
   3          
   4          #define  SCAN_CYCLE 300
   5          #define  uchar unsigned char    
   6          #define  uint unsigned int
   7          uchar num1[8]={0x99,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//,0x00
   8          uchar num2[8]={0x99,0x00,0x07,0x00,0x07,0x00,0x70,0x00};          //,0x07
   9          uchar get_num,getflag,flag=0;
  10          uint keyflag;
  11          uchar UARTKeyFlag=0xaa;
  12          static const char c_keyCode[16]={0x01,0x02,0x03,0x0a,            /*键盘1,2,3,A*/
  13                                           0x04,0x05,0x06,0x0b,            /*键盘4,5,6,B*/
  14                                           0x07,0x08,0x09,0x0c,            /*键盘7,8,9,C*/
  15                                           0x0e,0x00,0x0f,0x0d};           /*键盘*,0,#,D*/                
  16          uchar num3[8]={0xAA,0x01,0xfa,0xfb,0xfc,0xfd,0xfe,0xfa};                //,0xff
  17          
  18          sbit One_Wire_Bus=P3^7;
  19          
  20          //function define
  21          /********************************************************************/
  22          
  23          void One_Wire_Delay(unsigned char delay_time);
  24          void One_Wire_Write_Byte(unsigned char oww_dat);
  25          unsigned char One_Wire_Read_Byte(void);
  26          void Read_18B20(void);
  27          void Initize_One_Wire_Bus(void);
  28          /********************************************************************/
  29          data unsigned char GetScratchpad[2];
  30          
  31          
  32          code unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
  33          code unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
  34          unsigned char ResultTemperatureH=0x00;
  35          unsigned char ResultTemperatureLH=0x00;
  36          unsigned char ResultTemperatureLL=0x00;
  37          data unsigned char ResultSignal;
  38            //The signal of temperature;
  39          /********************************************************************/
  40          
  41          //ds18b20 part
  42          #define  SkipRom     0xcc
  43          #define  ConvertTemperature   0x44
  44          #define  ReadScratchpad    0xbe
  45                  
  46          void keyhandle();
  47          void scankey();
  48          void delay(uint value);
  49          void scrshift(uchar keyindex);
  50          void numjudge();
  51          void clearscr();
  52          void statussend(uchar num[8]);
  53          void statussend_one(uint num);
  54          uchar keyjudge();
  55          
C51 COMPILER V8.02   DS18B20                                                               06/12/2008 17:28:54 PAGE 2   

  56          
  57          void One_Wire_Delay(unsigned char delay_time);
  58          void One_Wire_Write_Byte(unsigned char oww_dat);
  59          unsigned char One_Wire_Read_Byte(void);
  60          void Read_18B20(void);
  61          void Initize_One_Wire_Bus(void);
  62          
  63          /********************************************************************/
  64          void Initize_One_Wire_Bus(void)
  65          { 
  66   1       One_Wire_Bus=0;
  67   1       One_Wire_Delay(80);//Bus master pulling low 488us 
  68   1       One_Wire_Bus=1;
  69   1       One_Wire_Delay(25);//Resister pull up 158us;
  70   1      }//Intialization the 1-wire devices;
  71          /***********************************************************************/
  72          
  73          /******************* ds 18b20 **********************************/
  74          void One_Wire_Delay(unsigned char delay_time)
  75          { 
  76   1       while(delay_time)delay_time--;//Delay time us :=(8+delay_time*6)us;
  77   1      }
  78          /***********************************************************************/
  79          unsigned char One_Wire_Read_Byte(void)
  80          { 
  81   1       bit temp_bit;
  82   1       unsigned char i,result=0;
  83   1       for(i=0;i<8;i++)
  84   1       { 
  85   2        One_Wire_Bus=0;
  86   2        One_Wire_Bus=1; 
  87   2        temp_bit=One_Wire_Bus;
  88   2        One_Wire_Delay(11);//delay 62 us
  89   2        if(temp_bit)
  90   2         result|=0x01<<i;
  91   2       }
  92   1       return(result);
  93   1        //return the result of the 1-wire device;
  94   1      }//Read a byte from the 1-wire bus;
  95          /***********************************************************************/
  96          void One_Wire_Write_Byte(unsigned char oww_dat)
  97          {  
  98   1       unsigned char i;
  99   1       for(i=0;i<8;i++)
 100   1       { 
 101   2        One_Wire_Bus=0;
 102   2        if(oww_dat&0x01)One_Wire_Bus=1; 
 103   2        One_Wire_Delay(25);//delay 128 us
 104   2        One_Wire_Bus=1;
 105   2        oww_dat>>=1;
 106   2       }
 107   1       One_Wire_Delay(10);
 108   1      }//Write a byte to the 1-wire bus;
 109          /***********************************************************************/
 110          void Read_18B20(void)
 111          { 
 112   1       unsigned char tempH,tempL;
 113   1       Initize_One_Wire_Bus();
 114   1         num3[7] = 0x11;
 115   1       One_Wire_Write_Byte(SkipRom);
 116   1         num3[5] = 0x11;
 117   1       _nop_();
C51 COMPILER V8.02   DS18B20                                                               06/12/2008 17:28:54 PAGE 3   

 118   1        //There is just one DS1820 on the bus;
 119   1       One_Wire_Write_Byte(ConvertTemperature);
 120   1              num3[5] = 0x22;
 121   1       One_Wire_Delay(5);
 122   1        //Start to convert temperature;
 123   1       Initize_One_Wire_Bus();
 124   1       num3[7] = 0x22;
 125   1       One_Wire_Write_Byte(SkipRom);
 126   1       num3[5] = 0x33;
 127   1       _nop_();
 128   1       One_Wire_Write_Byte(ReadScratchpad);
 129   1       num3[5] = 0x44;
 130   1       GetScratchpad[0]=One_Wire_Read_Byte();
 131   1         num3[6] = 0x11;
 132   1        //Master samples the LSB temperature from the scratchpad;
 133   1       GetScratchpad[1]=One_Wire_Read_Byte();
 134   1         num3[6] = 0x22;
 135   1        //Master samples the MSB temperature from the scratchpad;
 136   1       One_Wire_Delay(120);
 137   1       tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);  
 138   1       tempL=(GetScratchpad[0]&0x0f);
 139   1       Initize_One_Wire_Bus();
 140   1        //Issue a reset to terminate left parts;
 141   1       if(tempH&0x80)
 142   1       { 
 143   2        tempH=~tempH;
 144   2        tempL=~tempL+1;
 145   2        ResultSignal=1;
 146   2        //Negative temperature;
 147   2       }
 148   1       ResultTemperatureH=tempH;
 149   1       ResultTemperatureLL=decimalL[tempL];
 150   1       ResultTemperatureLH=decimalH[tempL];
 151   1      
 152   1       
 153   1      
 154   1        //Result of temperature; 
 155   1      }//Read the byte0 and byte1 from scratchpad;
 156          /************************************************************************/
 157          
 158          
 159          void main()
 160          {
 161   1              TMOD=0x11;                      /*Timer0工作在案MODE1,16位定时器,Timer1工作在MODE2,自动重新装载模式*/
 162   1                                              /*|    定时器1        |     定时器1       |*/
 163   1                                              /*| GATE|C/T| M1 | M0 | GATE|C/T| M1 | M0 |*/
 164   1              TH0=(65536-SCAN_CYCLE)/256; /*设定Timer0每隔SCAN_CYCLEu中断一次*/
 165   1          TL0=(65536-SCAN_CYCLE)%256;
 166   1          TR0=1;                                      /*启动TEMER0*/
 167   1              T2CON=0x38;               /*令定时器2作为串口波特率发生器*/
 168   1                                                              /*|TF2|EXF2|RCLK|TCLK|EXEN2|TR2|C/T2|CP/RL2|*/
 169   1              TL2=0xd9;              /*设定串口波特率为9600,晶振为12MHz*/
 170   1              TH2=0xff;
 171   1              RCAP2L=0xd9;           /*根据串口波特率9600设定自动重载寄存器*/
 172   1              RCAP2H=0xff;
 173   1              TR2=1;             /*启动定时器2*/
 174   1              SCON=0x50;         /*设定UART工作在MODE3模式,可传送和接收数据,REN=1*/
 175   1                                                  /*| SM0| SM1| SM2| REN| TB8| RB8| TI | RI |*/
 176   1          IE=0x9a;                    /*使能总中断、Timer0,1中断、外部中断1,2*/
 177   1                                              /*| EA| - | ET2| ES | ET1| EX1| ET0| EX0|*/
 178   1          IP=0x12;                    /*优先Timer0中断*/
 179   1                                              /*| - | - | PT2| PS | PT1| PX1| PT0| PX0|*/
C51 COMPILER V8.02   DS18B20                                                               06/12/2008 17:28:54 PAGE 4   

 180   1              
 181   1      //      P3=0xFF;
 182   1      //      p33     =0 ;
 183   1      //      p33 = 1;
 184   1      //      ds = 0 ;
 185   1              GetScratchpad[0] = 0x00;
 186   1              GetScratchpad[1] = 0x00;
 187   1              while(1)
 188   1              {
 189   2                      keyhandle();
 190   2      //              time_delay(1000000);            
 191   2              }
 192   1      }
 193          
 194                  
 195          void keyhandle()                                                                //按键处理                                                              
 196          {
 197   1              uchar temp1,key;
 198   1              uint aa=0,judge=0;
 199   1              key=keyjudge();
 200   1              if(key!=0x55)    
 201   1          {
 202   2                      temp1=c_keyCode[key];
 203   2                      switch(temp1)
 204   2                      {
 205   3                              case 0x0a:      statussend(num1);
 206   3                                                      break;
 207   3                              case 0x0b:  statussend(num2);
 208   3                                                      break;
 209   3                              case 0x0c: 
 210   3                                                      /*{
 211   3                                                              Initize_One_Wire_Bus();                                                                                                         
 212   3                                                              statussend(num3);
 213   3                                                              break;
 214   3                                                      } */
 215   3                                                      /*

⌨️ 快捷键说明

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