本帖最后由 gdqinci 于 2010-6-2 08:45 编辑
下面这段实验程序我详细分析了,而且在板子上运行的结果也跟预期中的一样,但就是对定时器1的整个运行过程不清楚,比如定时器1为什么只有开启没有关闭??然后又能重复运行?
#include <REG51.H>
#define uchar unsigned char
#define uint unsigned int
//----------------------------------------------------------------------
uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code ACT[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//----------------------------------------------------------------------
uchar status;
sbit OUT=P1^0;
void delay(uint k);
struct time
{
uchar sec;
uchar msec;
};
struct time run_time,set_time;
struct time *pt1,*pt2;
//--------------------
void initial(void)
{
TMOD=0x11;
TH0=-(10000/256);
TL0=-(10000%256);
TH1=-(1000/256);
TL1=-(1000%256);
ET0=1;ET1=1;TR1=1;
EA=1;
}
//-----------------------------
void time0(void)interrupt 1
{
TH0=-(10000/256);
TL0=-(10000%256);
run_time.msec++;
if(run_time.msec>99){run_time.sec++;run_time.msec=0;}
if(run_time.sec>99){run_time.msec++;run_time.sec=0;}
if((pt2->msec>0)||(pt2->sec>0))
{
if((pt1->msec==pt2->msec)&&(pt1->sec==pt2->sec)){TR0=0;OUT=0;}
}
}
//--------------------------------------------------------------
void display(uchar cnt)
{
switch(cnt)
{
case 0:P0=SEG7[run_time.msec%10];P2=ACT[0];break;
case 1:P0=SEG7[run_time.msec/10];P2=ACT[1];break;
case 2:P0=SEG7[run_time.sec%10];P2=ACT[2];break;
case 3:P0=SEG7[run_time.sec/10];P2=ACT[3];break;
case 4:P0=SEG7[set_time.msec%10];P2=ACT[4];break;
case 5:P0=SEG7[set_time.msec/10];P2=ACT[5];break;
case 6:P0=SEG7[set_time.sec%10];P2=ACT[6];break;
case 7:P0=SEG7[set_time.sec/10];P2=ACT[7];break;
default:break;
}
}
//--------------------------------------------------------------
void set_ms_display(uchar cnt)
{
switch(cnt)
{
case 0:P0=SEG7[run_time.msec%10];P2=ACT[0];break;
case 1:P0=SEG7[run_time.msec/10];P2=ACT[1];break;
case 2:P0=SEG7[run_time.sec%10];P2=ACT[2];break;
case 3:P0=SEG7[run_time.sec/10];P2=ACT[3];break;
case 4:P0=SEG7[set_time.msec%10]|0x80;P2=ACT[4];break;
case 5:P0=SEG7[set_time.msec/10]|0x80;P2=ACT[5];break;
case 6:P0=SEG7[set_time.sec%10];P2=ACT[6];break;
case 7:P0=SEG7[set_time.sec/10];P2=ACT[7];break;
default:break;
}
}
//--------------------------------------------------------------
void set_s_display(uchar cnt)
{
switch(cnt)
{
case 0:P0=SEG7[run_time.msec%10];P2=ACT[0];break;
case 1:P0=SEG7[run_time.msec/10];P2=ACT[1];break;
case 2:P0=SEG7[run_time.sec%10];P2=ACT[2];break;
case 3:P0=SEG7[run_time.sec/10];P2=ACT[3];break;
case 4:P0=SEG7[set_time.msec%10];P2=ACT[4];break;
case 5:P0=SEG7[set_time.msec/10];P2=ACT[5];break;
case 6:P0=SEG7[set_time.sec%10]|0x80;P2=ACT[6];break;
case 7:P0=SEG7[set_time.sec/10]|0x80;P2=ACT[7];break;
default:break;
}
}
//--------------------------------------------------
void time1(void) interrupt 3
{
static uchar cnt;
TH1=-(1000/256);
TL1=-(1000%256);
cnt++;
if(cnt>7)cnt=0;
switch(status)
{
case 5:display(cnt);break;
case 0:display(cnt);break;
case 1:set_ms_display(cnt);break;
case 2:set_s_display(cnt);break;
default:break;
}
}
//------------------------------------
void key_s1(void)
{
P3=0xff;
if(P3==0xfb)
{
switch(status)
{
case 1:if(set_time.msec==99)set_time.msec=99;
else set_time.msec++;break;
case 2:if(set_time.sec==99)set_time.sec=99;
else set_time.sec++;break;
default:break;
}
}
}
//------------------------------------------------------
void key_s2(void)
{
P3=0xff;
if(P3==0xf7)
{
switch(status)
{
case 1:if(set_time.msec==0)set_time.msec=0;
else set_time.msec--;break;
case 2:if(set_time.sec==0)set_time.sec=0;
else set_time.sec--;break;
default:break;
}
}
}
//------------------------------------------------------
void key_s4(void)
{
P3=0xff;
if(P3==0xdf){status++;}
if(status==3)status=1;
if(status==6)
{
run_time.msec=0;
run_time.sec=0;
status=0;
}
}
//------------------------------------------
void key_s3(void)
{
P3=0xff;
if(P3==0xef){status=5;TR0=1;}
}
//==================================
void main(void)
{
pt1=&run_time;pt2=&set_time;
initial();
for(;;)
{
key_s1();
key_s2();
key_s3();
key_s4();
delay(300);
}
}
//========================================
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{;}
}
}
//======================== |