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

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

蓝桥杯单片机第十一届国赛_第1张图片蓝桥杯单片机第十一届国赛_第2张图片
蓝桥杯单片机第十一届国赛_第3张图片

  • 总结:别被唬住了兄弟们,我一看题目这么少就想草草了事,没想到啊!大意了!
  • 做完以后压根不想总结了,我看着我的逻辑我都想吐,乱死了。
  • 命题老师您是真的牛! 得三刷都不过分。
  • 我仅仅把我的程序分享出来,过两天可能删掉,但会做一期总结,内容说实话不建议大家全看,根据自己需求,找找里面的优点,因为我写的真的是很菜。
#include "bsp_iic.h"
#include "bsp_init.h"
#include "bsp_key.h"
#include "bsp_led.h"
#include "bsp_turn.h"
#include "sys_timer.h"
#include "stdio.h"
//*****************************函数声明
void seg_pricedure();
void key_pricedure();
void per_pricedure();
//*****************************全局变量
/*按键*/
unsigned char key_number,Key_old,key_change;
/*显示*/
unsigned char seg_turn[8];
unsigned char seg_buf[10];
unsigned char seg_temp;
/*减速*/
unsigned char key_slow_down;
unsigned int seg_slow_down;
unsigned int per_slow_down;
/*外设*/
unsigned char ucled;
unsigned char init_password_number[6]; //初始密码
unsigned int write_password_number[6]; //输入密码
unsigned char password_temp = 0;  //记录数组元素
/*标志位*/
unsigned char running_mode = 0x00; //高四位 0——不输入 1——输入密码模式   第四位 0——正在输入 1——密码输入错误 2——密码输入正确 3——重置模式
/*滴答定时*/
unsigned long dida_number;
unsigned long led_5s_count;
unsigned char temp;
void main()
{
     
	timer0_init();
	close_peripheral();
	
	eeprom_read_fun(init_password_number,0,6);
	while(1)
	{
     
		seg_pricedure();
		key_pricedure();
		per_pricedure();
	}
}
//*****************************中断
void timer0() interrupt 1
{
     
	dida_number++;
	if(++key_slow_down == 20) key_slow_down = 0;
	if(++seg_slow_down == 200) seg_slow_down = 0;
	if(++per_slow_down == 100) per_slow_down = 0;

	seg_disp_fun(seg_turn,seg_temp);
	if(++seg_temp == 8) seg_temp = 0;
	
	led_disp(ucled);
	
	if(((running_mode == 0x11)|(running_mode == 0x12)) && (led_5s_count == 0))
	{
     
		led_5s_count = dida_number;
	}
	if(((dida_number - led_5s_count) >= 5000) && ((running_mode == 0x11)|(running_mode == 0x12)))
	{
     
		if(running_mode == 0x11)
		{
     
			led_5s_count = 0;
			ucled &= 0xfe;        //L1熄灭
			running_mode = 0x10;  //恢复输入模式
			password_temp = 0;
		}
		else
		{
     
			led_5s_count = 0;
			running_mode = 0;
			open_jdq_fun(0);
		}
	}
}
//*****************************显示
void seg_pricedure()
{
     
	if(seg_slow_down) return; 
	seg_slow_down = 1;
	if(running_mode & 0x10) //输入模式
	{
     
		switch(password_temp)
		{
     
			case 0:
				sprintf(seg_buf,"-       ");
				break;
			case 1:
				sprintf(seg_buf,"-      %d",write_password_number[0]);
				break;
			case 2:
				sprintf(seg_buf,"-     %d%d",write_password_number[1],write_password_number[0]);
				break;
			case 3:
				sprintf(seg_buf,"-    %d%d%d",write_password_number[2],write_password_number[1],write_password_number[0]);
				break;
			case 4:
				sprintf(seg_buf,"-   %d%d%d%d",write_password_number[3],write_password_number[2],write_password_number[1],write_password_number[0]);
				break;
			case 5:
				sprintf(seg_buf,"-   %d%d%d%d",write_password_number[3],write_password_number[2],write_password_number[1],write_password_number[0]);
				break;
			case 6:
				sprintf(seg_buf,"-   %d%d%d%d",write_password_number[3],write_password_number[2],write_password_number[1],write_password_number[0]);
				break;
		}
		if(password_temp == 5) 
		{
     
			sprintf (&seg_buf[3],"%d",write_password_number[4]);
		}
		else if(password_temp == 6) 
		{
     
			sprintf(&seg_buf[2],"%d",write_password_number[5]);
			sprintf(&seg_buf[3],"%d",write_password_number[4]);
		}
		
		if(running_mode == 0x13)  
		seg_buf[0] = 'C';
 		else if(running_mode == 0x12) //输入正确
		sprintf(seg_buf,"0   0PEN");
	}
	else 
		sprintf(seg_buf,"        ");
	seg_turn_fun(seg_turn,seg_buf);	
}

//*****************************按键
void key_pricedure()
{
     
	if(key_slow_down) return; 
	key_slow_down = 1;
	
	key_number = key_trigger();
	key_change = (key_number ^ Key_old) & key_number;
	Key_old = key_number;
	switch(key_change)
	{
     
		case 16:  //输入密码模式
			running_mode |= 0x10; //输入密码模式;
			break;
	}
	if(running_mode == 0x12) //输入正确
	{
     
		if(key_change == 12)
		{
     	
			led_5s_count = 0;
			running_mode = 0x13; //修改模式
		}
	}
	if((running_mode == 0x10) | (running_mode == 0x13))
	{
     
		switch(key_change)
		{
     
			case 8: //清楚
				password_temp = 0;
				break;
			case 7: //0
				write_password_number[password_temp] = 0;
				password_temp++;
				break;
			case 11: //1
				write_password_number[password_temp] = 1;
				password_temp++;
				break;
			case 15: //2
				write_password_number[password_temp] = 2;
				password_temp++;
				break;
			case 19: //3
				write_password_number[password_temp] = 3;
				password_temp++;
				break;
			case 6: //4
				write_password_number[password_temp] = 4;
				password_temp++;
				break;
			case 10: //5
				write_password_number[password_temp] = 5;
				password_temp++;
				break;
			case 14: //6
				write_password_number[password_temp] = 6;
				password_temp++;
				break;
			case 18: //7
				write_password_number[password_temp] = 7;
				password_temp++;
				break;	
			case 5: //8
				write_password_number[password_temp] = 8;
				password_temp++;
				break;
			case 9: //9
				write_password_number[password_temp] = 9;
				password_temp++;
				break;				
		}
	}
	
	if(password_temp == 6)  //输入完成6个数
	{
     
		if(running_mode == 0x10)
		{
     
			if((write_password_number[0] == init_password_number[0]) && (write_password_number[1] == init_password_number[1]) &&
			   (write_password_number[2] == init_password_number[2]) && (write_password_number[3] == init_password_number[3]) &&
			   (write_password_number[4] == init_password_number[4]) && (write_password_number[5] == init_password_number[5]))
			{
     
				running_mode = 0x12;  //密码输入正确
				open_jdq_fun(1);
				password_temp = 0;
			}
			else
			{
     
				ucled |= 0x01; //密码输入错误 led 1亮5s
				running_mode = 0x11;
				open_jdq_fun(0);
			}
		}
		else if(running_mode == 0x13)
		{
     
			unsigned char i;
			for(i=0;i<6;i++)
			init_password_number[i] = write_password_number[i];
			running_mode = 0x12;  //密码输入正确
			open_jdq_fun(1);
			password_temp = 0;
			eeprom_write_fun(init_password_number,0,6);
		}
	}	
}

//*****************************外设
void per_pricedure()
{
     
	if(per_slow_down) return; 
	per_slow_down = 1;
	
	if(running_mode == 0x10) ucled |= 0x40; 
	else ucled &= ~(0x40);
	
	if(running_mode == 0x13) ucled |= 0x80; 
	else ucled &= ~(0x80);
}

底层这里就不展示了,主要是能从中有收获就行。
蓝桥杯单片机第十一届国赛_第4张图片

你可能感兴趣的:(蓝桥杯国赛,单片机)