/*头文件区域*/
#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;
}
*/
}
}
51---矩阵按键(Led流水灯)
最新推荐文章于 2025-12-23 11:13:08 发布
4110

被折叠的 条评论
为什么被折叠?



