我之前写了个只改变一个LED的
#include <reg52.h>
#include <intrins.h>
typedef unsigned char u8;
typedef unsigned int u16;
#define led P2
#define GPIO_KEY P1
#define GPIO_DIG P2
u8 a;
u8 KeyValue;
u8 LED[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(u16 i)
{
while (i--);
}
void KeyDown()
{
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)
{
delay(10000);
if(GPIO_KEY!=0x0f)
{
GPIO_KEY=0x0f;
switch(GPIO_KEY)
{
case 0x07:
KeyValue=0;break;
case 0x0b:
KeyValue=1;break;
case 0x0d:
KeyValue=2;break;
case 0x0e:
KeyValue=3;break;
}
GPIO_KEY=0xf0;
switch(GPIO_KEY)
{
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)&&(GPIO_KEY!=0xf70))
{
delay(1000);
a++;
}
}
}
}
void keyspros()
{
if(GPIO_KEY==0)
{
delay(1000);
if(GPIO_KEY==0)
{
led=~led;
}
while (!GPIO_KEY);
}
}
void main()
{
while(1)
{
KeyDown();
GPIO_DIG=LED[KeyValue];
keyspros();
}
}
|