独立按键
#include<reg52.h>
sbit LED=P0^1;
sbit aa=P1^1;
sbit bb=P1^2;
sbit cc=P1^3;
sbit input=P3^6;
sbit output=P3^7;
void delay()
{
unsigned char i,j;
for(i=1000;i>0;i--)
for(j=0;j<100;j++)
;
}
void main()
{
aa=0;bb=0;cc=0;
input=1;
output=0;
LED=1;
while(1)
{
if(input==0)
{
LED=0;
delay();
LED=1;
}
}
}
矩阵键盘
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char NUM[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit aa=P1^1;
sbit bb=P1^2;
sbit cc=P1^3;
sbit dula=P1^0;
uchar temp;
void delay(uint x)
{
uint i;
uchar j;
for(i=0;i<x;i++)
for(j=0;j<110;j++)
;
}
void main()
{
aa=0;bb=0;cc=0;
while(1)
{
P0=0x00;
P2=0xfe;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:
P0=NUM[0];delay(1000);break;
case 0xd0:
P0=NUM[1];delay(1000);break;
case 0xb0:
P0=NUM[2];delay(1000);break;
case 0x70:
P0=NUM[3];delay(1000);break;
}
}
}
P0=0x00;
P2=0xfd;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:
P0=NUM[4];delay(1000);break;
case 0xd0:
P0=NUM[5];delay(1000);break;
case 0xb0:
P0=NUM[6];delay(1000);break;
case 0x70:
P0=NUM[7];delay(1000);break;
}
}
}
P0=0x00;
P2=0xfb;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:
P0=NUM[8];delay(1000);break;
case 0xd0:
P0=NUM[9];delay(1000);break;
case 0xb0:
P0=NUM[10];delay(1000);break;
case 0x70:
P0=NUM[11];delay(1000);break;
}
}
}
P0=0x00;
P2=0xf7;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0:
P0=NUM[12];delay(1000);break;
case 0xd0:
P0=NUM[13];delay(1000);break;
case 0xb0:
P0=NUM[14];delay(1000);break;
case 0x70:
P0=NUM[15];delay(1000);break;
}
}
}
}
}