这个是不带定时器的,需要带定时器的请看上一个文章
四位数码管,应用定时器中断,初始值显示00.00前面两位为秒,S1按下启动记时,S2按下停止,S3按下清零。-CSDN博客
#include<reg52.h>
unsigned char code SEGMENT_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit BIT_P10=P1^0;
sbit BIT_P11=P1^1;
sbit BIT_P12=P1^2;
sbit BIT_P13=P1^3;
sbit LED=P2^0;
unsigned char micro_second=0;
unsigned int second=0;
unsigned int ccc=0;
void delay_ms(unsigned int x)
{
unsigned int i=0;
unsigned int j=0;
for(i=0;i<x;i++)
{
for(j=0;j<100;j++)
{
}
}
}
void display()
{
unsigned char w1=0;
unsigned char w2=0;
unsigned char w3=0;
unsigned char w4=0;
w4=(micro_second/10);
w3=micro_second%10;
w1=(second%100)/10;
w2=(second%10);
P0=SEGMENT_CODE[w1];
BIT_P10=0;
BIT_P11=1;
BIT_P12=1;
BIT_P13=1;
delay_ms(5);
BIT_P10=1;
P0=SEGMENT_CODE[w2]|0x80;
BIT_P10=1;
BIT_P11=0;
BIT_P12=1;
BIT_P13=1;
delay_ms(5);
BIT_P11=1;
P0=SEGMENT_CODE[w3];
BIT_P10=1;
BIT_P11=1;
BIT_P12=0;
BIT_P13=1;
delay_ms(5);
BIT_P12=1;
P0=SEGMENT_CODE[w4];
BIT_P10=1;
BIT_P11=1;
BIT_P12=1;
BIT_P13=0;
delay_ms(5);
BIT_P13=1;
}
void main()
{
TMOD=0X01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
TMOD|=0X01;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
TR1=1;
while(1)
{
display();
}
}
void TIMER() interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
micro_second++;
if(micro_second>99)
{
micro_second=0;
second++;
}
}
void TIMER1() interrupt 3
{
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
ccc++;
if(ccc>=2)
{
ccc=0;
LED=~LED;
}
}