你这个功能必须用到定时器,我给你参考下我临时写的程序。//芯片为stc12c5a60s2,12T模式
#include<reg52.h>
typedef unsigned int uint_16;
typedef unsigned char uint_8;
sbit duan=P2^6;
sbit wei=P2^7;
sbit key = P3^2;
uint_16 num;
uint_8 keytmp;
uint_8 EXT0Flag = 0;
uint_8 code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴数码管
void delay(uint_16 tmp)//延时1ms
{
uint_16 i,j;
for ( i = tmp; i > 0; --i)
for(j = 110; j > 0; --j);
}
void display(uint_16 displaynum)
{
P0=0xff;
wei=1;
P0=0xfb;
wei=0;
duan=1;
P0=table[displaynum / 10];
duan=0;
delay(5);
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
duan=1;
P0=table[displaynum % 10];
duan=0;
delay(5);
}
void SysInit()
{
INT0 = 1;//下降沿有效
EA = 1;
EX0 = 0;
IT0 = 1;
}
void keyInit()
{
if(key == 1)keytmp = key;
}
void main(void)
{
SysInit();
keyInit();
while(1)
{
if (key == 0)
{
if(key != keytmp){EX0 = 1;EXT0Flag = 1;}
}
display(num);
}
}
void EXT0() interrupt 0
{
if(EXT0Flag == 1)num++;
if (num == 100)
{
num = 0;
}
}
|