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

📄 13.lst

📁 温度传感器液晶显示实验
💻 LST
字号:
C51 COMPILER V7.06   13                                                                    06/11/2012 22:50:37 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE 13
OBJECT MODULE PLACED IN 13.OBJ
COMPILER INVOKED BY: D:\C51\BIN\C51.EXE 13.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          #include <reg51.h>
   2          #include <intrins.h>
   3          #define uchar unsigned char
   4          #define uint unsigned int
   5          
   6          sbit DQ=P3^6;
   7          sbit BEEP=P3^7;
   8          sbit HI_LED=P1^4;
   9          sbit LO_LED=P1^5;
  10          
  11          //共阴数码管段码及空白显示
  12          uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
  13          
  14          //温度小数位对照表
  15          uchar code df_Table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
  16          //报警温度上下限,为进行正负数比较,此处注意设为char型
  17          //取值范围为-128~+127,DS18B20支持范围为-50~+125
  18          char Alarm_Temp_HL[2]={40,-20};
  19          uchar CurrentT=0;                                           //当前读取的温度整数部分
  20          uchar Temp_Value[]={0x00,0x00};         //从DS18B20读取的温度值
  21          uchar Display_Digit[]={0,0,0,0};               //待显示的各温度数位
  22          bit HI_Alarm=0,LO_Alarm=0;                      //高低温报警标志
  23          bit DS18B20_IS_OK=1;                               //传感器正常标志
  24          uint Time0_Count=0;                                     //定时器延时累加
  25          
  26          //延时
  27          void Delay(unsigned int x)
  28          {
  29   1       while(--x);
  30   1      }
  31          
  32          //初始化SD18B20
  33          uchar Init_DS18B20( )
  34          {
  35   1       uchar status;
  36   1       DQ=1;Delay(8);
  37   1       DQ=0;Delay(90);
  38   1       DQ=1;Delay(8);
  39   1       status=DQ;
  40   1       Delay(100);
  41   1       DQ=1;
  42   1       return status;              //初始化成功时返回0
  43   1      } 
  44          
  45          //读一字节
  46          uchar ReadOneByte( )
  47          {
  48   1       uchar i,dat=0;
  49   1       DQ=1;_nop_( );
  50   1       for(i=0;i<8;i++)
  51   1       {
  52   2        DQ=0;dat>>=1;DQ=1;_nop_( );_nop_( );
  53   2        if(DQ)dat|=0x80;Delay(30);DQ=1;
  54   2       }
  55   1       return dat;
C51 COMPILER V7.06   13                                                                    06/11/2012 22:50:37 PAGE 2   

  56   1      }
  57          
  58          //写一字节
  59          void WriteOneByte(uchar dat)
  60          {
  61   1       uchar i;
  62   1       for(i=0;i<8;i++)
  63   1       {
  64   2        DQ=0;DQ=dat&0x01;Delay(5);DQ=1;dat>>=1;
  65   2       }
  66   1      }
  67          
  68          //读取温度值
  69          void Read_Temperature( )
  70          {
  71   1       if(Init_DS18B20()==1)                        //DS18B20故障
  72   1       DS18B20_IS_OK=0;
  73   1       else
  74   1       {
  75   2        WriteOneByte(0xcc);                             //跳过序列号
  76   2        WriteOneByte(0x44);                             //启动温度转换
  77   2        Init_DS18B20( );
  78   2        WriteOneByte(0xcc);                             //跳过序列号
  79   2        WriteOneByte(0xbe);                                //读取温度寄存器
  80   2        Temp_Value[0]=ReadOneByte( );           //温度低八位
  81   2        Temp_Value[1]=ReadOneByte( );           //温度高八位
  82   2        Alarm_Temp_HL[0]=ReadOneByte( );        //报警温度TH
  83   2        Alarm_Temp_HL[1]=ReadOneByte( );        //报警温度TL
  84   2        DS18B20_IS_OK=1;
  85   2       }
  86   1      }
  87          
  88          //设置DS18B20温度报警值
  89          void Set_Alarm_Temp_Value( )
  90          {
  91   1       Init_DS18B20( );
  92   1       WriteOneByte(0xcc);                              //跳过序列号
  93   1       WriteOneByte(0x4e);                              //将设定的温度报警值写入DS18B20
  94   1       WriteOneByte(Alarm_Temp_HL[0]);          //写HL
  95   1       WriteOneByte(Alarm_Temp_HL[1]);          //写TL
  96   1       WriteOneByte(0x7f);                              //12为精确度
  97   1       Init_DS18B20( );
  98   1       WriteOneByte(0xcc);                              //跳过序列号
  99   1       WriteOneByte(0x48);                              //温度报警值存入DS18B20
 100   1      }
 101          
 102          //在数码管上显示温度
 103          void Display_Temperature( )
 104          {
 105   1       uchar i;
 106   1       uchar t=150;                                              //延时值
 107   1       uchar ng=0,np=0;                                          //负数标识及负号显示位置
 108   1       char Signed_Current_Temp;                         //注意类型为char
 109   1       
 110   1      //如果为负数则取反加1,并设置负号标识及负号显示位置
 111   1       if((Temp_Value[1]&0xf8)==0xf8)
 112   1       {
 113   2        Temp_Value[1]=~Temp_Value[1];
 114   2        Temp_Value[0]=~Temp_Value[0]+1;
 115   2        if(Temp_Value[0]==0x00)Temp_Value[1]++;
 116   2        ng=1;np=0xfd;                                                 //默认负号显示在左边第2位
 117   2       }
C51 COMPILER V7.06   13                                                                    06/11/2012 22:50:37 PAGE 3   

 118   1       
 119   1      //查表得到温度小数部分
 120   1       Display_Digit[0]=df_Table[Temp_Value[0]&0x0f];
 121   1       
 122   1      //获取温度整数部分(无符号)
 123   1       CurrentT=((Temp_Value[0]&0xf0)>>4)|((Temp_Value[1]&0x07)<<4);
 124   1       
 125   1      //有符号的当前温度值,注意定义为char,其值可以为-128~+127
 126   1       Signed_Current_Temp=ng?-CurrentT:CurrentT;
 127   1       
 128   1      //高低温报警标志设置(与定义为char类型的Alarm_Temp_HL比较,这样可区分正负比较)
 129   1       HI_Alarm=Signed_Current_Temp>=Alarm_Temp_HL[0]?1:0;
 130   1       LO_Alarm=Signed_Current_Temp<=Alarm_Temp_HL[1]?1:0;
 131   1       //将整数部分分解为三位待显示数字
 132   1       Display_Digit[3]=CurrentT/100;
 133   1       Display_Digit[2]=CurrentT%100/10;
 134   1       Display_Digit[1]=CurrentT%10;
 135   1       if(Display_Digit[3]==0)                                      //高位为0则不显示
 136   1       {
 137   2        Display_Digit[3]=10;
 138   2        np=0xfb;                                                                 //调整负号位置
 139   2        if(Display_Digit[2]==0)
 140   2        {
 141   3         Display_Digit[2]=10;
 142   3         np=0xf7;                                                                //调整负号位置
 143   3        }
 144   2       }
 145   1       
 146   1      //刷新显示若干时间
 147   1       for(i=0;i<30;i++)
 148   1       {
 149   2        P0=0x39;P2=0x7f;Delay(t);P2=0xff;                //显示C
 150   2        P0=0x63;P2=0xbf;Delay(t);P2=0xff;                //显示°
 151   2        P0=DSY_CODE[Display_Digit[0]];                      //小数位
 152   2        P2=0xdf;Delay(t);P2=0xff;
 153   2        P0=(DSY_CODE[Display_Digit[1]])|0x80;    //个位及小数
 154   2        P2=0xef;Delay(t);P2=0xff;
 155   2        P0=DSY_CODE[Display_Digit[2]];                       //十位
 156   2        P2=0xf7;Delay(t);P2=0xff;
 157   2        P0=DSY_CODE[Display_Digit[3]];                       //百位
 158   2        P2=0xfb;Delay(t);P2=0xff;
 159   2        if(ng)                         //如果为负则在调整后的位置显示"-"
 160   2        {
 161   3         P0=0x40;P2=np;Delay(t);P2=0xff;
 162   3        }
 163   2       }
 164   1      }
 165          
 166          //定时器中断,控制警报声音
 167          void T0_INT( )interrupt 1
 168          {
 169   1       TH0=-1000/256;
 170   1       TL0=-1000%256;
 171   1       BEEP=!BEEP;
 172   1       if(++Time0_Count==400)
 173   1       {
 174   2        Time0_Count=0;
 175   2        if(HI_Alarm)HI_LED=~HI_LED;else HI_LED=1;
 176   2        if(LO_Alarm)LO_LED=~LO_LED;else LO_LED=1;
 177   2       }
 178   1      }
 179          
C51 COMPILER V7.06   13                                                                    06/11/2012 22:50:37 PAGE 4   

 180          
 181          //主程序
 182          void main(void)
 183          {
 184   1       IE=0x82;
 185   1       TMOD=0x01;
 186   1       TH0=-1000/256;
 187   1       TL0=-1000%256;
 188   1       TR0=0;
 189   1       HI_LED=1;
 190   1       LO_LED=1;
 191   1       Set_Alarm_Temp_Value( );
 192   1       Read_Temperature( );
 193   1       Delay(50000);
 194   1       Delay(50000);
 195   1       while(1)
 196   1       {
 197   2        Read_Temperature( );
 198   2        if(DS18B20_IS_OK)
 199   2        {
 200   3         if(HI_Alarm==1||LO_Alarm==1)TR0=1;
 201   3         else TR0=0;
 202   3         Display_Temperature( );
 203   3        }
 204   2        else
 205   2        {
 206   3         P0=P2=0x00;
 207   3        }
 208   2       }
 209   1      } 


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    606    ----
   CONSTANT SIZE    =     27    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     11    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      3    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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