#include
#define uint unsigned int
#define uchar unsigned char
typedef unsigned int u16;
typedef unsigned char u8;
#define GPIO_KEY P1
sbit pwm=P0^1;
sbit led=P2^0;
int i=0;
int x=87;
uchar y;
u8 code time[]={40,47,50,55,60,63,71,77,79,82,85,90,95,97,100,103};
void delay(unsigned char i)//延时
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void Time0_Init() //定时器0初始化 发pwm波
{
TMOD|=0x01;
TH0=0xFF;
TL0=0x38;
EA=1;
ET0=1;
TR0=1;
}
void Time1_Init() //定时器1初始化 改变占空比
{
TMOD|=0x10;
TH1=0xD8;
TL1=0xF0;
EA=1;
ET1=1;
TR1=0;
}
void KeyDown(void)//矩阵按键
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//读取按键是否按下
{
delay(10);//消抖
if(GPIO_KEY!=0x0f)//再次确认
{
//测试列
GPIO_KEY=0X0f;
switch(GPIO_KEY)
{
case(0X07): y=0;break;
case(0X0b): y=1;break;
case(0X0d): y=2;break;
case(0X0e): y=3;break;
}
//测试行
GPIO_KEY=0Xf0;
switch(GPIO_KEY)
{
case(0X70): y=y; x=89;TR1=1;break;
case(0Xb0): y=y+4;x=89;TR1=1;break;
case(0Xd0): y=y+8;x=89;TR1=1;break;
case(0Xe0): y=y+12;x=89;TR1=1;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键释放
{
delay(10);
a++;
}
}
}
}
void Time0_Int()interrupt 1 //中断0
{
TH0=0xFF;
TL0=0x38;
i++;
if(i==100)
i=0;
if(i>x)
pwm=1;
else
pwm=0;
led=0;
}
void Time1_Int()interrupt 3 //中断1
{
static int u=0;
TH1=0xD8;
TL1=0xF0;
u++;
if(u==time[y])
{
u=0;
x=87;
TR1=0;
}
}
void main()
{
Time0_Init();
Time1_Init();
while(1)
{
KeyDown();
}
}