按键控制数码管闪烁

举报
yd_274589494 发表于 2023/07/21 12:12:17 2023/07/21
【摘要】 @TOC 一、数码管闪烁#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[]={...

@TOC

一、数码管闪烁

#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;
		}
	}
}


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。