Guillaume Fricker / KS0108_PCF8574

Dependencies:   BusEnums

Dependents:   Menu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCF8574_DataBus.cpp Source File

PCF8574_DataBus.cpp

00001 /* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data Bus
00002  * Copyright (c) 2011 Wim Huiskamp
00003  *
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  *
00006  * version 0.2 Initial Release
00007 */
00008 #include "mbed.h"
00009 #include "PCF8574_DataBus.h"
00010 
00011 
00012 /** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress
00013  *
00014  * @param I2C &i2c the I2C port to connect to 
00015  * @param char deviceAddress the address of the PCF8574
00016 */
00017 PCF8574_DataBus::PCF8574_DataBus(I2C &i2c, char deviceAddress) : _i2c(i2c) {
00018    _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write
00019    _readOpcode  = deviceAddress | 0x01; // low order bit = 1 for read
00020    _init();
00021 }
00022 
00023 /** Optimised DataBus write operation.
00024  * @param byte the datavalue to output on the bus
00025 */
00026 void PCF8574_DataBus::write(char byte) {
00027     char data[1];
00028     
00029     data[0] = byte;
00030     if (_i2c.write(_writeOpcode, data, 1)!=0)    // Write datavalue to bus   
00031         {
00032         printf("erreur i2C write");
00033         }
00034 }
00035 
00036 /** Optimised DataBus read operation.
00037  *
00038  * @returns current data from Databus 
00039 */
00040 char PCF8574_DataBus::read() {
00041     char data[1];
00042 
00043 //Make sure that databus is enabled for Read
00044     data[0] = 0xFF;                       // Init Port for datainput by Writing 0xFF 
00045    _i2c.write(_writeOpcode, data, 1);    // Write to bus   
00046     
00047    if( _i2c.read(_readOpcode, data, 1)!=0)      // Read data from bus
00048      {
00049         printf("erreur i2C read");
00050         data[0]=0xAA;
00051      }  
00052    return data[0];
00053 }
00054 
00055 
00056 /** Enable databus for Write or Read
00057  *
00058  * @param Bus_Dir bus_dir
00059 */
00060 void PCF8574_DataBus::busdir (Bus_Dir bus_dir) {
00061 
00062     if (bus_dir == READ) {
00063       // Make sure that databus is enabled for READ
00064       //write(0xFF);                          // Init Port as input by Writing 0xFF 
00065             
00066     }                
00067     else {
00068       // Make sure that databus is enabled for WRITE   
00069       //write(0xFF);                          // Not really needed, just Init Port to safe settings
00070     }      
00071 }
00072 void PCF8574_DataBus::output(void)
00073     {
00074     busdir (WRITE);
00075     }
00076 void PCF8574_DataBus::input(void)
00077     {
00078     busdir (READ);
00079     }
00080 
00081 
00082 
00083 /** Init PCF8574_DataBus
00084  * @param
00085  * @returns 
00086  */
00087 void PCF8574_DataBus::_init() {
00088  
00089     busdir(WRITE);                          // Init Port as output 
00090 }