蓝桥杯单片机CT107D实现秒表

使用CT107D开发板设计一个秒表,具有清零、启动和暂停功能。显示格式为:分—秒—0.05秒。

源码如下:

#include<reg52.h>
sbit S5 = P3^2;	   //定义独立按键S4,按下启动\暂停
sbit S4 = P3^3;	   //定义独立按键S5,按下清零
unsigned char minute = 0;   //分
unsigned char second = 0;	//秒
unsigned char count = 0;	//计数
unsigned char state = 0;    //标志位

unsigned char code SEG_code[18] = 
	{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
	 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
	 0xbf,0x7f};	             //定义共阳数码管段码内容:0~F,—,.   

void Delay_tube(unsigned int t)   //数码管延时
{
	while(t--);
}

void Delay_keys()        //延时去抖动
{
	unsigned char i = 108,j = 145;
	while(--i)
	{
		while(--j);
	}
}

void Select_HC138(unsigned char n)
{
	switch(n)
	{
		case 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
			break;
	}
}

void Show_tube(unsigned char position,unsigned char value)
{
	Select_HC138(6);
	P0 = 0x01 << position;
	Select_HC138(7);
	P0 = value;
}

void Display_tube()	       //数码管显示
{
	Show_tube(0,SEG_code[minute/10]);	  //显示分的十位
	Delay_tube(100);
	Show_tube(1,SEG_code[minute%10]);	  //显示分的个位
	Delay_tube(100);

	Show_tube(2,SEG_code[16]);			  //显示分隔符
	Delay_tube(100);
	
	Show_tube(3,SEG_code[second/10]);	  //显示秒的十位
	Delay_tube(100);
	Show_tube(4,SEG_code[second%10]);	  //显示秒的个位
	Delay_tube(100);

	Show_tube(5,SEG_code[16]);			  //显示分隔符
	Delay_tube(100);
	
	Show_tube(6,SEG_code[count/10]);	  //显示定时器计时次数
	Delay_tube(100);
	Show_tube(7,SEG_code[count%10]);
	Delay_tube(100);	
}

void Press_keys()		  //独立按键
{
	if(S4 == 0)
	{
		Delay_keys();     //去抖动
		if(S4 == 0)
		{
			TR0 = ~TR0;   //启动\暂停
			while(S4 == 0)    //保证每次按下只判断一次
			{
				Display_tube();
			}	
		}
	}

	else if(S5 == 0)
	{
		Delay_keys();
		if(S5 == 0)
		{
			minute = 0;
			second = 0;
			count = 0;
			while(S5 == 0)
			{
				Display_tube();
			}
		}
	}
}

void Init_timer0()       //定时器0初始化(中断初始化)
{
	TMOD = 0x01;      //选择定时器0,16位定时器,定时50ms
	TH0 = (65535 - 50000) / 256;    //将高八位给寄存器TH0
	TL0 = (65535 - 50000) % 256;	//将低八位给寄存器TL0

	EA  = 1;     //打开总中断
	ET0 = 1;     //打开定时器0的中断
	TR0 = 0;     //关闭定时器0
}

void Service_timer0() interrupt 1
{
	TH0 = (65535 - 50000) / 256;    //将高八位给寄存器TH0
	TL0 = (65535 - 50000) % 256;	//将低八位给寄存器TL0
	count++;
	if(count == 20)   //计时满1s
	{
		second++;
		count = 0;
	}	
	if(second == 60)  //计时满60s
	{
		minute++;
		second = 0;
		state = 1;
	}	
}

void Buzzer()		 //计时每满1min响一次
{
	if(state == 1)
	{
		Select_HC138(5);
		P0 = 0x40;      //打开蜂鸣器
		Delay_tube(60000);
		P0 = 0x00;
		state = 0;
	}
}
	
void main()
{
	Init_timer0();
	Select_HC138(5);
	P0 = 0x00;    //关闭蜂鸣器和继电器
	while(1)
	{
		Display_tube();
		Press_keys();
		Buzzer();
	}
}
发布了3 篇原创文章 · 获赞 3 · 访问量 201

猜你喜欢

转载自blog.csdn.net/qq_41412394/article/details/104226108