//程序: //晶振:11.0592MHz #include <AT89X52.h> unsigned char code seg8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; unsigned int number=1970; unsigned int interval=0; unsigned char point=0; //小数点的位置(从左数) sbit LED1=P2^7; sbit LED2=P2^6; sbit LED3=P2^5; sbit LED4=P2^4; //延迟n毫秒 void delay(unsigned int n) { while (n--) { TH2=0xfc; TL2=0x66; //11.0592MHz //TL2=0x18; //12MHz TF2=0; TR2=1; while (TF2==0); TF2=0; } } void display_seg8() { bit display_zero=0; //是否是最高位的0 LED1=1; if (point!=1 && display_zero==0 && number/1000==0) P0=0x00; else { P0=~seg8[number/1000]+(point==1)*0x80; display_zero=1; } delay(3); P0=0x00; LED1=0; LED2=1; if (point!=2 && display_zero==0 && number%1000/100==0) P0=0x00; else { P0=~seg8[number%1000/100]+(point==2)*0x80; display_zero=1; } delay(3); P0=0x00; LED2=0; LED3=1; if (point!=3 && display_zero==0 && number%100/10==0) P0=0x00; else { P0=~seg8[number%100/10]+(point==3)*0x80; display_zero=1; } delay(3); P0=0x00; LED3=0; LED4=1; P0=~seg8[number%10]+(point==4)*0x80; //末尾的0始终显示 delay(3); P0=0x00; LED4=0; } void main() { EA=1; //开总中断 TMOD=0x02; //定时器0为8位自动重装工作方式 //TL0=TH0=0x06; //定时0.25ms TL0=TH0=0x19; //11.0592MHz晶振下定时0.25ms ET0=1; //开定时器中断 TR0=1; //开定时器 T2MOD=0x01; //定时器2工作于16位查询工作方式 TR2=0; P2=0x00; while (1) display_seg8(); } void et0() interrupt 1 { //4次为1ms,4000次为1s interval++; if (interval>=4000) { interval=0; number++; if (number>9999) number=0; } }
|