#include
#define dataport P0
sbit key_add=P3^3;
sbit key_dec=P3^4;
sbit duan_latch=P2^2;//段锁存
sbit wei_latch=P2^3;//位锁存
unsigned char code duan_ma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0~9显示
unsigned char code wei_ma[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选择
unsigned char temp[8];//临时变量
//函数声明
void delayUs2x(unsigned char t);
void delayMs(unsigned char t);
void display(unsigned char firstbit,unsigned char number);
void Init_timer0(void);
main()
{unsigned int num=0;
key_add=1; //按键输入端口电平置高
key_dec=1;
Init_timer0();
while(1)
{
if(!key_add)
{
delayMs(10);
if(!key_add)
{
while(!key_add)
{if(num<99)
num++;}
}
}
if(!key_dec)
{
delayMs(10);
if(!key_dec)
{
while(!key_dec)
{if(num>0)
num--;}
}
}
temp[0]=duan_ma[num/10];
temp[1]=duan_ma[num%10];
}
}
void delayUs2x(unsigned char t)
{
while(--t);
}
void delayMs(unsigned char t)
{
while(t--)
{
delayUs2x(245);
delayUs2x(245);
}
}
void display(unsigned char firstbit, unsigned char number)
{
static unsigned int i=0;
dataport=0;
duan_latch=1;
duan_latch=0;
dataport=wei_ma[i+firstbit];
wei_latch=1;
wei_latch=0;
dataport=temp[i];
duan_latch=1;
duan_latch=0;
i++;
if(i==number)
i=0;
}
void Init_timer0(void)
{
TMOD|=0x01;
EA=1;
ET0=1;
TR0=1;
}
void timer0_isr(void) interrupt 1
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
display(0,8);
}
连到51开发板上,最后的结果是按一下加号键直接跳到99,然后不变。按减号键就从99跳到00,然后不变。