笔记: 地址范围允许 3 bits and
this means a 最多八个MCP23017 设备可以附加
to any 单一I2C总线。如果你想要更多你需要
使用第二个I2C总线或位爆炸一些引脚来模拟一个或做
巧妙的东西很聪明,即另一个微控制器拦截
未使用的I2C地址并转换它们以控制额外的
MCP23017s.
用于控制每个引脚的I / O方向,寄存器IODIR(A / B)让您设置
当写入零时并在输入时向输出到输出'1' is
写入寄存器位。这对于大多数微控制器来说是相同的方案
- 关键是要记住零('0') equates to the 'O' in Output.
GPPU上拉寄存器
设置一点高设置对应I / O引脚的上拉有效。
OLAT输出闩锁寄存器
这与18F系列PIC片中的I / O端口完全相同
can read back the "desired"端口引脚的输出是否实际
达到该销的状态。即,考虑一个强大的当前领导
引脚 - 易于将输出电压拉到销钉上
低于逻辑阈值I.E.如果从中读取,您将读回零
当实际上它应该是一个。读取OLAT寄存器位
returns a 'one'正如您所期望的软件工程点
view.
IPOL引脚反转寄存器
iPol(A / B)寄存器允许您选择性地反转任何输入引脚。这个
从此减少将其他设备接口到MCP23017所需的胶水逻辑
you won'T需要添加变频器逻辑芯片以获得正确的信号极性
into the MCP23017.
// MCP23017 Example: Slow key press reaction.
//
// Toggle LEDs and detect keypress.
//
// Example code showing slow reaction of 'button'
// LED to keypress. Leading into why interrupts
// are useful (See next example).
//
// Copyright : John Main
// Free for non commercial use.
#include<Wire.h>
#include<Adafruit_MCP23017.h>
#defineMCP_LED17
#defineMCP_INPUTPIN8
#defineMCP_LEDTOG111
#defineMCP_LEDTOG24
Adafruit_MCP23017mcp;
void设置(){mcp.begin();// Default device address 0
mcp.pinMode(MCP_LEDTOG1,OUTPUT);// Toggle LED 1
mcp.pinMode(MCP_LEDTOG2,OUTPUT);// Toggle LED 2
mcp.pinMode(MCP_LED1,OUTPUT);// LED output
mcp.digitalWrite(MCP_LED1,HIGH);
mcp.pinMode(MCP_INPUTPIN,INPUT);// Button i/p to GND
mcp.pullUp(MCP_INPUTPIN,HIGH);// Puled high to ~100k
}
// Alternate LEDTOG1 and LEDTOG2.
// Transfer pin input to LED1.
void环形(){
delay(300);
mcp.digitalWrite(MCP_LEDTOG1,HIGH);
mcp.digitalWrite(MCP_LEDTOG2,LOW);
delay(300);
mcp.digitalWrite(MCP_LEDTOG1,LOW);
mcp.digitalWrite(MCP_LEDTOG2,HIGH);
// Transfer input pin state to LED1
if(mcp.digitalRead(MCP_INPUTPIN)){
mcp.digitalWrite(MCP_LED1,HIGH);
}else{
mcp.digitalWrite(MCP_LED1,LOW);
}
}
// MCP23017 Example: Unexpected Interrupt failure.
//
// This code sends an interrupt from the MCP23017
// to an Arduino external interrupt pin when an
// MCP23017 input button press is detected.
// At the same time MCP LEDs are toggled on button
// release. THIS CODE FAILS FOR A VERY SUBTLE REASON.
//
// Copyright : John Main
// Free for non commercial use.
#include<Wire.h>
#include<Adafruit_MCP23017.h>
// MCP23017 setup
#defineMCP_LED17
#defineMCP_INPUTPIN8
#defineMCP_LEDTOG111
#defineMCP_LEDTOG24
// Register bits
#defineMCP_INT_MIRRORtrue// Mirror inta to intb.
#defineMCP_INT_ODRfalse// Open drain.
// Arduino pins
#defineINTPIN3// Interrupt on this Arduino Uno pin.
volatileuint16_tdmy;// Dummy for use within interrupt.
Adafruit_MCP23017mcp;
void设置(){
mcp.begin();// use default address 0
pinMode(INTPIN,INPUT);
mcp.pinMode(MCP_LEDTOG1,OUTPUT);// Toggle LED 1
mcp.pinMode(MCP_LEDTOG2,OUTPUT);// Toggle LED 2
mcp.pinMode(MCP_LED1,OUTPUT);// LED output
mcp.digitalWrite(MCP_LED1,LOW);
// This Input pin provides the interrupt source.
mcp.pinMode(MCP_INPUTPIN,INPUT);// Button i/p to GND
mcp.pullUp(MCP_INPUTPIN,HIGH);// Puled high ~100k
// On interrupt, polariy is set HIGH/LOW (last parameter).
mcp.setupInterrupts(MCP_INT_MIRROR,MCP_INT_ODR,LOW);
mcp.setupInterruptPin(MCP_INPUTPIN,FALLING);
mcp.readGPIOAB();// Initialise for interrupts.
// Enable interrupts - This is the Arduino interrupt control.
attachInterrupt(digitalPinToInterrupt(INTPIN),isr,FALLING);
}
// The interrupt routine handles MCP_LED1
// This is the button press since this is the only active interrupt.
voidISR(){
英石aticuint8_tledState=0;
// Debounce. Slow I2C: extra debounce between interrupts anyway.
// Can not use delay() in interrupt code.
delayMicroseconds(1000);dmy=mcp.readGPIOAB();
if(ledState){
mcp.digitalWrite(MCP_LED1,LOW);
}else{
mcp.digitalWrite(MCP_LED1,HIGH);
}
ledState=!ledState;
}
//////////////////////////////////////////////
void环形(){delay(300);
mcp.digitalWrite(MCP_LEDTOG1,HIGH);
mcp.digitalWrite(MCP_LEDTOG2,LOW);
delay(300);
mcp.digitalWrite(MCP_LEDTOG1,LOW);
mcp.digitalWrite(MCP_LEDTOG2,HIGH);
}
// MCP23017 Example: Interrupt operation.
//
// This code sends an interrupt from the MCP23017
// to an Arduino external interrupt pin when an
// MCP23017 input button press is detected.
// At the same time MCP LEDs are toggled on button
// release.
//
// Copyright : John Main
// Free for non commercial use.
#include<Wire.h>
#include<Adafruit_MCP23017.h>
// MCP23017 setup
#defineMCP_LED17
#defineMCP_INPUTPIN8
#defineMCP_INPUTPIN210
#defineMCP_LEDTOG111
#defineMCP_LEDTOG24
// Register bits
#defineMCP_INT_MIRRORtrue// Mirror inta to intb.
#defineMCP_INT_ODRfalse// Open drain.
// Arduino pins
#defineINTPIN3// Interrupt on this Arduino Uno pin.
#definecontrolArduioIntattachInterrupt(digitalPinToInterrupt(INTPIN),isr,FALLING)
Adafruit_MCP23017mcp;
//////////////////////////////////////////////
void设置(){
mcp.begin();// use default address 0
pinMode(INTPIN,INPUT);
mcp.pinMode(MCP_LEDTOG1,OUTPUT);// Toggle LED 1
mcp.pinMode(MCP_LEDTOG2,OUTPUT);// Toggle LED 2
mcp.pinMode(MCP_LED1,OUTPUT);// LED output
mcp.digitalWrite(MCP_LED1,LOW);
// This Input pin provides the interrupt source.
mcp.pinMode(MCP_INPUTPIN,INPUT);// Button i/p to GND
mcp.pullUp(MCP_INPUTPIN,HIGH);// Puled high ~100k
// Show a different value for interrupt capture data register = debug.
mcp.pinMode(MCP_INPUTPIN2,INPUT);// Button i/p to GND
mcp.pullUp(MCP_INPUTPIN2,HIGH);// Puled high ~100k
// On interrupt, polariy is set HIGH/LOW (last parameter).
mcp.setupInterrupts(MCP_INT_MIRROR,MCP_INT_ODR,LOW);// The mcp output interrupt pin.
mcp.setupInterruptPin(MCP_INPUTPIN,CHANGE);// The mcp action that causes an interrupt.
mcp.setupInterruptPin(MCP_INPUTPIN2,CHANGE);// No button connected, see effect on code=none.
mcp.digitalWrite(MCP_LED1,LOW);
mcp.readGPIOAB();// Initialise for interrupts.
controlArduioInt;// Enable Arduino interrupt control.
}
//////////////////////////////////////////////
// The interrupt routine handles LED1
// This is the button press since this is the only active interrupt.
voidISR(){
uint8_tp,v;
英石aticuint16_tledState=0;
noInterrupts();
// Debounce. Slow I2C: extra debounce between interrupts anyway.
// Can not use delay() in interrupt code.
delayMicroseconds(1000);// Stop interrupts from external pin.
detachInterrupt(digitalPinToInterrupt(INTPIN));
在 terrupts();// re-start interrupts for mcp
p=mcp.getLastInterruptPin();
// This one resets the interrupt state as it reads from reg INTCAPA(B).
v=mcp.getLastInterruptPinValue();
// Here either the button has been pushed or released.
if(p==MCP_INPUTPIN&&v==1){// Test for release - pin pulled high
if(ledState){
mcp.digitalWrite(MCP_LED1,LOW);
}else{
mcp.digitalWrite(MCP_LED1,HIGH);
}
ledState=!ledState;
}
controlArduioInt;// Reinstate interrupts from external pin.
}
//////////////////////////////////////////////
void环形(){
delay(300);
mcp.digitalWrite(MCP_LEDTOG1,HIGH);
mcp.digitalWrite(MCP_LEDTOG2,LOW);
delay(300);
mcp.digitalWrite(MCP_LEDTOG1,LOW);
mcp.digitalWrite(MCP_LEDTOG2,HIGH);
}
新的! Comments
让你说到你刚刚阅读的东西!留下下面的框中的评论。