I have discovered the MCP23008 I2C expander chip. I think I'll try the digital equivalent of "Hello World!" and make an LED blink. If this chip works like I think it should, then perhaps I can use it to save pins on my motor controller / encoder project!
Here is the breadboard image.
And here is the Arduino code to exercise the chip.
// Blink an LED using the MCP23008 I/O expander
// +------+
// A5 <--|1 18|--> +5v
// A4 <--|2 17|-->
// GND <--|3 16|-->
// GND <--|4 15|-->
// GND <--|5 14|-->
// +5v <--|6 13|-->
// <--|7 12|-->
// <--|8 11|-->
// GND <--|9 10|--> 330 ohm --> LED --> GND
// +------+
// Libraries
#include "Wire.h"
// Base address of device
#define MCP23008_ADDRESS 0x20
// Registers
#define MCP23008_IODIR 0x00
#define MCP23008_IPOL 0x01
#define MCP23008_GPINTEN 0x02
#define MCP23008_DEFVAL 0x03
#define MCP23008_INTCON 0x04
#define MCP23008_IOCON 0x05
#define MCP23008_GPPU 0x06
#define MCP23008_INTF 0x07
#define MCP23008_INTCAP 0x08
#define MCP23008_GPIO 0x09
#define MCP23008_OLAT 0x0A
// Globals
static const uint8_t i2caddr = 0x00;
// Main
void setup() {
// Initialize the wire library
Wire.begin();
// Initialize the MCP23008.
// Set the IODIR register.
// GP0 is output GP1-GP7 are inputs.
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write((byte)MCP23008_IODIR);
Wire.write((byte)0xFE);
Wire.endTransmission();
}
// Execution loop
void loop() {
// Set GP0 of the GPIO register.
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write((byte)MCP23008_GPIO);
Wire.write((byte)0x01);
Wire.endTransmission();
delay(1000);
// Clear GP0 of the GPIO register.
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write((byte)MCP23008_GPIO);
Wire.write((byte)0x00);
Wire.endTransmission();
delay(1000);
}
No comments:
Post a Comment