#include <REGX52.H>
#define uchar unsigned char
#define uint unsigned int
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit key1=P3^1;
uint bits,ten,hun,t,num;
uchar duanxuan[17]=
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void init()//初始化函数
{
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0x0B0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void delay1ms(void) //延时1ms
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void delay5ms(void) //延时5ms
{
unsigned char a,b;
for(b=19;b>0;b--)
for(a=130;a>0;a--);
}
void DigDisplay(uint num)//位选
{
bits=num%10;
ten=num/10%10;
hun=num/100;
LSA=1;LSB=1;LSC=1;
P0=duanxuan[hun];
delay1ms();
LSA=0;LSB=1;LSC=1;
P0=duanxuan[ten];
delay1ms();
LSA=1;LSB=0;LSC=1;
P0=duanxuan[bits];
delay1ms();
}
void keyscan()
{
if(key1==0)//按下
{
delay5ms();//消抖延时
if(key1==0)
{
TR0=0;//停止
while(!key1)
DigDisplay(num);
}
}
else if(key1==1)
TR0=1;//继续
}
void main()
{
init();
while(1)
{
keyscan();
DigDisplay(num);
}
}
void time0() interrupt 1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
t++;
if(t==2)//1%秒
{
t=0;
num++;
if(num==1000)
num=0;
}
}
【单片机】数码管前三位显示一个跑表,从000到999之间以1%秒速度运行,当按下一个独立键盘时跑表停止,松开手后跑表继续运行
最新推荐文章于 2022-04-05 23:24:34 发布