![]() |
馬上學習 發(fā)表于 2024-10-31 10:58 是有點毛病,delay應該放在第二個for循環(huán)里面,大意了 |
應該提出要求:比如燈具體要怎么流動,從左到右,從上到下,單個點亮,逐個點亮 |
謝謝Graves,你優(yōu)化后只能8只燈循環(huán) 謝謝飛云居士,你的優(yōu)化很好 |
飛云居士 發(fā)表于 2024-10-25 18:26 謝謝指導 |
#include <reg51.h> // AT89C51寄存器定義 // 定義LED點陣連接的端口 #define ROW P0 // 行連接到P0 #define COL P1 // 列連接到P1 // 延時函數 void delay(unsigned int time) { unsigned int i, j; for(i = 0; i < time; i++) for(j = 0; j < 120; j++); } // 流水燈顯示函數 void display() { unsigned char i, j; unsigned char row_pattern[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; // 行數據 unsigned char col_pattern[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; // 列數據 for(i = 0; i < 8; i++) { // 遍歷每一行 ROW = row_pattern[i]; // 設置當前行 for(j = 0; j < 8; j++) { COL = ~col_pattern[j]; // 設置當前列并反轉,點亮對應的LED delay(50); // 控制流水燈的速度 } } } void main() { while(1) { display(); // 不斷顯示流水燈效果 } } 代碼說明: 行與列的控制:ROW和COL分別定義了點陣的行和列連接的端口,程序中通過簡單的掃描來實現流水燈效果。 行列數據:row_pattern和col_pattern數組用于定義哪一行、哪一列的LED應該點亮。 延時:通過delay()函數控制流水燈的速度。 |
#include<reg51.h> const unsigned char P1_arry[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; const unsigned char P3_arry[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; void delay(unsigned int i) { while(i--); } void main() { u8 i; u8 j; while(1) { for(i=0;i<8;i++) { p3 = P3_arry[i]; for(j=0;j<8;j++) { p1 = P1_arry[j]; } delay(3000); } } } |