首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用pigpio和MCP23017进行中断读取

用pigpio和MCP23017进行中断读取
EN

Stack Overflow用户
提问于 2020-06-29 08:23:05
回答 1查看 495关注 0票数 0

我试着用pigpio读取来自MCP23017板的中断更改,但是我没有收到任何通知.我似乎不知道的是我需要做的初始化过程(查看文档http://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf,似乎没有使用正确的值.)。

我使用B侧作为输入,A用于输出:

代码语言:javascript
复制
i2cWriteByteData(i2cHandle, IODIRB, 0xFF); // inputs
i2cWriteByteData(i2cHandle, IODIRA, 0x00); // outputs

然后,我尝试设置中断状态(它似乎是错误的),但我是从http://abyz.me.uk/rpi/pigpio/examples.html的'I2C Sonar‘示例中复制的。

代码语言:javascript
复制
i2cWriteByteData(i2cHandle, GPINTENB, 0x00); // Disable.
i2cWriteByteData(i2cHandle, DEFVALB, 0x00) ; // N/A.
i2cWriteByteData(i2cHandle, INTCONB, 0x00) ; // On change.

然后注册处理程序以读取更改:

代码语言:javascript
复制
gpioSetMode(ALERTPIN, PI_INPUT); /* start capacitor recharge */
gpioSetISRFuncEx(ALERTPIN, EITHER_EDGE, 0, alert, nullptr); /* call alert when state changes */

所以,我的问题是,当状态发生变化时,我的警报函数永远不会被调用(奇怪的是,当我在控制台(sudo i2cdump -y 1 0x20)上转储时,它被调用了)。

另一个问题是,我的定义是否正确?为什么两组值(Address IOCON.BANK =1和0)?MCP23017的地址是什么?(我使用的是地址IOCON.BANK = 0):

代码语言:javascript
复制
#define IODIRA    0x00
#define IODIRB    0x01
#define IPOLA     0x02
#define IPOLB     0x03
#define GPINTENA  0x04
#define GPINTENB  0x05
#define DEFVALA   0x06
#define DEFVALB   0x07
#define INTCONA   0x08
#define INTCONB   0x09
#define IOCONA    0x0A
#define IOCONB    0x0B
#define GPPUA     0x0C
#define GPPUB     0x0D
#define INTFA     0x0E
#define INTFB     0x0F
#define INTCAPA   0x10
#define INTCAPB   0x11
#define GPIOA     0x12
#define GPIOB     0x13
#define OLATA     0x14
#define OLATB     0x15

#define ALERTPIN 26

更新

接下来,我展示了我所执行的所有代码,在阅读了文档之后,我认为我做的一切都是正确的,但是我似乎遗漏了一些初始化。奇怪的是,当按下连接到GPA0的输入按钮时,GPIOA读取的值为255 (0 0xff),如果不按,则为0.奇怪,按压的时候应该是1,对吧?

代码语言:javascript
复制
void estimulateMCP23017(unsigned i2cAddr=0x20,unsigned i2cBus=1){
    //open i2c MCP23017
    int i2cHandle = i2cOpen(i2cBus, i2cAddr, 0);
    //open is with success if is >=0
    if(i2cHandle>=0){
        //IODIR: I/O DIRECTION REGISTER (ADDR 0x00)
        //1 = Pin is configured as an input.
        //0 = Pin is configured as an output
        //we can set each separately if we want
        //set side A as output (0)
        i2cWriteByteData(i2cHandle, IODIRA, 0xFF); // A is inputs
        //set side B as input (1)
        i2cWriteByteData(i2cHandle, IODIRB, 0x00); // B is outputs

        //turn all outputs to on (pins 0 to 7 is the bit high in 0x00-0xFF)
        i2cWriteByteData(i2cHandle, OLATA, 0x00);
        i2cWriteByteData(i2cHandle, OLATB, 0xFF);

        //now listen for changes on side (B)
        // General purpose I/O interrupt-on-change bits <7:0>
        //1 = Enables GPIO input pin for interrupt-on-change event.
        //0 = Disables GPIO input pin for interrupt-on-change event.
        i2cWriteByteData(i2cHandle, GPINTENA, 0xFF); // Enable all
        i2cWriteByteData(i2cHandle, GPINTENB, 0x00); // disable
        //Sets the compare value for pins configured for interrupt-on-change from defaults <7:0>
        //If the associated pin level is the opposite from the register bit, an interrupt occurs
        i2cWriteByteData(i2cHandle, DEFVALA, 0x00); // does not matter
        i2cWriteByteData(i2cHandle, DEFVALB, 0x00); // does not matter
        // Controls how the associated pin value is compared for interrupt-on-change <7:0>
        //1 = Pin value is compared against the associated bit in the DEFVAL register.
        //0 = Pin value is compared against the previous pin value.
        i2cWriteByteData(i2cHandle, INTCONA, 0x00) ; // On change.
        i2cWriteByteData(i2cHandle, INTCONB, 0x00) ; // does not matter.

        cout << "waiting for changes...(input value: "<<i2cReadByteData(i2cHandle, GPIOA)<<")\n";

        gpioSetMode(ALERTPIN, PI_INPUT); /* set alert pin as input */
        gpioSetISRFuncEx(ALERTPIN, EITHER_EDGE, 0, alert, nullptr); /* call alert when state changes */

        cin.get();
        i2cClose(i2cHandle);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-06-29 20:45:49

由于A银行是您的输入,您应该使用:

代码语言:javascript
复制
i2cWriteByteData(i2cHandle, GPINTENA, 0xFF); // Enable.
i2cWriteByteData(i2cHandle, INTCONA, 0x00) ; // On change.

你的定义是对的。MCP23017有两种不同的寻址寄存器的模式。使用IOCON.BANK标志设置模式。只是不要碰那一点,没有理由这么做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62633970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档