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

📄 ds18b20+-

📁 DS18B20温度传感器输出显示
💻
字号:
C51 COMPILER V8.02   DS18B20温度確感芲输砡显蔩                                             08/07/2012 23:53:07 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE DS18B20温度確感芲输砡显蔩
OBJECT MODULE PLACED IN DS18B20温度传感器输出显示.OBJ
COMPILER INVOKED BY: F:\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 rs=P2^0;
   6          sbit rw=P2^1;
   7          sbit en=P2^2;
   8          sbit DQ=P3^3;
   9          bit DS_OK=1; //温度传感器正常标志
  10          uchar temp[]={0x00,0x00};  //用于存储高字节与低字节
  11          uchar display_digit[]={0,0,0,0};//存储显示温度字符
  12          uchar current=0;   //存储整数(高字节低三位与低字节高四位)
  13          uchar temp_display[]={"TEMP:         "};
  14          uchar code tab0[]={"welcome to here"};
  15          uchar code tab1[]={"----sky"};
  16          uchar code title[]={"THE TEMPPERATURE:"};
  17          uchar code df_tab[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
  18          void delay(uint z)
  19          {
  20   1              uint x;
  21   1              while(z--)
  22   1                      for(x=120;x>0;x--);
  23   1      }
  24          void delay_us()
  25          {_nop_();_nop_();_nop_();_nop_();}
  26          void delay_us1(uint a)
  27          {
  28   1              while(a--);
  29   1      }
  30          uchar LCD_check_busy()
  31          {
  32   1              uchar state;
  33   1              rs=0;
  34   1              rw=1;
  35   1              delay_us();
  36   1              en=1;
  37   1              state=P0;
  38   1              delay_us();
  39   1              en=0;
  40   1              delay_us();
  41   1              return state;
  42   1      
  43   1      }
  44          void LCD_write_cmd(uchar cmd)
  45          {
  46   1          while((LCD_check_busy()&0x80)==0x80);
  47   1              rs=0;
  48   1              rw=0;
  49   1              delay_us();
  50   1              en=1;
  51   1              P0=cmd;
  52   1              delay_us();
  53   1              en=0;
  54   1              delay_us();
  55   1      }
C51 COMPILER V8.02   DS18B20温度確感芲输砡显蔩                                             08/07/2012 23:53:07 PAGE 2   

  56          void LCD_write_data(uchar dat)
  57          {
  58   1              while((LCD_check_busy()&0x80)==0x80);
  59   1              rs=1;
  60   1              rw=0;
  61   1              delay_us();
  62   1              en=1;
  63   1              P0=dat;
  64   1              delay_us();
  65   1              en=0;
  66   1              delay_us();
  67   1      }
  68          void LCD_display(uchar *s)
  69          {
  70   1              uchar i;
  71   1              for(i=0;i<16;i++)
  72   1              {
  73   2                      LCD_write_data(s[i]);   
  74   2              }
  75   1      }
  76          void LCD_init()
  77          {
  78   1              LCD_write_cmd(0x38);
  79   1              LCD_write_cmd(0x0c);
  80   1              LCD_write_cmd(0x06);
  81   1              LCD_write_cmd(0x01);
  82   1      }
  83          //LCD*************************************************
  84          
  85          
  86          uchar DS_check()   //检查温度传感器
  87          {
  88   1              uchar state;
  89   1              DQ=1;
  90   1              delay_us1(8); //精确定时
  91   1              DQ=0;
  92   1              delay_us1(80);
  93   1              DQ=1;
  94   1              delay_us1(8);
  95   1              state=DQ;
  96   1              delay(100);
  97   1              return state;
  98   1      }
  99          void DS_write_byte(uchar dat)
 100          {
 101   1              uchar i;
 102   1              for(i=0;i<8;i++)
 103   1              {
 104   2                      
 105   2                      if((dat&0x01)==0) //写0
 106   2                      {
 107   3                              DQ=0;
 108   3                              delay_us1(4);
 109   3                              DQ=1;
 110   3                              delay_us1(1);
 111   3                      }
 112   2              
 113   2                      else                             //写1
 114   2                      {
 115   3                              DQ=0;
 116   3                              delay_us1(1);
 117   3                              DQ=1;
C51 COMPILER V8.02   DS18B20温度確感芲输砡显蔩                                             08/07/2012 23:53:07 PAGE 3   

 118   3                              delay_us1(4);
 119   3                      }       
 120   2                      dat>>=1;
 121   2              }
 122   1      }
 123          uchar DS_read_byte() //读一个字节
 124          {
 125   1              uchar i,dat=0;
 126   1              for(i=0;i<8;i++)
 127   1              {
 128   2                      DQ=0;
 129   2                      dat>>=1;
 130   2                      DQ=1;
 131   2                      if(DQ==1)
 132   2                              dat|=0x80;
 133   2                      else
 134   2                              dat|=0x00;
 135   2                      delay_us1(30);
 136   2                      DQ=1;
 137   2              }
 138   1              return dat;
 139   1      }
 140          
 141          void DS_read_temperature()
 142          {       
 143   1              if(DS_check()==1)        //检查初始化温度传感器
 144   1                      DS_OK=0;
 145   1              else
 146   1              {
 147   2                      DS_check();
 148   2                      DS_write_byte(0xcc); //跳过序列号
 149   2                      DS_write_byte(0x44); //启动温度转换
 150   2                      DS_check();                //重新检查温度传感器
 151   2                      DS_write_byte(0xcc);//
 152   2                      DS_write_byte(0xbe);//启动读取温度
 153   2      /****先读低8位,再读高8位,不可调换************/
 154   2                      temp[0]=DS_read_byte();
 155   2                      temp[1]=DS_read_byte();
 156   2                      DS_OK=1;                         //正常工作
 157   2              }
 158   1      }
 159          void LCD_display_temperature()
 160          {
 161   1              uchar flag=0;//负数标识
 162   1              /***高5位为1(0XF8)则为负数,为负数时取反加一****/
 163   1              if((temp[1]&0xf8)==0xf8)
 164   1              {
 165   2                      temp[1]=~temp[1];  //负数取反
 166   2                      temp[0]=~temp[0]+1;
 167   2                      if(temp[0]==0x00)
 168   2                              temp[1]++;
 169   2                      flag=1;//负数标识
 170   2              }
 171   1              /****温度小数部分*********/
 172   1              display_digit[3]=df_tab[temp[0]&0x0f];
 173   1          /*温度整数部分(高字节低3位低字节高4位,无符号)***/
 174   1              current=((temp[1]&0x07)<<4)|((temp[0]&0xf0)>>4);
 175   1              //分解整数部分
 176   1              display_digit[0]=current/100;
 177   1              display_digit[1]=current%100/10;
 178   1              display_digit[2]=current%10;
 179   1      
C51 COMPILER V8.02   DS18B20温度確感芲输砡显蔩                                             08/07/2012 23:53:07 PAGE 4   

 180   1         /***刷新LCD显示缓冲***/
 181   1          temp_display[11]=display_digit[3]+'0';
 182   1              temp_display[10]='.';
 183   1              temp_display[9]=display_digit[2]+'0';
 184   1              temp_display[8]=display_digit[1]+'0';
 185   1              temp_display[7]=display_digit[0]+'0';
 186   1              /*百位为0时不显示*/
 187   1              if(display_digit[0]==0)
 188   1                      temp_display[7]=' ';
 189   1                      /*百位为0,且十位为0时不显示      */
 190   1              if((display_digit[0]==0)&(display_digit[1]==0))
 191   1                      temp_display[8]=' ';
 192   1         // 负号显示在恰当位置
 193   1              if(flag)
 194   1              {
 195   2                      if(temp_display[8]==' ')
 196   2                              temp_display[8]='-';
 197   2                      else
 198   2                      {
 199   3                              if(temp_display[7]==' ')
 200   3                                      temp_display[7]='-';
 201   3                              else
 202   3                                      temp_display[6]='-';
 203   3                      }
 204   2              }
 205   1              
 206   1              LCD_write_cmd(0x80+0x00);
 207   1              LCD_display(title);
 208   1      
 209   1              LCD_write_cmd(0x80+0x40);
 210   1              LCD_display(temp_display);
 211   1              
 212   1              LCD_write_cmd(0x80+0x4e);
 213   1              LCD_write_data('C');
 214   1              
 215   1              LCD_write_cmd(0x80+0x4f);
 216   1              LCD_write_data(0x00);   //LCD最后一位不显示,可以不要   
 217   1      }
 218          void main()
 219          {
 220   1              uchar i;
 221   1              LCD_init();
 222   1              LCD_write_cmd(0x80+0x00);
 223   1              for(i=0;i<16;i++)
 224   1              {
 225   2                      LCD_write_data(tab0[i]);
 226   2                      delay(20);      
 227   2              }
 228   1              LCD_write_cmd(0x80+0x48);
 229   1              for(i=0;i<8;i++)
 230   1              {
 231   2                      LCD_write_data(tab1[i]);        
 232   2                      delay(20);
 233   2              }
 234   1              delay(1000);
 235   1              for(i=0;i<16;i++)
 236   1              {
 237   2                      LCD_write_cmd(0x1c);
 238   2                      delay(20);
 239   2              }
 240   1              LCD_write_cmd(0x01);
 241   1              while(1)
C51 COMPILER V8.02   DS18B20温度確感芲输砡显蔩                                             08/07/2012 23:53:07 PAGE 5   

 242   1              {
 243   2                      DS_read_temperature();
 244   2                      if(DS_OK)
 245   2                              LCD_display_temperature();
 246   2                      delay(100);
 247   2              }
 248   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    642    ----
   CONSTANT SIZE    =     58    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     22       3
   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 + -