|
/*****************************************
单4拍正转 zheng[]={0x01,0x08,0x04,0x02}
单4拍反转 fang[]={0x01,0x02,0x04,0x08}
双4拍正转 zheng[]={0x09,0x0c,0x06,0x03}
双4拍反转 fang[]={0x03,0x06,0x0c,0x09}
单双8拍正转 zheng[]={0x01,0x09,0x08,0x0c,0x04,0x06,0x02,0x03}
单双8拍反转 fang[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}
*****************************************/
#include"reg51.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
bit front_move,back_move;
uchar jzaj(void); //单4拍正转 zheng[]={0x01,0x08,0x04,0x02};单4拍反转 fang[]={0x01,0x02,0x04,0x08};
void ajcl(uchar jz);
void delay(uchar del);
uchar code zheng[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};
uchar code fang[] ={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};
void timer0() interrupt 1
{
static uchar jz;
TH0=0xfc;
TL0=0x18;
jz=jzaj();
if(jz)
ajcl(jz);
}
void main()
{
uchar count=0;
TMOD=0x01;
TH0=0xFC;
TL0=0x18;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(front_move)
{
P1=zheng[count];
delay(100);
count++;
if(count==8) count=0;
}
if(back_move)
{
P1=fang[count];
delay(100);
count++;
if(count==8) count=0;
}
}
}
uchar jzaj(void)
{
uchar hz,lz;
P2=0xf0; //置所有行为低电平,行扫描,列线输入(此时)
if((P2&0xf0)!=0xf0) //判断是否有有键按下(读取列的真实状态,若第4列有键按下则P1的值会变成0111 0000),有往下执行
{
delay(10); //延时去抖动(10ms)
if((P2&0xf0)!=0xf0) //再次判断列中是否是干扰信号,不是则向下执行
{
hz=0xfe; //逐行扫描初值(即先扫描第1行)
while((hz&0x10)!=0) //行扫描完成时(即4行已经全部扫描完成)sccode为1110 1111 停止while程序
{
P2=hz; //输出行扫描码
if ((P2&hz)!=hz) //***(P2&0xf0)!=0xf0***也可这样 本行有键按下(即P1(真实的状态)的高四位不全为1)
{
lz=(P2&0xf0)|0x0f; //列
while((P2&0xf0)!=0xf0);
return((~hz)|(~lz)); //返回行和列
break; //有按键返回 提前退出循环
}
else //所扫描的行没有键按下,则扫描下一行,直到4行都扫描,此时sccode值为1110 1111 退出while程序
hz=_crol_(hz,1); //行扫描码左移一位
}
}
}
else
return 0; //无键按下,返回0
}
void ajcl(uchar jz)
{
if(jz==0x11)
{
back_move=0;
front_move=1;
}
if(jz==0x21)
{
front_move=0;
back_move=1;
}
if(jz==0x41)
{
P0=0x00;
front_move=0;
back_move=0;
}
}
void delay(uchar del)
{
uchar i;
for(;del>0;del--)
for(i=0;i<12;i++)
{;}
} |
|