🛸 MCP23017 C++库-构建键盘、传感器、继电器和中断系统
MCP23017扩展器是Raspberry Pi、微控制器和许多其他具有I2C接口的平台的优秀GPIO扩展。
它具有16个额外的GPIO,可以配置为输出和输入引脚。
此MCP23017 C++API简化了I2C接口的工作、MCP的配置以及项目引脚的切换。
功能保持简单,并恢复扩展器的所有功能。
非常适合学生、业余爱好者和只需要GPIO控制的项目,而无需深入探究扩展器的寄存器。
______________________________________________________________________
✨ 特性
✔️ 简单的GPIO输入/输出
✔️ 单线引脚配置
✔️ 中断支持(上升、下降、两者兼有)
✔️ 中断读取并捕获引脚电平
✔️ 中断引脚输出模式(上升/下降POL、漏极开路、单独A/B输出引脚)
______________________________________________________________________
❗基础
📱 一条捷径!
基本功能简单明了。
只需三行代码,MCP就能为您工作并完成您的项目。
1.️⃣ 配置
2.️⃣ 写
3.️⃣ 阅读
🥵 你想让LED出汗吗?
没问题!使用 pinMode() 功能,并将任何MCP引脚指定为输出。
随着 pinWrite(),您可以通过在高和低之间切换LED来设置动画。
🔥 或者,也许你想让一个按钮发红光?
使用 pinMode() 并将引脚配置为输入。
想要一个稳定的高点作为你的低旗,但手头没有电阻器?
用内部上拉电阻器延长输入引脚。
现在怎么办?对, pinRead() 无论按下还是松开按钮,都能让你随时了解情况。
输出和进一步处理没有限制。
______________________________________________________________________
🤖 中断
💡 您不必配置所有内容。
中断功能非常易于使用——用于更多的控制, 您还可以启用微调选项,如镜像、开漏或慢边。
过程总是一样的:
1.️⃣ 设置模式(输出行为和触发类型)
2.️⃣ 启用中断
3.️⃣ 查询发生了什么
4.️⃣ 可选择重置或确认
什么都没有丢失。
无论您是实时查询级别、查看位掩码还是使用捕获功能,信息都会一直存储,直到您明确删除或通过读取GPIO进行确认。
捕捉就像一张照片。
这 getIntCapture() 函数记录中断触发时引脚上的状态。
如果你想知道中断发生的原因,而不仅仅是它发生了,那就太好了。
删除是可选的。
所有查询函数都有一个可选函数 clear 参数。
您可以决定:只读取值,或者也确认并清除它们。
单个引脚或一次全部引脚。
有专门的功能,如 clearInterrupt(pin) 对于单个引脚,
以及方便的功能,如 clearInterrupts() 马上把一切都清理干净。
📝 用户需要理解的一点:
MCP23017上的中断不是“主动运行”,而是“报告和等待”
一旦事件被触发,它就会被存储起来——直到你说:
“谢谢,我知道了。”
这正是明确功能的用途。
______________________________________________________________________
🧩 提供的功能
| 功能 | 说明 | |
|---|---|---|
pinMode(pin, OUTPUT, INPUT, INPUT_PULLUP) | 配置引脚方向 | |
pinWrite(pin, HIGH/LOW) | 设置引脚输出状态 | |
pinRead(pin) | 读取数字输入 | |
enableInt(pin, true/false) | 启用/禁用引脚上的中断 | |
intOutputMode(HIGH/LOW, ODR true, MIRROR true) (**) | 水平,明渠,分开A/B | |
intTriggerMode(RISING, FALLING, BOTH) | 上升,下降,两面旗 | \ |
getInterruptFlags(INTFA/B clear true)(**) | 将Int事件作为位掩码输出 | |
getInterruptPins(INTFA/B clear true)(**) | 将Int事件输出为pinnum | |
getIntCapture(INTCAPA/B clear true)(**) | 中断事件的边缘 | |
clearIntCapture(pin) | 重置捕获位 | |
clearInterrupts() | 重置Int配置和值 | |
| (\*\*)可选->默认为false |
______________________________________________________________________
🎁 看看这些例子。
作为你项目的灵感和想法的例子。
📁 示例/
├── blink.cpp // Make individual LEDs blink
├── taster.cpp // Query buttons
├── highlow.cpp // Set Pin high/low
├── interrupt.cpp // Interrupt on pins
└── keypad.cpp // A keypad matrix example
______________________________________________________________________
📐所解释的功能
/* Set pin mode Output/Input
*
* pinMode(PIN, OUTPUT/INPUT/INPUT_PULLUP)
*
* Output = You can use this setting for a high and low output signal
* Input = For connections with defined high/low signal or external pullup/pulldown resistor
* Input_Pullup = An input pin set to pull-up can be queried for a low signal.
*
*/
/* Set pin to HIGH/LOW signal
*
* pinWrite(PIN, HIGH/LOW)
*
* High = Signal at the level of the operating voltage, most 3.3 or 5 volts.
* Low = Signal indicating the absence of a voltage close to ~0 volts.
*/
/* Set pin to read a signal
*
* pinRead(PIN)
*
* It reproduces the signal that is present at the pin.
*/
/* Set interrupt output pins
*
* intOutputMode(HIGH/LOW, ODR true **, MIRROR true **)
*
* INTPOL = The output pin can be set to either a falling or rising edge.
* HIGH: Interrupt pin goes HIGH on event
* LOW: Interrupt pin goes LOW on event
* ODR = True: For external pull-up, INTPOL is automatically set to default.
* MIRROR = True: INTA and INTB Pins separately, default is false.
*
* ** If you don't need this setting, you can leave it out.
*
*/
/* Set interrupt input pins
*
* intTriggerMode(PIN, CHANGE/RISING/FALLING)
*
* CHANGE = Every change of flank
* RISING = On rising flank
* FALLING = On a falling flank
*
*/
/* Enable/Disable the Interrupt Pin
*
* enableInt(PIN, TRUE/FALSE)
*
* Enable or disable a pin as an interrupt
*
*/
/* Interrupt output as Pinnums
*
* getInterruptPins(clear ture **)
*
* Outputs of active interrupt pins as pin number
*
*/
/* Interrupt output as bitmask
*
* getInterruptFlags(clear ture **)
*
* Outputs of active interrupt pins as a bitmask
*
*/
/* Interrupt Capture reproduces the state of the pins during an interrupt.
*
* getIntCapture(clear ture **)
*
* Outputs the state of the interrupt pin at the time of an edge transition.
*
*/
/* Interrupt flag as boolean
*
* isInterruptOnPin(PIN, clear ture **)
*
* Outputs a Boolean value on a pin interrupt
*
*/
// ** Optionally clear, the values can be reset after output with true, default is false. If you don't need this setting, you can leave it out.
/* Interrupt Capture reset a specific pin.
*
* clearIntCapture(PIN)
*
* INTCAP can only be read, but according to the datasheet, reading the GPIOs is sufficient for resetting.
*
*/
/* Interrupt clean up.
*
* clearInterrupts()
*
* All interrupt settings and values will be reset.
*
*/
/********** MCP configure **********
* enableSlewRateControl(true/false)
*
* DISSLW = true: Only in cases of long cable runs or EMI problems I2C SDA.
* setSequentialOperation(true/false)
*
* SEQOP = true: Useful when a register needs to be continuously queried (read) or changed (written).
______________________________________________________________________
📄 许可证
MIT许可证\ 您可以自由使用、修改和分发此项目。
______________________________________________________________________
🤝 贡献
欢迎拉取请求和改进。\ 您可以随意分叉、增强或建议功能。
______________________________________________________________________
⭐ 如果这个库对你的项目有帮助——考虑把它放在GitHub上!
