蓝桥杯_独立按键(三行代码)

#include <STC15F2K60S2.H>

typedef unsigned char u8;
typedef unsigned int u16;
typedef char s8;
typedef int s16;
typedef unsigned long u32;

typedef struct
{
	u8 b0 : 1;
	u8 b1 : 1;
	u8 b2 : 1;
	u8 b3 : 1;
	u8 b4 : 1;
	u8 b5 : 1;
	u8 b6 : 1;
	u8 b7 : 1;
}bits;

typedef union
{
	u8 hex;
	bits b;
}HexToBin;

HexToBin led_ctrl,buzzer_ctrl;

void Device_Ctrl(u8 position,u8 dat)
{
	P0 = dat;
	P2 = position;
	P2 = 0;
}

void System_Init()
{
	led_ctrl.hex = 0xff;
	Device_Ctrl(0x80,led_ctrl.hex);
	buzzer_ctrl.hex = 0x00;
	Device_Ctrl(0xa0,buzzer_ctrl.hex);
}

void Timer2Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0xFB;		//定时器时钟12T模式
	T2L = 0x18;		//设置定时初值
	T2H = 0xFC;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
	IE2 |= 0x04;
	EA = 1;
}

u8 code SMG_duanxuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
u8 SMG_buf[8];
u8 num;

void SMG_Process()
{
	SMG_buf[0] = SMG_duanxuan[num/100];
	SMG_buf[1] = SMG_duanxuan[num/10%10];
	SMG_buf[2] = SMG_duanxuan[num%10];
	SMG_buf[3] = 0;
	SMG_buf[4] = 0;
	SMG_buf[5] = 0;
	SMG_buf[6] = 0;
	SMG_buf[7] = 0;
}
void SMG_Display()
{
	static u8 i;
	Device_Ctrl(0xc0,0x00);
	Device_Ctrl(0xe0,~SMG_buf[i]);
	Device_Ctrl(0xc0,0x01<<i);
	
	i = (i+1) % 8;
}

u8 cnt_BTN;
u8 Trig;
u8 Cont;
void BTN_Read_ThreeLine()
{
	u8 ReadData = P3 ^ 0xff;
	Trig = ReadData & (ReadData^Cont);
	Cont = ReadData;
}
void BTN_ThreeLine_Process()
{
	if(cnt_BTN >= 10)
	{
		cnt_BTN = 0;
		BTN_Read_ThreeLine();
		if(Trig & 0x01)//S7
		{
			num++;
		}
		if(Trig & 0x02)//S6
		{
			num--;
		}
		if(Trig & 0x04)//S5
		{
			num = num+10;
		}
		if(Trig & 0x08)//S4
		{
			num = num-10;
		}
		
	}
}

void main()
{
	System_Init();
	Timer2Init();
	while(1)
	{
		SMG_Process();
	}
}

void timer2_ISR() interrupt 12   //1毫秒中断
{
	cnt_BTN++;
	SMG_Display();
	BTN_ThreeLine_Process();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值