KS0108 LCD LIB with I2C I/O expander PCF8574 for Databus
PCF8574_DataBus.cpp@4:eeaa5069be9c, 2012-09-10 (annotated)
- Committer:
- GuiTwo
- Date:
- Mon Sep 10 16:33:35 2012 +0000
- Revision:
- 4:eeaa5069be9c
- Parent:
- 0:f2f71eab6aef
Classe fille de MendedDisplay ( Classe abstraite)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GuiTwo | 0:f2f71eab6aef | 1 | /* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data Bus |
GuiTwo | 0:f2f71eab6aef | 2 | * Copyright (c) 2011 Wim Huiskamp |
GuiTwo | 0:f2f71eab6aef | 3 | * |
GuiTwo | 0:f2f71eab6aef | 4 | * Released under the MIT License: http://mbed.org/license/mit |
GuiTwo | 0:f2f71eab6aef | 5 | * |
GuiTwo | 0:f2f71eab6aef | 6 | * version 0.2 Initial Release |
GuiTwo | 0:f2f71eab6aef | 7 | */ |
GuiTwo | 0:f2f71eab6aef | 8 | #include "mbed.h" |
GuiTwo | 0:f2f71eab6aef | 9 | #include "PCF8574_DataBus.h" |
GuiTwo | 0:f2f71eab6aef | 10 | |
GuiTwo | 0:f2f71eab6aef | 11 | |
GuiTwo | 0:f2f71eab6aef | 12 | /** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress |
GuiTwo | 0:f2f71eab6aef | 13 | * |
GuiTwo | 0:f2f71eab6aef | 14 | * @param I2C &i2c the I2C port to connect to |
GuiTwo | 0:f2f71eab6aef | 15 | * @param char deviceAddress the address of the PCF8574 |
GuiTwo | 0:f2f71eab6aef | 16 | */ |
GuiTwo | 0:f2f71eab6aef | 17 | PCF8574_DataBus::PCF8574_DataBus(I2C &i2c, char deviceAddress) : _i2c(i2c) { |
GuiTwo | 0:f2f71eab6aef | 18 | _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write |
GuiTwo | 0:f2f71eab6aef | 19 | _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read |
GuiTwo | 0:f2f71eab6aef | 20 | _init(); |
GuiTwo | 0:f2f71eab6aef | 21 | } |
GuiTwo | 0:f2f71eab6aef | 22 | |
GuiTwo | 0:f2f71eab6aef | 23 | /** Optimised DataBus write operation. |
GuiTwo | 0:f2f71eab6aef | 24 | * @param byte the datavalue to output on the bus |
GuiTwo | 0:f2f71eab6aef | 25 | */ |
GuiTwo | 0:f2f71eab6aef | 26 | void PCF8574_DataBus::write(char byte) { |
GuiTwo | 0:f2f71eab6aef | 27 | char data[1]; |
GuiTwo | 0:f2f71eab6aef | 28 | |
GuiTwo | 0:f2f71eab6aef | 29 | data[0] = byte; |
GuiTwo | 0:f2f71eab6aef | 30 | if (_i2c.write(_writeOpcode, data, 1)!=0) // Write datavalue to bus |
GuiTwo | 0:f2f71eab6aef | 31 | { |
GuiTwo | 0:f2f71eab6aef | 32 | printf("erreur i2C write"); |
GuiTwo | 0:f2f71eab6aef | 33 | } |
GuiTwo | 0:f2f71eab6aef | 34 | } |
GuiTwo | 0:f2f71eab6aef | 35 | |
GuiTwo | 0:f2f71eab6aef | 36 | /** Optimised DataBus read operation. |
GuiTwo | 0:f2f71eab6aef | 37 | * |
GuiTwo | 0:f2f71eab6aef | 38 | * @returns current data from Databus |
GuiTwo | 0:f2f71eab6aef | 39 | */ |
GuiTwo | 0:f2f71eab6aef | 40 | char PCF8574_DataBus::read() { |
GuiTwo | 0:f2f71eab6aef | 41 | char data[1]; |
GuiTwo | 0:f2f71eab6aef | 42 | |
GuiTwo | 0:f2f71eab6aef | 43 | //Make sure that databus is enabled for Read |
GuiTwo | 0:f2f71eab6aef | 44 | data[0] = 0xFF; // Init Port for datainput by Writing 0xFF |
GuiTwo | 0:f2f71eab6aef | 45 | _i2c.write(_writeOpcode, data, 1); // Write to bus |
GuiTwo | 0:f2f71eab6aef | 46 | |
GuiTwo | 0:f2f71eab6aef | 47 | if( _i2c.read(_readOpcode, data, 1)!=0) // Read data from bus |
GuiTwo | 0:f2f71eab6aef | 48 | { |
GuiTwo | 0:f2f71eab6aef | 49 | printf("erreur i2C read"); |
GuiTwo | 0:f2f71eab6aef | 50 | data[0]=0xAA; |
GuiTwo | 0:f2f71eab6aef | 51 | } |
GuiTwo | 0:f2f71eab6aef | 52 | return data[0]; |
GuiTwo | 0:f2f71eab6aef | 53 | } |
GuiTwo | 0:f2f71eab6aef | 54 | |
GuiTwo | 0:f2f71eab6aef | 55 | |
GuiTwo | 0:f2f71eab6aef | 56 | /** Enable databus for Write or Read |
GuiTwo | 0:f2f71eab6aef | 57 | * |
GuiTwo | 0:f2f71eab6aef | 58 | * @param Bus_Dir bus_dir |
GuiTwo | 0:f2f71eab6aef | 59 | */ |
GuiTwo | 0:f2f71eab6aef | 60 | void PCF8574_DataBus::busdir (Bus_Dir bus_dir) { |
GuiTwo | 0:f2f71eab6aef | 61 | |
GuiTwo | 0:f2f71eab6aef | 62 | if (bus_dir == READ) { |
GuiTwo | 0:f2f71eab6aef | 63 | // Make sure that databus is enabled for READ |
GuiTwo | 0:f2f71eab6aef | 64 | //write(0xFF); // Init Port as input by Writing 0xFF |
GuiTwo | 0:f2f71eab6aef | 65 | |
GuiTwo | 0:f2f71eab6aef | 66 | } |
GuiTwo | 0:f2f71eab6aef | 67 | else { |
GuiTwo | 0:f2f71eab6aef | 68 | // Make sure that databus is enabled for WRITE |
GuiTwo | 0:f2f71eab6aef | 69 | //write(0xFF); // Not really needed, just Init Port to safe settings |
GuiTwo | 0:f2f71eab6aef | 70 | } |
GuiTwo | 0:f2f71eab6aef | 71 | } |
GuiTwo | 0:f2f71eab6aef | 72 | void PCF8574_DataBus::output(void) |
GuiTwo | 0:f2f71eab6aef | 73 | { |
GuiTwo | 0:f2f71eab6aef | 74 | busdir (WRITE); |
GuiTwo | 0:f2f71eab6aef | 75 | } |
GuiTwo | 0:f2f71eab6aef | 76 | void PCF8574_DataBus::input(void) |
GuiTwo | 0:f2f71eab6aef | 77 | { |
GuiTwo | 0:f2f71eab6aef | 78 | busdir (READ); |
GuiTwo | 0:f2f71eab6aef | 79 | } |
GuiTwo | 0:f2f71eab6aef | 80 | |
GuiTwo | 0:f2f71eab6aef | 81 | |
GuiTwo | 0:f2f71eab6aef | 82 | |
GuiTwo | 0:f2f71eab6aef | 83 | /** Init PCF8574_DataBus |
GuiTwo | 0:f2f71eab6aef | 84 | * @param |
GuiTwo | 0:f2f71eab6aef | 85 | * @returns |
GuiTwo | 0:f2f71eab6aef | 86 | */ |
GuiTwo | 0:f2f71eab6aef | 87 | void PCF8574_DataBus::_init() { |
GuiTwo | 0:f2f71eab6aef | 88 | |
GuiTwo | 0:f2f71eab6aef | 89 | busdir(WRITE); // Init Port as output |
GuiTwo | 0:f2f71eab6aef | 90 | } |