单片机键盘的应用
扫描二维码
随时随地手机看文章
作业1:数码管前三位数显示一个跑表,从000到999,之间以百分之一秒的速度运行,当按下key1时跑表停止,再次按下时跑表开始
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula =P2^6;
sbit wela = P2^7;
sbit key1 = P3^7;
uchar aa,flag,bai,shi,ge,tt;
uint num,bb;
void init();
void keyscan();
void delay(uint z);
void display(uchar bai,uchar shi,uchar ge);
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
main()
{
init();
P3 = 0xff;
while(1)
{
display(bai,shi,ge);
if(key1==0)//检测是否按下
{
delay(10);
if(key1==0)//确实按下了
{
TR0 = 0;//定时器停止
bb ++;//计算按下的次数
while(!key1)
display(bai,shi,ge);
if(bb %2==0)
{
TH0 =(65536-50000)/256;//装初值
TL0 =(65536-50000)%256;
TR0 = 1;//重启定时器
}
}
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
{
TMOD = 0x01;
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void time0()interrupt 1
{
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
aa++;
if(aa==2)
{
aa = 0;
num ++;
if(num==1000)
num=0;
}
bai = num/100;
shi = num/10%10;
ge = num%10;
}
void display(uchar bai,uchar shi,uchar ge)
{
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x0;//消影操作
dula = 1;
P0 = table[bai];
dula = 0;
tt = 25;
while(tt--);
dula = 1;//关灯操作,高速度扫描时必备
P0 = 0;
dula = 0;
wela = 1 ;
P0 = 0xfd;
wela = 0;
P0= 0x0;//消影操作
dula = 1;
P0 = table[shi];
dula = 0;
tt = 25;
while(tt--);
dula = 1;//关灯操作,高速度扫描时必备
P0 = 0;
dula = 0;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x0;//消影操作
dula = 1;
P0 = table[ge];
dula =0;
tt=25;
while(tt --);
dula = 1;//关灯操作,高速度扫描时必备
P0 = 0;
dula = 0;
}
在上题目的基础上,用另外三个独立键盘实现按下第一个时计数停止,按下第二个时计数开始,按下第三个时计数清零重新开始
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula =P2^6;
sbit wela = P2^7;
sbit key1 = P3^3;
sbit key2 = P3^4;
sbit key3 = P3^5;
sbit key4 = P3^6;
uchar aa,flag,bai,shi,ge,tt;
uint num,bb;
void init();
void keyscan();
void delay(uint z);
void display(uchar bai,uchar shi,uchar ge);
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
main()
{
init();
P3 = 0xff;
while(1)
{
display(bai,shi,ge);
if(key1==0)//检测是否按下
{
delay(10);
if(key1==0)//确实按下了
{
TR0 = 0;//定时器停止
while(!key1)
display(bai,shi,ge);
TR0 = 1;//重启定时器
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
TR0 = 0;//定时器停止
while(!key2)
display(bai,shi,ge);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
TH0=(65536-50000)/256;
TL0 = (65536-50000)%256;
TR0 = 1;//重启定时器
while(!key3)
display(bai,shi,ge);//不加入这句话有很大的bug
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
num = 0;//重新开始计数
while(!key4)
display(bai,shi,ge);
}
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
{
TMOD = 0x01;
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void time0()interrupt 1
{
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
aa++;
if(aa==2)
{
aa = 0;
num ++;
if(num==1000)
num=0;
}
bai = num/100;
shi = num/10%10;
ge = num%10;
}
void display(uchar bai,uchar shi,uchar ge)
{
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x0;//消影操作
dula = 1;
P0 = table[bai];
dula = 0;
tt = 25;
while(tt--);
dula = 1;//关灯操作,高速度扫描时必备
P0 = 0;
dula = 0;
wela = 1 ;
P0 = 0xfd;
wela = 0;
P0= 0x0;//消影操作
dula = 1;
P0 = table[shi];
dula = 0;
tt = 25;
while(tt--);
dula = 1;//关灯操作,高速度扫描时必备
P0 = 0;
dula = 0;
wela = 1;
P0 = 0xfb;
wela = 0;
P0 = 0x0;//消影操作
dula = 1;
P0 = table[ge];
dula =0;
tt=25;
while(tt --);
dula = 1;//关灯操作,高速度扫描时必备
P0 = 0;
dula = 0;
}
作业3:按下16个矩阵键盘依次在数码管上显示1到16的平方
#in