Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5084

C/C++ • SPI & MCP23S17

$
0
0
Hi

i 've connected rotary encoder to the GPIO A pins to MCP23S17 and for the spi communication i'v connected the MCP to the default spi pins from the PICO. I have used the example from the BME280 from the pico_example to build this code :

Code:

#include <stdio.h>#include "pico/stdlib.h"#include "hardware/spi.h"#include "pico/binary_info.h"#include "pico/stdlib.h"/* pour lire il faut transmettre un registre sur lequel mcp écrit les valeurson utilise n controle byte composer de la sorte pour transmettre le registre{les 4 premiers bits = reg}{adresse sur 3 bit ici 0 car GND}{bit écriture soit 1}*/#define MCP23S17_ADDRESS 0x20#define SCK_PIN 18#define MOSI_PIN 19#define MISO_PIN 16#define SPI_PORT spi0#define CS_PIN 17#define IODIRA 0x00#define GPIOA 0x12int32_t t_fine;#ifdef PICO_DEFAULT_SPI_CSN_PINstatic inline void cs_select() {    asm volatile("nop \n nop \n nop");    gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 0);  // Active low    asm volatile("nop \n nop \n nop");}static inline void cs_deselect() {    asm volatile("nop \n nop \n nop");    gpio_put(PICO_DEFAULT_SPI_CSN_PIN, 1);    asm volatile("nop \n nop \n nop");}#endif#if defined(spi_default) && defined(PICO_DEFAULT_SPI_CSN_PIN)static void write_register(uint8_t reg, uint8_t data) {    uint8_t buf[3];    buf[0] = 0x40;  // MCP23S17 opcode for write 1000000    buf[1] = reg;    buf[2] = data;    cs_select();    spi_write_blocking(SPI_PORT, buf, 3);    cs_deselect();    sleep_ms(10);}static void read_registers(uint8_t reg, uint8_t *buf, uint16_t len) {    uint8_t tx_buf[2];    tx_buf[0] = 0x41;  // MCP23S17 opcode for read    tx_buf[1] = reg;    printf("fonction readregister txbuff avant: %p\n",tx_buf);    cs_select();    spi_write_blocking(SPI_PORT, tx_buf, 2);    spi_read_blocking(SPI_PORT, 0, buf, len);    printf("fonction readregister buff après réception : ");    for (int i = 0; i < len; i++) {        printf("%02X ", buf[i]);    }    printf("\n");    cs_deselect();    sleep_ms(1);}static void mcp23s17_init() {    // Set IODIRA register to configure GPIOA as inputs  (1 = input)    write_register(IODIRA, 0xFF);}static uint8_t read_encoder_value(uint8_t encoder_index) {    uint8_t buf[8]; //uint8_t buf[2];    read_registers(GPIOA, buf, 8); // read_registers(GPIOA, buf, 2);    return buf[encoder_index];}#endifint main() {    stdio_init_all();#if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)    puts("Default SPI pins were not defined");#else    printf("Hello, encoder! Reading values from MCP23S17 via SPI...\n");    // This example will use SPI0 at 0.5MHz.    spi_init(SPI_PORT, 500 * 1000);    gpio_set_function(MISO_PIN, GPIO_FUNC_SPI);    gpio_set_function(SCK_PIN, GPIO_FUNC_SPI);    gpio_set_function(MOSI_PIN, GPIO_FUNC_SPI);    // Make the SPI pins available to picotool    bi_decl(bi_3pins_with_func(MISO_PIN, MOSI_PIN, SCK_PIN, GPIO_FUNC_SPI));    // Chip select is active-low, so we'll initialise it to a driven-high state    gpio_init(CS_PIN);    gpio_set_dir(CS_PIN, GPIO_OUT);    gpio_put(CS_PIN, 1);    // Make the CS pin available to picotool    bi_decl(bi_1pin_with_name(CS_PIN, "SPI CS"));    mcp23s17_init();    while (1) {       for (int encoder_index = 0; encoder_index <= 7; ++encoder_index) {        uint8_t encoder_value = read_encoder_value(encoder_index);         printf("valeur brut encoder_value: %d\n", encoder_value);        // uint8_t encoder_pin_A = (encoder_value >> 7) & 1; //100010100        // uint8_t encoder_pin_B = encoder_value & 1; //(encoder_value >> 1) & 1; //100010010        uint8_t encoder_pin_A = (encoder_value >> (encoder_index * 2)) & 1;        uint8_t encoder_pin_B = (encoder_value >> (encoder_index * 2 + 1)) & 1;        // Faites quelque chose avec les valeurs des broches d'encodeur (par exemple, les afficher)        printf("Encoder %d - Pin A: %d, Pin B: %d\n", encoder_index, encoder_pin_A, encoder_pin_B);        if (encoder_index == 7){            sleep_ms(1000);        }    }    }#endif}
and this is the response i get :

Code:

 Encoder 0 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00 <---- After calling read_spi_blocking()valeur brut encoder_value: 0Encoder 1 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00valeur brut encoder_value: 0Encoder 2 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00valeur brut encoder_value: 0Encoder 3 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00valeur brut encoder_value: 0Encoder 4 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00valeur brut encoder_value: 0Encoder 5 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00valeur brut encoder_value: 0Encoder 6 - Pin A: 0, Pin B: 0fonction readregister txbuff avant: 20041FCCfonction readregister buff apr�ès r�éception : 00 00 00 00 00 00 00 00valeur brut encoder_value: 0Encoder 7 - Pin A: 0, Pin B: 0
As you can see in my code, i've setted the IODIR as Input, i pass the pointer to get the value from the MCP. I definitely miss something but what ?

Statistics: Posted by grungy1 — Mon Feb 12, 2024 11:21 am



Viewing all articles
Browse latest Browse all 5084

Trending Articles