51-小程序 利用数码管制作秒表

 秒表计时,计时单位精确到0.01s,计时达到60s时置零继续重新计时。

#include 

unsigned int second,msecond;

#define DataPort P2

sbit LATCH1 = P1^0;
sbit LATCH2 = P1^1;


unsigned char code Seg_mod[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 显示段码值0~9
unsigned char code Bit_mod[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//分别对应相应的数码管点亮,即位码
unsigned char TempData[8]; //存储显示值的全局变量
void Display(unsigned char FirstBit,unsigned char Num);

void DelayUs2x(unsigned char t)
{   
 while(--t);
}

void Init_Timer0()
{
	TMOD |= 0x01;
	
	TH0 = (65536-1000)/256;
	TL0 = (65536-1000)%256;
	
	EA = 1;
	ET0 = 1;
	TR0 = 1;
}

void T0_ISR(void) interrupt 1
{
	static unsigned char i;
	
	TH0 = (65536-1000)/256;
	TL0 = (65536-1000)%256;
	
	Display(0,4);
	
	
	i++;
		
	if(i == 10)
	{
		i = 0;
		
		msecond++;
		if(msecond == 100)
		{
			msecond = 0;
			second++;
			if(second == 60)
			{
				second = 0;
			}
		}
	}
}

void Display(unsigned char FirstBit,unsigned char Num)
{
      static unsigned char i;
	  
	   DataPort=0;   //清空数据,防止有交替重影
       LATCH1=1;     //段锁存
       LATCH1=0;

       DataPort=Bit_mod[i+FirstBit]; //取位码 
       LATCH2=1;     //位锁存
       LATCH2=0;

       DataPort=TempData[i]; //取显示数据,段码
       LATCH1=1;     //段锁存
       LATCH1=0;
     
       i++;
       if(i == Num)
       {
          i = 0;
        }
}

void main(void)
{
	Init_Timer0();
	while(1)
	{
	   TempData[0]=Seg_mod[second/10];//分解显示信息
	   TempData[1]=Seg_mod[second%10] | 0x80;

	   TempData[2]=Seg_mod[msecond/10];//分解显示信息
	   TempData[3]=Seg_mod[msecond%10];
	}
}

电路图

51-小程序 利用数码管制作秒表_第1张图片

你可能感兴趣的:(51-小程序 利用数码管制作秒表)