光立方资料:首先是原理图:
PCB:
程序:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar z=5; //亮灭闪烁的次数
//sbit hang1=P2^7;/*行定义*/sbit ceng1=P1^7;/*层定义*/sbit lie1=P0^0;/*列定义*/
//sbit hang2=P2^6; sbit ceng2=P1^6; sbit lie2=P0^1;
//sbit hang3=P2^5; sbit ceng3=P1^5; sbit lie3=P0^2;
//sbit hang4=P2^4; sbit ceng4=P1^4; sbit lie4=P0^3;
//sbit hang5=P2^3; sbit ceng5=P1^3; sbit lie5=P0^4;
//sbit hang6=P2^2; sbit ceng6=P1^2; sbit lie6=P0^5;
//sbit hang7=P2^1; sbit ceng7=P1^1; sbit lie7=P0^6;
//sbit hang8=P2^0; sbit ceng8=P1^0; sbit lie8=P0^7;
void delay(uint s) //s毫秒延时
{
uint a1,a2;
for(a1=s;a1>0;a1--)
for(a2=120;a2>0;a2--);
}
void huayang1() //样式一 从顶层慢慢一层地往下亮。
{
uchar code led1[]={
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0x7e,0xbe,0xde,0xee,0xf6,0xfa,0xfc,
0x7c,0xbc,0xdc,0xec,0xf4,0xf8,
0x78,0xb8,0xd8,0xe8,0xf0,
0x70,0xb0,0xd0,0xe0,
0x60,0xa0,0xc0,
0x40,0x80,
0x00};
uchar b,b1=60;
P2=0XFF;
P0=0Xff;
for(b=0;b<36;b++)
{
P1=led1[b];
delay(b1);
}
}
void huayang2() //样式二 从底层慢慢一层地往上暗。
{
uchar code led1[]={
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0x7e,0xbe,0xde,0xee,0xf6,0xfa,0xfc,
0x7c,0xbc,0xdc,0xec,0xf4,0xf8,
0x78,0xb8,0xd8,0xe8,0xf0,
0x70,0xb0,0xd0,0xe0,
0x60,0xa0,0xc0,
0x40,0x80,
0x00};
uchar b,b1=60;
P0=0X00;
P2=0XFF;
P0=0Xff;
P2=0XFF;
for(b=35;b>0;b--)
{
P1=led1[b];
delay(b1);
}
P1=0x7f;
delay(51);
}
void huayang3() //样式三 从左边往右边一层一层地亮。
{
uchar code led2[]={
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};
uchar c,c2=100;
P1=0X00;
P2=0XFF;
for(c=0;c<36;c++)
{
c2=100;
P0=led2[c];
delay(c2);
c2=c2+c;
}
}
void huayang4() //样式四 从右边往左边一层一层地暗。
{
uchar code led2[]={
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};
uchar c,c2=100;
P1=0X00;
P2=0XFF;
for(c=35;c>0;c--)
{
c2=100;
P0=led2[c];
delay(c2);
c2=c2-c;
}
P0=0x01;
delay(c2);
}
void huayang5() //样式五 全部从暗慢慢的亮
{
uchar i,k,t,j;
k=0;
P0=0X00;
P2=0XFF;
P0=0XFF;
P1=0xff;
for(i=0;i<=170;i++)
{
if(0==(i%3)) k++;
for(t=0;t<=60;t++)
{
P1=0X00;
for(j=0;j<=60;j++)
if(j==k)
P1=0Xff;
}
}
}
void huayang6() //样式六 全部从亮慢慢的变暗
{
uchar i,k,t,j;
k=0;
P0=0X00;
P2=0XFF;
P0=0XFF;
P1=0xff;
for(i=0;i<=170;i++)
{
if(0==(i%3)) k++;
for(t=0;t<=60;t++)
{
P1=0XFF;
for(j=0;j<=60;j++)
if(j==k)
P1=0X00;
}
}
P1=0XFF;
delay(130);
}
void huayang7() //样式七 亮灭闪烁z次
{
P0=0X00;
P2=0XFF;
for(z;z>0;z--)
{
P2|=0Xff;
P0|=0XFF;
P1=0X00;
delay(130);
P2|=0Xff;
P0|=0XFF;
P1=0Xff;
delay(130);
}
}
void huayang8() //花样八 正四边形慢慢变小,直到消失
{
uchar sp1[]={0x00,0x81,0xc3,0xe7,0XFF};
uchar sp2[]={0xff,0x7e,0x3c,0x18,0XFF};
uchar aa=0;
P0=0X00;
P2=0XFF;
for(aa=0;aa<5;aa++)
{
P2=0XFF;
P0=0X00;
P2=sp2[aa];
P1=sp1[aa];
P0=sp2[aa];
delay(150);
}
}
void huayang9() //花样九 正四边形慢慢变大
{
uchar sp1[]={0xFF,0xe7,0xc3,0x81,0X00};
uchar sp2[]={0xFF,0x18,0x3c,0x7e,0XFF};
uchar aa=0;
P0=0X00;
P2=0XFF;
for(aa=0;aa<5;aa++)
{
P2=0XFF;
P0=0X00;
P2=sp2[aa];
P1=sp1[aa];
P0=sp2[aa];
delay(150);
}
}
void huayang10() //显示数字“0”
{
uchar shu1[]={0x3C,0x66,0x66,0x66,0x66,0x66,0x66,0x3C}; //"0"
uchar a,b,c,d,e;
c=0X03;
b=0x7f;
for(d=0;d<7;d++)
{
P0=0X00;
P2=0XFF;
P2=c;
c=_crol_(c,1);
e=4;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
e=120;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
void huayang11() //显示数字“1”
{
uchar shu1[]={0x7E,0x7E,0x18,0x18,0x18,0x18,0x1c,0x18}; //"1"
uchar a,b,c,d,e;
c=0X03;
b=0x7f;
for(d=0;d<7;d++)
{
P0=0X00;
P2=0XFF;
P2=c;
c=_crol_(c,1);
e=4;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
e=120;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
void huayang12() //显示数字“2”
{
uchar shu1[]={0x7E,0x7E,0x0c,0x18,0x30,0x66,0x7E,0x3c}; //"1"
uchar a,b,c,d,e;
c=0X03;
b=0x7f;
for(d=0;d<7;d++)
{
P0=0X00;
P2=0XFF;
P2=c;
c=_crol_(c,1);
e=4;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
e=120;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
void huayang13() //显示数字“3”
{
uchar shu1[]={0x3c,0x62,0x60,0x60,0x38,0x60,0x62,0x3c}; //"0"
uchar a,b,c,d,e;
c=0X03;
b=0x7f;
for(d=0;d<7;d++)
{
P0=0X00;
P2=0XFF;
P2=c;
c=_crol_(c,1);
e=4;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
e=120;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
void huayang14() //显示数字“4”
{
uchar shu1[]={0x30,0x30,0x7e,0x36,0x36,0x34,0x38,0x30}; //"0"
uchar a,b,c,d,e;
c=0X03;
b=0x7f;
for(d=0;d<7;d++)
{
P0=0X00;
P2=0XFF;
P2=c;
c=_crol_(c,1);
e=4;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
e=120;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
void huayang15() //显示数字“5”
{
uchar shu1[]={0x1C,0x3E,0x62,0x60,0x62,0x3E,0x06,0x3E}; //"0"
uchar a,b,c,d,e;
c=0X03;
b=0x7f;
for(d=0;d<7;d++)
{
P0=0X00;
P2=0XFF;
P2=c;
c=_crol_(c,1);
e=4;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
e=120;
while(e--)
{
for(a=0;a<8;a++)
{
P1=b;
P0=shu1[a];
b=_cror_(b,1);
delay(1);
}
}
}
void huayang16() //显示心
{
uchar shu60[]={0x00,0x00,0x18,0x3c,0x18,0x00,0x00,0x00}; //心
uchar shu61[]={0x00,0x00,0x66,0x7e,0x3c,0x18,0x00,0x00}; //心
uchar shu62[]={0x00,0x66,0xff,0xff,0x7e,0x3c,0x18,0x00}; //心
uchar a,b,n,m=10;
while(m--)
{
b=0xfe;
n=20;
while(n--)
{
for(a=0;a<8;a++)
{
P0=0X00;
P2=0X18;
P1=b;
P0=shu60[a];
b=_crol_(b,1);
delay(1);
}
}
b=0xfe;
n=5;
while(n--)
{
for(a=0;a<8;a++)
{
P0=0X00;
P2=0X18;
P1=b;
P0=shu61[a];
b=_crol_(b,1);
delay(1);
}
}
b=0xfe;
n=50;
while(n--)
{
for(a=0;a<8;a++)
{
P0=0X00;
P2=0X3c;
P1=b;
P0=shu62[a];
b=_crol_(b,1);
delay(1);
}
}
b=0xfe;
n=5;
while(n--)
{
for(a=0;a<8;a++)
{
P0=0X00;
P2=0X18;
P1=b;
P0=shu61[a];
b=_crol_(b,1);
delay(1);
}
}
}
}
void main()
{
delay(1500);
while(1)
{
/**/ huayang5();
huayang16();
huayang15();
huayang12();
huayang10();
huayang11();
huayang13();
huayang11();
huayang14();
huayang7();
huayang2();
huayang3();
huayang4();
huayang1();
delay(300);
huayang8();
delay(100);
huayang9();
huayang6();
}
}