Hi !
I'm currently trying to read value from digital encoder connected on a bit expander but i've not found much example how to proceed. I've create a table that i communicate to my function to read the value from spi but it doesnt show anything. Here is my code for the moment :
Do you know example which i could used to achieve this task ?
I'm currently trying to read value from digital encoder connected on a bit expander but i've not found much example how to proceed. I've create a table that i communicate to my function to read the value from spi but it doesnt show anything. Here is my code for the moment :
Code:
#include "pico/stdlib.h"#include <stdio.h>#include "tusb.h"#include "hardware/spi.h"#define CS_PIN 17 //chip select GPIO 17 #define MCP23S17_ADDRESS 0x20 //MCP23S17 ADDRESSvoid getMCP23S17Values(uint8_t* rx_buffer);int main(){ //Pin MCP23S17 const uint cs_pin = 17; const uint sck_pin = 18; const uint mosi_pin = 19; const uint miso_pin = 16; // Ports spi_inst_t *spi = spi0; stdio_init_all(); // Initialize CS pin high gpio_init(cs_pin); gpio_set_dir(cs_pin, GPIO_OUT); gpio_put(cs_pin, 1); // Set SPI format spi_set_format( spi0, // SPI instance 16, // Number of bits per transfer 1, // Polarity (CPOL) 1, // Phase (CPHA) SPI_MSB_FIRST); //SPI INTERFACE CONFIGURATION spi_init(spi0, 10000000); // SPI0 à 10 MHz gpio_set_function(cs_pin, GPIO_FUNC_SPI); // GPIO17 pour le signal CS gpio_set_function(sck_pin, GPIO_FUNC_SPI); // GPIO19 pour le signal CLK gpio_set_function(mosi_pin, GPIO_FUNC_SPI); // GPIO16 pour le signal MOSI -TX gpio_set_function(miso_pin, GPIO_FUNC_SPI); // GPIO17 pour le signal MISO -RX stdio_init_all(); /* MIDI_CHANNEL */ uint8_t midiChannel = 1; while (true) { tud_task(); uint8_t rx_buffer[14]; getMCP23S17Values(rx_buffer); // Interprétation des valeurs pour obtenir les encodeurs de 10 à 16 uint8_t enc10A = (rx_buffer[1] >> 0) & 1; uint8_t enc10B = (rx_buffer[1] >> 1) & 1; uint8_t enc11A = (rx_buffer[3] >> 0) & 1; uint8_t enc11B = (rx_buffer[3] >> 1) & 1; uint8_t enc12A = (rx_buffer[5] >> 0) & 1; uint8_t enc12B = (rx_buffer[5] >> 1) & 1; uint8_t enc13A = (rx_buffer[7] >> 0) & 1; uint8_t enc13B = (rx_buffer[7] >> 1) & 1; uint8_t enc14A = (rx_buffer[9] >> 0) & 1; uint8_t enc14B = (rx_buffer[9] >> 1) & 1; uint8_t enc15A = (rx_buffer[11] >> 0) & 1; uint8_t enc15B = (rx_buffer[11] >> 1) & 1; uint8_t enc16A = (rx_buffer[13] >> 0) & 1; uint8_t enc16B = (rx_buffer[13] >> 1) & 1; printf("%d\n",enc10A) } return 0;}void getMCP23S17Values(uint8_t* rx_buffer) { uint8_t tx_buffer[10] = { 0x41, // Adresse du registre GPIOA (lecture des valeurs) 0x00, // Donnée vide (lecture des valeurs) 0x42, // Adresse du registre GPIOB (lecture des valeurs) 0x00, // Donnée vide (lecture des valeurs) 0x43, // Adresse du registre GPIOC (lecture des valeurs) 0x00, // Donnée vide (lecture des valeurs) 0x44, // Adresse du registre GPIOD (lecture des valeurs) 0x00, // Donnée vide (lecture des valeurs) 0x45, // Adresse du registre GPIOE (lecture des valeurs) 0x00 // Donnée vide (lecture des valeurs) }; spi_write_blocking(spi0, tx_buffer, sizeof(tx_buffer)); spi_read_blocking(spi0, 0x00, rx_buffer, sizeof(rx_buffer));}
Statistics: Posted by grungy1 — Tue Feb 06, 2024 9:10 am