51---矩阵按键(Led流水灯)

/*头文件区域*/
#include <REGX52.H>
#include <intrins.h>
/*延时函数*/
void Delay(unsigned int xms)	//@12.000MHz
{
	unsigned char data i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

/*变量声明区域*/
unsigned char ucled = 0xfe;
unsigned int time = 500;
unsigned char Key_Val , Key_Down , Key_Up , Key_Old;
bit System_Flag; //默认等于0

/*按键读取函数*/
unsigned char Key_Read()
{
	unsigned char temp = 0;
	//选中第一行,扫描每一列
	P3_0 = 0; P3_1 = 1; P3_2 = 1; P3_3 = 1;
	if(P3_4 == 0) temp = 1;
	if(P3_5 == 0) temp = 2;
	if(P3_6 == 0) temp = 3;
	if(P3_7 == 0) temp = 4;
	
	P3_0 = 1; P3_1 = 0; P3_2 = 1; P3_3 = 1;
	if(P3_4 == 0) temp = 5;
	if(P3_5 == 0) temp = 6;
	if(P3_6 == 0) temp = 7;
	if(P3_7 == 0) temp = 8;
	
	P3_0 = 1; P3_1 = 1; P3_2 = 0; P3_3 = 1;
	if(P3_4 == 0) temp = 9;
	if(P3_5 == 0) temp = 10;
	if(P3_6 == 0) temp = 11;
	if(P3_7 == 0) temp = 12;
	
	P3_0 = 1; P3_1 = 1; P3_2 = 1; P3_3 = 0;
	if(P3_4 == 0) temp = 13;
	if(P3_5 == 0) temp = 14;
	if(P3_6 == 0) temp = 15;
	if(P3_7 == 0) temp = 16;
	return temp;
}

/*Main*/
void main()
{
	while(1)
	{
		Key_Val = Key_Read(); //读取键码值
		Key_Down = Key_Val & (Key_Val ^ Key_Old); //检测下降沿(按键按下的那一瞬间,等于相应按键对应的值)
		Key_Up = ~ Key_Val & (Key_Val ^ Key_Old); //检测上升沿,抬手的瞬间
		Key_Old = Key_Val; //扫描辅助变量(一直按下的值)
		
		if(System_Flag == 1){
			ucled = _crol_(ucled,1);
			P1 = ucled;
			Delay(time);
		}
		
		switch (Key_Down)
		{
			case 1:
				System_Flag = 1;
				break;
			case 2:
				System_Flag = 0;
				P1 = 0xff;
				break;
			case 3:
				time += 100;
				break;
			case 4:
				time -= 100;
				break;
		}
		/*
		if(Key_Down == 1) System_Flag = 1;
		if(Key_Down == 2){
			System_Flag = 0;
			P1 = 0xff;
		}
		*/
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值