本文為您講解基于51單片機的4*4*4光立方程序實現原理及程序代碼。LED光立方的復位電路、時鐘電路、每層LED燈電路控制邏輯,系統總原理圖,工作流程及相關C語言源碼實現。希望能夠對你學習了解LED光立方程序編寫及LED立方實體制作起到一定的幫助指導作用。
3.1單片機資源分配情況
將4*4*4的LED光立方分層4層,分別由單片機的P1.0,P1.1,P1.2,P1.3,四個口來控制每一層,且給低電平有效,再有P0口和P1的總共16個口來控制16列,給高電平有效,P0和P1口加上位排阻,再給每一列加三極管放大,三極管的基極接單片機的P0和P1口,集電極接5V電源,發射極接每一列。這樣就可以通過控制每個口的輸出信號來控制每個的亮滅。
3.2復位電路
通過某種方式,是單片機內部各類寄存器的值變為出事狀態的操作稱為復位。單片機的復位是通過外部電路來實現的,復位引腳RET通過一個施密特觸發器與復位電路相連接,施密特觸發器用作噪聲抑制,偉哥機器周期的S5P2時刻,復位電路采樣一次施密特輸出電平,獲得內部復位操作所需要的信號。單片機的時鐘電路正常工作后,CPU在RET/VPD引腳上連續采集到兩個機器周期的高電平后就可以完成復位操作了,但實際應用時,復位電平的脈沖寬度一般大于1ms。復位電路通常采用上電復位、手動按鍵復位和看門狗復位三種方式,本次采用的是上電復位方式。
3.3時鐘電路
單片機的各個功能部件的運行都是是時鐘控制信號為基準,一拍一拍的工作。因此時鐘頻率直接影響單片機的速度,時鐘電路的質量也直接影響單片機
系統的可靠性和穩定性。常用的時鐘電路設計為內部時鐘方式,單片機內部有一個由反向放大器構成的震蕩電路,芯片上的XTAL1和XTAL2分別為震蕩電路的輸入和輸出端。只要在這兩個引腳上接一個石英晶體振蕩器和兩個微調電容就構成內部方式的振蕩器電路,有振蕩器產生自激振蕩,便構成一個完整的震蕩信號發生器。
一般選用石英晶體振蕩器。此電路在加電大約延遲10ms后振蕩器起振,在XTAL2引腳產生幅度為3V左右的正弦波時鐘信號,其振蕩頻率主要由石英晶振的頻率確定。電路中兩個電容C1、C2的作用有兩個:一是幫助振蕩器起振;二是對振蕩器的頻率進行微調。本系統的C1、C2的值為33pf。
單片機在工作時,有內部振蕩器產生或由外直接輸入的送至內部控制邏輯單元的時鐘信號的周期稱為時鐘周期。其大小是時鐘信號頻率的倒數,f表示。圖2中的時鐘頻率為12MHz,即f=12MHz,則時鐘周期為1/12us。
附注: 三極管可以不接,只是會影響光源亮度。
3.4系統的工作原理圖
光立方一、二層:
光立方三、四層:
單片機口點連接:
三極管放大電路:
總原理圖:
3.5 焊接實物圖
元器件清單:
3.3 C語言程序
#include
#define uchar unsigned char
void delay100ms(uchar data1ms)
{uchar i,j,m;
for(i=0;i
for(j=0;j<110;j++)
for(m=0;m<100;m++)
;
}
void disp(uchar ceng,uchar lie1,uchar lie2 )
{
delay100ms(7);
P1=ceng;
P2=lie1;
P0=lie2;
}
void main(void)
{unsigned char hang,shu1;
unsigned char code C1[][16]={
{0x07,0x0B,0x0D,0x0E,0x0E,0x0C,0x08,0x00,0x00,0x08,0x0C,0x0E,0x0C,0x08,0x00,0x00},
{0x08,0x0C,0x0E,0x0C,0x08,0x00,0x00,0x08,0x0C,0x0E,0x0C,0x08,0x00,0x00,0x08,0x0C},
{0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E},
{0x0C,0x08,0x00,0x00,0x08,0x0C,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x0C,0x0E,0x0F,0x0E,0x0C,0x08,0x00},
{0x00,0x00,0x00,0x00,0x00,0x0D,0x0D,0x0D,0x05,0x09,0x09,0x09,0x0D,0x0D,0x0D,0x05},
{0x09,0x09,0x09,0x00,0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09},
{0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09}
};
unsigned char code L1[][16]={
{0xFF,0xFF,0xFF,0xFF,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x96,0x96,0x96,0x96,0x96},
{0x96,0x96,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC},
{0x01,0x03,0x07,0x0F,0x8E,0x8C,0x88,0x80,0x00,0x00,0x00,0x10,0x30,0x70,0x70,0x60},
{0x60,0x60,0x60,0x60,0x60,0x60,0x60,0xCC,0x0F,0x33,0x66,0xCC,0xF0,0x33,0x66,0xCC},
{0x66,0x33,0xF0,0xCC,0x66,0x33,0xF0,0xCC,0xCC,0xCC,0xCC,0xCC,0x60,0x60,0x60,0x60},
{0xE8,0x74,0x32,0x11,0x00,0x06,0x66,0x60,0x60,0x60,0x00,0x00,0x06,0x66,0x60,0x60},
{0x60,0x00,0x00,0x00,0x80,0x40,0x20,0x20,0x20,0x60,0x62,0x60,0x30,0x10,0x80,0xC0},
{0x60,0x60,0x60,0x60,0x64,0x60,0x30,0x10}
// {0x01,0x02,0x04,0x08,0x80,0x40,0x20,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//列掃描
};
unsigned char code L2[][16]={
{0xFF,0xFF,0xFF,0xFF,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x69,0x69,0x69,0x69,0x69},
{0x69,0x69,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33,0x33,0x33,0x33},
{0x00,0x00,0x00,0x00,0x00,0x08,0x88,0xC8,0xE8,0xF0,0x71,0x31,0x11,0x10,0x04,0x06},
{0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x33,0xF0,0xCC,0x66,0x33,0x0F,0xCC,0x66,0x33},
{0x66,0xCC,0x0F,0x33,0x66,0xCC,0x0F,0x33,0x33,0x33,0x33,0x33,0x06,0x06,0x06,0x06},
{0x8E,0x47,0x23,0x11,0x00,0x00,0x00,0x06,0x06,0x06,0x66,0x60,0x00,0x00,0x06,0x06},
{0x06,0x66,0x60,0x00,0x08,0x0C,0x06,0x66,0x06,0x06,0x06,0x06,0x03,0x01,0x00,0x08},
{0x04,0x64,0x06,0x06,0x06,0x06,0x03,0x01}
// {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x08,0x80,0x40,0x20,0x10}
};
while(1)
{
for(hang=0;hang<8;hang++)
{for(shu1=0;shu1<16;shu1++)
disp(C1[hang][shu1],L1[hang][shu1],L2[hang][shu1]);
}
// for(hang=0;hang<1;hang++)
// {
// for(shu1=0;shu1<16;shu1++)
// {shu2=0;
// disp(C1[shu2][shu2],L1[hang][shu1],L2[hang][shu1]);
// }
// }
}
}
評論
查看更多