按键计数显示
时间:10-02
整理:3721RD
点击:
我想做一个按键计数显示的程序,可是一直不对,下面的代码是我写的,有请大神帮忙。
实现功能:数码管开始显示为0,按键按下计数器加一,并在数码管上显示。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
void delayms(uchar z);
sbit dual1=P1^0;
sbit key1=P1^1;
//sbit duan=P0;
//sbit wei=P2;
uchar num;
uchar duan_table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//段选字段码
uchar wei_table[]={0x00,0x04,0x08,0x0c,
0x10,0x14,0x18,0x1c};//位选字段码
void display(uchar num)//显示子函数
{
uchar shi,ge;
shi=num/10;
ge=num%10;
P2=wei_table[6];
//dual1=0;
P0=duan_table[shi];
//dual1=1;
delayms(500);
P2=wei_table[7];
//dual1=0;
P0=duan_table[ge];
//dual1=1;
delayms(500);
}
void keyscan()//按键检测去抖子函数
{
if(key1==0)
{
delayms(10);
if(key1==0)
{
num++;
while(!key1);
}
}
}
void delayms(uchar z)//延时子函数
{
uchar i,j;
for(i=z;i>0;i--)
for(j=0;j<110;j++);
}
jishu()//计数
{
P0=0xff;
P2=0xff;
TMOD=0x01;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
while(1);
}
void main()//主函数
{
TMOD=0x01; //中断初始化
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
while(1);
{
keyscan();
jishu();
while(1)
{
//keyscan();
display(num);
///delayms(500);
}
}
}
实现功能:数码管开始显示为0,按键按下计数器加一,并在数码管上显示。
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
void delayms(uchar z);
sbit dual1=P1^0;
sbit key1=P1^1;
//sbit duan=P0;
//sbit wei=P2;
uchar num;
uchar duan_table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//段选字段码
uchar wei_table[]={0x00,0x04,0x08,0x0c,
0x10,0x14,0x18,0x1c};//位选字段码
void display(uchar num)//显示子函数
{
uchar shi,ge;
shi=num/10;
ge=num%10;
P2=wei_table[6];
//dual1=0;
P0=duan_table[shi];
//dual1=1;
delayms(500);
P2=wei_table[7];
//dual1=0;
P0=duan_table[ge];
//dual1=1;
delayms(500);
}
void keyscan()//按键检测去抖子函数
{
if(key1==0)
{
delayms(10);
if(key1==0)
{
num++;
while(!key1);
}
}
}
void delayms(uchar z)//延时子函数
{
uchar i,j;
for(i=z;i>0;i--)
for(j=0;j<110;j++);
}
jishu()//计数
{
P0=0xff;
P2=0xff;
TMOD=0x01;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
while(1);
}
void main()//主函数
{
TMOD=0x01; //中断初始化
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
while(1);
{
keyscan();
jishu();
while(1)
{
//keyscan();
display(num);
///delayms(500);
}
}
}