51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机源码 > 详细内容
一个18B20的温控C程序
发布时间:2009/6/2  阅读次数:2571  字体大小: 【】 【】【

作者:佚名         来源:网络

#include <REGX51.H>
#include <keyboardscan.H>
#include "DS18B20.H"

sbit bit1=P0^0;
sbit bit2=P0^1;
sbit bit3=P0^2;
sbit bit4=P0^3;

sbit bit5=P0^4;
sbit bit6=P0^5;
sbit bit7=P0^6;
sbit bit8=P0^7;
sbit heat=P3^5;
sbit fan=P3^6;
sbit ring=P3^7;


unsigned char code Numbernomal[]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,
                                                   0x01,0x09,0x11,0xC1,0x63,0x85,0x61,0X03,0Xff,0x63};                             //{0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09};
                                                  

unsigned char code Numberdecimal[]={0x02,0x9E,0x24,0x0C,0x98,0x48,0x40,0x1E,
                                                   0x00,0x08,0x10,0xC0,0x62,0x84,0x60,0x70};
unsigned char showdata[8]={16,16,16,16,5,2,0,0};
unsigned char count,count1=0,temp0=0,temp1=0,temp2=0,temp3=0;
unsigned char hts1=0,hts2=0,x1=0Xff,x2=0Xff,x3=0Xff,x4=0Xff;
unsigned int temperature,nowtem=0;

//==============================延时函数,1毫秒
void Delay1ms(unsigned char count)              
  {
               unsigned char i,j;
               for(i=0;i<count;i++)
               for(j=0;j<120;j++);
  }

//====================初始计数器==================================

void InitInterrupt(void)   //initialize the interrupt
  {
    
     TMOD=0x01;
    TH0=0x20;
      TL0=0x18;
    IT0=1;
    
    ET0=1;
    TR0=1;
    EA=1;
  }

Colect()interrupt 1 using 1               //读键盘
{
  temp0=keyboardscan();
  if(temp0==14)
  {
      x1=0Xff;
      x2=0Xff;
      x3=0Xff;
      x4=0Xff;
      temp1=0;
      temp3=1;
  }
  
  if((temp0<14)&(temp3==1))
  {
      if(temp0<10)
      {
    
               if(temp1==1)
            x2=x1;
      
          if(temp1==2)
          {
            x3=x2;
            x2=x1;
          }

          showdata[0]=temp0;
            x1=Numbernomal[showdata[0]];
          temp1++;
      }
      if(temp0==10)
      {
          count1=0;
          while(Numbernomal[count1++]!=x1);
          x1=Numberdecimal[count1-1];
      }

      if(temp1==3)
          temp1=0;  
       }
    
  
  if(temp0==15)
  {
      temp3=0;
      x4=x3;
      x3=x2;
      x2=x1;
      x1=0x63;
  }
  
}

show()
{
                          
                
        P0=0xfe;
        P3=Numbernomal[showdata[4]];
        Delay1ms(5);        
          
                               P0=0xfd;
        P3=Numberdecimal[showdata[5]];
             Delay1ms(5);
                  
        P0=0xfb;
             P3=Numbernomal[showdata[6]];  
        Delay1ms(5);
    

        P0=0xf7;
        P3=0x63;
        Delay1ms(5);
  
        
             P3=x1;
               P0=0x7f;
        Delay1ms(5);

        
        P3=x2;
        P0=0xbf;
        Delay1ms(5);
           
             P0=0xdf;
        P3=x3;
        Delay1ms(5);

        P0=0xef;
        P3=x4;
        Delay1ms(5);
        P0=0xff;

}

Control()
{
    nowtem=x4*100+x3*10+x2;    
    if(temperature<nowtem-20)
    {
      heat=1;
      fan=0;
    }    
    else heat=0;
    if(temperature>nowtem-20)
    {

        heat=1;
        Delay1ms(10);
        heat=0;

        fan=1;
        Delay1ms(10);
        fan=0;

      if(hts1==201)
        hts1=0;
    }
    if((temperature>nowtem)&(temperature<nowtem+20))
    {
      hts1++;
      if(hts1%3==0)
      {
        fan=1;
        Delay1ms(10);
        fan=0;
      }
      if(hts1%6==0)
      {
        heat=1;
        Delay1ms(10);
        heat=0;
      }  
      if(hts1==201)
        hts1=0;  
    }
    if(temperature>nowtem+20)
    {
      heat=0;
      fan=1;
    }
    if(temperature>nowtem-20)
    {
      ring=1;
      Delay1ms(20);
    }
    else ring=0;

}

main()
{
  InitInterrupt() ;
  while(1)
    {
      show();
      Control();
      temp2++;
      if(temp2==20)
      {
        temperature=Read_Temperature();
        temperature=temperature*0.625;
        
        showdata[4]=temperature/100;
        showdata[5]=(temperature/10)%10;
        showdata[6]=temperature%10;
        temp2=0;
        TH0=0xff;
        TL0=0xff;

      }  
    }
}

我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号