智能温度监测系统

要求:1、通过硬件电路设计和软件编程驱动的结合方式,实现0℃~99℃范围内的温度智能监测。
              2.可通过LCD实时显示实际温度和预设温度。
              3.当温度超出预设范围时及时报警。

              4. 报警声用电子乐曲或音乐音符实现(选做)。

智能温度监测系统_第1张图片

#include
#include
#define uchar unsigned char
#define uint unsigned int

sbit DQ = P3^6;
sbit BEEP = P3^7;
sbit HI_LED = P1^4;
sbit LO_LED = P1^5;

uchar code DSY_CODE[]= { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};

uchar code df_Table[]= {0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};

char Alarm_Temp_HL[2]={0,99};

uchar CurrentT = 0;
uchar Temp_Value[]= {0x00,0x00};
uchar Dispaly_Digit[] = {0,0,0,0};
bit HI_Alarm = 0,LO_Alarm = 0;
bit DS18B20_IS_OK = 1;
uint Time0_Count = 0 ;

void delay (uint x)
{
	while(--x);
}

uchar Init_DS18B20()
{	
	uchar status;
	DQ = 1;
	delay(8);
	DQ = 0;
	delay(90);
	DQ = 1;
	delay(8);
	status = DQ;
	delay(100);
	DQ = 1;
	return status;
}

uchar ReadOneByte()
{
	uchar i,dat = 0;
	DQ = 1;
	_nop_();
	for(i = 0 ; i < 8 ; i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		_nop_();
		_nop_();
		if(DQ)
		dat |= 0x80;
		delay(30);
		DQ = 1;
	}
	return dat;
}

void WriteOneByte(uchar dat)
{
	uchar i;
	for(i = 0 ; i < 8 ; i++)
	{
		DQ = 0;
		DQ = dat & 0x01;
		delay(5);
		DQ =1;
		dat >>=1;
	}
}

void Read_Temperature()
{
	if(Init_DS18B20() == 1)
	{
		DS18B20_IS_OK = 0;
	}
	else
	{
		WriteOneByte(0xcc);
		WriteOneByte(0x44);
		Init_DS18B20();
		WriteOneByte(0xcc);
		WriteOneByte(0xbe);
		Temp_Value[0]= ReadOneByte();
		Temp_Value[1] = ReadOneByte();
		//Alarm_Temp_HL[0] = ReadOneByte();
		//Alarm_Temp_HL[1] = ReadOneByte();
		DS18B20_IS_OK = 1;
	}
}

void Set_Alarm_Temp_Value()
{
	Init_DS18B20();
	WriteOneByte(0xcc);
	WriteOneByte(0x4e);
	//WriteOneByte(Alarm_Temp_HL[0]);
	//WriteOneByte(Alarm_Temp_HL[1]);
	WriteOneByte(0x7f);
	Init_DS18B20();
	WriteOneByte(0xcc);
	WriteOneByte(0x48);
}

void Display_T()
{
	uchar i;
	uchar t = 150;
	uchar ng = 0,np = 0;
	char Signed_Current_Temp;
	if((Temp_Value[1] & 0xf8) == 0xf8)
	{
		Temp_Value[1] = ~Temp_Value[1];
		Temp_Value[0] = Temp_Value[0] +1;
		if(Temp_Value[0] == 0x00)Temp_Value[1]++;
		ng = 1;
		np = 0xfd;
	}
	
	//Dispaly_Digit[0] = df_Table[Temp_Value[0]&0x0f];
	CurrentT = ((Temp_Value[0] & 0xf0)>>4 ) |((Temp_Value[1] & 0x07) <<4);
	Signed_Current_Temp = ng ? - CurrentT : CurrentT;
	
	if(Signed_Current_Temp >= Alarm_Temp_HL[0])
		HI_Alarm = 0;
	else 
		HI_Alarm = 1;
	
	if(Signed_Current_Temp <= Alarm_Temp_HL[1])
		LO_Alarm = 0;
	else 
		LO_Alarm = 1;
	Dispaly_Digit[3] = CurrentT / 100;
	Dispaly_Digit[2] = ( CurrentT % 100 ) / 10;
	Dispaly_Digit[1] = CurrentT %10;
	
	for(i =  0 ; i < 30 ; i++)
	{
		P0 = 0x39; P2 = 0x7f; delay(t); P2 = 0xff;//c
		P0 = 0x63 ; P2 = 0xbf; delay(t) ; P2 = 0xff;//。\

		P0 = DSY_CODE[Dispaly_Digit[1]] ; P2 = 0xdf;//显示个位
		delay(t);P2 = 0xff;
		P0 =  DSY_CODE[Dispaly_Digit[2]]; P2 = 0xef;//显示十位
		delay(t);P2 = 0xff;
		if(Dispaly_Digit[3] != 0 ){
		P0 =  DSY_CODE[Dispaly_Digit[3]]; P2 = 0xf7;//显示百位
		delay(t);P2 = 0xff;
		}
	}
}

void T0_Int() interrupt 1
{
	TH0 = -1000 / 256;
	TL0 = -1000 % 256;
	BEEP = !BEEP;
	if( ++Time0_Count == 400)
	{
		Time0_Count = 0;
		if(HI_Alarm)
			HI_LED = ~HI_LED ;
		else 
			HI_LED = 1;
		
		if(LO_Alarm)
			LO_LED =  ~LO_LED;
		else 
			LO_LED = 1;
			
	}	
}

void main(void)
{
	IE = 0X82;
	TMOD= 0X01;
	TH0 = -1000 / 256;
	TL0 = -1000 % 256;
	TR0 = 0;
	HI_LED = 1;
	LO_LED = 1;
	Set_Alarm_Temp_Value();
	Read_Temperature();
	delay(50000);
	delay(50000);
	while(1)
	{
		Read_Temperature();
		if(DS18B20_IS_OK)
		{	
			if(HI_Alarm == 1 || LO_Alarm == 1)
				TR0 = 1;
			else 
				TR0 = 0;
			BEEP = 0;
			DisPlay_T();
		}
		else
		{
			P0 = P2 = 0x00;
		}
	}
}


你可能感兴趣的:(单片机89C51)