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

General discussion • Trouble with non sequential reads on a MCP23017

$
0
0
I have a RPi 4B connected to two MCP23017 chips via I2C reading 32 input lines. I'm trying to poll the GPIOA and GPIOB registers at about 1 Hz. When I have sequential reading set, it cycles through all the registers correctly but when I try to just access the GPIOA and GPIOB registers, it always returns the last write which in this case is the value from the GPPUB and GPPUB. I am leaving the default of Bank 0 set.

What am I missing when I try to disabled sequential addressing?

Code:

#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <stdint.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/ioctl.h>#include <linux/i2c-dev.h>#define MCP0_ADDR 0x20#define MCP1_ADDR 0x21#define IODIRA 0x00#define IODIRB 0x01#define IPOLA  0x02#define IPOLB  0x03#define IOCON  0x0A#define GPPUA  0x0C#define GPPUB  0x0D#define GPIOA  0x12#define GPIOB  0x13#define OLATA  0x14#define OLATB  0x15#define INPUT 0xFF#define PULLUP 0xFAstatic const char *device = "/dev/i2c-1";uint8_t buffer[2]; int mcp0, mcp1, mcp2;int mcp_init(void) {   // Initialize 1st MCP23017 with 0x20 address:   mcp0 = open(device, O_RDWR);   ioctl(mcp0, I2C_SLAVE, MCP0_ADDR);   // Initialize 2nd MCP23017 with 0x21 address:   mcp1 = open(device, O_RDWR);   ioctl(mcp1, I2C_SLAVE, MCP1_ADDR);   // Chip 0x20   buffer[0] = IOCON;   buffer[1] = 0x60;          // Bank 0, Seq disabled   write(mcp0, buffer, 2);   // Chip 0x21   buffer[0] = IOCON;   buffer[1] = 0x60;          // Bank 0, Seq disabled   write(mcp1, buffer, 2);      // Chip 0x20   buffer[0] = IODIRA;   buffer[1] = INPUT;   write(mcp0, buffer, 2);    // set IODIRA to all inputs   buffer[0] = IODIRB;   buffer[1] = INPUT;   write(mcp0, buffer, 2);    // set IODIRB to all inputs      // Chip 0x21   buffer[0] = IODIRA;   buffer[1] = INPUT;   write(mcp1, buffer, 2);    // set IODIRA to all inputs   buffer[0] = IODIRB;   buffer[1] = INPUT;   write(mcp1, buffer, 2);    // set IODIRB to all inputs   // Chip 0x20   buffer[0] = GPPUA;   buffer[1] = PULLUP;   write(mcp0, buffer, 2);    // set IODIRA to all inputs   buffer[0] = GPPUB;   buffer[1] = PULLUP;   write(mcp0, buffer, 2);    // set IODIRB to all inputs      // Chip 0x21   buffer[0] = GPPUA;   buffer[1] = PULLUP;   write(mcp1, buffer, 2);    // set IODIRA to all inputs   buffer[0] = GPPUB;   buffer[1] = PULLUP;   write(mcp1, buffer, 2);    // set IODIRB to all inputs}int main(void) {   // Initialize the chips:   mcp_init();   printf("init\n");   sleep(1);   while(1)   {      buffer[0] = GPIOA;      buffer[1] = 0x00;      printf("MCP0 ");      if (read(mcp0, buffer, 2) == 2)      {         printf("%02X %02X ", buffer[0], buffer[1]);      }      else      {         printf("XX XX ");      }            buffer[0] = GPIOA;      buffer[1] = 0x00;      printf("MCP1 ");      if (read(mcp1, buffer, 2) == 2)      {         printf("%02X %02X \n", buffer[0], buffer[1]);      }      else      {         printf("XX XX \n");      }      sleep(1);   }  return 0 ;}

Statistics: Posted by gjm27n — Wed Dec 18, 2024 1:09 am



Viewing all articles
Browse latest Browse all 4919

Trending Articles