第十五届蓝桥杯单片机省一代码

感悟与总结

这套题,考场上错了2个小细节。第一个dac输出时,因为变量类型的原因,我实际实现的是分段输出,输出的是1,2,3,4,5V这样的结果,没有做到连续输出,解决方法是换变量类型。第二个是存频率的fre开小了,如果频率过33000的话(我考场的板拧到最大也不会溢出,但考虑到fre修正可以是正数,仍有溢出的可能),fre会溢出,解决方法也是换变量类型,把int改为long int就可以。在各个群里交流,我应该只错了这2个点。选择题的话是对了5个,最后出成绩是广东省一前部。

我认为国赛的备考应该更加规范这些细节处理,对常见的这种(如这次的电压连续输出),做到不要错。后续应该会在4t上开几套国赛题测测看,毕竟自己做老是觉得全对,对这种细节很难把控。

修改错误点后自认为的满分代码

#include <STC15F2K60S2.H>
#include "iic.h"
#include "ds1302.h"
#include "intrins.h"

#define control(x,y) P0=y;P2=x;P2=0
typedef unsigned char uchar ;
typedef unsigned int uint ;

code unsigned char Seg_Code[] = 
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xff, //10
0x88, //A11
0x83, //b12
0xc6, //C13
0xa1, //d14
0x86, //E15
0x8e ,//F16
0xbf, //-17
0x8c,//P18
0x89,//H19
0xc7//L20
	
};

uchar Seg_Bit[9]={10,10,10,10,10,10,10,10,10};
uchar interface;
uchar interface1;
uchar interface2;
uchar count_key;

uint count_555;
uchar count_time;
uchar hour,min,sec;
uchar hourm,minm,secm;
long int fre;
uint set_fre=2000;
int jiao_fre=0;
uint jiao_fres;
uint fre_max=0;
uchar L[3];
uchar count_dac;
float dac_data;
bit flag_error;
uchar count_led1;
uchar count_led2;

void Timer0Init(void)		
{

	TMOD |= 0x05;		
	TL0 = 0x00;		
	TH0 = 0x00;		
	TR0 = 1;		
}
void Delay500ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 23;
	j = 205;
	k = 120;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void Timer2Init(void)		
{
	AUXR |= 0x04;		//?????1T??
	T2L = 0x20;		//??????
	T2H = 0xD1;		//??????
	AUXR |= 0x10;		//???2????
	IE2|= 0x04;
	EA=1;
}

void t2int() interrupt 12           //????
{		static Seg_Com=1;
	///ÖÜÆÚË¢ÐÂ
	++count_time;
	++count_key;
	++count_dac;
	++count_led1;
	++count_led2;
///ÊýÂë¹Ü
	control(0xc0,0x00);
	control(0xe0,Seg_Code[Seg_Bit[Seg_Com]]);
	control(0xc0,1<<(Seg_Com-1));
	if(++Seg_Com>8)	Seg_Com=1;	
	
	///led/
	control(0x80,~(L[2]<<1 | L[1]));
	/555//
	if(++count_555==500)
	{count_555=0;
		TR0=0;
		
		fre=((uint)TH0<<8|TL0)*2+jiao_fre;
		TL0 = 0x00;		
		TH0 = 0x00;		
		TR0=1;
	}
}

void System_Init()      
{   
  control(0x80,0xff);
	control(0xa0,0x00);
	
	Set_Time(0x13,0x05,0x59);
		hour=Read_Ds1302_Byte(0x85);
		min=Read_Ds1302_Byte(0x83);
		sec=Read_Ds1302_Byte(0x81);
	Timer0Init();
	Timer2Init();
	Delay500ms();

}

void Data_Task()       
{   
    if(T2H<0xd9)
	{	
		
		if(count_time>150)
	{
		{count_time=0;
			hour=Read_Ds1302_Byte(0x85);
			min=Read_Ds1302_Byte(0x83);
			sec=Read_Ds1302_Byte(0x81);
		}
	}
		else if(count_dac>150)
	{count_dac=0;
		Write_DAC(dac_data*51+0.5);
	}
	
	}
}

void Logic_Task()      
{	//ÎÞЧÅжÏ
		if(fre<0)
		{flag_error=1;
		}
		else
		{flag_error=0;
		}
		//ÎÞЧÅжÏ
	if(!flag_error)
	{
     if(fre_max>=fre)//û¸ü´ó²»»»
		 {fre_max=fre_max;}
		else
		{fre_max=fre;
			hourm=hour;
			minm=min;
			secm=sec;	
		}
	}
		

	
		
dac//
			 
			if(fre<0)
			{dac_data=0;
			}
			 else if(fre<=500)
			 {dac_data=1;
			 }
			 else if(fre>set_fre)
			 {dac_data=5;
			 }
			 else
				 {
			 dac_data=((4/(set_fre*1.0-500))*(fre*1.0-500)+1);

			 }
			
/led
		 if(interface==0)
		 {
			 if(count_led1>=200)
			 {L[1]=!L[1];
				 count_led1=0;
			 }
		 }
		 else
		 {
			 count_led1=0;
			 L[1]=0;
		 }
		 
		 if(flag_error)
		 {
		 L[2]=1;
		 }
		 else
		{
					 if(fre>set_fre)
			 {
				 if(count_led2>=200)
				 {L[2]=!L[2];
					 count_led2=0;
				 }
			 }
			 else
			 {
				 count_led2=0;
				 L[2]=0;
			 }
		}
}

void Display_Task()    
{
    	if(interface==0)//
	{

		Seg_Bit[1]=16;
		Seg_Bit[2]=10;
		Seg_Bit[3]=10;
		if(flag_error)
		{		Seg_Bit[4]=10;
		Seg_Bit[5]=10;
		Seg_Bit[6]=10;
		Seg_Bit[7]=20;
		Seg_Bit[8]=20;
		
		}
		else
		{
		Seg_Bit[4]=(fre>9999)?(fre/10000%10):(10);
		Seg_Bit[5]=(fre>999)?(fre/1000%10):(10);
		Seg_Bit[6]=(fre>99)?(fre/100%10):(10);
		Seg_Bit[7]=(fre>9)?(fre/10%10):(10);
		Seg_Bit[8]=fre%10;
		}

	}
	 	if(interface==1&&interface1==0)//²ÎÊýÉèÖÃ
	{

		Seg_Bit[1]=18;
		Seg_Bit[2]=1;
		Seg_Bit[3]=10;
		Seg_Bit[4]=10;
		Seg_Bit[5]=set_fre/1000;
		Seg_Bit[6]=set_fre/100%10;
		Seg_Bit[7]=set_fre/10%10;
		Seg_Bit[8]=set_fre%10;
	}
		 	if(interface==1&&interface1==1)//
	{

		Seg_Bit[1]=18;
		Seg_Bit[2]=2;
		Seg_Bit[3]=10;
		Seg_Bit[4]=10;
		Seg_Bit[5]=(jiao_fre<0)?(17):(10);
		
		if(jiao_fre<0)
		{jiao_fres=-jiao_fre;}
		else
		{jiao_fres=jiao_fre;
		}
		
		Seg_Bit[6]=(jiao_fres!=0)?(jiao_fres/100%10):(10);
		Seg_Bit[7]=(jiao_fres!=0)?(jiao_fres/10%10):(10);
		Seg_Bit[8]=jiao_fres%10;
	}
	 	if(interface==2)//ʱ¼ä
	{

		Seg_Bit[1]=hour/16;
		Seg_Bit[2]=hour%16;
		Seg_Bit[3]=17;
		Seg_Bit[4]=min/16;
		Seg_Bit[5]=min%16;
		Seg_Bit[6]=17;
		Seg_Bit[7]=sec/16;
		Seg_Bit[8]=sec%16;
	}
	 	if(interface==3&&interface1==0)//»ØÏÔ1
	{

		Seg_Bit[1]=19;
		Seg_Bit[2]=16;
		Seg_Bit[3]=10;
		Seg_Bit[4]=(fre_max>9999)?(fre_max/10000%10):(10);
		Seg_Bit[5]=(fre_max>999)?(fre_max/1000%10):(10);
		Seg_Bit[6]=(fre_max>99)?(fre_max/100%10):(10);
		Seg_Bit[7]=(fre_max>9)?(fre_max/10%10):(10);
		Seg_Bit[8]=fre_max%10;
	}
		 	if(interface==3&&interface1==1)//»ØÏÔ2
	{

		Seg_Bit[1]=19;
		Seg_Bit[2]=11;
		Seg_Bit[3]=hourm/16;
		Seg_Bit[4]=hourm%16;
		
		Seg_Bit[5]=minm/16;
		Seg_Bit[6]=minm%16;
		Seg_Bit[7]=secm/16;
		Seg_Bit[8]=secm%16;
	}
}

uchar Read_Key()
{	static uchar count_press=0;
	static uchar flag_press=0;

	uchar value;
	uchar x=0,y=0;
	
	P32=1;P33=1;P42=0;P44=0;

	if(P32==0) x=1;
	if(P33==0) x=0;	
	
	P32=0;P33=0;P42=1;P44=1;
	if(P42==0) y=2;
	if(P44==0) y=1;
	

	if(y)
	{
		if((++count_press>=8)&&(flag_press==0))
		{	
			count_press=0;
			flag_press=1;
		value=x+y*4;
		}
	}
	
	else
	{ 
		count_press=0;
		flag_press=0;
	}
	
return value;
}


void Key_Task()         
{
uchar key_value=0;
	if(count_key>5)
	{count_key=0;
		key_value=Read_Key();
	}
    if(key_value==4)
		{
			interface1=0;
		if(++interface==4)
		{interface=0;
		}
		
		}
		if(key_value==5)
		{
			interface1=!interface1;
		
		}
				if(key_value==8)
		{
			if(interface==1&&interface1==0)
			{
			set_fre+=1000;
				if(set_fre==10000)
				{set_fre=1000;
				}
			}
		if(interface==1&&interface1==1)
			{
			jiao_fre+=100;
				if(jiao_fre==900)
				{jiao_fre=-900;
				}
			}
		}
		
						if(key_value==9)
		{
			if(interface==1&&interface1==0)
			{
			set_fre-=1000;
				if(set_fre==0)
				{set_fre=9000;
				}
			}
		if(interface==1&&interface1==1)
			{
			jiao_fre-=100;
				if(jiao_fre==-900)
				{jiao_fre=900;
				}
			}
		
		}

}

int main()
{
    System_Init(); 
    
    while(1)
    {
        Data_Task();       
        Logic_Task();
        Display_Task();   
        Key_Task();
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值