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

📄 ds18b20.lst

📁 一个比较经典的利用温度传感器DS18B20来测温
💻 LST
字号:
C51 COMPILER V7.02a   DS18B20                                                              05/17/2007 17:11:34 PAGE 1   


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

stmt level    source

   1          //ds18b20 drive program
   2          //for 51 mcu with lcd1602 display
   3          //designed by zhaoliang
   4          //2005-6-15 20:11
   5          #include "reg51.h"
   6          #include <intrins.h>
   7          /*******************************************************************/
   8          //lcd part
   9          #define  LINE1     0
  10          #define  LINE2     1
  11          #define  LINE1_HEAD    0x80
  12          #define  LINE2_HEAD    0xC0
  13          #define  LCD_DELAY_TIME   40
  14          #define  DATA_MODE    0x38
  15          #define  OPEN_SCREEN    0x0C
  16          #define  DISPLAY_ADDRESS   0x80
  17          #define  CLEARSCREEN    LCD_en_command(0x01)
  18          #define  COMMAND_SLOT   LCD1602_RS=LOW; LCD1602_RW=LOW;LCD1602_EN=LOW
  19          #define  DATA_SLOT    LCD1602_RS=HIGH;LCD1602_RW=LOW;LCD1602_EN=LOW
  20          //common part 
  21          #define  HIGH     1
  22          #define  LOW      0
  23          #define  TRUE      1
  24          #define  ZERO      0 
  25          #define  MSB       0x80
  26          //ds18b20 part
  27          #define  SkipRom     0xcc
  28          #define  ConvertTemperature   0x44
  29          #define  ReadScratchpad    0xbe
  30          
  31          /*******************************************************************/
  32          //change this part at different board
  33          #define  LCDIO     P0
  34          sbit LCD1602_RS=P1^2;   //data command select  1 data  0 command  pin 4 
  35          sbit LCD1602_RW=P1^1;   //read write select   1 read   0 write     pin 5
  36          sbit LCD1602_EN=P1^0;   //LCD enable signal             pin 6
  37          
  38          
  39          sbit One_Wire_Bus=P3^3;
  40          
  41          //function define
  42          /********************************************************************/
  43          void LCD_delay(void);//lcd delay function 
  44          void LCD_en_command(unsigned char command);//write command function
  45          void LCD_en_dat(unsigned char temp);//write data function
  46          void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
  47          void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
  48          void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function
  49          void LCD_init(void);//lcd initize function
  50          
  51          void One_Wire_Delay(unsigned char delay_time);
  52          void One_Wire_Write_Byte(unsigned char oww_dat);
  53          unsigned char One_Wire_Read_Byte(void);
  54          void Read_18B20(void);
  55          void Initize_One_Wire_Bus(void);
C51 COMPILER V7.02a   DS18B20                                                              05/17/2007 17:11:34 PAGE 2   

  56          /********************************************************************/
  57          data unsigned char GetScratchpad[2];
  58          code unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
  59          code unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
  60          unsigned char ResultTemperatureH;
  61          unsigned char ResultTemperatureLH,ResultTemperatureLL;
  62          data unsigned char ResultSignal;
  63            //The signal of temperature;
  64          /********************************************************************/
  65          void main(void)
  66          { 
  67   1       unsigned char i,j;
  68   1       Initize_One_Wire_Bus(); 
  69   1       LCD_init(); 
  70   1       while(TRUE )    
  71   1       {
  72   2        Read_18B20();
  73   2        i=ResultTemperatureH/10;
  74   2        j=ResultTemperatureH-(i*10);
  75   2        LCD_write_string(0,LINE1,"  DS1B20 TEST  ");
  76   2        LCD_write_string(0,LINE2,"XiaorunyiT:   .");
  77   2        LCD_write_char(0x0c,LINE2,i|0x30);
  78   2        LCD_write_char(0x0d,LINE2,j|0x30);
  79   2        LCD_write_char(0x0f,LINE2,(ResultTemperatureLH/10)|0x30);
  80   2       }
  81   1      }
  82          /********************************************************************/
  83          /******************** LCD PART *************************************/
  84          void LCD_delay(void)   
  85          {
  86   1       unsigned char i;
  87   1       for(i=LCD_DELAY_TIME;i>ZERO;i--);//be sure lcd reset
  88   1      }
  89          /********************************************************************/  
  90          void LCD_en_command(unsigned char command)
  91          {
  92   1       LCDIO=command;
  93   1       COMMAND_SLOT;
  94   1       LCD_delay();
  95   1       LCD1602_EN=HIGH;
  96   1      }
  97          /********************************************************************/
  98          void LCD_en_dat(unsigned char dat)
  99          {
 100   1       LCDIO=dat;
 101   1       DATA_SLOT;
 102   1       LCD_delay();
 103   1       LCD1602_EN=HIGH;
 104   1      }
 105          /********************************************************************/
 106          void LCD_set_xy( unsigned char x, unsigned char y )
 107          {
 108   1       unsigned char address;
 109   1       if (y == LINE1) 
 110   1        address = LINE1_HEAD + x;
 111   1       else 
 112   1           address = LINE2_HEAD + x;
 113   1       LCD_en_command(address); 
 114   1      }
 115          /********************************************************************/
 116          void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
 117          {
C51 COMPILER V7.02a   DS18B20                                                              05/17/2007 17:11:34 PAGE 3   

 118   1       LCD_set_xy( x, y ); 
 119   1       LCD_en_dat(dat);
 120   1      }
 121          /********************************************************************/
 122          void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
 123          {
 124   1          LCD_set_xy( X, Y ); //set address 
 125   1          while (*s)  // write character
 126   1          {
 127   2           LCDIO=*s;
 128   2              LCD_en_dat(*s);   
 129   2        s ++;
 130   2          }
 131   1      }
 132          /********************************************************************/
 133          void LCD_init(void)
 134          { 
 135   1       CLEARSCREEN;//clear screen 
 136   1       LCD_en_command(DATA_MODE);//set 8 bit data transmission mode 
 137   1       LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
 138   1       LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address 
 139   1       CLEARSCREEN;//clear screen
 140   1      }
 141          /********************************************************************/
 142          void Initize_One_Wire_Bus(void)
 143          { 
 144   1       One_Wire_Bus=0;
 145   1       One_Wire_Delay(80);//Bus master pulling low 488us 
 146   1       One_Wire_Bus=1;
 147   1       One_Wire_Delay(25);//Resister pull up 158us;
 148   1      }//Intialization the 1-wire devices;
 149          /***********************************************************************/
 150          /******************* ds 18b20 **********************************/
 151          void One_Wire_Delay(unsigned char delay_time)
 152          { 
 153   1       while(delay_time)delay_time--;//Delay time us :=(8+delay_time*6)us;
 154   1      }
 155          /***********************************************************************/
 156          unsigned char One_Wire_Read_Byte(void)
 157          { 
 158   1       bit temp_bit;
 159   1       unsigned char i,result=0;
 160   1       for(i=0;i<8;i++)
 161   1       { 
 162   2        One_Wire_Bus=0;
 163   2        One_Wire_Bus=1; 
 164   2        temp_bit=One_Wire_Bus;
 165   2        One_Wire_Delay(9);//delay 62 us
 166   2        if(temp_bit)
 167   2         result|=0x01<<i;
 168   2       }
 169   1       return(result);
 170   1        //return the result of the 1-wire device;
 171   1      }//Read a byte from the 1-wire bus;
 172          /***********************************************************************/
 173          void One_Wire_Write_Byte(unsigned char oww_dat)
 174          {  
 175   1       unsigned char i;
 176   1       for(i=0;i<8;i++)
 177   1       { 
 178   2        One_Wire_Bus=0;
 179   2        if(oww_dat&0x01)One_Wire_Bus=1; 
C51 COMPILER V7.02a   DS18B20                                                              05/17/2007 17:11:34 PAGE 4   

 180   2        One_Wire_Delay(20);//delay 128 us
 181   2        One_Wire_Bus=1;
 182   2        oww_dat>>=1;
 183   2       }
 184   1       One_Wire_Delay(10);
 185   1      }//Write a byte to the 1-wire bus;
 186          /***********************************************************************/
 187          void Read_18B20(void)
 188          { 
 189   1       unsigned char tempH,tempL;
 190   1       Initize_One_Wire_Bus();
 191   1       One_Wire_Write_Byte(SkipRom);
 192   1       _nop_();
 193   1        //There is just one DS1820 on the bus;
 194   1       One_Wire_Write_Byte(ConvertTemperature);
 195   1       One_Wire_Delay(5);
 196   1        //Start to convert temperature;
 197   1       Initize_One_Wire_Bus();
 198   1       One_Wire_Write_Byte(SkipRom);
 199   1       _nop_();
 200   1       One_Wire_Write_Byte(ReadScratchpad);
 201   1       GetScratchpad[0]=One_Wire_Read_Byte();
 202   1        //Master samples the LSB temperature from the scratchpad;
 203   1       GetScratchpad[1]=One_Wire_Read_Byte();
 204   1        //Master samples the MSB temperature from the scratchpad;
 205   1       One_Wire_Delay(120);
 206   1       tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);  
 207   1       tempL=(GetScratchpad[0]&0x0f);
 208   1       Initize_One_Wire_Bus();
 209   1        //Issue a reset to terminate left parts;
 210   1       if(tempH&0x80)
 211   1       { 
 212   2        tempH=~tempH;
 213   2        tempL=~tempL+1;
 214   2        ResultSignal=1;
 215   2        //Negative temperature;
 216   2       }
 217   1       ResultTemperatureH=tempH;
 218   1       ResultTemperatureLL=decimalL[tempL];
 219   1       ResultTemperatureLH=decimalH[tempL];
 220   1        //Result of temperature; 
 221   1      }//Read the byte0 and byte1 from scratchpad;
 222          /************************************************************************/


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    404    ----
   CONSTANT SIZE    =     64    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      6       4
   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 + -