PCF8574 I2C Portexpanders used to provide data, address and controlbus interface

Dependents:   mbed_bus

Files at this revision

API Documentation at this revision

Comitter:
wim
Date:
Sun Jan 25 17:50:03 2015 +0000
Commit message:
PCF8574 Bus Class. First release, converted into lib.

Changed in this revision

BusDefines.h Show annotated file Show diff for this revision Revisions of this file
BusEnums.h Show annotated file Show diff for this revision Revisions of this file
PCF8574_AddressBus.cpp Show annotated file Show diff for this revision Revisions of this file
PCF8574_AddressBus.h Show annotated file Show diff for this revision Revisions of this file
PCF8574_DataBus.cpp Show annotated file Show diff for this revision Revisions of this file
PCF8574_DataBus.h Show annotated file Show diff for this revision Revisions of this file
PCF8574_EnableBus.cpp Show annotated file Show diff for this revision Revisions of this file
PCF8574_EnableBus.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BusDefines.h	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,38 @@
+/* BusDefines - Use the MBED Port pins and PCF8574s for controlling the Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#ifndef _BUS_DEFINES_H
+#define _BUS_DEFINES_H
+
+
+//Pin Defines for MBED Control Bus (inputs)
+#define D_CDINT                p20
+//Pin Defines for MBED Control Bus (outputs)
+#define D_DTR                  p21
+#define D_CDBUF                p22
+#define D_RD                   p23
+#define D_WR                   p24
+
+#define D_CTRL_MSK             0xFF
+
+//Pin Defines for I2C Bus
+//#define D_SDA                  p9
+//#define D_SCL                  p10
+#define D_SDA                  p28
+#define D_SCL                  p27
+
+//I2C Bus Address Defines for PCF8574 slaves
+#define D_I2C_DATA_BUS        0x40
+#define D_I2C_ADDR_BUS        0x42
+#define D_I2C_ENA_BUS         0x44
+
+
+//Host PC Baudrate (Virtual Com Port on USB)
+#define D_BAUDRATE            9600
+//#define D_BAUDRATE            57600
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BusEnums.h	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,17 @@
+/* BusEnums - Use the MBED Port pins and PCF8574s for controlling the Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#ifndef _BUS_ENUMS_H
+#define _BUS_ENUMS_H
+
+//Enums for Control Bus and Enable Bus
+enum Bit_Level { LOW, HIGH };
+enum Bus_Dir   { READ, WRITE };
+enum Bus_Ena   { ENABLE, DISABLE };
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8574_AddressBus.cpp	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,40 @@
+/* PCF8574_AddressBus - Use the PCF8574 I2C Port Extender for controlling the Address Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#include "mbed.h"
+#include "PCF8574_AddressBus.h"
+
+/** Create an PCF8574_AddressBus object connected to the specified I2C object and using the specified deviceAddress
+ *
+ * @param I2C &i2c the I2C port to connect to 
+ * @param char deviceAddress the address of the PCF8574
+*/
+PCF8574_AddressBus::PCF8574_AddressBus(I2C &i2c, char deviceAddress) : _i2c(i2c) {
+   _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
+   _readOpcode  = deviceAddress | 0x01; // low order bit = 1 for read
+   _init();
+}
+
+/** Optimised AddressBus write operation.
+ * @param address the addressvalue to output on the bus
+*/
+void PCF8574_AddressBus::write(char address) {
+    char data[1];
+    
+    data[0] = address;
+    _i2c.write(_writeOpcode, data, 1);    // Write addressvalue to bus   
+}
+
+
+/** Init PCF8574_AddressBus
+ * @param
+ * @returns 
+ */
+void PCF8574_AddressBus::_init() {
+ 
+    write(0x00);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8574_AddressBus.h	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,41 @@
+/* PCF8574_AddressBus - Use the PCF8574 I2C Port Extender for controlling the Address Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#ifndef _PCF8574_ADDRESSBUS_H
+#define _PCF8574_ADDRESSBUS_H
+
+
+//Pin Defines for PCF8574 Address Bus
+#define D_A0                   0x01
+#define D_A1                   0x02
+#define D_A2                   0x04
+#define D_A3                   0x08
+#define D_A4                   0x10
+#define D_A5                   0x20
+#define D_A6                   0x40
+#define D_A7                   0x80
+
+#define D_ADDR_MSK             0x3F
+
+
+/** Create an PCF8574_AddressBus object connected to the specified I2C object and using the specified deviceAddress
+ *
+ * @param I2C &i2c the I2C port to connect to 
+ * @param char deviceAddress the address of the PCF8574
+*/
+class PCF8574_AddressBus {
+public:
+    PCF8574_AddressBus(I2C &i2c, char deviceAddress);
+    void write(char address);
+protected:
+    I2C &_i2c;
+    char _readOpcode;
+    char _writeOpcode; 
+    void _init(); 
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8574_DataBus.cpp	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,75 @@
+/* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#include "mbed.h"
+#include "PCF8574_DataBus.h"
+
+/** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress
+ *
+ * @param I2C &i2c the I2C port to connect to 
+ * @param char deviceAddress the address of the PCF8574
+*/
+PCF8574_DataBus::PCF8574_DataBus(I2C &i2c, char deviceAddress) : _i2c(i2c) {
+   _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
+   _readOpcode  = deviceAddress | 0x01; // low order bit = 1 for read
+   _init();
+}
+
+/** Optimised DataBus write operation.
+ * @param byte the datavalue to output on the bus
+*/
+void PCF8574_DataBus::write(char byte) {
+    char data[1];
+    
+    data[0] = byte;
+    _i2c.write(_writeOpcode, data, 1);    // Write datavalue to bus   
+}
+
+/** Optimised DataBus read operation.
+ *
+ * @returns current data from Databus 
+*/
+char PCF8574_DataBus::read() {
+    char data[1];
+
+//Make sure that databus is enabled for Read
+//    data[0] = 0xFF;                       // Init Port for datainput by Writing 0xFF 
+//   _i2c.write(_writeOpcode, data, 1);    // Write to bus   
+    
+    _i2c.read(_readOpcode, data, 1);      // Read data from bus
+     
+   return data[0];
+}
+
+
+/** Enable databus for Write or Read
+ *
+ * @param Bus_Dir bus_dir
+*/
+void PCF8574_DataBus::busdir (Bus_Dir bus_dir) {
+
+    if (bus_dir == READ) {
+      // Make sure that databus is enabled for READ
+      write(0xFF);                          // Init Port as input by Writing 0xFF 
+            
+    }                
+    else {
+      // Make sure that databus is enabled for WRITE   
+      write(0xFF);                          // Not really needed, just Init Port to safe settings
+    }      
+}
+
+
+
+/** Init PCF8574_DataBus
+ * @param
+ * @returns 
+ */
+void PCF8574_DataBus::_init() {
+ 
+    busdir(WRITE);                          // Init Port as output 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8574_DataBus.h	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,45 @@
+/* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#ifndef _PCF8574_DATABUS_H
+#define _PCF8574_DATABUS_H
+
+//Pin Defines for PCF8574 Data Bus
+#define D_D0                   0x01
+#define D_D1                   0x02
+#define D_D2                   0x04
+#define D_D3                   0x08
+#define D_D4                   0x10
+#define D_D5                   0x20
+#define D_D6                   0x40
+#define D_D7                   0x80
+
+#define D_DATA_MSK             0xFF
+
+//Enums for Data Bus
+#include "BusEnums.h"
+
+
+/** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress
+ *
+ * @param I2C &i2c the I2C port to connect to 
+ * @param char deviceAddress the address of the PCF8574
+*/
+class PCF8574_DataBus {
+public:
+    PCF8574_DataBus(I2C &i2c, char deviceAddress);
+    char read();
+    void write(char byte);
+    void busdir (Bus_Dir bus_dir);    
+protected:
+    I2C &_i2c;
+    char _readOpcode;
+    char _writeOpcode; 
+    void _init(); 
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8574_EnableBus.cpp	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,130 @@
+/* PCF8574_EnableBus - Use the PCF8574 I2C Port Extender for controlling the Chip Enable Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#include "mbed.h"
+#include "PCF8574_EnableBus.h"
+
+
+/** Create an PCF8574_EnableBus object connected to the specified I2C object and using the specified deviceAddress
+ *
+ * @param I2C &i2c the I2C port to connect to 
+ * @param char deviceAddress the address of the PCF8574
+*/
+PCF8574_EnableBus::PCF8574_EnableBus(I2C &i2c, char deviceAddress) : _i2c(i2c) {
+   _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
+   _readOpcode  = deviceAddress | 0x01; // low order bit = 1 for read
+   _init();
+}
+
+
+/** Set or Reset Chip Select pins on Enable Bus
+ *
+ * @param CS_Pin cs_pin 
+ * @param Bit_Level cs_level
+*/
+void PCF8574_EnableBus::chipselect (CS_Pin cs_pin, Bit_Level cs_level) {
+    int result = 1;
+      
+    switch (cs_pin) {
+     case CS_SWITCH : if (cs_level == LOW)
+                         _enable_bus = ~D_CS_SWITCH; // CS Pin Low, make sure that only one CS is active
+                      else
+                         _enable_bus |= D_CS_SWITCH; // CS Pin High
+                      break;
+     case LATCHEN_1 : if (cs_level == LOW)
+                         _enable_bus = ~D_LATCHEN_1; // CS Pin Low, make sure that only one CS is active
+                      else
+                         _enable_bus |= D_LATCHEN_1; // CS Pin High
+                      break;
+     case LATCHEN_2 : if (cs_level == LOW)
+                         _enable_bus = ~D_LATCHEN_2; // CS Pin Low, make sure that only one CS is active
+                      else
+                         _enable_bus |= D_LATCHEN_2; // CS Pin High
+                      break;
+     case CS_BRIGHT : if (cs_level == LOW)
+                         _enable_bus = ~D_CS_BRIGHT; // CS Pin Low, make sure that only one CS is active
+                      else
+                         _enable_bus |= D_CS_BRIGHT; // CS Pin High
+                      break;
+     case CS_DISP : if (cs_level == LOW)
+                         _enable_bus = ~D_CS_DISP;   // CS Pin Low, make sure that only one CS is active
+                      else
+                         _enable_bus |= D_CS_DISP;   // CS Pin High
+                      break;
+     
+     default:          // Oops, we should never end up here....
+                      result = -1;
+    }
+
+    _write();                              // Write chip enable bits to bus
+}
+
+/** Set or Clear the Reset pin on Enable Bus
+ *
+ * @param Bit_Level rst_level
+*/
+void PCF8574_EnableBus::reset (Bit_Level rst_level) {
+
+    if (rst_level == LOW) {
+        _reset_pin = 0x00;                 // Reset Pin Low
+      }               
+    else {
+        _reset_pin = D_RESET;              // Reset Pin High
+    }     
+    
+    _write();                              // Write RST bit to bus
+}
+ 
+               
+/** Set or Clear the NoGo pin on Enable Bus
+ *
+ * @param Bit_Level nogo_level
+*/
+void PCF8574_EnableBus::nogo (Bit_Level nogo_level) {
+
+    if (nogo_level == LOW) {
+        _nogo_pin = 0x00;                  // NOGO Pin Low
+      }               
+    else {
+        _nogo_pin = D_NOGO;                // NOGO Pin High
+    }     
+    
+    _write();                              // Write NoGo bit to bus
+}
+ 
+
+/** Optimised EnableBus write operation.
+ * @param byte the value to output on the bus
+*/
+void PCF8574_EnableBus::_write(char byte) {
+    char data[1];
+    
+    data[0] = byte;
+    _i2c.write(_writeOpcode, data, 1);    // Write value to bus   
+}
+
+/** Optimised EnableBus write operation.
+ * @param 
+*/
+void PCF8574_EnableBus::_write() {
+    char data[1];
+    
+    data[0] = (_enable_bus & D_ENABLE_MSK) | _reset_pin | _nogo_pin;      // Combine enable bits and control bits
+    _i2c.write(_writeOpcode, data, 1);    // Write value to bus   
+}
+
+
+/** Init PCF8574_EnableBus
+ * @param
+ * @returns 
+ */
+void PCF8574_EnableBus::_init() {
+    _enable_bus = 0xFF;                    // Make sure that all CS pins are disabled
+    _reset_pin  = D_RESET;                 // Make sure that Reset pin is disabled
+    _nogo_pin   = D_NOGO;                  // Make sure that NoGo pin is disabled    
+    _write();                              // Write value to bus    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8574_EnableBus.h	Sun Jan 25 17:50:03 2015 +0000
@@ -0,0 +1,53 @@
+/* PCF8574_EnableBus - Use the PCF8574 I2C Port Extender for controlling the Chip Enable Bus
+ * Copyright (c) 2011 Wim Huiskamp
+ *
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * version 0.2 Initial Release
+*/
+#ifndef _PCF8574_ENABLEBUS_H
+#define _PCF8574_ENABLEBUS_H
+
+//Pin Defines for PCF8574 Enable Bus
+//Note: 'Reset' causes all devices on the 'Control & Display Unit' to be reset!
+#define D_CS_SWITCH            0x01
+#define D_LATCHEN_1            0x02
+#define D_LATCHEN_2            0x04
+#define D_CS_BRIGHT            0x08
+#define D_CS_DISP              0x10
+#define D_CS_SPARE             0x20
+#define D_RESET                0x40
+#define D_NOGO                 0x80
+
+#define D_ENABLE_MSK           0x3F
+
+//Enums for Enable Bus
+#include "BusEnums.h"
+enum CS_Pin { CS_SWITCH, LATCHEN_1, LATCHEN_2, CS_BRIGHT, CS_DISP, CS_SPARE };
+
+
+/** Create an PCF8574_EnableBus object connected to the specified I2C object and using the specified deviceAddress
+ *
+ * @param I2C &i2c the I2C port to connect to 
+ * @param char deviceAddress the address of the PCF8574
+*/
+class PCF8574_EnableBus {
+public:
+    PCF8574_EnableBus(I2C &i2c, char deviceAddress);
+    void chipselect (CS_Pin cs_pin, Bit_Level cs_level);
+    void reset (Bit_Level rst_level);
+    void nogo (Bit_Level nogo_level);
+protected:
+    I2C &_i2c;
+    char _readOpcode;
+    char _writeOpcode; 
+    char _enable_bus;   
+    char _reset_pin;   
+    char _nogo_pin;   
+//    char _read();
+    void _write();  
+    void _write(char byte);      
+    void _init(); 
+};
+
+#endif
\ No newline at end of file