0
点赞
收藏
分享

微信扫一扫

按键控制数码管闪烁

(文章目录)

一、数码管闪烁

#include<STC15F2K60S2.H>
unsigned char mode=0;//模式变量
unsigned char stat=0;//闪烁位变量
unsigned int count_smg=0;//数码管闪烁计数变量
unsigned char f_open=0;//闪烁标志
sbit S7=P3^0;
unsigned char code table[]={0xc0,0xf9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xbf,0x7f};
void select(unsigned char n) //打通译码器
{
	switch(n)
	{
		case 4:
			P2=(P2&0x1f)|0x80; 
		  break;
		case 5:
			P2=(P2&0x1f)|0xa0;
   		break;
		case 6:
			P2=(P2&0x1f)|0xc0;
   		break;
		case 7:
			P2=(P2&0x1f)|0xe0;
   		break;
	}
}
void guanbi() //关闭无关设备
{
	select(5);
	P0=0x00;
	select(4);
	P0=0xff;
}
//**************数码管段选位选函数
void shuma(unsigned char pos,unsigned char date) 
{
	select(7);
	P0=0xff;
	select(6);
	P0=0x01<<pos;
	select(7);
	P0=date;
}
//*************延时函数
void delayms(unsigned int x)
{
	while(x--);
}
//************数码管显示函数
void display()
{
	if(f_open==0)
	{
		shuma(0,table[1]);
		delayms(100);
		shuma(1,table[2]);
		delayms(100);
	}
	else 
	{
		shuma(0,0xff);
		shuma(1,0xff);
	}
	  shuma(2,table[16]);
		delayms(100);
		shuma(3,table[3]);
		delayms(100);
	  shuma(4,table[4]);
	  delayms(100);
	  shuma(5,table[16]);
		delayms(100);
    shuma(6,table[5]);
		delayms(100);
	  shuma(7,table[6]);
	  delayms(100);	
}
void display1()
{
	  shuma(2,table[16]);
		delayms(100);
		shuma(0,table[1]);
		delayms(100);
	  shuma(1,table[2]);
	  delayms(100);
	  shuma(5,table[16]);
		delayms(100);
    shuma(6,table[5]);
		delayms(100);
	  shuma(7,table[6]);
	  delayms(100);	
	if(f_open==0)
	{
		shuma(3,table[3]);
		delayms(100);
		shuma(4,table[4]);
		delayms(100);
	}
	else 
	{
		shuma(3,0xff);
		shuma(4,0xff);
	}
}
void display2()
{
	  shuma(2,table[16]);
		delayms(100);
		shuma(0,table[1]);
		delayms(100);
	  shuma(1,table[2]);
	  delayms(100);
	  shuma(5,table[16]);
		delayms(100);
    shuma(3,table[3]);
		delayms(100);
	  shuma(4,table[4]);
	  delayms(100);	
	if(f_open==0)
	{
		shuma(6,table[5]);
		delayms(100);
		shuma(7,table[6]);
		delayms(100);
	}
	else 
	{
		shuma(6,0xff);
		shuma(7,0xff);
	}
}
void INT()
{
	TMOD=0x01;
	TH0=(65535-1000)/256;
	TL0=(65535-1000)%256;
	EA=1;
	ET0=1;
	TR0=1;
}
void sreviceINT() interrupt 1
{
	 TH0=(65535-1000)/256;
	 TL0=(65535-1000)%256;
	 count_smg++;
   if(count_smg==1000)
	 {
		 count_smg=0;
		 f_open=~f_open;
	 }
}
void scan()
{
	if(S7==0)
	{
		delayms(10);
		if(S7==0)
		{
			stat++;
			if(stat==4)
			{
				stat=1;
			}
			while(S7==0) //等待按键松开
			{
				if(stat==1)
				{
					display();
				}
				else if(stat==2)
				{
					display1();
				}
				else
				{
					display2();
				}
			}
		}
	}
}
void main()
{
	guanbi();
	INT();
	while(1)
	{
		scan();
		switch(stat)
		{
			case 1:
				display();
			break;
			case 2:
				display1();
			break;
			case 3:
				display2();
			break;
		}
	}
}


举报

相关推荐

0 条评论