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

📄 wendukongzhi.c

📁 基于DS18B20的温度控制器
💻 C
字号:
#include<at89x52.h>
sbit warmer=P1^4;
sbit led_run=P1^0;
sbit k_power=P3^3; 
sbit ge=P2^1;
sbit shi=P2^0;
sbit DQ =P3^1;   //定义通信端口
sbit jw=P3^6;	  //降温
sbit sw=P3^7;	  //升温
sbit Key_SET=P0^0;
sbit Key_UP=P0^1;
sbit Key_DOWN=P0^2;
bit SET_TF;			  //设置标致
unsigned char result;
unsigned char TH;	// 上限
unsigned char TL;  //下限
unsigned char dispbitcnt; 
unsigned char dispbuf[4]={10,10,10,10};
unsigned char dispbitcode[]={0x01,0xf02,0x04,0x08}; 
unsigned char dispcode[]={ 0xc0,0xf9,0xa4,0xb0,0x99,   //	0    1    2    3    4 
                            0x92,0x82,0xf8,0x80,0x90};  // 5    6    7    8    9 
void delay10ms()   //该延时子程序是12Mhz下的X*1ms延时
{
 unsigned char i,j;

  for (i=0;i<20;i++)
  for(j=0;j<248;j++);
 }
  

void delay(unsigned int i)			//延时函数
{
     while(i--);
    
}


void kz(void)   //控制程序
{
 if (result>TH)
 jw=0;
 else
 jw=1;
 if (result<TL)
 sw=0;
 else
 sw=1;
 }


void KEY(void) 			 //按键加减
{
  if (!Key_SET)   //按下SET键
  {
   delay10ms();
   if (!Key_SET)
   SET_TF=~SET_TF;
  }
  if (SET_TF)
 {
  if (!Key_UP)
  {
   delay10ms();
   if (!Key_UP)
   TH = TH + 1;    //上调上限温度
   while(!Key_UP);	
   if (TH==99)		//当上限调为99时跳转为初值
   TH=40;
  }
  if (!Key_DOWN)
 {
   delay10ms();
   if (!Key_DOWN)
   TL = TL - 1;    //下调下限温度
   while(P0_2==0);
   if (TL==0)		//当下限调为0时跳转为初值
   TL=10;
 } 
  dispbuf[0]=TH/10;
  dispbuf[1]=TH%10;
  dispbuf[2]=TL/10;
  dispbuf[3]=TL%10;		  
   P1=dispcode[dispbuf[dispbitcnt]];
   P2=dispbitcode[dispbitcnt];
   delay(1000);
   dispbitcnt++;
   if (dispbitcnt==4)
   dispbitcnt=0;
 }
}



//初始化函数
Init_DS18B20(void)
{
 unsigned char x=0;
 DQ = 1;    //DQ复位
 delay(8);  //稍做延时
 DQ = 0;    //单片机将DQ拉低
 delay(80); //精确延时 大于 480us
 DQ = 1;    //拉高总线
 delay(14);
 x=DQ;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
 delay(20);
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
 {
  DQ = 0; // 给脉冲信号
  dat>>=1;
  DQ = 1; // 给脉冲信号
  if(DQ)
   dat|=0x80;
  delay(4);
 }
 return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat)
{
 unsigned char i=0;
 for (i=8; i>0; i--)
 {
  DQ = 0;
  DQ = dat&0x01;
  delay(5);
  DQ = 1;
  dat>>=1;
 }
delay(4);
}
//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();   //读取温度值低位
b=ReadOneChar();   //读取温度值高位
a=a>>4;            //低位右移4位,舍弃小数部分
t=b<<4;            //高位左移4位,舍弃符号位
t=t|a; 
result=t;            
return(t);
}
void display_tempmain(unsigned char i)             //主程序温度显示函数
{
  P1=dispcode[i/10];
  P2=0X01;
  delay(1000);
  P1=dispcode[i%10];
  P2=0X02;
  delay(800);
  P1=0xc6;
  P2=0X04;
 
}
void main(void)
{
  unsigned int  temp  ;
  TH=40;
  TL=10;
  while(1)                         //主循环
   {
	 KEY();
	 if (!SET_TF)
	 {
     temp=ReadTemperature();
     display_tempmain(temp);
     kz();
	} 
 }   

}

⌨️ 快捷键说明

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