Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* https://www.reddit.com/r/olkb/comments/vthz1k/help_with_my_custom_matrix_for_demultiplexer/
- * Code to scan a keyboard matrix using three 74HC238 demultiplexer chips.
- */
- /* This should go into config.h
- *
- * #define MATRIX_ROWS 11
- * #define MATRIX_COLS 20
- *
- * #define MATRIX_ROW_PINS {B3, A10, B15, B14, B9, B12, B4, B5, B6, B7, B8}
- * #define MATRIX_COL_PINS {NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN}
- *
- * #define DMUX_SELECT_PINS {A5, C13, C14}
- * #define NUM_DMUX_SELECT_PINS 3
- *
- * #define DMUX_ENABLE_PINS {C15, A0}
- * #define NUM_DMUX_ENABLE_PINS 2
- *
- */
- // In matrix.c ----------------------------------------------------------------
- /* This keyboard uses three 74HC238 demultiplexer chips to scan the columns of its matrix.
- * Columns 0-7 will use the first chip, enabled by a pullup resistor.
- * Columns 8-15 will use the second chip by writing to its enable pin, disabling the other 2 chips.
- * Columns 8-19 will use the third chip by writing to its enable pin, disabling the other 2 chips.
- *
- * The select pins are then used to produce output at the specified column.
- */
- #include <stdint.h>
- #include <stdbool.h>
- #include "quantum.h"
- #include "wait.h"
- #include "matrix.h"
- // Arrays of the pins to be used
- static const pin_t row_pins[MATRIX_ROWS] = ROW_PINS;
- static const pin_t dmux_select_pins[NUM_DMUX_SELECT_PINS] = DMUX_SELECT_PINS;
- static const pin_t dmux_enable_pins[NUM_DMUX_ENABLE_PINS] = DMUX_ENABLE_PINS;
- // Initialize pins to be used for matrix scanning
- void matrix_init_custom(void) {
- // Set all row_pins to inputs, with pulldown resistors to detect HIGH values from columns
- for (int row = 0; row < MATRIX_ROWS; row++) {
- setPinInputLow(row_pins[row]);
- }
- // Set all dmux_select_pins to outputs, and write LOW
- for (int pin = 0; pin < NUM_DMUX_SELECT_PINS; pin++) {
- setPinOutput(dmux_select_pins[pin]);
- writePinLow(dmux_select_pins[pin]);
- }
- // Set all dmux_enable_pins to outputs, and write LOW
- for (int pin = 0; pin < NUM_DMUX_ENABLE_PINS; pin++) {
- setPinOutput(dmux_enable_pins[pin]);
- writePinLow(dmux_enable_pins[pin]);
- }
- }
- /* In binary, the first 3 bits of a number refer to its value between 0-7
- * These are written to the dmux_select_pins.
- *
- * The 4th bit is active for columns 8-15, in which case the first dmux_enable_pin is set
- * The 5th bit is active for columns 16-19, in which case the second dmux_enable_pin is set
- */
- // Write dmux pins in order to produce output at specified column
- static void select_column(uint8_t col_index) {
- writePin(dmux_select_pins[0], col_index & 0b001); // is 1st bit of col_index true?
- writePin(dmux_select_pins[1], col_index & 0b010); // is 2nd bit of col_index true?
- writePin(dmux_select_pins[2], col_index & 0b100); // is 3rd bit of col_index true?
- writePin(dmux_enable_pins[0], col_index & 8); // is 4th bit of col_index true?
- writePin(dmux_enable_pins[1], col_index & 16); // is 5th bit of col_index true?
- wait_us(0.015); // Wait 0.015 microseconds to propagate the signal
- }
- static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
- bool matrix_changed = false;
- // For each row
- for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
- // Store last value of row prior to reading
- matrix_row_t last_row_value = current_matrix[row_index];
- matrix_row_t current_row_value = last_row_value;
- // Check row pin state
- if (readPin(row_pins[row_index]) == 1) {
- // HIGH signal from column means a key is pressed, set column bit
- current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
- } else {
- // No signal from column means no key is pressed, clear column bit
- current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
- }
- // Determine if the matrix changed state, update current_matrix if needed
- if ((last_row_value != current_row_value)) {
- matrix_changed = true;
- current_matrix[row_index] = current_row_value;
- }
- }
- return matrix_changed; // True if any rows had a change, otherwise false
- }
- bool matrix_scan_custom(matrix_row_t current_matrix[]) {
- bool matrix_has_changed = false;
- // For each column
- for (uint8_t col = 0; col < MATRIX_COLS; col++) {
- // Write dmux_pins for current column
- select_column(col);
- // Read the rows for this column.
- // If any rows have changed, matrix_has_changed will be true
- matrix_has_changed |= read_rows_on_col(current_matrix, col);
- }
- return matrix_has_changed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement