7/31/2014

[Share] Code Led cube 8x8x8 - Code C - 8051 - 74HC595


Chip thấy trên mạng chưa có ai Share code led cube dùng 8051 + 74HC595 nên tính viết thử, nhưng thuật toán hiệu ứng load nặng quá..nhanh full bộ nhớ, Chip post lên chia sẻ để bạn nào có  ý tưởng hay thì phát triển tiếp, mong các bạn cũng chia sẻ lại. ^^


Code lập trình 74hc595:

#include <REGX51.H>//---------ChipKool_Online----www.echipkool.com---
#define xung P1_0
#define dulieu P1_1
#define chot P1_2
#define L1 P2_0
#define L2 P2_1
#define L3 P2_2
#define L4 P2_3
#define L5 P2_4
#define L6 P2_5
#define L7 P2_6
#define L8 P2_7
unsigned char temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8;
unsigned int j,p,y;
//----------------------
void delay(unsigned char ms )
{
   int l,m;
   for(l=0;l<ms;l++)
    {
 for(m=0;m<800;m++);

 }
}
//----------------------------
void hienthi(unsigned char x)
{
  int k,tam;
  for(k=0;k<8;k++)
     {
 tam=x;
 tam=tam&0x80;
 if(tam==0x80) dulieu=1;
 else dulieu=0;
 x=x<<1;
 xung=0;
 xung=1;
 }
}

//--------------------------------
void xuat(unsigned char temp1,unsigned char temp2,unsigned char temp3,unsigned char temp4,unsigned char temp5,unsigned char temp6,unsigned char temp7,unsigned char temp8)
{
hienthi(temp1);
hienthi(temp2);
hienthi(temp3);
hienthi(temp4);
hienthi(temp5);
hienthi(temp6);
hienthi(temp7);
hienthi(temp8);
chot=0;   //xuat du lieu ra output
chot=1;
delay(50);
}
//-----------------------

//------------------------
void sangchayduoi()
{

unsigned char code chay1[1][9]={{0x01,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}};
for(p=1;p<=1;p++)
{
for(j=1;j<=9;j++)
  {
  y=chay1[p][j];
  xuat(y,y,y,y,y,y,y,y);// sang cung luc
  delay(20);
  }
}

}
void sangchaydan()

unsigned char code chay2[1][9]={{0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff}};
for(p=1;p<=1;p++)
{
for(j=1;j<=9;j++)
  {
  y=chay2[p][j];
  xuat(y,y,y,y,y,y,y,y);// sang cung luc
  delay(20);
  }
}
}
//=============================
void chaychu()
{

unsigned char code quet[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char code chaychu[8][9]={{0x01,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff},
  {0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff}};
for(p=1;p<=8;p++)
{
for(j=1;j<=9;j++)
  {
  P2=quet[p];
  y=chaychu[p][j];
  xuat(0,0,0,0,0,0,0,y);// day chu a ra
  xuat(0,0,0,0,0,0,y,0);// day chu a ra
  xuat(0,0,0,0,0,y,0,0);// day chu a ra
  xuat(0,0,0,0,y,0,0,0);// day chu a ra
  xuat(0,0,0,y,0,0,0,0);// day chu a ra
  xuat(0,0,y,0,0,0,0,0);// day chu a ra
  xuat(0,y,0,0,0,0,0,0);// day chu a ra
  xuat(y,0,0,0,0,0,0,0);// day chu a ra
  delay(20);
  }
  
}

}
//-------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>----------------<<<<<<<<<<<<<<<<<<<<<<<
void sangchay()
{
unsigned char code chay[9]={0x01,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//for(p=1;p<=2;p++)
//{
L1=L2=L3=L4=L5=L6=L7=L8=1;
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,0,0,0,chay[j]);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,0,0,chay[j],0);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,0,chay[j],0,0);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,chay[j],0,0,0);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,chay[j],0,0,0,0);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,chay[j],0,0,0,0,0);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,chay[j],0,0,0,0,0,0);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(chay[j],0,0,0,0,0,0,0);
//delay(20);
}

//}
}
//=============================================================
void sangdan()
{
unsigned char code dan[9]={0x01,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
//for(p=1;p<=2;p++)
//{
L1=L2=L3=L4=L5=L6=L7=L8=1;
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,0,0,0,dan[j]);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,0,0,dan[j],255);// thay 1 bang 255 se sang.
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,0,dan[j],255,255);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,0,dan[j],255,255,255);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,0,dan[j],255,255,255,255);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,0,dan[j],255,255,255,255,255);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(0,dan[j],255,255,255,255,255,255);
//delay(20);
}
for(j=1;j<=9;j++)
{
xuat(dan[j],255,255,255,255,255,255,255);
//delay(20);
}

//}
}
//========================================
void chayvong()
{
// Canh 1
unsigned char z,q,o;
y=0x01;//00000001
xuat(0,0,0,0,0,0,0,y);
delay(20);
xuat(0,0,0,0,0,0,y,1);
delay(20);
xuat(0,0,0,0,0,y,1,1);
delay(20);
xuat(0,0,0,0,0,y,1,1);
delay(20);
xuat(0,0,0,0,y,1,1,1);
delay(20);
xuat(0,0,0,y,1,1,1,1);
delay(20);
xuat(0,0,y,1,1,1,1,1);
delay(20);
xuat(0,y,1,1,1,1,1,1);
delay(20);
xuat(y,1,1,1,1,1,1,1);
delay(20);
// canh 2
xuat(0x01,1,1,1,1,1,1,1);
delay(20);
xuat(0x03,1,1,1,1,1,1,1);
delay(20);
xuat(0x07,1,1,1,1,1,1,1);
delay(20);
xuat(0x0f,1,1,1,1,1,1,1);
delay(20);
xuat(0x1f,1,1,1,1,1,1,1);
delay(20);
xuat(0x3f,1,1,1,1,1,1,1);
delay(20);
xuat(0x7f,1,1,1,1,1,1,1);
delay(20);
xuat(0xff,1,1,1,1,1,1,1);
delay(20);
// canh 3
z=0x81;//10000001
xuat(0xff,z,1,1,1,1,1,1);
delay(20);
xuat(0xff,z,z,1,1,1,1,1);
delay(20);
xuat(0xff,z,z,z,1,1,1,1);
delay(20);
xuat(0xff,z,z,z,z,1,1,1);
delay(20);
xuat(0xff,z,z,z,z,z,1,1);
delay(20);
xuat(0xff,z,z,z,z,z,z,1);
delay(20);
xuat(0xff,z,z,z,z,z,z,z);
delay(20);
// canh 4
xuat(0xff,z,z,z,z,z,z,0x81);
delay(20);
xuat(0xff,z,z,z,z,z,z,0xc1);
delay(20);
xuat(0xff,z,z,z,z,z,z,0xe1);
delay(20);
xuat(0xff,z,z,z,z,z,z,0xf1);
delay(20);
xuat(0xff,z,z,z,z,z,z,0xf9);
delay(20);
xuat(0xff,z,z,z,z,z,z,0xfd);
delay(20);
xuat(0xff,z,z,z,z,z,z,0xff);
delay(20);
//canh 5
q=0x83;
xuat(0xff,z,z,z,z,z,q,0xff);
delay(20);
xuat(0xff,z,z,z,z,q,q,0xff);
delay(20);
xuat(0xff,z,z,z,q,q,q,0xff);
delay(20);
xuat(0xff,z,z,q,q,q,q,0xff);
delay(20);
xuat(0xff,z,q,q,q,q,q,0xff);
delay(20);
xuat(0xff,q,q,q,q,q,q,0xff);
delay(20);
//canh 6
xuat(0xff,0x83,q,q,q,q,q,0xff);
delay(20);
xuat(0xff,0x87,q,q,q,q,q,0xff);
delay(20);
xuat(0xff,0x8f,q,q,q,q,q,0xff);
delay(20);
xuat(0xff,0x9f,q,q,q,q,q,0xff);
delay(20);
xuat(0xff,0xbf,q,q,q,q,q,0xff);
delay(20);
xuat(0xff,0xff,q,q,q,q,q,0xff);
delay(20);
//canh 7
o=0xc3;//11000011
xuat(0xff,0xff,o,q,q,q,q,0xff);
delay(20);
xuat(0xff,0xff,o,o,q,q,q,0xff);
delay(20);
xuat(0xff,0xff,o,o,o,q,q,0xff);
delay(20);
xuat(0xff,0xff,o,o,o,o,q,0xff);
delay(20);
xuat(0xff,0xff,o,o,o,o,o,0xff);
delay(20);
//canh 8
xuat(0xff,0xff,o,o,q,q,0xe3,0xff);
delay(20);
xuat(0xff,0xff,o,o,o,q,0xf3,0xff);
delay(20);
xuat(0xff,0xff,o,o,o,o,0xfb,0xff);
delay(20);
xuat(0xff,0xff,o,o,o,o,0xff,0xff);
delay(20);
//canh 9














}
//=========================
void main()
{
while(1)
{
chayvong();
//sangchay(); // sang chay tung cot
//sangdan(); // sang dan tung cot
//sangchayduoi(); //
//sangchaydan();
//chaychu();

}
}



( Mô phỏng Protues 8.0 + Code C Compile By Keil C 4)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút  hiện ra ở góc phải phía trên, nhấn vào để đến trang download