搞了个单片机4:数码管动态显示(数码管表白)(蓝桥杯单片机速成)(零基础!超详细!小白速成!教你跑遍单片机例程!)

在说动态显示之前,我们有时候写中断的时候会看到有程序在写中断的时候是这样写的
在这里插入图片描述
后面的using 1其实是使用一些特殊的寄存器,其实会给我们自动配置,所以在一般情况下是不需要写这个的。

我们先说一个案例,让每一个数码管依次显示不同的数

我们先把段选和位选的条件都列出来,制成一个数组,看看位选有没有什么规律
在这里插入图片描述
由此我们可以看见,这是1一直在左移,那么,我们可以用操作符来解决这个事情,顺便让8个数码管,显示1-8的数字,注意,这和我们之前的全部开启是不一样的,之前是数码管都显示一样的操作,现在是每个数码管依次打开,并且亮不同的数字。下面是程序:

这里用到的 都是我之前说过的。
这里我尝试使用了while( 判断),while里面用判断,结果不行,所以我们老老实实用if判断。
代码如下:
这里没有新东西,就是给大家看一下效果。

#include <STC15F2K60S2.H>
#define uchar unsigned char
uchar times = 0;
uchar numd = 0;
uchar numw = 0;
unsigned int code listd[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
unsigned int code listw[]={0x01,0x02,0x04,0x08,
0x10,0x20,0x40,0x80};

void time0() interrupt 1
{
		TH0 = (65535-10000)/256;
		TL0 = (65535-10000)%256;
		times +=1;
}
          
void main(void)
{
	TMOD = 0xf0;
	TH0 = (65535 - 10000)/256;
	TL0	= (65535 - 10000)%256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;

	while(1)
	{
			if(times ==1)
			{
					times = 0;
					P2 =((P2&0x1f)|0xc0);
					P0 = listw[numw];
					P2 &= 0x1f;
					numw += 1;
					if(numw==8)
					{
							numw=0;
					}

					numd += 1;
						if(numd==10)
					{
							numd = 1;
					}
					P2 =((P2&0x1f)|0xe0);
					P0 = listd[numd];
					P2 &= 0x1f;
			}		
	}
}

在这里插入图片描述 视频点这里观看

数码管动态2:分离百位数

我们封装函数,然后调用,会使得主程序更简洁。
分离百位数就是一个取模,取商的思想,%n,就可以得到0~(n-1)的数。

#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar times = 0;
uchar tempw = 0x01;
uint digit = 0;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};

void delay(uint k)
{
		uint i = 0;
		uint j = 0;
		for(i=700;i>0;i--)
		{
				for(j=0;j<k;j++)
			{
					;
			}
		}
}

void time0() interrupt 1
{
		TH0 = (65536-5000)/256;
		TL0 = (65536-5000)%256;
		times +=1;
}
void init_time0()
{
	TMOD = 0xf0;
	TH0 = (65536 - 50000)/256;
	TL0	= (65536 - 50000)%256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
}
void display(uint hundred ,uint ten,uint single)
{
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;	
	
			P2 =((P2&0x1f)|0xe0);
			P0 = list[hundred];
			P2 &= 0x1f;
			
	
			tempw <<= 1;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;
	
			P2 =((P2&0x1f)|0xe0);
			P0 = list[ten];
			P2 &= 0x1f;


			tempw <<= 1;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;
			
			P2 =((P2&0x1f)|0xe0);
			P0 = list[single];
			P2 &= 0x1f;

}
void main(void)
{
	init_time0();
	display(digit/100,digit%100/10,digit%10);
	while(1)
	{
			if(times ==20)
			{
					digit += 1;
					display(digit/100,digit%100/10,digit%10);	
					times = 0;
			}		
	}
}

代码敲出来是没有问题的,出了没有调用delay函数的警告。
展示一下!!!
在这里插入图片描述 视频点这里观看
我们发现根本不是我们想要的结果
在这里插入图片描述
我们可以观察到是这么个显示结果,首先,这个数字的确是加1显示的,0-1-2,但是这个程序只加到了2,说明就是有问题的,而且,这个数码管显示的位置,不是我们所期待的。
下面,我们先从位这个地方来研究是哪个部分出错。
我们发现第一步显示就是有问题的,那我们考虑一下是不是程序太快,单片机搞不过来。那我们延时一下看看。

#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar times = 0;
uchar tempw = 0x01;
uint digit = 0;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};

void delay(uint k)
{
		uint i = 0;
		uint j = 0;
		for(i=700;i>0;i--)
		{
				for(j=0;j<k;j++)
			{
					;
			}
		}
}

void time0() interrupt 1
{
		TH0 = (65536-5000)/256;
		TL0 = (65536-5000)%256;
		times +=1;
}
void init_time0()
{
	TMOD = 0xf0;
	TH0 = (65536 - 50000)/256;
	TL0	= (65536 - 50000)%256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
}
void display(uint hundred ,uint ten,uint single)
{
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;	
			
			P2 =((P2&0x1f)|0xe0);
			P0 = list[hundred];
			P2 &= 0x1f;
			delay(5);
	
			tempw <<= 1;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;
	
			P2 =((P2&0x1f)|0xe0);
			P0 = list[ten];
			P2 &= 0x1f;
			delay(5);

			tempw <<= 1;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;
			
			P2 =((P2&0x1f)|0xe0);
			P0 = list[single];
			P2 &= 0x1f;
			delay(5);
}
void main(void)
{
	init_time0();
	display(digit/100,digit%100/10,digit%10);
	
	while(1)
	{
			if(times ==20)
			{
					digit += 1;
					display(digit/100,digit%100/10,digit%10);	
					times = 0;
			}		
	}
}

我们改了一改,发现出现变化了。
在这里插入图片描述视频点击这里观看
我们看到其实有一点点往我们想要的方向发展了,但是,还不够完美,这说明,其实我们的程序,还是因为太快了,导致了一些问题,所以,我们还是要抓紧研究这个延时的调用。
太快了,可能会导致赋值的残留,那么,我们需要给他们都加上一些延时程序。

#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar times = 0;
uchar tempw = 0x01;
uint digit = 0;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};

void delay(uint k)
{
		uint i = 0;
		uint j = 0;
		for(i=700;i>0;i--)
		{
				for(j=0;j<k;j++)
			{
					;
			}
		}
}

void time0() interrupt 1
{
		TH0 = (65536-50000)/256;
		TL0 = (65536-50000)%256;
		times +=1;
}
void init_time0()
{
	TMOD = 0xf0;
	TH0 = (65536 - 50000)/256;
	TL0	= (65536 - 50000)%256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
}
void display(uint hundred ,uint ten,uint single)
{
			tempw = 0x01;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;	
			//delay(10);
			P2 =((P2&0x1f)|0xe0);
			P0 = list[hundred];
			P2 &= 0x1f;
			delay(50);
	
			tempw <<= 1;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;
		//	delay(10);
			P2 =((P2&0x1f)|0xe0);
			P0 = list[ten];
			P2 &= 0x1f;
			delay(50);

			tempw <<= 1;
			P2 =((P2&0x1f)|0xc0);
			P0 = tempw;
			P2 &= 0x1f;
	//		delay(10);
			P2 =((P2&0x1f)|0xe0);
			P0 = list[single];
			P2 &= 0x1f;
			delay(50);
}
void main(void)
{
	
	display(digit/100,digit%100/10,digit%10);
	init_time0();
	while(1)
	{
			if(times == 20)
			{
					digit += 1;
					display(digit/100,digit%100/10,digit%10);	
					times = 0;
			}		
	}
}

展示一下!!!!!
在这里插入图片描述 视频点击这里观看
简单的数码管动态我们已经完结了,我们下期再见。

数码管表白

现在我们设想一个程序:
首先,我们让流水灯一个一个流,然后双向奔赴,到一定程度,全闪。就像感情,只有经过细水长流,双向奔赴,才能开花结果。
接下来,我们让数码管显示520 1314.YU,I CAN By YOUR SIDE FOREVER ,I LOVE YOU 。
我们先让它们一个一个显示出来,最终一句话就全体闪烁一下,最终到I LOVE YOU 的部分,我们就一直闪烁,和LED一起闪烁。

那么,我们开始逐步分析吧:
数字部分是肯定能显示出来的,所以我们研究字母部分就行了
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这是大概的情况,虽然我们是沉迷于技术学习的孩子,但是,我们心里时时刻刻都想着对象,你们说是吧!赶紧自己设计一段话,设计好排版,考虑在目前这只有8个数码管的小板子上,表达对你女朋友的爱意吧!当然,你还可以考虑加入蜂鸣器的版本,就是让蜂鸣器唱歌,众所周知,振动是发声的来源,所以,蜂鸣器不同的振动频率和延时效果可以让单片机唱歌,阴乐不分高低!但我不会

好嘞,开始编想程序吧!
我们可以考虑把字母的码子,先搞出来:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
好嘞!字母搞出来了!
现在开始把需要的2个定时器都设定好,主程序框架也设定好:
在这里插入图片描述
在这里插入图片描述
这是我们用到的中断
先 主框架:

#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar t0,t1;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};

void time0() interrupt 1
{
		TH0 = (65536-50000)/256;
		TL0 = (65536-50000)%256;
		t0 +=1;
}
void time1() interrupt 3
{
		TH0 = (65536-50000)/256;
		TL0 = (65536-50000)%256;
		t1 +=1;
}
void delay(uint k)
{
		uint i = 0;
		uint j = 0;
		for(i=700;i>0;i--)
		{
				for(j=0;j<k;j++)
			{
					;
			}
		}
}

void initAll()
{
	TMOD = 0x00;
	TH0 = (65536 - 50000)/256;
	TL0	= (65536 - 50000)%256;
	TH1 = (65536 - 50000)/256;
	TL1	= (65536 - 50000)%256;
	EA = 1;
	ET0 = 1;
	ET1 = 1;
	TR0 = 1;
	TR1	= 1; 
}
void display()
{
	
}
void main(void)
{
	
	initAll();
	while(1)
	{
	}
}

接下来我们完善整个函数,根据自己想要的情形
我觉得520 1314 太土了,就没用了,加了别的

#include <STC15F2K60S2.H>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar t0,t1,wei,duan,flag1;
uchar oled1 = 0xfe;
uchar oled2 = 0x7f;

unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};

void time0() interrupt 1
{
		TH0 = (65536-50000)/256;
		TL0 = (65536-50000)%256;
		t0 +=1;
}
void time1() interrupt 3
{
		TH0 = (65536-50000)/256;
		TL0 = (65536-50000)%256;
		t1 +=1;
}
void delay(uint k)
{
		uint i = 0;
		uint j = 0;
		for(i=700;i>0;i--)
		{
				for(j=0;j<k;j++)
			{
					;
			}
		}
}

void InitAll()
{
	TMOD = 0x00;
	TH0 = (65536 - 50000)/256;
	TL0	= (65536 - 50000)%256;
	TH1 = (65536 - 50000)/256;
	TL1	= (65536 - 50000)%256;
	EA = 1;
	ET0 = 1;
	ET1 = 1;
	TR0 = 1;
	TR1	= 1; 
}
//void DisplayLed1()
//{
//	again:	
//		if(flag1 != 8)
//		{
//			flag1 += 1;
//			P2=((P2&0x1f)|0x80);
//			P0=oled1;
//			P2 &= 0x1f;
//			oled1 = _crol_(oled1,1);
//			t0 = 0;
//			goto again;
//		}
//		else
//			{
//				P2=((P2&0x1f)|0x80);
//				P0=0x00;
//				P2 &= 0x1f;
//				delay(100);
//				P2=((P2&0x1f)|0x80);
//				P0=0xff;
//				P2 &= 0x1f;
//				delay(100);
//				P2=((P2&0x1f)|0x80);
//				P0=0x00;
//				P2 &= 0x1f;
//			}

//}
//void DisplayLed2()
//{
//		oled2 = 0x7f;
//		P2=((P2&0x1f)|0x80);
//		P0=oled2;
//		P2 &= 0x1f;
//		oled2 = _cror_(oled2,1);
continue....	
//}
//void DisplaySmg1()
//{
//		wei = 0x01, 
5
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[5];
//		P2 &= 0x1f;
//		delay(500);
2
//		wei<<=1;
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[2];
//		P2 &= 0x1f;
//		delay(500);
0
//		wei<<=1;
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[0];
//		P2 &= 0x1f;
//		delay(500);
1
//		wei<<=2;
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[1];
//		P2 &= 0x1f;
//		delay(500);
3
//		wei<<=1;
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[3];
//		P2 &= 0x1f;
//		delay(500);
1
//		wei<<=1;
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[1];
//		P2 &= 0x1f;
//		delay(500);
4	
//		wei<<=1;
//	  P2=((P2&0x1f)|0xc0);
//		P0=wei;
//		P2 &= 0x1f;
//		P2=((P2&0x1f)|0xe0);
//		P0=list[4];
//		P2 &= 0x1f;
//		delay(500);
//}
void DisplaySmg2()
{
		wei = 0x01, 
//y
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x91;
		P2 &= 0x1f;
		delay(500);
//u
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc1;
		P2 &= 0x1f;
		delay(500);
//i
		wei<<=2;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xcf;
		P2 &= 0x1f;
		delay(500);
//c
		wei<<=2;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x46;
		P2 &= 0x1f;
		delay(500);
//a
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x08;
		P2 &= 0x1f;
		delay(500);
//n
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc8;
		P2 &= 0x1f;
		delay(500);
}

void DisplaySmg3()
{
		wei = 0x02, 
//b
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x03;
		P2 &= 0x1f;
		delay(500);
//e
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x06;
		P2 &= 0x1f;
		delay(500);
//b
		wei<<=2;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x03;
		P2 &= 0x1f;
		delay(500);
//y
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x91;
		P2 &= 0x1f;
		delay(500);

}
void DisplaySmg4()
{
		wei = 0x01, 
//y
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x91;
		P2 &= 0x1f;
		delay(500);
//0
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc0;
		P2 &= 0x1f;
		delay(500);
//u
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc1;
		P2 &= 0x1f;
		delay(500);
//r
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xce;
		P2 &= 0x1f;
		delay(500);
//s
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x12;
		P2 &= 0x1f;
		delay(500);
//i
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xcf;
		P2 &= 0x1f;
		delay(500);
//d
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x21;
		P2 &= 0x1f;
		delay(500);
//e
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x06;
		P2 &= 0x1f;
		delay(500);
}
	void DisplaySmg5()
{
		wei = 0x01;
//i
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xcf;
		P2 &= 0x1f;
		delay(500);
//l
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc7;
		P2 &= 0x1f;
		delay(500);
//0
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc0;
		P2 &= 0x1f;
		delay(500);
//v
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc1;
		P2 &= 0x1f;
		delay(500);
//e
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x06;
		P2 &= 0x1f;
		delay(500);
//y
    wei <<= 1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x91;
		P2 &= 0x1f;
		delay(500);
//0
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc0;
		P2 &= 0x1f;
		delay(500);
//u
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0xc1;
		P2 &= 0x1f;
		delay(500);
}
void DisplaySmg6()
{
		wei = 0x01, 
//b
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x03;
		P2 &= 0x1f;
		delay(500);
//e
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x06;
		P2 &= 0x1f;
		delay(500);
//H	
		wei<<=2;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x89;
		P2 &= 0x1f;
		delay(500);
//A	
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x08;
		P2 &= 0x1f;
		delay(500);
//P	
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x8C;
		P2 &= 0x1f;
		delay(500);
//P	
		wei<<=1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x8C;
		P2 &= 0x1f;
		delay(500);
//y
    wei <<= 1;
	  P2=((P2&0x1f)|0xc0);
		P0=wei;
		P2 &= 0x1f;
		P2=((P2&0x1f)|0xe0);
		P0=0x91;
		P2 &= 0x1f;
		delay(500);
}
void main(void)
{
	P2=((P2&0x1f)|0xa0);
	P0=0;
	P2 &= 0x1f;
	InitAll();
	DisplaySmg2();
	delay(500);
	DisplaySmg3();
	delay(500);
	DisplaySmg4();
	delay(500);
	DisplaySmg5();
	delay(500);
	DisplaySmg6();
}

你们可以改我的代码。可以考虑一下时间的管理,因为我失败了,直接就上了,没用定时器了。
视频是我对象一个人的私有浪漫,就不放在博客里面了。
大家可以自己弄出来,发给对象。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
数码管是一种常见的数字显示器件,可以用于显示各种数字、字母等字符。在单片机应用中,数码管通常用于显示计数器、计时器、温度、湿度等实时数据。 数码管的种类有很多,包括共阳数码管、共阴数码管、共阳共阴混合数码管等。其中,共阳数码管是最常见的一种,也是本文所涉及的数码管类型。 单片机控制数码管的原理是通过对数码管的各个管脚进行控制,使其显示相应的数字或字符。数码管的控制方式有两种,即静态显示和动态显示。 静态显示是指将要显示的数字或字符的每一位分别输出到数码管的每个管脚上,然后使其保持不变,从而实现显示效果。静态显示的缺点是需要使用大量的I/O口,且不能灵活地改变显示内容。 动态显示是指将要显示的数字或字符的每一位依次输出到数码管的每个管脚上,并在短时间内快速切换下一个数字或字符,从而形成连续的显示效果。动态显示的优点是可以使用较少的I/O口,且可以灵活地改变显示内容。 以下是一个简单的动态显示数码管的实现示例: 1. 定义数码管的引脚 ```c #define DIG_PORT P2 // 数码管位选端口 #define DIG_COM 0x00 // 数码管位选端口初始值 #define LED_PORT P0 // 数码管段选端口 ``` 2. 定义数码管显示的数字或字符 ```c unsigned char code ledChar[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f }; ``` 3. 实现数码管动态显示函数 ```c void display(unsigned char i) { unsigned char j, k; for (j = 0; j < 8; j++) { DIG_PORT = DIG_COM | (1 << j); // 选择数码管位(从左到右) for (k = 0; k < 100; k++); // 延时,视情况可调整 LED_PORT = ledChar[i]; // 显示数码管上的数字或字符 } } ``` 4. 调用数码管动态显示函数 ```c int main() { unsigned char i = 0; while (1) { display(i % 10); // 显示数字 i 的个位数 i++; } return 0; } ``` 以上就是一个简单的数码管动态显示的实现示例。需要注意的是,数码管的控制方式和具体实现方法可能因不同的硬件平台和编程语言而有所不同。因此,在具体应用中需要根据实际情况进行适当的调整和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值