|
#include <reg51.h> // AT89C51寄存器定義
// 定義LED點(diǎn)陣連接的端口
#define ROW P0 // 行連接到P0
#define COL P1 // 列連接到P1
// 延時(shí)函數(shù)
void delay(unsigned int time) {
unsigned int i, j;
for(i = 0; i < time; i++)
for(j = 0; j < 120; j++);
}
// 流水燈顯示函數(shù)
void display() {
unsigned char i, j;
unsigned char row_pattern[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; // 行數(shù)據(jù)
unsigned char col_pattern[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; // 列數(shù)據(jù)
for(i = 0; i < 8; i++) { // 遍歷每一行
ROW = row_pattern[i]; // 設(shè)置當(dāng)前行
for(j = 0; j < 8; j++) {
COL = ~col_pattern[j]; // 設(shè)置當(dāng)前列并反轉(zhuǎn),點(diǎn)亮對(duì)應(yīng)的LED
delay(50); // 控制流水燈的速度
}
}
}
void main() {
while(1) {
display(); // 不斷顯示流水燈效果
}
}
代碼說(shuō)明:
行與列的控制:ROW和COL分別定義了點(diǎn)陣的行和列連接的端口,程序中通過(guò)簡(jiǎn)單的掃描來(lái)實(shí)現(xiàn)流水燈效果。
行列數(shù)據(jù):row_pattern和col_pattern數(shù)組用于定義哪一行、哪一列的LED應(yīng)該點(diǎn)亮。
延時(shí):通過(guò)delay()函數(shù)控制流水燈的速度。 |
|