蓝桥杯单片机第十三届国赛

代码如下。功能都已经实现,仅供参考,有更好的建议可以提出来

#include "reg52.h"
#include "measuring.h"
#include "read_ne555.h"
#include "selectHC573.h"
#include "iic.h"
#include "adc_dac.h"
#include "eeprom.h"

sfr T2H = 0xd6;
sfr T2L = 0xd7;
sfr AUXR = 0x8e;
sfr IE2 = 0xaf;

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
void Display_Info();
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
    0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

unsigned char code SMG_Dot[10]={0x40,0x79,
    0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned int frequency = 0;
unsigned char stat_5 = 0x00;
unsigned char stat_led = 0xff;
unsigned char count_PWM,PWM_puty = 0;
unsigned char stat_m,count_m = 0;
unsigned char adc_value = 0;
float humidity = 0;
unsigned int humidity_smg = 0;
float adc_volt = 0;
float set_volt = 0;
unsigned char stat_01,count_01,stat_shan = 0;
unsigned char stat_ok,stat_J,count_J,stat_SJ,count_SJ = 0;
unsigned char set_value = 0;
unsigned int distance = 0;
unsigned char UI = 0;//0-频率界面 1-湿度界面 2-测距界面 3-参数界面
unsigned char UI_param = 30;//30-频率参数 31-湿度参数 32-距离参数
unsigned int param_P = 90;//1.5KHz
unsigned char param_S = 40;
unsigned char param_J = 6; //0.5M
unsigned char UI_CM_M = 0; //0-厘米显示 1-米显示
unsigned char UI_Hz_KHz = 0; //0-Hz显示 1-KHz显示
void Init_Timer2()
{
	T2H = (65535 - 10000) / 256;
	T2L = (65535 - 10000) % 256;
	AUXR = AUXR | 0x10;
    IE2 = IE2 | 0x04;
    EA = 1;	
}
void Sevice_timer2() interrupt 12
{
	
    Display_Info();
	count_PWM++;
	count_m++;
	if(stat_01 == 1)
	{
		stat_01 = 0;
		count_01++;
	if(count_01 == 10)
	{
		count_01 = 0;
		if(stat_shan == 0)
		{
			stat_shan = 1;
		}
		else
		{
			stat_shan = 0;
		}	
	}
}
	if(count_PWM == PWM_puty)
	{
		 stat_5 = stat_5 | 0x20;
	}
	if(count_PWM == 100)
	{
		stat_5 = stat_5 & 0xdf;
		count_PWM = 0;
	}
	SelectHC573(5,stat_5);
	if(stat_ok == 1)
	{
		count_SJ++;
		if(count_SJ == 100)
		{
			stat_SJ = 1;
			count_SJ = 0;
		}
	}
	if(count_m == 20)
	{
		stat_m = 1;
		count_m = 0;
	}
}
void Display_Info()
{
	switch(UI)
	{
		case 0:
		if(UI_Hz_KHz == 0)
		{
		if(frequency > 99999)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(2,SMG_NoDot[frequency / 100000]);
			DisplaySMG_Bit(3,SMG_NoDot[frequency / 10000 % 10]);
			DisplaySMG_Bit(4,SMG_NoDot[frequency / 1000 % 10]);
			DisplaySMG_Bit(5,SMG_NoDot[frequency / 100 % 10]);
			DisplaySMG_Bit(6,SMG_NoDot[frequency / 10 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency % 10]);
		}
		else if(frequency > 9999)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(3,SMG_NoDot[frequency / 10000]);
			DisplaySMG_Bit(4,SMG_NoDot[frequency / 1000 % 10]);
			DisplaySMG_Bit(5,SMG_NoDot[frequency / 100 % 10]);
			DisplaySMG_Bit(6,SMG_NoDot[frequency / 10 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency % 10]);
		}
		else if(frequency > 999)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(4,SMG_NoDot[frequency / 1000]);
			DisplaySMG_Bit(5,SMG_NoDot[frequency / 100 % 10]);
			DisplaySMG_Bit(6,SMG_NoDot[frequency / 10 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency % 10]);
		}
		else if(frequency > 99)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(5,SMG_NoDot[frequency / 100]);
			DisplaySMG_Bit(6,SMG_NoDot[frequency / 10 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency % 10]);
		}
		else
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(6,SMG_NoDot[frequency / 10 ]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency % 10]);
		}
	}
		else
		{
			if(frequency > 99999)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(4,SMG_NoDot[frequency / 100000]);
			DisplaySMG_Bit(5,SMG_NoDot[frequency / 10000 % 10]);
			DisplaySMG_Bit(6,SMG_Dot[frequency / 1000 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency / 100 % 10]);
		}
		else if(frequency > 9999)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(5,SMG_NoDot[frequency / 10000]);
			DisplaySMG_Bit(6,SMG_Dot[frequency / 1000 % 10]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency / 100 % 10]);

		}
		else if(frequency > 999)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(6,SMG_Dot[frequency / 1000]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency / 100 % 10]);
		}
		else if(frequency > 99)
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(6,SMG_Dot[0]);
			DisplaySMG_Bit(7,SMG_NoDot[frequency / 100]);
		}
		else
		{
			DisplaySMG_Bit(0,0x8e);
			DisplaySMG_Bit(6,SMG_Dot[0 ]);
			DisplaySMG_Bit(7,SMG_NoDot[0]);
		}
	}
	
		break;
		case 1:
		DisplaySMG_Bit(0,0x89);
		DisplaySMG_Bit(6,SMG_NoDot[humidity_smg / 10]);
		DisplaySMG_Bit(7,SMG_NoDot[humidity_smg % 10]);
		break;
		case 2:
		if(UI_CM_M == 0)
		{
	    if(distance > 99)
		{
			DisplaySMG_Bit(0,0x88);
			DisplaySMG_Bit(5,SMG_NoDot[distance / 100]);
			DisplaySMG_Bit(6,SMG_NoDot[distance / 10 %10]);
			DisplaySMG_Bit(7,SMG_NoDot[distance % 10]);
		}
		else
		{
			DisplaySMG_Bit(0,0x88);
			DisplaySMG_Bit(6,SMG_NoDot[distance / 10]);
			DisplaySMG_Bit(7,SMG_NoDot[distance % 10]);
		}
	}
		else
		{
			if(distance > 99)
		{
			DisplaySMG_Bit(0,0x88);
			DisplaySMG_Bit(5,SMG_Dot[distance / 100]);
			DisplaySMG_Bit(6,SMG_NoDot[distance / 10 %10]);
			DisplaySMG_Bit(7,SMG_NoDot[distance % 10]);
		}
		else
		{
			DisplaySMG_Bit(0,0x88);
			DisplaySMG_Bit(5,SMG_Dot[0]);
			DisplaySMG_Bit(6,SMG_NoDot[distance / 10]);
			DisplaySMG_Bit(7,SMG_NoDot[distance % 10]);
		}
		}
		break;
		case 3:
		if(UI_param == 30)
		{			
			DisplaySMG_Bit(0,0x8c);
			DisplaySMG_Bit(1,SMG_NoDot[1]);
			if(param_P <= 99)
			{
				DisplaySMG_Bit(6,SMG_Dot[param_P / 10]);
				DisplaySMG_Bit(7,SMG_NoDot[param_P % 10]);
			}
			else
			{
				DisplaySMG_Bit(5,SMG_NoDot[param_P / 100]);
				DisplaySMG_Bit(6,SMG_Dot[param_P / 10 % 10]);
				DisplaySMG_Bit(7,SMG_NoDot[param_P % 10]);
			}
		}
		else if(UI_param == 31)
		{
			DisplaySMG_Bit(0,0x8c);
			DisplaySMG_Bit(1,SMG_NoDot[2]);
			DisplaySMG_Bit(6,SMG_NoDot[param_S / 10]);
			DisplaySMG_Bit(7,SMG_NoDot[param_S % 10]);
		}
		else
		{
			DisplaySMG_Bit(0,0x8c);
			DisplaySMG_Bit(1,SMG_NoDot[3]);
			DisplaySMG_Bit(6,SMG_Dot[param_J / 10]);
			DisplaySMG_Bit(7,SMG_NoDot[param_J % 10]);
		}
		break;
}
}
void read_data_deal()
{
	if(stat_m == 1)
	{
		stat_m = 0;
		distance = Ut_Wave_data();
	}
	frequency = return_frequency();
	adc_value = read_pcf8591_adc();
	adc_volt = adc_value * (5.0 / 255);
	humidity = adc_volt * 20;
	humidity_smg = humidity;
	if(humidity_smg < param_S)
	{
		set_volt = 1.0;
	}
	else if(humidity_smg < 80)
	{
		set_volt =(float)(4 / (80 - param_S))* humidity_smg + (1-(float)((4*param_S)/(float)(80-param_S)));
	}
	else
	{
		set_volt = 5.0;
	}
	set_value = set_volt/(float)(5.0/255);
	Set_pcf8591_dac(set_value);
	if(distance > (param_J*10))
	{
		stat_5 = stat_5 | 0x10;
		SelectHC573(5,stat_5);
		if(stat_J == 0)
		{
			count_J = count_J + 1;
			stat_J = 1;
		}
		
	}
	else
	{
		stat_5 = stat_5 & 0xef;
		SelectHC573(5,stat_5);
		if(stat_J == 1)
		{
			count_J = count_J + 1;
			stat_J = 0;
		}
	}
	write_eeprom(0x00,count_J);
	if(frequency > (param_P * 100))
	{
		PWM_puty = 80;
	}
	else
	{
		PWM_puty = 20;
	}
	if(UI == 0)
	{
		stat_led = 0xfe;
		SelectHC573(4,stat_led);
	}
	else if(UI == 1)
	{
		stat_led = 0xfd;
		SelectHC573(4,stat_led);
	}
	else if(UI == 2)
	{
		stat_led = 0xfb;
		SelectHC573(4,stat_led);
	}
	else
	{
		stat_01 = 1;
		if(UI_param == 30)
		{
			if(stat_shan == 1)
			{
				stat_led = 0xfe;
				SelectHC573(4,stat_led);
			}
			else
			{
				stat_led = 0xff;
				SelectHC573(4,stat_led);
			}
		}
		else if(UI_param == 31)
		{
			if(stat_shan == 1)
			{
				stat_led = 0xfd;
				SelectHC573(4,stat_led);
			}
			else
			{
				stat_led = 0xff;
				SelectHC573(4,stat_led);
			}
		}
		else 
		{
			if(stat_shan == 1)
			{
				stat_led = 0xfb;
				SelectHC573(4,stat_led);
			}
			else
			{
				stat_led = 0xff;
				SelectHC573(4,stat_led);
			}
		}
	}
	if(frequency > (param_P * 100))
	{
		stat_led = stat_led & 0xf7;
		SelectHC573(4,stat_led);
	}
	else
	{
		stat_led = stat_led | 0x08;
		SelectHC573(4,stat_led);
	}
	if(distance > (param_J*10))
	{
		stat_led = stat_led & 0xdf;
		SelectHC573(4,stat_led);
	}
	else
	{
		stat_led = stat_led | 0x20;
		SelectHC573(4,stat_led);
	}
	if(humidity_smg > param_S)
	{
		stat_led = stat_led & 0xef;
		SelectHC573(4,stat_led);
	}
	else
	{
		stat_led = stat_led | 0x10;
		SelectHC573(4,stat_led);
	}
}
void scan_key()
{
	if(S4 == 0)
	{
		void Delay20ms();
		if(S4 == 0)
		{
			if(UI == 0)
			{
				UI = 1;
			}
			else if(UI == 1)
			{
				UI = 2;
				UI_param = 30;
			}
			else if(UI == 2)
			{
				UI = 3;
			}
			else 
			{
				UI = 0;
			}
			while(S4 == 0)
			{
				Display_Info();
			}
		}
	}
	else if(S5 == 0)
	{
		Delay20ms();
		if(S5 == 0)
		{
			if(UI == 3)
			{
				if(UI_param == 30)
				{
					UI_param = 31;
				}
				else if(UI_param == 31)
				{
					UI_param = 32;
				}
				else
				{
					UI_param = 30;
				}
			}
			while(S5 == 0)
			{
				Display_Info();
			}
		}
	}
	if(S6 == 0)
	{
		Delay20ms();
		if(S6 == 0)
		{
			if((UI == 3) && (UI_param == 30))
			{
				if(param_P == 120)
				{
					param_P = 10;
				}
				else
				{
					param_P = param_P + 5;
				}
			}
			else if((UI == 3) && (UI_param == 31))
			{
				if(param_S == 60)
				{
					param_S = 10;
				}
				else
				{
					param_S = param_S + 10;
				}
				
			}
			else if((UI == 3) && (UI_param == 32))
			{
				if(param_J == 12)
				{
					param_J = 1;
				}
				else
				{
					param_J = param_J + 1;
				}
				
			}
			if(UI == 2)
			{
				if(UI_CM_M == 0)
				{
					UI_CM_M = 1;
				}
				else
				{
					UI_CM_M = 0;
				}
			}
			while(S6 == 0)
			{
				Display_Info();
			}
		}
	}
	if(S7 == 0)
	{
		Delay20ms();
		if(S7 == 0)
		{
			if((UI == 3) && (UI_param == 30))
			{
				if(param_P == 10)
				{
					param_P = 120;
				}
				else
				{
					param_P = param_P - 5;
				}
			}
			else if((UI == 3) && (UI_param == 31))
			{
				if(param_S == 10)
				{
					param_S = 60;
				}
				else
				{
					param_S = param_S - 10;
				}
				
			}
			else if((UI == 3) && (UI_param == 32))
			{
				if(param_J == 1)
				{
					param_J = 12;
				}
				else
				{
					param_J = param_J - 1;
				}
				
			}
			if(UI == 0)
			{
				if(UI_Hz_KHz == 0)
				{
					UI_Hz_KHz = 1;
				}
				else
				{
					UI_Hz_KHz = 0;
				}
			}
			stat_ok = 1;
			while(S7 == 0)
			{
				Display_Info();
				if(UI == 1)
				{
					stat_ok = 1;
					if(stat_SJ == 1)
					{
						count_J = 0;
						write_eeprom(0x00,count_J);
						stat_SJ = 0;
					}
				}
			}
			stat_ok = 0;
		}
	}
}

void InitSystem()
{
	SelectHC573(0,0x00);
	SelectHC573(4,0xff);
	SelectHC573(5,0x00);
	DisplaySMG_All(0xff);
	Init_Timer2();
	Init_timer0_timer1();
}
void main()
{
	InitSystem();
	while(1)
	{
		scan_key();
		read_data_deal();
//		Display_Info();
		scan_key();
	}
}

selectHC573.c

#include "reg52.h"
void SelectHC573(unsigned char channel,unsigned char dat)
{
	P2 = (P2 & 0x1f) | 0x00;
	P0 = dat;
	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;
	}
	P2 = (P2 & 0x1f) | 0x00;
}
void DelaySMG(unsigned int t)
{
	while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char value)
{
	SelectHC573(6,0x01 << pos);
	SelectHC573(7,value);
	DelaySMG(500);
	SelectHC573(6,0x01 << pos);
	SelectHC573(7,0xff);
}
void DisplaySMG_All(unsigned char value)
{
	SelectHC573(6,0xff);
	SelectHC573(7,value);
}

adc_dac.c

#include "reg52.h"
#include "iic.h"
unsigned char volt_value;
unsigned char read_pcf8591_adc()
{
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x43);
	I2CWaitAck();
	I2CStop();
	
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();
	volt_value = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	return volt_value;
}

void Set_pcf8591_dac(unsigned char volt_value)
{
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x43);
	I2CWaitAck();
	I2CSendByte(volt_value);
	I2CWaitAck();
	I2CStop();
}

需完整代码可私信

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值