Quantcast
Viewing all articles
Browse latest Browse all 5439

SDK • Using PIO for a keyboard matrix

Hello!
I'm trying to create a board that has a 6x4 key matrix. Since the pico is doing a few other tasks, I thought I could offload the matrix scanning to the PIO. I found an example https://github.com/GitJer/Some_RPI-Pico ... matrix_4x4 online, modified it to my application, but cannot get it to work at all. I tested the hardware with non-PIO code and it worked fine, so I think it's something in my application.
This is the assembly code

Code:

.program button_matrixstart:.wrap_target    set pins 1 [31]  ; set 0001 on the 4 output pins (activate first row) and wait for the signal to stabilize    in pins 6        ; shift the input pins into the ISR    set pins 8 [31]  ; set 0010 on the 4 output pins (activate second row) and wait for the signal to stabilize    in pins 6        ; shift the input pins into the ISR    set pins 4 [31]  ; set 0100 on the 4 output pins (activate third row) and wait for the signal to stabilize    in pins 6        ; shift the input pins into the ISR    set pins 2 [31]  ; set 1000 on the 4 output pins (activate fourth row) and wait for the signal to stabilize    in pins 6        ; shift the input pins into the ISR.wrap
the code to initialize it:

Code:

        pio = pio1;    sm = pio_claim_unused_sm(pio, true);    for (int i = 0; i < 4; i++) {      pio_gpio_init(pio, base_output + i);    }    for (int i = 0; i < 6; i++) {      pio_gpio_init(pio, base_input + i);      gpio_pull_down(base_input + i);    }    // load the pio program into the pio memory    uint offset = pio_add_program(pio, &button_matrix_program);    // make a sm config    pio_sm_config c = button_matrix_program_get_default_config(offset);    sm_config_set_in_pins(&c, base_input);    pio_sm_set_consecutive_pindirs(pio, sm, base_output, 4, true);    sm_config_set_set_pins(&c, base_output, 4);    //6*4=24, so send it once it's full    sm_config_set_in_shift(&c, 1, 1, 24);    // init the pio sm with the config    pio_sm_init(pio, sm, offset, &c);    // enable the sm    pio_sm_set_enabled(pio, sm, true);
And the code to read it:

Code:

  // read the 4x4 matrix  int read(void) {    // value is used to read from the fifo    uint32_t value = 0;    // clear the FIFO, we only want a currently pressed key    pio_sm_clear_fifos(pio, sm);    // give the sm some time to fill the FIFO if a key is being pressed    sleep_ms(1);    // check that the FIFO isn't empty    if (pio_sm_is_rx_fifo_empty(pio, sm)) {      return -1;    }    // read one data item from the FIFO    return pio_sm_get(pio, sm);  }  
It seems to be able to differentiate between the input columns being read, but not the output rows, outputting things like 00001000001000000000000000000000 or 00000100000100000100000100000000, where there is a repeat every 6 numbers, as if the row is stuck on. I've tried autoshift on/off, shifting in manually, and a few other things with no changes. Am I misundering a part of the PIO system? Any help is greatly appreciated.

Statistics: Posted by miakizz — Sat Mar 30, 2024 4:18 am



Viewing all articles
Browse latest Browse all 5439

Trending Articles