基于51单片机的秒表,单独按键控制启动,暂停,复位。

2023年1月15日07:57:26

实验功能

1.程序运行开始时显示“00”,随后显示数值逐渐增大。待计数到59后,自动从“00”开始。

2.第一次按下按键计数启动,第二次按下按键计数暂停,如此循环,长按直接清零。

实验内容

1.该实验仅使用一个按键,进行启动、暂停、复位控制,需要进行长短按检测,在长按中清零,短按中进行启动、暂停控制。

#include<regx51.h>
#define uchar unsigned char
#define uint unsigned int
uchar Nixie[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar sec,keystate,press0,press1;
uint count,keytick,keytime;
bit start=1;

void Delay(unsigned char Time1)
{
	unsigned char Time2,Time3;
	for(Time2=0;Time2<Time1;Time2++)	//外循环,由Time1决定,外250*1=250ms
	for(Time3=0;Time3<250;Time3++);	//内循环,次数250次,内4us*250=1ms
}

void Timer0_init()
{
	TMOD=0x01;
	TH0=0x3c;
  	TL0=0xb0;	
	EA=1;
	ET0=1;
	TR0=1;
}



void main()
{
	Timer0_init();
	while(1)
	{
		Delay(50);
		press0=press1;
		press1=P3_2;
		if (press1==0 && press0==1)
			keystate=1;
		else if (press1==1 && press0==0)
			keystate=2;
		else
			keystate=0;
		if (keystate==1)
			keytime=keytick;
		if (keystate==2)
		{
			if (keytick-keytime>10)
			{
				sec=0;
				start=0;
			}
			else
				start=~start;
		}
		P0=Nixie[sec/10];
		P2=Nixie[sec%10];
	}
}

void Timer0() interrupt 1
{
	TL0=0xb0;
	TH0=0x3c;
	keytick++;
	if (start==1)
	{
		count++;
		if (count==20)
		{
			sec++;
			count=0;
		}
		if (sec==60)
			sec=0;
	}	
}

 

 

 

  • 作者:万万没想到LX
  • 原文链接:https://blog.csdn.net/weixin_58701549/article/details/124235064
    更新时间:2023年1月15日07:57:26 ,共 999 字。