51单片机0.1秒起计的秒表

 

3个特点,可停止中断,中断消抖(巧借数码显示消抖),按钮操作不影响数码显示.https://ask.csdn.net/questions/757899

#include "reg52.h"
#include "math.h"
sbit QiTin = P3^0;//启停按键
sbit duan = P2^6;//数码段
sbit wei = P2^7;//数码位
bit k=0;//标记
unsigned char code ShuMaGuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0X80};//0~9隐.小数点
unsigned char code WeiXuanBiao[] = { 0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f };
unsigned char Js=0;//中断计时
unsigned char Hm=0;//毫秒
unsigned char Mo=0;//秒
void ShuMaXianShi(unsigned char s,unsigned char w,unsigned char d)
{
	unsigned char YanShi=255;
	P0=WeiXuanBiao[w];
	wei=1;
	wei=0;
	P0=ShuMaGuan[s]|ShuMaGuan[d];//d显示小数点11,10关闭
	duan=1;
	while(YanShi--);
	P0=ShuMaGuan[10];//清除段信息为下次显示消除影响
	duan=0;
}
void ZhongDuanSheZhi()
{
	TH0+=0X4c;/*定时器赋初值,定时50ms触发中断,自动补偿方式*/
	TL0+=0X00; 
	TMOD=0X01;
	TR0=1; 
	ET0=1; //开启定时器
	EA=1; //全局中断开关
}
void JiShi(/*时间计算*/)
{
	if(!K)
	{
		if(Js%2) if(++Hm>=10) Hm=0;
		if(Js>=20)
		{
			Js=0;
			++Mo;
		}			
		if(Mo>=60) Mo=0;
	}
}
void XianShi()
{
	ShuMaXianShi(Hm,3,10);
	ShuMaXianShi(Mo%10,2,11);
	ShuMaXianShi(Mo/10,1,10);
	ShuMaXianShi(10,4,10);
	ShuMaXianShi(10,5,10);
	ShuMaXianShi(10,6,10);
	ShuMaXianShi(10,7,10);
	ShuMaXianShi(10,8,10);
	JiShi();	
}
void ZhongDuanAnNiu()
{
	if(!QiTin)
	{
		//XianShi(/*巧借8个数码显示消抖*/);
		unsigned char Yan_Shi=Js;
		if(!TR0)TR0=k=1;//重启中断
		while(Js==Yan_Shi);//中断消抖
		if(!QiTin)if(!k)TR0=k; else k=0;//关闭中断,消抖后清除标记
		while(!QiTin) XianShi(/*按下期间显示*/);
	}
}
void main()
{
	ZhongDuanSheZhi();
	while(1)
	{
		XianShi();
		ZhongDuanAnNiu();
	}
}
void ZhongDuan() interrupt 1
{
	++Js;
	TH0+=0X4c;
	TL0+=0X00;        
}

51单片机0.1秒起计的秒表_第1张图片

你可能感兴趣的:(51单片机0.1秒起计的秒表)