原理图可以从 http://www.51hei.com/f/ks51.pdf 这里下载,单片机型号是stc89c51
/**
*功能:点阵向上流动显示I Love U -Love是漂亮的心形
*时间:2014年3月10日16:19:14
*作者:寒竹子
*备注:动画显示原理:动画由32张图片形成,每一毫秒刷新一帧图片,每250ms变化一帧,共32帧。
**/
#include <reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
//74HC138
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
//I Love U 取模编码
uchar code table[] = {/*宽8,高40*/
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,//开始全黑
0xFF,0xC3,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,//I
0xFF,0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,//heart
0xFF,0x99,0x99,0x99,0x99,0x99,0x81,0xC3,//U
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF//消隐,去掉乱码
};
uchar counter = 0, index = 0;//图片刷新索引(要刷新32张,每张间隔250ms)
void timer0_init();
void HC138_init();
void main(void)
{
timer0_init();
HC138_init();
while (1);
}
/********timer0 init**********/
void timer0_init()
{
TMOD |= 0x01;//T1 方式1
TH0 = 0xFC;
TL0 = 0x67;//装入初值,定时1ms
TR0 = 1;//start T0
EA = 1;//开总中断
ET0 = 1;//开定时器T0中断
}
/**********74HC138 init************/
void HC138_init()
{
ADDR3 = 0;
ENLED = 0;//使能U4
}
/**************timer0 interrupt**************/
void timer0_int() interrupt 1
{
static uchar j = 0;//用于刷新每一帧图片,动画由32张图片构成
TH0 = 0xFC;
TL0 = 0x67;//1ms
counter++;//ms++
P0 = 0xFF;//消隐
//动态刷新(刷新每一帧)
switch (j)
{
case 0: ADDR2 = 0; ADDR1 = 0; ADDR0 = 0;
break;
case 1: ADDR2 = 0; ADDR1 = 0; ADDR0 = 1;
break;
case 2: ADDR2 = 0; ADDR1 = 1; ADDR0 = 0;
break;
case 3: ADDR2 = 0; ADDR1 = 1; ADDR0 = 1;
break;
case 4: ADDR2 = 1; ADDR1 = 0; ADDR0 = 0;
break;
case 5: ADDR2 = 1; ADDR1 = 0; ADDR0 = 1;
break;
case 6: ADDR2 = 1; ADDR1 = 1; ADDR0 = 0;
break;
case 7: ADDR2 = 1; ADDR1 = 1; ADDR0 = 1;
break;
default: break;
}
P0 = table[index + j++];//动态刷新每一帧图片
if (j >= 8)
{
j = 0;
}
//变化帧
if (counter >= 250)
{
counter = 0;
index++;//帧数++
if (index >= 32)
{
index = 0;//最多32帧图片
}
}
}