u010877150 于 2013.06.21 19:31 提问

k4为模式选择有三种模式，按压切换。k1~k3为设置个十百位的按键，按压加一，大9回0。

#include
#include
#include
sbit P2_0=P2^0;

sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
sbit K1=P2^4;

sbit K2=P2^5;
sbit K3=P2^6;
sbit K4=P2^7;
sbit out =P1^5;

sbit P1_3 = P1^3;

unsigned int t,a,b,c,second20,second;
unsigned char s,kc;
unsigned char display_code[13]=

{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,

0xf8,0x80,0x90,0x88,0x83,0xc6

};
/*******延时1ms程序********/
void delay(unsigned int count)
{
unsigned int i,k;
for(i=0;i {
for(k=0;k }
}
/*******延时1us程序********/
void delay_us(unsigned int us)
{
unsigned int i;
us=us*5/4;
for(i=0;i }
/********初始化子程序*************/
void init(void)
{
TMOD=0x01;
TH0=0x3C;
TL0=0X0B;
TF0=0;
TR0=0;
a=0;
b=0;
c=0;
s=10;
second20=0;
second=0;
ET0=1;
EA=1;
}
/******设置键判断程序********/
void key()
{
if(K4==0)
{
kc=(kc+1)%3;
switch(kc)
{
case 0: s=10;
if(P1_3==0)
{ out=0; }
else
{ out=1; }
break;
case 1: s=11;
if(P1_3==0)
out=0;
else TR0=1;
break;
case 2: s=12;
if(P1_3==0)
{
TR0=1;
}
else
{ out=1; }
break;
default: break;
}
}
}
void aj_t()
{
if(K3==0)
{
delay(10);
if(K3==0) a++;
{if(a>=10) a=0;}
}
if(K2==0)
{
delay(10);
if(K2==0) b++;
{if(b>=10) b=0; }

}
if(K1==0)
{
delay(10);
if(K1==0) c++;
{if(c>=10) c=0;}
}
}
void main()
{

init();

K3 = 1;
K2 = 1;
K1 = 1;
K4 = 1;
t=a*100+b*10+c;
key();
aj_t();
while(1)
{

P0=display_code[s];
P2_3=1;
delay(1);
P2_3=0;
delay_us(100);
P0=display_code[a];
P2_2=1;
delay(1);
P2_2=0;
delay_us(100);
P0=display_code[b];
P2_1=1;
delay(1);
P2_1=0;
delay_us(100);
P0=display_code[c];
P2_0=1;
delay(1);
P2_0=0;
}
}
/******中断计时程序********/
void timer1(void) interrupt 1 using 0
{
TH0=0x3C;
TL0=0x0B;
second20++;
if(second20==20)
{
second20=0;
second++;
if(t==second)
{
second=0;
TR0=0;
if(P1_3==0)
out=0;
else
out=1;
}
}
}