PCA9672 Library (I2C IO Expander Interface)

Simple output port toggling with PCA9672

#include "mbed.h"
#include "PCA9672.h"

PCA9672 ioxp(P0_10, P0_11); //I2C connected to PCA9672 in LPC800-MAX

int main()
{
    ioxp.frequency(100000);

    while (1) {
        ioxp.write(0xFF);
        wait(.250);
        ioxp.write(0x00);
        wait(.250);
    }

}

Files at this revision

API Documentation at this revision

Comitter:
viswesr
Date:
Sat Sep 21 05:28:44 2013 +0000
Commit message:
PCA9672 Library (I2C IO Expander Interface) : First version

Changed in this revision

PCA9672.cpp Show annotated file Show diff for this revision Revisions of this file
PCA9672.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9672.cpp	Sat Sep 21 05:28:44 2013 +0000
@@ -0,0 +1,54 @@
+/* mbed PCA9672 I2C I/O Expander Library
+ * Copyright (c) 2013 viswesr
+ *
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "PCA9672.h"
+
+PCA9672::PCA9672(PinName sda, PinName scl) : _i2c(sda, scl)
+{
+    // Software Reset
+    _i2c.start();
+    while(_i2c.write(0x00) !=1);
+    while(_i2c.write(0x06) !=1);
+    _i2c.stop();
+
+    /* Software reset is not required. But, gives an 
+       indication if the selected I2C bus frequency works */
+}
+
+void PCA9672::frequency(int hz)
+{
+    _i2c.frequency(hz);
+}
+
+void PCA9672::write(char value)
+{
+    _i2c.write(PCA9672_ADDR, &value, 1);
+}
+
+int PCA9672::read(void)
+{
+    _i2c.read(PCA9672_ADDR | 0x01);
+}
+
+PCA9672::~PCA9672()
+{
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9672.h	Sat Sep 21 05:28:44 2013 +0000
@@ -0,0 +1,69 @@
+/* mbed PCA9672 I2C I/O Expander Library
+ * Copyright (c) 2013 viswesr
+ *
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+ * and associated documentation files (the "Software"), to deal in the Software without restriction, 
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or 
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+#ifndef PCA9672_H
+#define PCA9672_H
+ 
+#include "mbed.h"
+ 
+//  PCA9672 IIC slave address
+#define  PCA9672_ADDR 0x46
+ 
+
+//!Library for the PCA9672 I/O expander.
+/*!
+The PCA9672 is an I2C I/O expander. It has 8 I/O pins.
+*/
+class PCA9672
+{
+public:
+  /*!
+  Connect PCA9672 to I2C port pins sda and scl.
+  */
+  PCA9672(PinName sda, PinName scl);
+  
+  /*!
+  Set the frequency of the I2C interface.
+  */
+  void frequency(int hz);
+
+  /*!
+  Write the value to the IO Expander (pins XP0-XP7 output)
+  */
+  void write(char value);
+  
+  /*!
+  Read the value of the IO Expander (pins XP0-XP7 input)
+  */
+  int read(void);
+    
+  /*!
+  Destroys instance.
+  */ 
+  ~PCA9672();
+  
+private:
+  
+  I2C _i2c;
+ 
+};
+ 
+#endif
\ No newline at end of file