Start typing to search

# Matrix Display Control with Arduino ## TL;DR; This Arduino script controls a matrix display by initializing the necessary pins, sending data to the display, and refreshing the display to show the desired pattern. The main functions include initializing the matrix, sending bytes of data, selecting rows, refreshing the display, setting pixel arrays, and clearing the display. ## Detailed Explanation ### Pin Definitions The script defines several pins for controlling the matrix display. Each pin has a specific role: ```cpp #define LED_PORT GPIOB #define LEDARRAY_D PB0 // Row selection, Bit 3 #define LEDARRAY_C PB1 // Row selection, Bit 2 #define LEDARRAY_B PB2 // Row selection, Bit 1 #define LEDARRAY_A PB3 // Row selection, Bit 0 #define LEDARRAY_G PB4 // Global enable #define LEDARRAY_DI PB5 // Data line #define LEDARRAY_CLK PB6 // Clock line #define LEDARRAY_LAT PB7 // Latch line ``` ### Display Buffer The display buffer is a 2D array that holds the data for the matrix display. It has 2 rows and 16 columns, each containing 32 bits: ```cpp unsigned long Display_Buffer[2][16]; ``` ### Function Definitions #### `void initMatrix()` This function initializes the pins and clears the display: ```cpp void initMatrix() { pinMode(LEDARRAY_D, OUTPUT); pinMode(LEDARRAY_C, OUTPUT); pinMode(LEDARRAY_B, OUTPUT); pinMode(LEDARRAY_A, OUTPUT); pinMode(LEDARRAY_G, OUTPUT); pinMode(LEDARRAY_DI, OUTPUT); pinMode(LEDARRAY_CLK, OUTPUT); pinMode(LEDARRAY_LAT, OUTPUT); digitalWrite(LEDARRAY_D, LOW); digitalWrite(LEDARRAY_C, LOW); digitalWrite(LEDARRAY_B, LOW); digitalWrite(LEDARRAY_A, LOW); digitalWrite(LEDARRAY_G, HIGH); // LEDs off digitalWrite(LEDARRAY_DI, LOW); digitalWrite(LEDARRAY_CLK, LOW); digitalWrite(LEDARRAY_LAT, LOW); clearDisplay(); // Reset display } ``` #### `void sendByte(unsigned char data)` This function sends a byte of data to the shift register. The data is inverted because 1 means off and 0 means on: ```cpp void sendByte(unsigned char data) { data = ~data; // Invert data, 1 is off, 0 is on for (int i = 0; i < 8; i++) { digitalWrite(LEDARRAY_CLK, LOW); digitalWrite(LEDARRAY_DI, (data & 0x80) ? HIGH : LOW); digitalWrite(LEDARRAY_CLK, HIGH); data <<= 1; } } ``` #### `void selectRow(unsigned char row)` This function selects a row for display by setting the appropriate pins: ```cpp void selectRow(unsigned char row) { digitalWrite(LEDARRAY_D, (row & 0x08) ? HIGH : LOW); digitalWrite(LEDARRAY_C, (row & 0x04) ? HIGH : LOW); digitalWrite(LEDARRAY_B, (row & 0x02) ? HIGH : LOW); digitalWrite(LEDARRAY_A, (row & 0x01) ? HIGH : LOW); } ``` #### `void refreshDisplay()` This function refreshes the display by sending data to the matrix. It iterates through each row, sends the data, and activates the row: ```cpp void refreshDisplay() { for (int row = 0; row < 16; row++) { digitalWrite(LEDARRAY_G, HIGH); // Disable LEDs during update // Send upper and lower 16 bits of the 32-bit wide matrix sendByte((Display_Buffer[1][row] >> 24) & 0xFF); // Upper 8 bits of upper 16 bits sendByte((Display_Buffer[1][row] >> 16) & 0xFF); // Middle 8 bits of upper 16 bits sendByte((Display_Buffer[1][row] >> 8) & 0xFF); // Lower 8 bits of upper 16 bits sendByte(Display_Buffer[1][row] & 0xFF); // Lower 8 bits sendByte((Display_Buffer[0][row] >> 24) & 0xFF); // Upper 8 bits of lower 16 bits sendByte((Display_Buffer[0][row] >> 16) & 0xFF); // Middle 8 bits of lower 16 bits sendByte((Display_Buffer[0][row] >> 8) & 0xFF); // Lower 8 bits of lower 16 bits sendByte(Display_Buffer[0][row] & 0xFF); // Lower 8 bits digitalWrite(LEDARRAY_LAT, HIGH); // Latch to store data digitalWrite(LEDARRAY_LAT, LOW); // Deactivate latch selectRow(row); // Activate row digitalWrite(LEDARRAY_G, LOW); // Enable LEDs delayMicroseconds(500); // Increased pause for better stability (adjust if needed) } } ``` #### `void setPixelArray(const unsigned long dat[1][16])` This function sets the pixel array for the display. It copies the provided data into the display buffer: ```cpp void setPixelArray(const unsigned long dat[1][16]) { long buffer[1][16] = { 0b0000000000000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; for (int i = 0; i < 16; i++) { Display_Buffer[0][i] = dat[0][i]; Display_Buffer[1][i] = buffer[0][i]; } } ``` #### `void clearDisplay()` This function clears the display by setting all values in the display buffer to zero: ```cpp void clearDisplay() { for (int i = 0; i < 16; i++) { Display_Buffer[0][i] = 0x00000000; Display_Buffer[1][i] = 0x00000000; } } ``` #### `void customExample()` This function provides a custom example pattern for the display. It first clears the display and then sets a specific pattern: ```cpp void customExample() { clearDisplay(); // REPLACE THIS ARRAY WITH YOUR OWN PATTERN (from the MatrixBuilder) unsigned long matrix[1][16] = { 0b1000000000000001, 0b0100000000000010, 0b0010000000000100, 0b0001111111111000, 0b0001000000001000, 0b0001011111101000, 0b0001010000101000, 0b0001010110101000, 0b0001010110101000, 0b0001010000101000, 0b0001011111101000, 0b0001000000001000, 0b0001111111111000, 0b0010000000000100, 0b0100000000000010, 0b1000000000000001, }; setPixelArray(matrix); } ``` ### Main Program The main program consists of the [`setup`](#void-setup) and [`loop`](#void-loop) functions. The [`setup`](#void-setup) function initializes the matrix, and the [`loop`](#void-loop) function continuously updates the display with the custom example pattern: ```cpp void setup() { initMatrix(); // Initialize matrix } void loop() { customExample(); refreshDisplay(); // Update display } ``` # Use with MatrixBuilder This script can be used with the MatrixBuilder tool to create custom patterns for the matrix display. The `customExample` function can be replaced with the pattern generated by the MatrixBuilder tool. The pattern should be copied into the `matrix` array in the `customExample` function. ## Step-by-Step Instructions 1. Create a custom pattern using the MatrixBuilder tool. [MatrixBuilder](https://test.jm26.net/16x16-matrix-editor/) 2. Copy the generated Binary Array from the MatrixBuilder tool. 3. Copy the code below into your and replace the `matrix` array with the copied Binary Array. 4. Upload the code to your board and see the custom pattern displayed on the matrix. ```cpp #include #define LED_PORT GPIOB #define LEDARRAY_D PB0 // Row selection, Bit 3 #define LEDARRAY_C PB1 // Row selection, Bit 2 #define LEDARRAY_B PB2 // Row selection, Bit 1 #define LEDARRAY_A PB3 // Row selection, Bit 0 #define LEDARRAY_G PB4 // Global enable #define LEDARRAY_DI PB5 // Data line #define LEDARRAY_CLK PB6 // Clock line #define LEDARRAY_LAT PB7 // Latch line void clearDisplay(); // Forward declaration // Initialize the matrix display void initMatrix() { pinMode(LEDARRAY_D, OUTPUT); pinMode(LEDARRAY_C, OUTPUT); pinMode(LEDARRAY_B, OUTPUT); pinMode(LEDARRAY_A, OUTPUT); pinMode(LEDARRAY_G, OUTPUT); pinMode(LEDARRAY_DI, OUTPUT); pinMode(LEDARRAY_CLK, OUTPUT); pinMode(LEDARRAY_LAT, OUTPUT); digitalWrite(LEDARRAY_D, LOW); digitalWrite(LEDARRAY_C, LOW); digitalWrite(LEDARRAY_B, LOW); digitalWrite(LEDARRAY_A, LOW); digitalWrite(LEDARRAY_G, HIGH); // Disable LEDs digitalWrite(LEDARRAY_DI, LOW); digitalWrite(LEDARRAY_CLK, LOW); digitalWrite(LEDARRAY_LAT, LOW); clearDisplay(); // Display reset } // Send a byte of data to the shift register void sendByte(unsigned char data) { data = ~data; // Invert data, 1 is off, 0 is on for (int i = 0; i < 8; i++) { digitalWrite(LEDARRAY_CLK, LOW); digitalWrite(LEDARRAY_DI, (data & 0x80) ? HIGH : LOW); digitalWrite(LEDARRAY_CLK, HIGH); data <<= 1; } } // Auswahl der Zeile void selectRow(unsigned char row) { digitalWrite(LEDARRAY_D, (row & 0x08) ? HIGH : LOW); digitalWrite(LEDARRAY_C, (row & 0x04) ? HIGH : LOW); digitalWrite(LEDARRAY_B, (row & 0x02) ? HIGH : LOW); digitalWrite(LEDARRAY_A, (row & 0x01) ? HIGH : LOW); } // Refresh the display void refreshDisplay() { for (int row = 0; row < 16; row++) { digitalWrite(LEDARRAY_G, HIGH); // Disable LEDs during update // Send upper and lower 16 bits of the 32-bit wide matrix sendByte((Display_Buffer[1][row] >> 24) & 0xFF); // Upper 8 bits of upper 16 bits sendByte((Display_Buffer[1][row] >> 16) & 0xFF); // Middle 8 bits of upper 16 bits sendByte((Display_Buffer[1][row] >> 8) & 0xFF); // Lower 8 bits of upper 16 bits sendByte(Display_Buffer[1][row] & 0xFF); // Lower 8 bits sendByte((Display_Buffer[0][row] >> 24) & 0xFF); // Upper 8 bits of lower 16 bits sendByte((Display_Buffer[0][row] >> 16) & 0xFF); // Middle 8 bits of lower 16 bits sendByte((Display_Buffer[0][row] >> 8) & 0xFF); // Lower 8 bits of lower 16 bits sendByte(Display_Buffer[0][row] & 0xFF); // Lower 8 bits digitalWrite(LEDARRAY_LAT, HIGH); // Activate latch to store data digitalWrite(LEDARRAY_LAT, LOW); // Deactivate latch selectRow(row); // Activate row digitalWrite(LEDARRAY_G, LOW); // Enable LEDs delayMicroseconds(100); // Increased pause for better stability (adjust if needed) } } void setPixelArray(const unsigned long dat[1][16]) { long buffer[1][16] = { 0b0000000000000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; for (int i = 0; i < 16; i++) { Display_Buffer[0][i] = dat[0][i]; Display_Buffer[1][i] = buffer[0][i]; } } // Clear the display void clearDisplay() { for (int i = 0; i < 16; i++) { Display_Buffer[0][i] = 0x00000000; Display_Buffer[1][i] = 0x00000000; } } void customExample() { unsigned long matrix[1][16] = { 0b0000000000000000, 0b0000111111110000, 0b0001000000001000, 0b0010000000000100, 0b0100111001110010, 0b0100101001010010, 0b0100111001110010, 0b0100000000000010, 0b0100000000000010, 0b0101000000001010, 0b0101000000001010, 0b0100100000010010, 0b0010011111100100, 0b0001000000001000, 0b0000111111110000, 0b0000000000000000, }; setPixelArray(matrix); } // Main program void setup() { initMatrix(); } void loop() { customExample(); refreshDisplay(); } ``` # 404 Page Not Found The page you are looking for does not exist. [Go back to the startpage](?)