一、前言
花样流水灯就是让LED灯按照一定规律进行各种变化,流水灯在生活中也随处可见,例如在建筑物的棱角上装上流水灯,可以起到变换闪烁达到美不胜收的效果。此设计使用16个LED灯,有四种花样,循环往复显示,每隔15S左右的时间进行一次花样变换。也可手动切换花样。单片机系统装有按键复位电路可直接复位电路。
二、模块介绍及原理说明
单片机最小系统:
单片机最小系统完成系统运行的最基本电路,单片机最小系统可配合其他模块或自行搭建电路完成各种实验功能,单片机最小系统接口设计灵活,电路简洁,可完成基本的驱动任务。单片机最小系统包括晶振电路、复位电路。复位电路我使用按键复位,方便系统的重启。
三、硬件连接
本设计使用P0、P2控制LED灯由于LED灯所需电量少,所以不需要加驱动就可以完成显示。P3^0为按键控制端口,用于控制花样灯的切换。
仿真连接
实物连接
四、程序说明
主程序,用以调用不同的函数。
main()
{
Timer0Init();
P2=0x00;
P0=0x00;
while(1)
{
key();
if(num==0)
{
P2=0x00;
P0=0x00;
}
else if(num==1)
num1();
else if(num==2)
num2();
else if(num==3)
num3();
else if(num==4)
num4();
}
}
定时器函数,用来控制不同花样切换的时间,更加准确,此设计控制每种花样的显示时间为15S,可根据喜好自己调节。
void Timer0() interrupt 1
{
TH0=(65536-6000)/256; //给定时器赋初值,定时10ms
TL0=(65536-6000)%256;
ssec++;
if(ssec>=100) //毫秒 时钟
{
ssec=0;
sec++;
if(sec>=15) //秒
{
sec=0;
num++;
}
if(num==5)
num=1;
}
}
花样表格函数,将各种花样流水灯分开放入表格中,在主函数中进行调用。通过查表的方式显示各种花样,实用性更强,在花样较多时也方便进行管理。花样相对简单,可自己进行编写。
unsigned char code seg1[]={
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe,
0xff,0xff,0x00,0x00,
0x55,0x55,0xaa,0xaa
};
unsigned char code seg2[]={0x01,0x03,0x07,0x0f,
0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff
};
unsigned char code seg3[]={0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x80,0xc0,0xe0,0xf0,
0xf8,0xfc,0xfe,0xff,
};
unsigned char code seg4[]={0x01,0x03,0x07,0x0f,
0x1f,0x3f,0x7f,0xff,
0xff,0x7f,0x3f,0x1f,
0x0f,0x07,0x03,0x01
};
unsigned char code seg5[]={
0x01,0x03,0x07,0x0f,
0x1f,0x3f,0x7f,0xff,
0xff,0x7f,0x3f,0x1f,
0x0f,0x07,0x03,0x01
};
unsigned char code seg6[]={
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,
0x81,0x82,0x84,0x88,0x90,0xa0,0xc0,
0xc1,0xc2,0xc4,0xc8,0xd0,0xe0,
0xe1,0xe2,0xe4,0xe8,0xf0,
0xf1,0xf2,0xf4,0xf8,
0xf9,0xfa,0xfc,
0xfd,0xfe,
0xff
};
unsigned char code seg7[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x81,0x41,0x21,0x11,0x09,0x05,0x03,
0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x83,0x43,0x23,0x13,0x0b,0x07,
0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x87,0x47,0x27,0x17,0x0f,
0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x0f,0x8f,0x4f,0x2f,0x1f,
0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x9f,0x5f,0x3f,
0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0xbf,0x7f,
0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,
0xff,0xff,
0xff,
};
按键函数,可以通过按键进行LED灯花样的切换,按键函数中需要加入消抖函数,以防止按键误触。
void key()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
num++;
if(num==5) num=0;
}
while(!key1);
}
}
五、效果演示
仿真演示
实物演示
六、代码链接
欢迎留言评论分享自己的看法,如有错误欢迎指正。关注公众号:“小小创客者”回复“花样流水灯“获得源码及仿真。
|