女朋友的生日要到了,絞盡腦汁想送一個有意義的禮物。
后來在網站上看到了這個八階流水燈,感覺非常漂亮,準備做一個送給女友。
受限于網絡資料有限,浪費了不少的錢還浪費了不少的精力。
在這里分享給各位,希望能給大家帶來一點幫助。
自己寫的程序,使用了8*8*8個led小燈。
既可以使用220V電源供電也可以使用充電寶供電。
電路圖奉上
主要使用的是74hc595芯片的聯級,當時為了找合適的芯片可是費了好大功夫。
真的是從o開始學的,推薦學習視頻https://www.bilibili.com/video/B ... story.content.click
實物展示
夜晚效果最好
外殼
制作過程,視頻展示,自己賬號(理科生的浪漫)有哪個女孩子能拒絕這樣的生日禮物呢基于51單片機的8手工八階流水燈
部分單片機代碼展示
#include "74HC595.h"
#include "math.h"
#include "showfont.h"
#include "various.H"
#include "key.h"
unsigned char team = 0;
unsigned char fast = 25;
unsigned char tiao = 0;
unsigned char style = 0,biao = 0,folt2 = 0,folt_2 = 0,folt3 = 0,folt4 = 0,folt7 = 0;
unsigned char anxia = 0,anxia_t = 0;
unsigned char Time0_count = 0,Time1_count = 0,Time2_count = 0,Time3_count = 0;
void Show_style(void)
{
switch(style)
{
case 0:
fast=15;UPorDOWN(Rotate_buf[0],Time0_count); //跳動的心
break;
case 1:
fast=80;RotateS(1,Time0_count); //旋轉
break;
case 2:
fast=15;Show_rain2(Time0_count); //代碼(小)雨
break;
case 3:
fast=40;CubeL(Time0_count); //縮放空心正方體
break;
case 4:
fast=40;Cube(Time0_count); //縮放實心正方體
break;
case 5:
fast=40;Show_rain(Time0_count); //代碼(大)雨
break;
case 6:
fast=8;all_Surface(Time3_count); //基面平移
break;
case 7:
fast=10;Cube2(Time3_count); //縮放實心正方體2
break;
}
if(TR0 == 0)
{
if(++Time0_count > 15)
{
if(++folt7 == 7)folt7 = 0;
Time0_count=0;
}
if(++Time1_count > 28)Time1_count=0;
if(++Time2_count > 21)Time2_count=0;
if(++Time3_count > 13)
{
Time3_count = 0;
if(++folt2 == 2)folt2 = 0;
if(++folt4 == 4)
{
folt4 = 0;
if(++folt3 == 3)folt3 = 0;
}
}
TR0 = 1;
}
}
///////////**********************************/////////////////////
///////////**********************************/////////////////////
///////////**********************************/////////////////////
///////////**********************************/////////////////////
void main()
{
HC_595_Init();
Clean(); //清除 外部芯片寄存器
Time0_Init();
style = 255;
while(!Show_word(Time1_count) && (!Scan_key() || Time0_count<4))
Show_style();
Clean(); //清除 外部芯片寄存器
Time0_count = Time3_count = style = 0;
while(1)
{
Show_all();
key_anxia();
xiaoyedeng();
Show_style();
}
}
/*************************按鍵****************************/
bit Scan_key(void)
{
static bit state=0;
if(!state && !KEY)
{
delay(2000);
if(!KEY)
{
state = 1;
return 1;
}
}
if(KEY) state = 0;
return 0;
}
void key_anxia()
{
if(Scan_key())
{
Clean();
folt4 = 0;
Time0_count=0;
Time1_count=0;
Time3_count=0;
if(++style > 9) style=0;
if(KEY==0)
{
anxia = team = 0;
while(KEY==0)Show_style();
anxia_t=anxia;
if(anxia_t>2)
{
Time2_count = 0;
biao = style;
style = 100;
}
anxia_t= 0;
}
}
}
/**********************定 時 器***************************/
void Time0() interrupt 1
{
static unsigned char count=0;
TH0 = (65535 - 50000) / 256;
TL0 = (65535 - 50000) % 256;
if(++team>=20)
{
team=0;
if(++anxia>=4)anxia=4;
}
if(++count > fast) {TR0 = 0,count=0;}
}
/************************************varoious********************************************/
void Show_all() //-------------------全 部 循 環---------------
{
if(style==9)
{
while(!Scan_key())Show_layer_T(Rotate_buf[2],6);
}
if(style==8)
{
folt7 = 0;tiao = 0;
while(!Scan_key())
{
if(Scan_key()&&tiao == 1)
{style = 9;break;}
else if(tiao == 1)break;
if(folt7 == 0) style = 0;
else if(folt7 == 1)style = 2;
else if(folt7 == 2)style = 2;
else if(folt7 == 3)style = 3;
else if(folt7 == 4)style = 4;
else if(folt7 == 5)style = 5;
else if(folt7 == 6)
{
Time3_count = 0;folt4 = 0;
while(!Scan_key())
{
if(tiao == 1)break;
if(folt3 == 0)style = 6;
else if(folt3 == 1)style = 7;
else
while(!Scan_key())
{
if(Scan_key())tiao = 1;
else {tiao = 1;style = 8;}
if(tiao == 1)break;
}
Show_style();
}
}
Show_style();
}
}
}
void xiaoyedeng()//小夜燈,長按
{
if(style==100)
{
fast=25;
while(!SCY521(Time2_count))Show_style();
while(!SCY521_(Time2_count))Show_style();
while(style==100)
{
led_all();
key_anxia();
}
style = biao;biao=0;
}
}
void led_all()
{
unsigned char code a[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
GND = 0;
LED16_HC595_drive(a);
}
//void scroll(unsigned char state) //滾動------------(有問題)
//{
// static unsigned char Show_[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
// state %= 16;
// if(state < 4)
// {
// Show_[0] = 0;Show_[1] = 0;Show_[2] = 0;Show_[3] =0; Show_[4] = 0;Show_[5] = 0; Show_[6] = 0;
// Show_[7] = 0xff;
// GND = 0;
// }
// else if(state < 8)
// {
// Show_[0] =0xff; Show_[1] =0xff; Show_[2] = 0xff;Show_[3] = 0xff;Show_[4] = 0xff;Show_[5] = 0xff; Show_[6] = 0xff;Show_[7] = 0xff;
// GND = ~0x80;
// }
// else if(state < 12)
// {
// Show_[1] = 0x00;Show_[2] = 0x00;Show_[3] = 0x00;Show_[4] = 0x00;Show_[5] = 0x00;Show_[6] = 0x00;Show_[7] = 0x00;
// Show_[0] = 0xff;
// GND = ~0;
// }
// else
// {
// Show_[1] = 0xff;Show_[2] = 0xff;Show_[3] = 0xff;Show_[4] = 0xff;Show_[5] = 0xff;Show_[6] = 0xff;Show_[7] = 0xff;
// GND = 0x01;
// }
// LED16_HC595_drive(Show_);
//}
void Show_layer(unsigned char *image,unsigned char lay) //顯示某 上 層圖像
{
LED16_HC595_drive(image);
P0 = ~(0x01<<(lay));
}
void Show_layer_T(unsigned char *image, unsigned char lay) //顯示某 正 層圖像
{
unsigned char j,i,k;
unsigned char showbuf[8]={0,0,0,0,0,0,0,0};
for(j=0; j<8; j++)
{
k = image[7-j];
for(i=0; i<8;i++)
showbuf[lay] |= (((k>>i)&0x01)<<(7-i));
Show_layer(showbuf,j);
showbuf[lay] = 0;
}
}
void Show_layer_T2(unsigned char *image, unsigned char lay) //顯示某 正 2層圖像
{
unsigned char j,i,k;
unsigned char showbuf[8]={0,0,0,0,0,0,0,0};
for(j=0; j<8; j++)
{
k=image[7-j];
for(i=0;i<8;i++)
{
showbuf[lay] |= (((k>>i)&0x01)<<(7-i));
showbuf[lay+1] |= (((k>>i)&0x01)<<(7-i));
}
Show_layer(showbuf,j);
showbuf[lay] = 0;
showbuf[lay+1] = 0;
}
}
//void Show_layer_L(unsigned char *image, unsigned char lay) //顯示某 側 層圖像
//{
// unsigned char i,j;
// unsigned char showbuf[8]={0,0,0,0,0,0,0,0};
// for(i=0; i<8; i++)
// {
// for(j=0; j<8; j++)
// {
// showbuf[j] = (image[7-i]>>j)&0x01;
// showbuf[j] <<= lay;
// }
// Show_layer(showbuf,i);
// }
//}
void Show_2L(unsigned char *image) //顯示
{
unsigned char i,j;
unsigned char showbuf[8]={0,0,0,0,0,0,0,0};
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
showbuf[j] = (image[7-i]>>j)&0x01;
showbuf[j] <<= 7;
showbuf[j] |= showbuf[j] >>= 7;
}
Show_layer(showbuf,i);
}
}
unsigned char Read_point(unsigned char *imag, unsigned char x, unsigned char y)
{
unsigned char buf;
buf = imag[y];
buf = (buf>>(7-x))&0x01;
return buf;
}
void Write_point(unsigned char *imag, unsigned char x, unsigned char y,unsigned char dat)
{
imag[y] = (imag[y]&(~(0x01<<(7-x)))) | (dat<<(7-x));
}
void Square(u8 *imag, u8 x1, u8 y1, u8 x2, u8 y2) //正方面
{
unsigned char y3;
for(; x1<=x2; x1++)
{
y3 = y1;
for(y3; y3<=y2; y3++)
{
Write_point(imag,x1,y3,1);
}
}
}
// ---- 線 正 方 體 -----
void SquareL(u8 *imag, u8 x1, u8 y1, u8 x2, u8 y2)
{
unsigned char i=0;
for(i=x1; i<=x2; i++)
{
Write_point(imag,i,y1,1);
Write_point(imag,i,y2,1);
}
for(i=y1; i<=y2; i++)
{
Write_point(imag,x1,i,1);
Write_point(imag,x2,i,1);
}
}
void SquareP(u8 *imag, u8 x1, u8 y1, u8 x2, u8 y2)
{
Write_point(imag,x1,y1,1);
Write_point(imag,x1,y2,1);
Write_point(imag,x2,y1,1);
Write_point(imag,x2,y2,1);
}
// ------ 旋 轉 --------
unsigned char* Rotate(unsigned char *imag, unsigned char state)
{
unsigned char showbuf[8]={0,0,0,0,0,0,0,0};
unsigned char i,j,k;
unsigned char x,y;
state %= 4;
if(1<=state && state <=3)
{
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(state == 1) x=i,y=j;
else if(state == 2) x=j,y=7-i;
else if(state == 3) x=7-i,y=7-j;
k = Read_point(imag,7-j,i);
if(!k) continue;
Write_point(showbuf,x,y,k);
}
}
return showbuf;
}
else
return imag;
}
void Clean(void) //清除 外部芯片寄存器
{
unsigned char showbuf[8];
unsigned char i;
for(i=0; i<8; i++)
{
Show_layer(showbuf,i);
}
}
/* ===================================================== */
//--- 代 碼 雨 ---
void Show_rain(unsigned char state)
{
unsigned char i;
state%=8;
for(i=0; i<8; i++)
{
Show_layer(rain_buf[7 - state],i);
if(++state > 7) state = 0;
}
}
//--- 代 碼 雨 2 ---
void Show_rain2(unsigned char state)
{
unsigned char i;
state%=8;
for(i=0; i<8; i++)
{
Show_layer(rain_buf2[7 - state],i);
if(++state > 7) state = 0;
}
}
// ------- 縮 放 正 方 體 -----
void Cube(unsigned char state) //縮 放 正 方 體 -----
{
unsigned char Show_Buf[8] = {0,0,0,0,0,0,0,0};
state %= 8;
if(state >= 4) state = 7-state;
Square(Show_Buf, 3-state, 3-state, 4+state, 4+state);
LED16_HC595_drive(Show_Buf); GND = ( (0xE0<<state) | (0x07>>state) );
}
void Cube2(unsigned char state) //縮 放 正 方 體2 -----
{
unsigned char Show_Buf[8] = {0,0,0,0,0,0,0,0};
state %= 14;
if(folt4 == 0)
{
if(state < 8)Square(Show_Buf,0,0,state,state);
else
{
state = 14-state;
Square(Show_Buf,7-state,0,7,state);
}
LED16_HC595_drive(Show_Buf);
GND = 0xfe<<state;
}
else if(folt4 == 1)
{
if(state < 8) {Square(Show_Buf,7-state,0,7,state);GND = 0xfe<<state;}
else
{
state = 14-state;
Square(Show_Buf,0,0,state,state);
GND = 0x7f>>state;
}
LED16_HC595_drive(Show_Buf);
}
else if(folt4 == 2)
{
if(state < 8){Square(Show_Buf,0,0,state,state);}
else
{
state = 14-state;
Square(Show_Buf,7-state,0,7,state);
}
LED16_HC595_drive(Show_Buf);
GND = 0x7f>>state;
}
else if(folt4 == 3)
{
if(state < 8) {Square(Show_Buf,7-state,0,7,state);GND = 0x7f>>state;}
else
{
state = 14-state;
Square(Show_Buf,0,0,state,state);
GND = 0xfe<<state;
}
LED16_HC595_drive(Show_Buf);;
}
}
// ----- 縮 放 線 正方體 ------
void CubeL(unsigned char state)
{
unsigned char Show_Buf[8] = {0,0,0,0,0,0,0,0};
unsigned char i = 0;
state %= 8;
if(state >= 4) state = 7-state;
SquareL(Show_Buf, 3-state, 3-state, 4+state, 4+state);
GND = 0xff;LED16_HC595_drive(Show_Buf); GND = (~((0x10<<state) | (0x08>>state)));delay(1000);GND = 0xff;
Show_Buf[0]=Show_Buf[1]=Show_Buf[2]=Show_Buf[3]=
Show_Buf[4]=Show_Buf[5]=Show_Buf[6]=Show_Buf[7]=0;
SquareP(Show_Buf, 3-state, 3-state, 4+state, 4+state);
GND = 0xff;LED16_HC595_drive(Show_Buf); GND = ( (0xf0<<state) | (0x0f>>state) );delay(1000);GND = 0xff;
}
void all_Surface(unsigned char state)// ------ 基 面 平 移 ------
{
if(folt4==0)Surface0(state);
if(folt4==1)Surface2(state);
if(folt4==2)Surface3(state);
if(folt4==3)Surface3(state);
}
void Surface0(unsigned char state) ////-------前后---------
{
unsigned char Show_[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
state %= 14;
GND = 0;
if(state <= 7)
{
Show_[state-1]=0;
Show_[state]=0xff;
LED16_HC595_drive(Show_);
}
else
{
Show_[14-state]=0;
Show_[13-state]=0xff;
LED16_HC595_drive(Show_);
}
}
//void Surface1(unsigned char state) ////-------上下---------上下基面平移(硬件問題,如果小燈發光不正常,請及時重啟,以免燒壞內部電路)
//{
// unsigned char Show_[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
// state %= 14;
// if(state < 7)Show_layer(Show_,state);
// else Show_layer(Show_,13-state);
//}
void Surface2(unsigned char state) ////-------左右---------
{
unsigned char Show_[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
state %= 14;
GND = 0;
if(state <= 7)
{
Show_[0] = 0x01 << state;
Show_[1] = 0x01 << state;
Show_[2] = 0x01 << state;
Show_[3] = 0x01 << state;
Show_[4] = 0x01 << state;
Show_[5] = 0x01 << state;
Show_[6] = 0x01 << state;
Show_[7] = 0x01 << state;
LED16_HC595_drive(Show_);
}
else
{
Show_[0] = 0x80 >> state-6;
Show_[1] = 0x80 >> state-6;
Show_[2] = 0x80 >> state-6;
Show_[3] = 0x80 >> state-6;
Show_[4] = 0x80 >> state-6;
Show_[5] = 0x80 >> state-6;
Show_[6] = 0x80 >> state-6;
Show_[7] = 0x80 >> state-6;
LED16_HC595_drive(Show_);
}
}
void Surface3(unsigned char state) ////-------斜面---------
{
unsigned char Show_[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
state %= 14;
GND = 0;
if(state < 7)
{
if(folt2==0)
{
Show_[0] = 0x80 >> state-0;
Show_[1] = 0x80 >> state-1;
Show_[2] = 0x80 >> state-2;
Show_[3] = 0x80 >> state-3;
Show_[4] = 0x80 >> state-4;
Show_[5] = 0x80 >> state-5;
Show_[6] = 0x80 >> state-6;
Show_[7] = 0x80 >> state-7;
}
else if(folt2==1)
{
Show_[0] = 0x01 << state-0;
Show_[1] = 0x01 << state-1;
Show_[2] = 0x01 << state-2;
Show_[3] = 0x01 << state-3;
Show_[4] = 0x01 << state-4;
Show_[5] = 0x01 << state-5;
Show_[6] = 0x01 << state-6;
Show_[7] = 0x01 << state-7;
}
LED16_HC595_drive(Show_);
}
else
{
if(folt2==0)
{
Show_[0] = 0x01 << 6-state;
Show_[1] = 0x01 << 7-state;
Show_[2] = 0x01 << 8-state;
Show_[3] = 0x01 << 9-state;
Show_[4] = 0x01 << 10-state;
Show_[5] = 0x01 << 11-state;
Show_[6] = 0x01 << 12-state;
Show_[7] = 0x01 << 13-state;
}
else if(folt2==1)
{
Show_[0] = 0x80 >> 6-state;
Show_[1] = 0x80 >> 7-state;
Show_[2] = 0x80 >> 8-state;
Show_[3] = 0x80 >> 9-state;
Show_[4] = 0x80 >> 10-state;
Show_[5] = 0x80 >> 11-state;
Show_[6] = 0x80 >> 12-state;
Show_[7] = 0x80 >> 13-state;
}
LED16_HC595_drive(Show_);
}
}
// ----- 顯 示 字 -----
bit Show_word(unsigned char state)
{
static bit floag = 0;
state %= 28;
if(state == 27) floag = 1;
if(floag == 1 && state == 0) return 1;
if(state<7) Show_layer_T2(WordFont[0], state);
else if(state<14) Show_layer_T2(WordFont[1],state-7);
else if(state<21) Show_layer_T2(WordFont[2],state-14);
else Show_layer_T2(WordFont[3],state-21);
return 0;
}
bit SCY521(unsigned char state)
{
static bit floag = 0;
state %= 21;
if(state == 20) floag = 1;
if(floag == 1 && state == 0){ floag = 0; return 1;}
if(state<7) Show_layer_T2(WordFont[4],state);
else if(state<14) Show_layer_T2(WordFont[5],state-7);
else Show_layer_T2(WordFont[6],state-14);
return 0;
}
bit SCY521_(unsigned char state)
{
static bit floag = 0;
state %= 21;
if(state == 20) floag = 1;
if(floag == 1 && state == 0){ floag = 0; return 1;}
if(state<7) Show_layer_T2(WordFont[7],state);
else if(state<14) Show_layer_T2(WordFont[8],state-7);
else Show_layer_T2(WordFont[9],state-14);
return 0;
}
// ------ 旋 轉 -------
void RotateS(unsigned char w,unsigned char state)
{
unsigned char *p;
state%=4;
p = Rotate(Rotate_buf[w],state);
Show_layer(p,7);
GND = 0;
}
// ------- 下 降 上 升 ------
void UPorDOWN(unsigned char *imag,unsigned char state)
{
unsigned char showbuf[8]={0,0,0,0,0,0,0,0};
unsigned char i,j;
state %= 8;
if(state>3) state = 7-state;
for(i=state,j=0; i<8; i++,j++)
{
showbuf[j]=imag[ i];
}
Show_layer_T2(showbuf,6);
Show_2L(Rotate_buf[3]);
}
// -- time0_init --
void Time0_Init(void)
{
TH0 = (65535 - 50000) / 256;
TL0 = (65535 - 50000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
|