其他分享
首页 > 其他分享> > 第7章 变量进阶与点阵 LED

第7章 变量进阶与点阵 LED

作者:互联网

尽量减少全局变量的使用。

全局变量均是静态变量。静态局部变量,特点是在整个生存期中只赋一次初值,在第一次执行该函数时,它的值就是给定的那个初值,而之后在该函数所有的执行次数中,它
的值都是上一次函数执行结束后的值,即它可以保持前次的执行结果。

点阵 LED 显示屏特点:灵活的显示面积(可任意分割和拼装)、高亮度、长寿命、数字化、实时性 。

实物图:

原理图:

 

对应原理图:

使用点阵显示心型,用取模软件取模,得到对应的字节数组存起来,使用定时器0,每一毫秒执行中断函数,点亮点阵的一行,在10ms之内点亮完毕点阵即可实现类似静态显示的效果。具体代码如下:

#include<reg52.h>

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

unsigned char code image[] = {
	0xFF,0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,
	};	
void main()
{
	EA = 1;		  //使能总中断
	ENLED = 0;
	ADDR3 = 0;
	TMOD = 0x01;	 //设置定时器模式1
	TH0 = 0xFC;		//定时器赋初值
	TL0 = 0x67;
	ET0 = 1;		//打开定时器0中断
	TR0 = 1;		//启动定时器0
	while(1);
}

void InterruptTimer0() interrupt 1
{
	static unsigned char i = 0;
	TH0 = 0xFC;
	TL0 = 0x67;

	P0 = 0xFF;
	switch(i)
	{
		case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[0];break;
		case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[1];break;
		case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[2];break;
		case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[3];break;
		case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[4];break;
		case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[5];break;
		case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[6];break;
		case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[7];break;
		default: break;

	}

}

显示效果图:

 

点阵显示动态效果,每毫秒执行一次中断函数,点亮点阵的某一行。同样在中断函数中计时,每一秒钟更换一张图片,相当于数组下角标整体往后挪动一位,同样计数,只要挪动到最后,需要重新回归到第一张图片。代码如下:

#include<reg52.h>

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

unsigned char code image[] = {
	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7D,0x01,0x01,0x7D,0xFF,0xFF,0xE3,0xC1,
0x81,0x03,0x03,0x81,0xC1,0xE3,0xFF,0xFF,0x81,0x01,0x3F,0x3F,0x3F,0x01,0x81,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
	};	
void main()
{
	EA = 1;
	ENLED = 0;
	ADDR3 = 0;
	TMOD = 0x01;
	TH0 = 0xFC;
	TL0 = 0x67;
	ET0 = 1;
	TR0 = 1;
	while(1);
}

void InterruptTimer0() interrupt 1
{
	static unsigned char i = 0;
	static unsigned char index = 0;
	static unsigned char tmr = 0;
	TH0 = 0xFC;
	TL0 = 0x67;

	P0 = 0xFF;
	switch(i)
	{
		case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[index + 0];break;
		case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[index + 1];break;
		case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[index + 2];break;
		case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[index + 3];break;
		case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[index + 4];break;
		case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[index + 5];break;
		case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[index + 6];break;
		case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[index + 7];break;
		default: break;
	}

	tmr++;
	if(tmr >= 250)
	{
		tmr = 0;
		index++;
		if(index >= 32)
		{
			index = 0;
		}
	}


}

效果图如下 :

 

标签:P0,LED,进阶,image,break,ADDR0,ADDR1,0xFF,点阵
来源: https://www.cnblogs.com/lingdong24/p/16147267.html