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