STC89C52单片机实验 DAY3 独立按键&&矩阵按键

独立按键控制LED

#include <reg52.h>
sbit led=P2^0;
sbit k1=P3^1;
void delay(int i)
{
	while(i--);
}
void keypros()
{
	if(k1==0)   
	{
		delay(1000);
		if(k1==0)
			led=~led;
		while(!k1);
	}
}
void main()
{ 
	while(1){
		keypros();
	}
}

矩阵按键

#include<reg51.h>
#include<instrins.h>
typedef unsigned char u8;
typedef unsigned int u16;

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

#define GPIO_KEY P1
#define GPIO_DIG P0

u8 code smgduan[]={ 0x3f  , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d,
					0x7d , 0x07 , 0x7f  , 0x6f , 0x77 , 0x7c,
					0x39 , 0x5e , 0x79 , 0x71 , 0x00};

void delay(int i)
{
	while(i--);
}
u8 KeyValue;

void KeyDown()
{
	u8 a;
	P1=0x0f;
	if(P1!=0x0f){
		delay(1000);
		if(P1!=0x0f)
		{
			P1=0x0f;
			switch(P1)
			{
				case(0x07):  KeyValue=0;break;
				case(0x0b):  KeyValue=1;break;
				case(0x0d):  KeyValue=2;break;
				case(0x0e):  KeyValue=3;break;
			}
			P1=0xf0;
			switch(P1)
			{
				case(0x70):  KeyValue=KeyValue;break;
				case(0xb0):  KeyValue=KeyValue+4;break;
				case(0xd0):  KeyValue=KeyValue+8;break;
				case(0xe0):  KeyValue=KeyValue+12;break;
			}
			//while((a<50) && (P1!=0xf0))
			//{
				//delay(1000);
				//a++;
			//}
		}
	}
}
int main()
{
	int i;
	LSA=0;
	LSB=0;
	LSC=0;
	while(1){
		KeyDown();
		P0=smgduan[KeyValue];
	}
}
发布了33 篇原创文章 · 获赞 46 · 访问量 5934

猜你喜欢

转载自blog.csdn.net/matafeiyanll/article/details/104068251