自己在开发板上运行下面的程序(Frequency:11.0592MHz, MCU: AT89S52)
搞不明白initial字函数是如何实现定时50ms的,大家帮忙看看
T0在方式1下工作,应该是2的16次方才对,怎么会出现50000呢
#include <REG51.H>
#define uchar unsigned char
#define uint unsigned int
uchar code SEG7[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //changed, common anode
uchar code ACT[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uint hour;
uchar min,sec,cnt;
void delay(uint k);
//**********************************
struct deda
{
uint dhour;
uchar dmin;
uchar dsec;
};
struct deda dis_buff;
//***********************************
void initial(void)
{
TMOD=0x01;
TH0=-(50000/256);
TL0=-(50000%256);
ET0=1;
TR0=1;
EA=1;
}
//***********************************
void time0(void) interrupt 1
{
TH0=-(50000/256);
TL0=-(50000%256);
cnt++;
if(cnt>=20){sec++;cnt=0;}
if(sec>=60){min++;sec=0;}
if(min>=60){hour++;min=0;}
if(hour>9999){hour=0;}
dis_buff.dhour=hour;
dis_buff.dmin=min;
dis_buff.dsec=sec;
}
//***********************************
void main(void)
{
initial();
for(;;) // change showing mode from right to left
{
P0=SEG7[dis_buff.dhour%10];
P2=ACT[3];
delay(1);
P0=SEG7[(dis_buff.dhour%100)/10];
P2=ACT[2];
delay(1);
P0=SEG7[(dis_buff.dhour%1000)/100];
P2=ACT[1];
delay(1);
P0=SEG7[dis_buff.dhour/1000];
P2=ACT[0];
delay(1);
P0=SEG7[dis_buff.dsec%10];
P2=ACT[7];
delay(1);
P0=SEG7[dis_buff.dsec/10];
P2=ACT[6];
delay(1);
P0=SEG7[dis_buff.dmin%10];
P2=ACT[5];
delay(1);
P0=SEG7[dis_buff.dmin/10];
P2=ACT[4];
delay(1);
}
}
//***********************************
void delay(uint k)
{
uint data i,j;
for(i=0;i<k;i++){
for(j=0;j<121;j++)
{;}}
} |