Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Committer:
wim
Date:
Wed Aug 31 19:45:31 2011 +0000
Revision:
0:2467aed99127
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:2467aed99127 1 /* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data Bus
wim 0:2467aed99127 2 * Copyright (c) 2011 Wim Huiskamp
wim 0:2467aed99127 3 *
wim 0:2467aed99127 4 * Released under the MIT License: http://mbed.org/license/mit
wim 0:2467aed99127 5 *
wim 0:2467aed99127 6 * version 0.2 Initial Release
wim 0:2467aed99127 7 */
wim 0:2467aed99127 8 #include "mbed.h"
wim 0:2467aed99127 9 #include "PCF8574_DataBus.h"
wim 0:2467aed99127 10
wim 0:2467aed99127 11 /** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress
wim 0:2467aed99127 12 *
wim 0:2467aed99127 13 * @param I2C &i2c the I2C port to connect to
wim 0:2467aed99127 14 * @param char deviceAddress the address of the PCF8574
wim 0:2467aed99127 15 */
wim 0:2467aed99127 16 PCF8574_DataBus::PCF8574_DataBus(I2C &i2c, char deviceAddress) : _i2c(i2c) {
wim 0:2467aed99127 17 _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
wim 0:2467aed99127 18 _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read
wim 0:2467aed99127 19 _init();
wim 0:2467aed99127 20 }
wim 0:2467aed99127 21
wim 0:2467aed99127 22 /** Optimised DataBus write operation.
wim 0:2467aed99127 23 * @param byte the datavalue to output on the bus
wim 0:2467aed99127 24 */
wim 0:2467aed99127 25 void PCF8574_DataBus::write(char byte) {
wim 0:2467aed99127 26 char data[1];
wim 0:2467aed99127 27
wim 0:2467aed99127 28 data[0] = byte;
wim 0:2467aed99127 29 _i2c.write(_writeOpcode, data, 1); // Write datavalue to bus
wim 0:2467aed99127 30 }
wim 0:2467aed99127 31
wim 0:2467aed99127 32 /** Optimised DataBus read operation.
wim 0:2467aed99127 33 *
wim 0:2467aed99127 34 * @returns current data from Databus
wim 0:2467aed99127 35 */
wim 0:2467aed99127 36 char PCF8574_DataBus::read() {
wim 0:2467aed99127 37 char data[1];
wim 0:2467aed99127 38
wim 0:2467aed99127 39 //Make sure that databus is enabled for Read
wim 0:2467aed99127 40 // data[0] = 0xFF; // Init Port for datainput by Writing 0xFF
wim 0:2467aed99127 41 // _i2c.write(_writeOpcode, data, 1); // Write to bus
wim 0:2467aed99127 42
wim 0:2467aed99127 43 _i2c.read(_readOpcode, data, 1); // Read data from bus
wim 0:2467aed99127 44
wim 0:2467aed99127 45 return data[0];
wim 0:2467aed99127 46 }
wim 0:2467aed99127 47
wim 0:2467aed99127 48
wim 0:2467aed99127 49 /** Enable databus for Write or Read
wim 0:2467aed99127 50 *
wim 0:2467aed99127 51 * @param Bus_Dir bus_dir
wim 0:2467aed99127 52 */
wim 0:2467aed99127 53 void PCF8574_DataBus::busdir (Bus_Dir bus_dir) {
wim 0:2467aed99127 54
wim 0:2467aed99127 55 if (bus_dir == READ) {
wim 0:2467aed99127 56 // Make sure that databus is enabled for READ
wim 0:2467aed99127 57 write(0xFF); // Init Port as input by Writing 0xFF
wim 0:2467aed99127 58
wim 0:2467aed99127 59 }
wim 0:2467aed99127 60 else {
wim 0:2467aed99127 61 // Make sure that databus is enabled for WRITE
wim 0:2467aed99127 62 write(0xFF); // Not really needed, just Init Port to safe settings
wim 0:2467aed99127 63 }
wim 0:2467aed99127 64 }
wim 0:2467aed99127 65
wim 0:2467aed99127 66
wim 0:2467aed99127 67
wim 0:2467aed99127 68 /** Init PCF8574_DataBus
wim 0:2467aed99127 69 * @param
wim 0:2467aed99127 70 * @returns
wim 0:2467aed99127 71 */
wim 0:2467aed99127 72 void PCF8574_DataBus::_init() {
wim 0:2467aed99127 73
wim 0:2467aed99127 74 busdir(WRITE); // Init Port as output
wim 0:2467aed99127 75 }