Max7219 + 8x8 LED Matrix
滚动显示 I ?? U。
算法很简单,首先定义滚动画面8x32矩阵。通过generate_8x8_matrix函数生成当前循环中应当显示的8x8矩阵,通过LedControl模块的setLed设置对应的点阵即可。
不要放太久,因为times是一个int型,并未进行溢出情况的处理。
1 #include <LedControl.h> 2 3 int DIN = 12; 4 int CS = 11; 5 int CLK = 10; 6 7 char TOTAL[8][32] = { 8 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0}, 9 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0}, 10 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0}, 11 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0}, 12 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0}, 13 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0}, 14 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0}, 15 {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0}, 16 }; 17 18 void generate_8x8_matrix(char in[8][32], int current_column, char *out_matrix) 19 { 20 int lines = 8; 21 int columns = 32; 22 int i = 0; 23 int j = 0; 24 for (i = 0; i < lines; ++i) 25 { 26 for (j = current_column; j < current_column + 8; ++j) 27 { 28 out_matrix[i * lines + j - current_column] = in[i][j % columns]; 29 } 30 } 31 } 32 33 34 LedControl lc = LedControl(DIN, CLK, CS, 4); 35 36 void setup() { 37 lc.shutdown(0, false); 38 lc.setIntensity(0, 8); 39 lc.clearDisplay(0); 40 } 41 42 int times = 0; 43 void loop() { 44 char matrix[64] = {0}; 45 generate_8x8_matrix(TOTAL, times++, matrix); 46 int i = 0; 47 int j = 0; 48 for (i = 0; i < 8; ++i) 49 { 50 for (j = 0; j < 8; ++j) 51 { 52 lc.setLed(0, i, j, matrix[(i * 8 + j)] == 1 ? true : false); 53 } 54 } 55 delay(100); 56 }
时间: 2024-10-05 04:44:52