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

📄 温度.lst

📁 温度传感器按键设置上下温度1602的应用
💻 LST
字号:
C51 COMPILER V9.00   蝊度                                                                  02/26/2010 11:44:53 PAGE 1   


C51 COMPILER V9.00, COMPILATION OF MODULE 蝊度
OBJECT MODULE PLACED IN 温度.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE 温度.c DEBUG OBJECTEXTEND

line level    source

   1          ////////温度测量报警系统,设置20~29度为安全范围
   2          #include<reg52.h>
   3          #define uchar unsigned char 
   4          #define uint unsigned int
   5          sbit ds=P2^7;
   6          sbit bemp=P3^7;
   7          sbit lcden=P2^2;
   8          sbit lcdrs=P2^0;
   9          sbit lcdrw=P2^1;
  10          sbit sh_zeng=P1^7;
  11          sbit sh_jian=P1^6;
  12          sbit xia_zeng=P1^5;
  13          sbit xia_jian=P1^4;
  14          uchar code table1[]="temp:";
  15          uchar code table2[]="up:";
  16          uchar code table3[]="down:";
  17          uchar code table4[]="0123456789.-";
  18          uchar bai,shi,ge,flag,num,shi_sh,shi_xia,ge_sh,ge_xia;
  19          int temp;
  20          int warn_sh=29;   //温度上限值
  21          int warn_xia=20;//温度下限值
  22           
  23          void delayms(uint z)
  24          {
  25   1         uint x,y;
  26   1         for(x=z;z>0;z--)
  27   1           for(y=110;y>0;y--);
  28   1      }
  29          void dsreset()  //DS18B20初始化
  30          {
  31   1         uint i;
  32   1         ds=0;
  33   1         i=103;
  34   1         while(i>0)i--;
  35   1         ds=1;
  36   1         i=1;
  37   1         while(i>0)i--;
  38   1      }
  39          bit tempreadbit(void)  //读一位数据函数
  40          {
  41   1         uint i;
  42   1         bit dat;
  43   1         ds=0;i++;   //延时
  44   1         ds=1;i++;i++;
  45   1         dat=ds;
  46   1         i=8;while(i>0)i--;
  47   1         return(dat);
  48   1      }
  49          uchar tempread(void)
  50          {
  51   1        uchar i,j,dat;
  52   1        dat=0;
  53   1        for(i=1;i<=8;i++)
  54   1        {
  55   2           j=tempreadbit();
C51 COMPILER V9.00   蝊度                                                                  02/26/2010 11:44:53 PAGE 2   

  56   2           dat=(j<<7)|(dat>>1);//将位转换为字节
  57   2        }
  58   1        return(dat);
  59   1      }
  60          void tempwrite(uchar dat)//向DS18B20写一个字节数据函数
  61          {
  62   1        uint i;
  63   1        uchar j;
  64   1        bit testb;
  65   1        for(j=1;j<=8;j++)
  66   1        {
  67   2          testb=dat&0x01;
  68   2          dat=dat>>1;
  69   2          if(testb)
  70   2          {
  71   3            ds=0;
  72   3            i++;i++;
  73   3            ds=1;
  74   3            i=8;while(i>0)i--;
  75   3          }
  76   2          else
  77   2          {
  78   3            ds=0;
  79   3            i=8;while(i>0)i--;
  80   3            ds=1;
  81   3            i++;i++;
  82   3          }
  83   2        }
  84   1      }
  85          void tempchange() //DS18B20开始获取温度并转换
  86          {
  87   1        dsreset();
  88   1        delayms(1);
  89   1        tempwrite(0xcc);//写跳过读ROM指令
  90   1        tempwrite(0x44);//写温度转换指令
  91   1      }
  92          void get_temp()  //读取寄存器中存储的温度数据
  93          {
  94   1         uchar a,b;
  95   1         dsreset();
  96   1         delayms(1);
  97   1         tempwrite(0xcc);
  98   1         tempwrite(0xbe);
  99   1         a=tempread();
 100   1         b=tempread();
 101   1         temp=b;
 102   1         temp<<=8;
 103   1         temp=temp|a;
 104   1         if(!(b&0xf8))
 105   1         temp=temp*0.625;//温度在寄存器中为12为,分辨率为0.0625,加0.5是为了四舍五入
 106   1         else
 107   1         {
 108   2            temp=(~temp+1)*0.625;
 109   2            flag=1;
 110   2         }
 111   1      }
 112           void write_com(uchar com)
 113          {
 114   1         lcdrs=0;
 115   1         P0=com;
 116   1         delayms(5);
 117   1         lcden=1;
C51 COMPILER V9.00   蝊度                                                                  02/26/2010 11:44:53 PAGE 3   

 118   1         delayms(5);
 119   1         lcden=0;
 120   1      }
 121          void write_date(uchar date)
 122          {
 123   1        lcdrs=1;
 124   1        P0=date;
 125   1        delayms(5);
 126   1        lcden=1;
 127   1        delayms(5);
 128   1        lcden=0;
 129   1      }
 130          void init_1602()
 131          {
 132   1       
 133   1      }
 134          void display(uint ch) //显示正温度数据
 135          {
 136   1         write_com(0x80);
 137   1         for(num=0;num<5;num++)
 138   1         {
 139   2            write_date(table1[num]);
 140   2         }
 141   1         write_date(table4[ch]);
 142   1         write_date(table4[bai]);
 143   1         write_date(table4[shi]);
 144   1         write_date(table4[10]);
 145   1         write_date(table4[ge]);
 146   1         write_com(0x80+0x40);
 147   1         for(num=0;num<3;num++)
 148   1         {
 149   2           write_date(table2[num]);
 150   2         }
 151   1         write_date(table4[shi_sh]);
 152   1         write_date(table4[ge_sh]);
 153   1         write_com(0x80+0x40+0x07);
 154   1          for(num=0;num<5;num++)
 155   1         {
 156   2           write_date(table3[num]);
 157   2         }
 158   1         write_date(table4[shi_xia]);
 159   1         write_date(table4[ge_xia]);   
 160   1      }
 161          void warn(uchar s,uchar led,uint ch)//温度报警
 162          {
 163   1         uchar i,j;
 164   1         i=s;
 165   1         j=s/2;
 166   1         P1=~led;
 167   1         while(i--)
 168   1         {
 169   2            bemp=0;
 170   2            display(ch); 
 171   2         }
 172   1         P1=0xff;
 173   1         while(j--)
 174   1         {
 175   2            display(ch);
 176   2            bemp=1;
 177   2         }
 178   1      }
 179          void deal(int t)
C51 COMPILER V9.00   蝊度                                                                  02/26/2010 11:44:53 PAGE 4   

 180          {
 181   1         if(t>warn_sh*10)
 182   1         {
 183   2            warn(2,0x01,12);
 184   2         }
 185   1         else if((t<warn_xia*10)&&(t>=0))
 186   1         {
 187   2             warn(2,0x02,12);
 188   2         }
 189   1         else if(t<0)
 190   1         {
 191   2             warn(2,0x02,11);
 192   2         }
 193   1         else 
 194   1         display(12);
 195   1      }
 196          void key()  //按键扫描函数
 197          {
 198   1         if(sh_zeng==0)
 199   1         {
 200   2            delayms(5);
 201   2            if(sh_zeng==0)
 202   2            {
 203   3                warn_sh++;
 204   3                if(warn_sh==100)
 205   3                warn_sh=99;
 206   3            }
 207   2            while(!sh_zeng);
 208   2         }
 209   1         if(sh_jian==0)
 210   1         {
 211   2            delayms(5);
 212   2            if(sh_jian==0)
 213   2            {
 214   3                warn_sh--;
 215   3                if(warn_sh==-55)
 216   3                warn_sh=-54;
 217   3            while(!sh_jian);
 218   3                }
 219   2         }
 220   1         if(xia_zeng==0)
 221   1         {
 222   2            delayms(5);
 223   2            if(xia_zeng==0)
 224   2            {
 225   3                warn_xia++;
 226   3                if(warn_xia==100)
 227   3                warn_sh=99;
 228   3            }
 229   2            while(!xia_zeng);
 230   2         }
 231   1         if(xia_jian==0)
 232   1         {
 233   2            delayms(5);
 234   2            if(xia_jian==0)
 235   2            {
 236   3                warn_xia--;
 237   3                if(warn_xia==-55)
 238   3                warn_xia=-54;
 239   3                 while(!xia_jian);
 240   3                }
 241   2         }
C51 COMPILER V9.00   蝊度                                                                  02/26/2010 11:44:53 PAGE 5   

 242   1      }
 243          void init()
 244          {
 245   1        lcden=0;
 246   1        P2=0;
 247   1        flag=0;
 248   1        P0=0xff;
 249   1        P1=0xff;
 250   1        write_com(0x38); 
 251   1        write_com(0x0c);
 252   1        write_com(0x06);
 253   1        write_com(0x01); 
 254   1      }
 255          void main()
 256          {
 257   1         init(); 
 258   1         while(1)
 259   1         {  
 260   2            key();
 261   2            tempchange();
 262   2            get_temp();
 263   2                bai=temp/100;
 264   2            shi=temp%100/10;
 265   2            ge=temp%10;
 266   2                shi_sh=warn_sh%100/10;
 267   2            ge_sh=warn_sh%10;
 268   2                shi_xia=warn_xia%100/10;
 269   2            ge_xia=warn_xia%10;
 270   2            if(flag==0)
 271   2            {
 272   3               deal(temp);
 273   3                       display(12);
 274   3            }
 275   2            else
 276   2            {
 277   3              flag=0;
 278   3                      display(11);
 279   3              warn(2,0x02,11);
 280   3            }
 281   2         }
 282   1      } 


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1033    ----
   CONSTANT SIZE    =     29    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     15       7
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       2
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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