Permalink
Browse files

Basic I/O handling library. Kinda tested.

  • Loading branch information...
0 parents commit 215885caf156e163ce667c4312ffa5349987b413 @ladyada ladyada committed Sep 1, 2010
Showing with 203 additions and 0 deletions.
  1. +143 −0 MCP23008.cpp
  2. +34 −0 MCP23008.h
  3. +6 −0 README
  4. +20 −0 keywords.txt
@@ -0,0 +1,143 @@
+// Code by Adafruit Industries/Limor Fried
+// License: LGPL
+
+#include <Wire.h>
+#include <avr/pgmspace.h>
+#include "MCP23008.h"
+#include <WProgram.h>
+
+
+////////////////////////////////////////////////////////////////////////////////
+// RTC_DS1307 implementation
+
+void MCP23008::begin(uint8_t addr) {
+ if (addr > 7) {
+ addr = 7;
+ }
+ i2caddr = addr;
+
+ Wire.begin();
+
+ // set defaults!
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_IODIR);
+ Wire.send(0xFF); // all inputs
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.send(0x00);
+ Wire.endTransmission();
+
+}
+
+void MCP23008::begin(void) {
+ begin(0);
+}
+
+void MCP23008::pinMode(uint8_t p, uint8_t d) {
+ uint8_t iodir;
+
+
+ // only 8 bits!
+ if (p > 7)
+ return;
+
+ // read the current IODIR
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_IODIR);
+ Wire.endTransmission();
+
+ Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
+ iodir = Wire.receive();
+
+ // set the pin and direction
+ if (d == INPUT) {
+ iodir |= 1 << p;
+ } else {
+ iodir &= ~(1 << p);
+ }
+
+ // write the new IODIR
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_IODIR);
+ Wire.send(iodir);
+ Wire.endTransmission();
+}
+
+
+void MCP23008::digitalWrite(uint8_t p, uint8_t d) {
+ uint8_t gpio;
+
+ // only 8 bits!
+ if (p > 7)
+ return;
+
+ // read the current GPIO output latches
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_OLAT);
+ Wire.endTransmission();
+
+ Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
+ gpio = Wire.receive();
+
+ // set the pin and direction
+ if (d == HIGH) {
+ gpio |= 1 << p;
+ } else {
+ gpio &= ~(1 << p);
+ }
+
+ // write the new GPIO
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_GPIO);
+ Wire.send(gpio);
+ Wire.endTransmission();
+}
+
+void MCP23008::pullUp(uint8_t p, uint8_t d) {
+ uint8_t gppu;
+
+ // only 8 bits!
+ if (p > 7)
+ return;
+
+ // read the current pullup resistor set
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_GPPU);
+ Wire.endTransmission();
+
+ Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
+ gppu = Wire.receive();
+
+ // set the pin and direction
+ if (d == HIGH) {
+ gppu |= 1 << p;
+ } else {
+ gppu &= ~(1 << p);
+ }
+
+ // write the new GPIO
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_GPPU);
+ Wire.send(gppu);
+ Wire.endTransmission();
+}
+
+uint8_t MCP23008::digitalRead(uint8_t p) {
+ // only 8 bits!
+ if (p > 7)
+ return 0;
+
+ // read the current GPIO
+ Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
+ Wire.send(MCP23008_GPIO);
+ Wire.endTransmission();
+
+ Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
+ return (Wire.receive() >> p) & 0x1;
+}
@@ -0,0 +1,34 @@
+// i2c expander library - slow I/O!
+
+// also works with the MCP23009
+
+// Don't forget the Wire library
+class MCP23008 {
+public:
+ void begin(uint8_t addr);
+ void begin(void);
+
+ void pinMode(uint8_t p, uint8_t d);
+ void digitalWrite(uint8_t p, uint8_t d);
+ void pullUp(uint8_t p, uint8_t d);
+ uint8_t digitalRead(uint8_t p);
+
+ private:
+ uint8_t i2caddr;
+};
+
+#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
+
@@ -0,0 +1,6 @@
+To download, click "Download Source" in the top right corner. Then install as indicated in our tutorial:
+http://www.ladyada.net/library/arduino/libraries.html
+
+This is very much beta, it seems to work fine but its not optimized and doesn't currently suport the interrupt capability of the chip
+
+Enjoy and send pull requests!
@@ -0,0 +1,20 @@
+#######################################
+# Syntax Coloring Map for MCP23008
+#######################################
+
+#######################################
+# Datatypes (KEYWORD1)
+#######################################
+
+MCP23008 KEYWORD1
+
+#######################################
+# Methods and Functions (KEYWORD2)
+#######################################
+
+pullUp KEYWORD2
+
+
+#######################################
+# Constants (LITERAL1)
+#######################################

0 comments on commit 215885c

Please sign in to comment.