蓝桥杯单片机CT107D_03_数码管动态显示

#include<reg52.h>
sbit S7 = P3^0;
unsigned char a = 0;  //状态位
bit b = 1;       
unsigned char count = 0;
unsigned char month = 1;
unsigned char code SEG_code[19] = 
	{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
	 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
	 0xbf,0x7f,0xff};	             //定义共阳数码管段码内容:0~F,—,.            

void Delay(unsigned int t)
{
	while(t--);
}

//void delay_m(unsigned int n)   //延时n秒
//{
//    unsigned int i=0,j=0;
//    for(i=0;i<n;i++)
//        for(j=0;j<21738;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 Dynamic_display()
{
	if((a == 1)&&(b == 1))
	{
		Show_tube(0,SEG_code[18]);
		Show_tube(1,SEG_code[18]);
	}
	else
	{
		Show_tube(0,SEG_code[2]);
		Delay(500);
		Show_tube(1,SEG_code[3]);
		Delay(500);
	}

	Show_tube(2,SEG_code[16]);
	Delay(500);
	
	if((a == 2)&&(b == 1))
	{
		Show_tube(3,SEG_code[18]);
		Show_tube(4,SEG_code[18]);
	}
	else
	{
		Show_tube(3,SEG_code[5]);
		Delay(500);
		Show_tube(4,SEG_code[9]);
		Delay(500);
	}

	Show_tube(5,SEG_code[16]);
	Delay(500);

	if((a == 3)&&(b == 1))
	{
		Show_tube(6,SEG_code[18]);
		Show_tube(7,SEG_code[18]);
	}
	else
	{
		Show_tube(6,SEG_code[5]);
		Delay(500);
		Show_tube(7,SEG_code[0]);
		Delay(500);
	}
}

//void Delay_dynamic(unsigned char t)	    //延时,让最后两位数码管的值发生变化(从1变到12)
//{
//	while(t--)
//	{
//		Dynamic_display();
//	}
//} 

void Init_timer0()      //定时器0初始化(中断初始化)
{
	TMOD = 0x01;                  //选择定时器0(16位定时器),定时50ms(间隔50000个脉冲)
    TH0 = (65535 - 50000) / 256;  //取高八位(3c)给TH0寄存器
	TL0 = (65535 - 50000) % 256;  //取低八位(af)给TL0寄存器

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

void Service_timer0() interrupt 1   //定时器0中断服务函数
{
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;    //一次计时完后需要给TH0和TL0赋初值
	count++;
	if(count == 10)      //定时0.5s(间隔1s)
	{
		b = ~b;		  
		count = 0;
	}
}

void Delay_keys()
{
	unsigned char i,j;
	i = 108;
	j = 145;
	do
	{
		while(--j);
	}while(--i);
}

void Press_keys()
{
	if(S7 == 0)
	{	
		Delay_keys();
		if(S7 == 0)
		{
			switch(a)
			{
				case 0:
					a = 1;
					TR0 = 1;
					break;
				case 1:
					a = 2;
					break;
				case 2:
					a = 3;
					break;
				case 3:
					a = 0;
					TR0 = 0;
					break;
			}
			while(S7 == 0)
			{
				Dynamic_display();
			}
		}
	}
}
		
void main()
{
	Init_timer0();
	Select_HC138(5);
	P0 = 0x00;
	while(1)
	{
		//Dynamic_display();
//		if(month > 12)
//		{
//			month = 1;
//		}	
//		Delay_dynamic(200);
//		month++;
		Dynamic_display();
		Press_keys();
	}
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值