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

📄 温度测试与输入显示.c

📁 此为基于AT89C51及DS18B20实现的智能温度控制系统的模型
💻 C
字号:
#include<at89x51.h>
#include<absacc.h>
#include<intrins.h>
#define uchar unsigned char
uchar idata com1,com2; //键盘扫描用
uchar fu;        //测量温度用
int n;

sbit DQ =P3^0;//定义通信端口

uchar code LED[] ={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0123456789 
uchar code LED1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//0-9的带小数点的段选码
uchar code tab[] ={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//P2选通数组
int b[4]={0,0,0,0}; //测量温度缓存数组
int c[4]={0,0,0,0};	  //设定温度缓存数组


//延时函数
void delay(unsigned int i)
{
	while(i--);    
}
//初始化函数
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);
}

//读取温度
float ReadTemperature(void)
{
	float ttt;
	unsigned char a=0;
	unsigned char b=0;
	unsigned int t=0;

	float tt=0;
	Init_DS18B20();     //初始化函数DS18B20
	WriteOneChar(0xCC); // 跳过读序号列号的操作
	WriteOneChar(0x44); // 启动温度转换
	Init_DS18B20();
	WriteOneChar(0xCC); //跳过读序号列号的操作
	WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
	a=ReadOneChar();
	b=ReadOneChar();
	//a=0xeb;   -33.3125(0x215)
	//b=0xfd;
	t=b;
	t<<=8;
	t=t|a;
	fu=b;
	fu=fu&0x08;
	if(fu!=0)
   	{t=~t+1;}

	tt=t*0.0625; //25.0625

	ttt=tt;
	return(ttt);
}


uchar key()
{
	 uchar i;
	 uchar com;
	 com1=0x00;
	 com2=0x00;
     delay(100);                      //消除键盘抖动  延时10ms 
     P1=0xf0;
     if(P1!=0xf0)
	 {
        com1=P1;
        P1=0x0f;
        com2=P1;        
      }
	  P1=0xf0;
	  while(P1!=0xf0);	  
	  com=com1|com2;

	  if(com==0xee)i=0;
	  if(com==0xed)i=1;
	  if(com==0xeb)i=2;
	  if(com==0xe7)i=3;
	  if(com==0xde)i=4;
	  if(com==0xdd)i=5;
	  if(com==0xdb)i=6;
	  if(com==0xd7)i=7;
	  if(com==0xbe)i=8;
	  if(com==0xbd)i=9;
	  if(com==0xbb)i=10;
	  if(com==0xb7)i=11;
	  if(com==0x7e)i=12;
	  if(com==0x7d)i=13;
	  if(com==0x7b)i=14;
	  if(com==0x77)i=15;
	  return(i); 

}
void zhd() interrupt 0
{	uchar j;
	uchar g;
	//key();
	j=key();
	if(j==10)
	{
		n=0;
		for(g=0;g<4;g++)
	 	{
			c[g]=0;
		 }
		P2=0x00;
	} 
	else
	{	 
		c[n]=j;
		n=n+1;
	}
}  
void main()
{
	EA=1;
	EX0=1;
	IT0=1;

   	while(1)
   	{	
		float show;
		unsigned int test,set;
		int m,n;
		int j;
		show=ReadTemperature();//读温度

		m=show;//25 
		b[3]=m/10;//十位
		b[2]=m%10;//个位
		n=((show-m)*100+0.5);
		b[1]=n/10;//十分位
		b[0]=n%10;//百分位

		P2=0;
		for(j=0;j<4;j++)
		{
			 P2=tab[j];
			 if(j==2)P0=LED1[b[j]];
			 else P0=LED[b[j]];
			 delay(50);
		} 
		P1=0xf0;
		for(j=4;j<8;j++)
		{	
			P2=tab[j];
			if(j==5)P0=LED1[c[j-4]];
			else P0=LED[c[j-4]];   
			delay(50);	
		}

		test=b[3]*1000+b[2]*100+b[1]*10+b[0];
		set =c[0]*1000+c[1]*100+c[2]*20+c[3];
		if(test>set)

	}
}

⌨️ 快捷键说明

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