Basic library of routines to interface to a Microchip MCP23017 16-bit I/O expander using an I2C interface.

Dependents:   acd52832_beep_buzzer_ints

Fork of MCP23017 by jim herd

Revision:
12:6d9d2b277f26
Parent:
9:e08c29541bc4
Child:
14:2e65b7f39273
diff -r 8726c7be6d72 -r 6d9d2b277f26 MCP23017.cpp
--- a/MCP23017.cpp	Mon Nov 29 11:46:57 2010 +0000
+++ b/MCP23017.cpp	Mon Nov 29 12:41:09 2010 +0000
@@ -1,5 +1,6 @@
 /*  MCP23017 library for Arduino
     Copyright (C) 2009 David Pye    <davidmpye@gmail.com
+    Modified for use on the MBED ARM platform
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -23,8 +24,7 @@
     uint16_t value16;
 } tmp_data;
 
-// using namespace mbed;
-/*
+/*-----------------------------------------------------------------------------
  *
  */
 MCP23017::MCP23017(PinName sda, PinName scl, int i2cAddress)  : _i2c(sda, scl) {
@@ -32,7 +32,7 @@
     reset();                                  // initialise chip to power-on condition
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * reset
  * Set configuration (IOCON) and direction(IODIR) registers to initial state
  */
@@ -60,31 +60,7 @@
     shadow_IPOL  = 0;
 }
 
-/* -----------------------------------------------------------------------------
- * pinMode
- */
-void MCP23017::pinMode(int pin, int mode) {
-    if (DIR_INPUT) {
-        shadow_IODIR |= 1 << pin;
-    } else {
-        shadow_IODIR &= ~(1 << pin);
-    }
-    writeRegister(IODIR, (unsigned short)shadow_IODIR);
-}
-
-/* -----------------------------------------------------------------------------
- * digitalRead
- */
-int MCP23017::digitalRead(int pin) {
-    shadow_GPIO = readRegister(GPIO);
-    if ( shadow_GPIO & (1 << pin)) {
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * write_bit
  * Write a 1/0 to a single bit of the 16-bit port
  */
@@ -97,7 +73,7 @@
     writeRegister(GPIO, (unsigned short)shadow_GPIO);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * Write a combination of bits to the 16-bit port
  */
 void MCP23017::write_mask(unsigned short data, unsigned short mask) {
@@ -105,7 +81,7 @@
     writeRegister(GPIO, (unsigned short)shadow_GPIO);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * read_bit
  * Read a single bit from the 16-bit port
  */
@@ -114,7 +90,7 @@
     return  ((shadow_GPIO >> bit_number) & 0x0001);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * read_mask
  */
 int  MCP23017::read_mask(unsigned short mask) {
@@ -122,7 +98,7 @@
     return (shadow_GPIO & mask);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * Config
  * set direction and pull-up registers
  */
@@ -135,7 +111,71 @@
     writeRegister(IPOL, (unsigned short)shadow_IPOL);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
+ * writeRegister
+ * write a byte
+ */
+void MCP23017::writeRegister(int regAddress, unsigned char data) {
+    char  buffer[2];
+
+    buffer[0] = regAddress;
+    buffer[1] = data;
+    _i2c.write(MCP23017_i2cAddress, buffer, 2);
+}
+
+/*----------------------------------------------------------------------------
+ * write Register
+ * write two bytes
+ */ 
+void MCP23017::writeRegister(int regAddress, unsigned short data) {
+    char  buffer[3];
+
+    buffer[0] = regAddress;
+    tmp_data.value16 = data;
+    buffer[1] = tmp_data.value8[0];
+    buffer[2] = tmp_data.value8[1];
+
+    _i2c.write(MCP23017_i2cAddress, buffer, 3);
+}
+
+/*-----------------------------------------------------------------------------
+ * readRegister
+ */
+int MCP23017::readRegister(int regAddress) {
+    char buffer[2];
+
+    buffer[0] = regAddress;
+    _i2c.write(MCP23017_i2cAddress, buffer, 1);
+    _i2c.read(MCP23017_i2cAddress, buffer, 2);
+
+    return ((int)(buffer[0] + (buffer[1]<<8)));
+}
+
+/*-----------------------------------------------------------------------------
+ * pinMode
+ */
+void MCP23017::pinMode(int pin, int mode) {
+    if (DIR_INPUT) {
+        shadow_IODIR |= 1 << pin;
+    } else {
+        shadow_IODIR &= ~(1 << pin);
+    }
+    writeRegister(IODIR, (unsigned short)shadow_IODIR);
+}
+
+/*-----------------------------------------------------------------------------
+ * digitalRead
+ */
+int MCP23017::digitalRead(int pin) {
+    shadow_GPIO = readRegister(GPIO);
+    if ( shadow_GPIO & (1 << pin)) {
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
+/*-----------------------------------------------------------------------------
  * digitalWrite
  */
 void MCP23017::digitalWrite(int pin, int val) {
@@ -161,7 +201,7 @@
     }
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * digitalWordRead
  */
 unsigned short MCP23017::digitalWordRead() {
@@ -169,7 +209,7 @@
     return shadow_GPIO;
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * digitalWordWrite
  */
 void MCP23017::digitalWordWrite(unsigned short w) {
@@ -177,14 +217,14 @@
     writeRegister(GPIO, (unsigned short)shadow_GPIO);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * inputPolarityMask
  */
 void MCP23017::inputPolarityMask(unsigned short mask) {
     writeRegister(IPOL, mask);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * inputoutputMask
  */
 void MCP23017::inputOutputMask(unsigned short mask) {
@@ -192,7 +232,7 @@
     writeRegister(IODIR, (unsigned short)shadow_IODIR);
 }
 
-/* -----------------------------------------------------------------------------
+/*-----------------------------------------------------------------------------
  * internalPullupMask
  */
 void MCP23017::internalPullupMask(unsigned short mask) {
@@ -200,43 +240,3 @@
     writeRegister(GPPU, (unsigned short)shadow_GPPU);
 }
 
-//PRIVATE
-/* -----------------------------------------------------------------------------
- * writeRegister
- * write a byte
- */
-void MCP23017::writeRegister(int regAddress, unsigned char data) {
-    char  buffer[2];
-
-    buffer[0] = regAddress;
-    buffer[1] = data;
-    _i2c.write(MCP23017_i2cAddress, buffer, 2);
-}
-
-/* -----------------------------------------------------------------------------
- * writeRegister
- * write two bytes
- */
-void MCP23017::writeRegister(int regAddress, unsigned short data) {
-    char  buffer[3];
-
-    buffer[0] = regAddress;
-    tmp_data.value16 = data;
-    buffer[1] = tmp_data.value8[0];
-    buffer[2] = tmp_data.value8[1];
-
-    _i2c.write(MCP23017_i2cAddress, buffer, 3);
-}
-
-/* -----------------------------------------------------------------------------
- * readRegister
- */
-int MCP23017::readRegister(int regAddress) {
-    char buffer[2];
-
-    buffer[0] = regAddress;
-    _i2c.write(MCP23017_i2cAddress, buffer, 1);
-    _i2c.read(MCP23017_i2cAddress, buffer, 2);
-
-    return ((int)(buffer[0] + (buffer[1]<<8)));
-}