第十一届第二次蓝桥杯单片机组省赛程序代码

#include "reg52.h"
#include "onewire.h"
#include "iic.h"

sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;

sbit L1=P0^0;
sbit L2=P0^1;
sbit L3=P0^2;
sbit L4=P0^3;


unsigned char SMG_stat=0;//数码管状态显示,0表示数据界面,1表示参数界面
int TMAX=30;//温度参数上限
int TMIN=20;//温度参数下限
unsigned int temp_TMAX=30;
unsigned int temp_TMIN=20;
bit k5=0;//0调整下限,1调整上限

unsigned char code SMG_duanma[18]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

void selectHC573(unsigned char channel)
{
	switch(channel)
	{
		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;
		case 0:P2=(P2&0x1f)|0x00;break;
	}
}

void delay(unsigned int t)
{
	while(t--);
}
void delay_key(unsigned char t)
{
	while(t--);
}

//=====================温度提取===============================
unsigned int temper=0;
void DisSMG();
void Read_temper()
{
	unsigned char LSB,MSB;
	DisSMG();
	
	init_ds18b20();
	DisSMG();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	DisSMG();
	
	init_ds18b20();
	DisSMG();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	DisSMG();
	
	temper=MSB;
	temper=(temper<<=8)|LSB;
	temper=temper*0.0625;	
	DisSMG();
}
//===============================================================


//======================数码管显示===================================
void DisSMG_bit(unsigned char dat,unsigned char pos)
{
	selectHC573(6);
	P0=0x01<<pos;
	selectHC573(7);
	P0=dat;
	delay(500);
	selectHC573(0);
}
void DisSMG_all(unsigned char dat)
{
	selectHC573(6);
	P0=0xff;
	selectHC573(7);
	P0=dat;
	delay(300);
	selectHC573(0);
}
void DisSMG()
{
	if(SMG_stat==0)
	{
		DisSMG_bit(0xc6,0);
		DisSMG_bit(SMG_duanma[temper/10],6);
		DisSMG_bit(SMG_duanma[temper%10],7);
	}
	if(SMG_stat==1)
	{
		DisSMG_bit(0x8c,0);
		
		DisSMG_bit(SMG_duanma[TMAX/10],3);
		DisSMG_bit(SMG_duanma[TMAX%10],4);
		
		DisSMG_bit(SMG_duanma[TMIN/10],6);
		DisSMG_bit(SMG_duanma[TMIN%10],7);
	}
	DisSMG_all(0xff);
}
//===============================================================


//======================纯DAC=================================
float DAC=0;
void Read_DAC(unsigned char dat)
{
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x43);
	I2CWaitAck();
	I2CSendByte(dat);
	I2CWaitAck();
	I2CStop();
}
void DAC_out()
{
	if(temper>TMAX)
	{
		Read_DAC(4.0);
	}
	if(temper>=TMIN&&temper<=TMAX)
	{
		Read_DAC(3.0);
	}
	if(temper<TMIN)
	{
		Read_DAC(2.0);
	}
}
//===============================================================


//=======================按键================================
void csan_key()
{
	if(S4==0)
	{
		delay_key(20);
		if(S4==0)
		{
			while(S4==0)
			{
				DisSMG();
			}
			if(SMG_stat==0)
			{
				SMG_stat=1;
				k5=0;
				temp_TMAX=TMAX;
				temp_TMIN=TMIN;
			}
			else if(SMG_stat==1)//参数--温度,需要判断
			{
				if(TMAX>=TMIN)
				{
					SMG_stat=0;
				}
				else
				{
					SMG_stat=1;
					TMAX=temp_TMAX;
					TMIN=temp_TMIN;
				}
			}
		}
	}
	if(S5==0)
	{
		delay_key(20);
		if(S5==0)
		{
			while(S5==0)
			{
				DisSMG();
			}
			if(SMG_stat==1)
			{
				k5=~k5;
			}
		}
	}
	if(S6==0)
	{
		delay_key(20);
		if(S6==0)
		{
			while(S6==0)
			{
				DisSMG();
			}
			if(SMG_stat==1)
			{
				if(k5==0)//调整下限
				{
					TMIN++;
					if(TMIN>99)
					{
						TMIN=99;
					}
				}
				else if (k5==1)//调整上限
				{
					TMAX++;
					if(TMAX>99)
					{
						TMAX=99;
					}
				}
			}		
		}
	}
	if(S7==0)
	{
		delay_key(20);
		if(S7==0)
		{
			while(S7==0)
			{
				DisSMG();
			}
			if(SMG_stat==1)
			{
				if(k5==0)//调整下限
				{
					TMIN--;
					if(TMIN<0)
					{
						TMIN=0;
					}
				}
				else if (k5==1)//调整上限
				{
					TMAX--;
					if(TMAX<0)
					{
						TMAX=0;
					}
				}
			}
		}
	}
}
//===============================================================


//=======================LED===================================
void Running()
{
	selectHC573(4);
	if(temper>TMAX)
	{
		L1=0;
	}
	else 
	{
		L1=1;
	}
	if(temper>=TMIN&&temper<=TMAX)
	{
		L2=0;
	}
	else 
	{
		L2=1;
	}
	if(temper<TMIN)
	{
		L3=0;
	}
	else 
	{
		L3=1;
	}
	if(TMAX<TMIN)
	{
		L4=0;
	}
	else
	{
		L4=1;
	}
	selectHC573(0);
}
//===============================================================

void Init_system()
{
	selectHC573(5);      //控制蜂鸣器与继电器
	P0=0x00;
	selectHC573(4);      //控制LED灯
	P0=0xff;
}

void main()
{
	Init_system();
	while(1)
	{
		Read_temper();
		DisSMG();
		csan_key();
		Running();
		DAC_out();
	}
}

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值