Good catches PhilE
I've taken care of those issues, did a little cleanup and removed the third chip to simplify while debugging but still not getting successful reads. Not sure if my re-write is getting me closer or farther. Here is the revised code.
I've taken care of those issues, did a little cleanup and removed the third chip to simplify while debugging but still not getting successful reads. Not sure if my re-write is getting me closer or farther. Here is the revised code.
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 GPIOA 0x12#define GPIOB 0x13#define INPUT 0xFFstatic 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 0 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 1 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}int main(void) { // Main loop: // Initialize the chips: mcp_init(); printf("init\n"); sleep(1); while(1) { buffer[0] = GPIOA; if (read(mcp0, buffer, 1) == 1) { printf("Good read MCP0A %02X ", buffer[0]); } else { printf("Bad read MCP0 "); } buffer[0] = GPIOB; if (read(mcp0, buffer, 1) == 1) { printf("MCP0B %02X ", buffer[0]); } else { printf("Bad read MCP0B "); } buffer[0] = GPIOA; if (read(mcp1, buffer, 1) == 1) { printf("MCP1A %02X %02X ", buffer[0]); } else { printf("Bad read MCP1A "); } buffer[0] = GPIOB; if (read(mcp1, buffer, 1) == 1) { printf("MCP1B %02X ", buffer[0]); } else { printf("Bad read MCP1B "); } printf("\n"); sleep(1); } return 0 ;}
Statistics: Posted by gjm27n — Sat Dec 14, 2024 3:54 pm