第一次做光立方,走了些弯路,但好在做出来了,效果还不错。
材料清单:5mm雾状灯珠64个,9x15cm洞洞板,STC12C5A60S2单片机,20p插针两个,5VDC电源座一个,开关一个,导线若干。
直接用单片机驱动,不用任何其他芯片。灯一定不要贪小便宜,之前买的30块钱1000个3mm的灯珠,用之前是好的,焊成一排后发现很多都不亮,第一次做还以为因为没上电,结果焊到板子上还是不亮,耽误了几天,跟客服还吵了一架,郁闷。
采用层共阴束共阳接法,未用P0口驱动小灯,用P1和P3口控制束,P2口控制层。
写程序就容易多了,人眼的视觉暂留嘛,简单说就是把自己想要的动画分成帧,每一时帧后加延时就好了。
原理图
单片机源程序如下:
- #include<reg51.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- sbit led1=P2^4;
- sbit led2=P2^5;
- sbit led3=P2^6;
- sbit led4=P2^7;
- sbit gongyin1=P2^3; //最顶层
- sbit gongyin2=P2^2; //第三层
- sbit gongyin3=P2^1; //第二层
- sbit gongyin4=P2^0; //最底层
- uchar wei1[]={1,0,1,0,1,0,1,0,1};
- uchar wei2[]={0,1,1,1,0,1,1,1};
- uchar wei3[]={0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1};
- uchar gongyang10[]={0x0f,0x8e,0x8c,0x88,0x80,0x00,0x00,0x00,0x10,0x11,0x60,0x40,0x00,0x20};
- uchar gongyang30[]={0x00,0x00,0x08,0x88,0xc8,0xe8,0xf0,0x71,0x31,0x11,0x00,0x04,0x06,0x02};
- uchar gongyang11[]={0x01,0x33,0x77,0xff,0xe0,0x00,0x00,0x08,0xcc,0xee,0xff,0x70,0x00,0x00};
- uchar gongyang31[]={0x00,0x00,0x07,0xff,0xee,0xcc,0x80,0x00,0x00,0x0e,0xff,0x77,0x33,0x10};
- uchar gongyang12[]={0x48,0x44,0x22,0x21,0x30,0xc0};
- uchar gongyang32[]={0x12,0x22,0x44,0x84,0x0c,0x03};
- uchar gongyang13[]={0x0f,0x8e,0x8c,0x88,0x80,0x00,0x00,0x00,0x10,0x11,0x12,0x17};
- uchar gongyang33[]={0x00,0x00,0x08,0x88,0xc8,0xe8,0xf0,0x71,0x31,0x11,0x01,0x00};
- void delay(uint time) //延时函数
- {
- unsigned int i,j;
- for(i=0;i<time;i++)
- for(j=0;j<250;j++);
- }
- void display1() //动画1:全亮并闪烁
- {
- uchar i,j;
- j=10;
- while(j--)
- {
- for(i=0;i<2;i++)
- {
- gongyin1=wei1[i];
- gongyin2=wei1[i+2];
- gongyin3=wei1[i+4];
- gongyin4=wei1[i+6];
- P1=0XFF;
- P3=0XFF;
- delay(350);
- }
- }
- }
- void display2() //动画2:上下每层闪动
- {
- uint i,j;
- j=5;
- while(j--)
- {
- for(i=0;i<4;i++)
- {
- gongyin1=wei2[i];
- gongyin2=wei2[i+3];
- gongyin3=wei2[i+2];
- gongyin4=wei2[i+1];
- P1=0XFF;
- P3=0XFF;
- delay(800);
- }
- }
- }
- void display3() //动画3:从上旋转而下,一直有四个灯亮
- {
- uint i,j;
- for(i=0;i<4;i++)
- {
- gongyin1=wei2[i];
- gongyin2=wei2[i+3];
- gongyin3=wei2[i+2];
- gongyin4=wei2[i+1];
- for(j=0;j<9;j++)
- {
- P1=gongyang10[j];
- P3=gongyang30[j];
- delay(300);
- }
- }
- }
- void display4() //动画4:四面循环,由外向内
- {
- uint i,j;
- j=3;
- while(j--)
- {
- for(i=0;i<14;i++)
- {
- gongyin1=0;
- gongyin2=0;
- gongyin3=0;
- gongyin4=0;
- P1=gongyang10[i];
- P3=gongyang30[i];
- delay(350);
- }
- delay(200);
- }
- }
- void display5() //动画5:由外角向内点亮
- {
- uchar i=10;
- while(i--)
- {
- gongyin1=0;
- gongyin2=1;
- gongyin3=1;
- gongyin4=0;
- P1=0x09;
- P3=0x90;
- delay(450);
- gongyin1=1;
- gongyin2=0;
- gongyin3=0;
- gongyin4=1;
- P1=0x60;
- P3=0x06;
- delay(450);
- }
- }
- void display6() //动画6:角对角
- {
- uint i,j,k;
- k=1;
- while(k--)
- {
- for(i=0;i<14;i++)
- {
- gongyin1=wei3[i];
- gongyin2=wei3[i+6];
- gongyin3=wei3[i+5];
- gongyin4=wei3[i+4];
- P1=gongyang11[i];
- P3=gongyang31[i];
- delay(500);
- }
- for(j=13;j>0;j--)
- {
- gongyin1=wei3[j];
- gongyin2=wei3[j+6];
- gongyin3=wei3[j+5];
- gongyin4=wei3[j+4];
- P1=gongyang11[j];
- P3=gongyang31[j];
- delay(500);
- }
- delay(200);
- }
- }
- void display7() //动画7:一竖排旋转
- {
- uint i,j=
- j=5;
- while(j--)
- {
- for(i=0;i<6;i++)
- {
- gongyin1=0;
- gongyin2=0;
- gongyin3=0;
- gongyin4=0;
- P1=gongyang12[i];
- P3=gongyang32[i];
- delay(350);
- }
- delay(200);
- }
- }
- void display8() //动画8:竖排左右
- {
- uint a=2;
- while(a--)
- {
- …………
- …………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
- delay(70);
- }
- z3=3;
- while(z3--)
- {
- gongyin1=0;
- gongyin2=1;
- gongyin3=1;
- gongyin4=1;
- P1=0xff;
- P3=0xff;
- delay(15);
- gongyin1=1;
- gongyin2=1;
- gongyin3=1;
- gongyin4=0;
- P1=0x09;
- P3=0x00;
- delay(70);
- }
- z4=3;
- while(z4--)
- {
- gongyin1=0;
- gongyin2=1;
- gongyin3=1;
- gongyin4=1;
- P1=0xff;
- P3=0xff;
- delay(70);
- }
- }
- }
- void main()
- {
- while(1)
- {
- led1=1;
- led2=1;
- led3=1;
- led4=1;
- display1(); //动画1:全亮并闪烁
- display2(); //动画2:上下每层闪动
- display3(); //动画3:从上旋转而下,一直有四个灯亮
- display4(); //动画4:四面循环,由外向内
- display5(); //动画5:由外角向内点亮
- display6(); //动画6:角对角
- display7(); //动画7:一竖排旋转
- display16(); //动画16:四侧面循环
- display17(); //动画17:四底面循环-两排
- display8(); //动画8:竖排左右
- display9(); //动画9:左右波浪
- display10(); //动画10:前后波浪
- display18(); //动画18:四底面循环 -单排
- display11(); //动画11:X字形前后左右
- display6(); //动画6:角对角
- display19(); //动画19:随机下落
- display12(); //动画12:正方体大小变换
- display13(); //动画13:方框大小变换
- display14(); //动画14:方块随机移动
- display15(); //动画15:方块形成大方块
-
- }
- }
复制代码
所有资料51hei提供下载:
程序2.zip
(109.32 KB, 下载次数: 126)
|