5届蓝桥杯单片机简易温度采集与控制

main.c

#include "sys.h"
#include "onewire.h"
uint temp;
uchar section = 0;
void Function();
void main()
{
	InitSystem();
	Timer0Init();
	while(1)
	{
		temp = rd_temperature();
		KeyScans();
		Operate_key_val();
		OperatePara();
		Function();
		DisplaySMG();
	}
}

void Function()
{
	if(temp < T_Min)
	{
		section = 0;
		if(k0)
			OperateLed(1,0);
		else
			OperateLed(1,1);
		Operatews(0x10,0);
	}
	else if((temp >= T_Min) && (temp <= T_Max))
	{
		section = 1;
		Operatews(0x10,0);
		if(k1)
			OperateLed(1,0);
		else
			OperateLed(1,1);
	}
	else if(temp > T_Max)
	{
		section = 2;
		Operatews(0x10,1);
		if(k2)
			OperateLed(1,0);
		else
			OperateLed(1,1);
	}
}

sys.c

#include "sys.h"
#include "onewire.h"
void Select_74HC138(uchar channel)
{
	switch(channel)
	{
		case 0: P2 = P2 & 0X1F; break;
		case 4: P2 = (P2 & 0X1F) | 0X80; break;
		case 5: P2 = (P2 & 0X1F) | 0XA0; break;
		case 6: P2 = (P2 & 0X1F) | 0XC0; break;
		case 7: P2 = (P2 & 0X1F) | 0XE0; break;
	}
}

void InitSystem()
{
	Select_74HC138(4);
	P0 = 0xff;
	Select_74HC138(5);
	P0 = 0x00;
	Select_74HC138(0);
}

void Delay(uint ms)
{
	uint i;
	for(ms; ms > 0; ms--)
		for(i = 921; i > 0; i--);
}

void OperateLed(uchar who,status)
{
	static uchar temp;
	uchar zt;
	switch(status)
	{
		case 0:
			temp = temp | _crol_(0x01,who-1);
			P0 = ~temp;
			break;
		case 1:
			if(who == 0xff)
			{
				P0 = 0xff;
				temp = 0;
			}
			else
			{
				zt = (~temp) | _crol_(0x01,who-1);
				if(temp != (~zt))
					temp = ~zt;
				P0 = ~temp;
			}
			break;
		case 2:
			P0 = ~who;
			temp = who;
			break;
		default: break;
	}
	Select_74HC138(4);
	Select_74HC138(0);
}

void Operatews(uchar num,status)
{
	static uchar t;
	if(status)
	{
		t = num;
		P0 = t;
	}
	else
	{
		t = t & (~num);
		P0 = t;
	}
	Select_74HC138(5);
	Select_74HC138(0);
}
uchar t0,t1,t2;
uchar k0,k1,k2;
void Timer0Init(void)		//50毫秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0xB0;		//设置定时初值
	TH0 = 0x3C;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
	EA = 1;
}

void ServiceTimer0() interrupt 1
{
	if(section == 0)
	{
		t0++;
		if(t0 == 16)
		{
			t0 = 0;
			k0 = !k0;
		}
	}	
	else if(section == 1)
	{
		t1++;
		if(t1 == 8)
		{
			t1 = 0;
			k1 = !k1;
		}
	}
	else if(section == 2)
	{
		t2++;
		if(t2 == 4)
		{
			t2 = 0;
			k2 = !k2;
		}
	}
}

nixie.c

#include "sys.h"

uchar code nixie[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar T_Max = 25;
uchar T_Min = 20;
uchar interface = 0;
uchar yi = 11,er = 11,liu = 11,qi = 11;
void DisplayBit(uchar pos,value)
{
	P0 = 0X00;
	Select_74HC138(6);
	Select_74HC138(0);
	
	P0 = nixie[value];
	Select_74HC138(7);
	Select_74HC138(0);
	
	P0 = _crol_(0x01,pos);
	Select_74HC138(6);
	Select_74HC138(0);
	
	Delay(1);
}

void DisplayTemp()
{
	DisplayBit(0,10);
	DisplayBit(1,section);
	DisplayBit(2,10);
	
	DisplayBit(6,temp / 10);
	DisplayBit(7,temp % 10);
}

void DisplayPara()
{
	DisplayBit(0,10);
	DisplayBit(1,yi);
	DisplayBit(2,er);
	
	DisplayBit(5,10);
	DisplayBit(6,liu);
	DisplayBit(7,qi);
}

//void DisplayTemp_p()
//{
//	DisplayBit(0,10);
//	DisplayBit(1,T_Max / 10);
//	DisplayBit(2,T_Max % 10);
//	
//	DisplayBit(5,10);
//	DisplayBit(6,T_Min / 10);
//	DisplayBit(7,T_Min % 10);
//}

void DisplaySMG()
{
	switch(interface)
	{
		case 0: DisplayTemp(); break;
		case 1: DisplayPara(); break;
//		case 2: DisplayTemp_p(); break;
	}
}

key.c

#include "sys.h"

uchar key_val = 20;
uchar key;
uchar bz = 0;
void KeyScans()
{
	uchar hang;
	if(key_val == 20)
	{
		for(hang = 0; hang < 4; hang++)
		{
			P3 = _cror_(0xf7,hang) & 0xff;
			if(l1 == 0)
			{
				Delay(10);
				key_val = hang + 4*1;
				while(l1 == 0)
				{
					DisplaySMG();
				}
			}
			if(l2 == 0)
			{
				Delay(10);
				key_val = hang + 4*2;
				while(l2 == 0)
				{
					DisplaySMG();
				}
			}
			if(l3 == 0)
			{
				Delay(10);
				key_val = hang + 4*3;
				while(l3 == 0)
				{
					DisplaySMG();
				}
			}
			if(l4 == 0)
			{
				Delay(10);
				key_val = hang + 4*4;
				while(l4 == 0)
				{
					DisplaySMG();
				}
			}
		}
	}
	
}

void Operate_key_val()
{
	if(key_val != 20)
	{
		switch(key_val)
		{
			case 4: key = 9; bz++; break;
			case 5: key = 6; bz++; break;
			case 6: key = 3; bz++; break;
			case 7: key = 0; bz++; break;
			case 8:
				interface = (interface + 1) % 2;
				if(interface == 1)
				{
					yi = 11; er = 11; liu = 11; qi = 11;
				}
				
				break;  //设置
			case 9: key = 7; bz++; break;
			case 10: key = 4; bz++; break;
			case 11: key = 1; bz++; break;
			case 12:
				bz = 0;
				yi = 11; er = 11; liu = 11; qi = 11;
				break;   //清除
			case 13: key = 8; bz++; break;
			case 14: key = 5; bz++; break;
			case 15: key = 2; bz++; break;
			default: break;
		}
	}
	key_val = 20;
}

void OperatePara()
{
	if(interface == 1)
	{
		switch(bz)
		{
			case 1: yi = key; break;
			case 2: er = key; break;
			case 3: liu = key; break;
			case 4: qi = key; bz = 0; break;
			default: break;
		}
		
	}
	if((interface == 0) && (yi!=11)&&(er!=11)&&(liu!=11)&&(qi!=11))
	{
		T_Max = yi*10+er;
		T_Min = liu*10+qi;
		if(T_Max < T_Min)
		{
			TR0 = 0;
			k0 = k1 = k2 = 0;
			OperateLed(2,0);
		}
			
		else
		{
			TR0 = 1;
			OperateLed(2,1);
		}
			
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wu小燕呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值