我们把 9 脚置成高电平、13 脚置成低电平的话,左上角的那个 LED
小灯就会亮了。下面我们就用程序来实现一下,特别注意,控制点阵左侧引脚的 74HC138是原理图上的 U4,8 个引脚自上而下依次由 U4 的 Y0~Y7 输出来控制。
#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,0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,0xFF,0x99,0x99,0x99,0x99,0x99,0x81,0xC3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};void main()
{EA = 1; //使能总中断ENLED = 0; //使能 U4,选择 LED 点阵ADDR3 = 0;TMOD = 0x01; //设置 T0 为模式 1TH0 = 0xFC; //为 T0 赋初值 0xFC67,定时 1msTL0 = 0x67;ET0 = 1; //使能 T0 中断TR0 = 1; //启动 T0while (1);
}
/* 定时器 0 中断服务函数 */
void InterruptTimer0() interrupt 1
{static unsigned char i = 0; //动态扫描的索引static unsigned char tmr = 0; //250ms 软件定时器static unsigned char index = 0; //图片刷新索引TH0 = 0xFC; //重新加载初值TL0 = 0x67;
//以下代码完成 LED 点阵动态扫描刷新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;}//以下代码完成每 250ms 改变一帧图像tmr++;if (tmr >= 250) //达到 250ms 时改变一次图片索引{tmr = 0;index++;if (index >= 32) //图片索引达到 32 后归零{index = 0;}}
}
效果图: