本文為您講解基于51單片機(jī)的4*4*4光立方程序?qū)崿F(xiàn)原理及程序代碼。LED光立方的復(fù)位電路、時(shí)鐘電路、每層LED燈電路控制邏輯,系統(tǒng)總原理圖,工作流程及相關(guān)C語(yǔ)言源碼實(shí)現(xiàn)。希望能夠?qū)δ銓W(xué)習(xí)了解LED光立方程序編寫及LED立方實(shí)體制作起到一定的幫助指導(dǎo)作用。
3.1單片機(jī)資源分配情況
將4*4*4的LED光立方分層4層,分別由單片機(jī)的P1.0,P1.1,P1.2,P1.3,四個(gè)口來(lái)控制每一層,且給低電平有效,再有P0口和P1的總共16個(gè)口來(lái)控制16列,給高電平有效,P0和P1口加上位排阻,再給每一列加三極管放大,三極管的基極接單片機(jī)的P0和P1口,集電極接5V電源,發(fā)射極接每一列。這樣就可以通過(guò)控制每個(gè)口的輸出信號(hào)來(lái)控制每個(gè)的亮滅。
3.2復(fù)位電路
通過(guò)某種方式,是單片機(jī)內(nèi)部各類寄存器的值變?yōu)槌鍪聽顟B(tài)的操作稱為復(fù)位。單片機(jī)的復(fù)位是通過(guò)外部電路來(lái)實(shí)現(xiàn)的,復(fù)位引腳RET通過(guò)一個(gè)施密特觸發(fā)器與復(fù)位電路相連接,施密特觸發(fā)器用作噪聲抑制,偉哥機(jī)器周期的S5P2時(shí)刻,復(fù)位電路采樣一次施密特輸出電平,獲得內(nèi)部復(fù)位操作所需要的信號(hào)。單片機(jī)的時(shí)鐘電路正常工作后,CPU在RET/VPD引腳上連續(xù)采集到兩個(gè)機(jī)器周期的高電平后就可以完成復(fù)位操作了,但實(shí)際應(yīng)用時(shí),復(fù)位電平的脈沖寬度一般大于1ms。復(fù)位電路通常采用上電復(fù)位、手動(dòng)按鍵復(fù)位和看門狗復(fù)位三種方式,本次采用的是上電復(fù)位方式。
3.3時(shí)鐘電路
單片機(jī)的各個(gè)功能部件的運(yùn)行都是是時(shí)鐘控制信號(hào)為基準(zhǔn),一拍一拍的工作。因此時(shí)鐘頻率直接影響單片機(jī)的速度,時(shí)鐘電路的質(zhì)量也直接影響單片機(jī)
系統(tǒng)的可靠性和穩(wěn)定性。常用的時(shí)鐘電路設(shè)計(jì)為內(nèi)部時(shí)鐘方式,單片機(jī)內(nèi)部有一個(gè)由反向放大器構(gòu)成的震蕩電路,芯片上的XTAL1和XTAL2分別為震蕩電路的輸入和輸出端。只要在這兩個(gè)引腳上接一個(gè)石英晶體振蕩器和兩個(gè)微調(diào)電容就構(gòu)成內(nèi)部方式的振蕩器電路,有振蕩器產(chǎn)生自激振蕩,便構(gòu)成一個(gè)完整的震蕩信號(hào)發(fā)生器。
一般選用石英晶體振蕩器。此電路在加電大約延遲10ms后振蕩器起振,在XTAL2引腳產(chǎn)生幅度為3V左右的正弦波時(shí)鐘信號(hào),其振蕩頻率主要由石英晶振的頻率確定。電路中兩個(gè)電容C1、C2的作用有兩個(gè):一是幫助振蕩器起振;二是對(duì)振蕩器的頻率進(jìn)行微調(diào)。本系統(tǒng)的C1、C2的值為33pf。
單片機(jī)在工作時(shí),有內(nèi)部振蕩器產(chǎn)生或由外直接輸入的送至內(nèi)部控制邏輯單元的時(shí)鐘信號(hào)的周期稱為時(shí)鐘周期。其大小是時(shí)鐘信號(hào)頻率的倒數(shù),f表示。圖2中的時(shí)鐘頻率為12MHz,即f=12MHz,則時(shí)鐘周期為1/12us。
附注: 三極管可以不接,只是會(huì)影響光源亮度。
3.4系統(tǒng)的工作原理圖
光立方一、二層:
光立方三、四層:
單片機(jī)口點(diǎn)連接:
三極管放大電路:
總原理圖:
3.5 焊接實(shí)物圖
元器件清單:
3.3 C語(yǔ)言程序
#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]);
// }
// }
}
}
評(píng)論
查看更多