Solutions for the I2C experiments for LPC812 MAX

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
embeddedartists
Date:
Sun Nov 24 12:34:47 2013 +0000
Commit message:
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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld 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	Sun Nov 24 12:34:47 2013 +0000
@@ -0,0 +1,48 @@
+#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::direction(uint8_t inputs)
+{
+    _pins = inputs & 0xff;
+}
+ 
+void PCA9672::write(char value)
+{
+    if (_pins > 0)
+    {
+        _i2c.start();
+        _i2c.write(PCA9672_ADDR);
+        _i2c.write(value | _pins); // all input pins must have a logic 1 as value
+        _i2c.stop();
+    }
+}
+ 
+int PCA9672::read(void)
+{
+    _i2c.start();
+    _i2c.write(PCA9672_ADDR | 1);
+    uint8_t val = _i2c.read(0); // expect NACK
+    _i2c.stop();
+    return val;
+}
+ 
+PCA9672::~PCA9672()
+{
+ 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9672.h	Sun Nov 24 12:34:47 2013 +0000
@@ -0,0 +1,54 @@
+#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);
+ 
+  /*!
+  Setup pin direction (bit = 1 for inputs, 0 for outputs)
+  */
+  void direction(uint8_t inputs);
+  
+  /*!
+  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;
+  uint8_t _pins;
+ 
+};
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Nov 24 12:34:47 2013 +0000
@@ -0,0 +1,54 @@
+#include "mbed.h"
+#include "PCA9672.h"
+ 
+PCA9672 ioxp(P0_10, P0_11); //I2C connected to PCA9672 in LPC800-MAX
+ 
+Serial pc(USBTX, USBRX); // tx, rx
+
+static void experiment1_alt1()
+{
+    ioxp.frequency(100000);
+    ioxp.direction(0x80); // mark the push-button pin as input and the rest as outputs
+ 
+    while (1) {
+        uint8_t val = ioxp.read();
+        pc.printf("Reading 0x%02x - bit 7 is %d\n", val, val>>7);
+        if (val & 0x80)
+        {
+            ioxp.write(0x10);
+        }
+        else
+        {
+            ioxp.write(0x20);
+        }
+        wait(.50);
+    }
+}
+
+static void experiment1_alt2()
+{
+    ioxp.frequency(100000);
+    ioxp.direction(0x80 | 0x20); // mark the push-button pins (on breadboard and onboard) as inputs
+ 
+    while (1) {
+        uint8_t val = ioxp.read();
+        pc.printf("Reading 0x%02x - bit 7 is %d, bit 5 is %d\n", val, (val>>7)&1, (val>>5)&1);
+        if (((val & 0xa0)==0xa0) || ((val & 0xa0)==0x00))
+        {
+            // both or neither buttons are pressed
+            ioxp.write(0x10);
+        }
+        else
+        {
+            // exactly one of the buttons are pressed
+            ioxp.write(0x00);
+        }
+        wait(.50);
+    }
+}
+
+int main()
+{
+    //experiment1_alt1(); //onboard button controls yellow (pressed) and green (released) LEDs
+    experiment1_alt2(); //two buttons. Yellow LED lit with exactly one is pressed
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Nov 24 12:34:47 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f
\ No newline at end of file