Developing Library for the MAX7314 I2C 16-bit I/O Expander w/ PWM
Dependencies: mbed PCA9538_Expander
PCF8575.cpp@0:7e32b01354d3, 2011-04-13 (annotated)
- Committer:
- uasenden
- Date:
- Wed Apr 13 23:17:22 2011 +0000
- Revision:
- 0:7e32b01354d3
Rev 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uasenden | 0:7e32b01354d3 | 1 | /* mbed PCF8574 Library, for driving the I2C I/O Expander |
uasenden | 0:7e32b01354d3 | 2 | * Copyright (c) 2008-2010, cstyles, sford (Originally PCF8574 lib) |
uasenden | 0:7e32b01354d3 | 3 | * new created by Lerche |
uasenden | 0:7e32b01354d3 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
uasenden | 0:7e32b01354d3 | 5 | * of this software and associated documentation files (the "Software"), to deal |
uasenden | 0:7e32b01354d3 | 6 | * in the Software without restriction, including without limitation the rights |
uasenden | 0:7e32b01354d3 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
uasenden | 0:7e32b01354d3 | 8 | * copies of the Software, and to permit persons to whom the Software is |
uasenden | 0:7e32b01354d3 | 9 | * furnished to do so, subject to the following conditions: |
uasenden | 0:7e32b01354d3 | 10 | * |
uasenden | 0:7e32b01354d3 | 11 | * The above copyright notice and this permission notice shall be included in |
uasenden | 0:7e32b01354d3 | 12 | * all copies or substantial portions of the Software. |
uasenden | 0:7e32b01354d3 | 13 | * |
uasenden | 0:7e32b01354d3 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
uasenden | 0:7e32b01354d3 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
uasenden | 0:7e32b01354d3 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
uasenden | 0:7e32b01354d3 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
uasenden | 0:7e32b01354d3 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
uasenden | 0:7e32b01354d3 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
uasenden | 0:7e32b01354d3 | 20 | * THE SOFTWARE. |
uasenden | 0:7e32b01354d3 | 21 | */ |
uasenden | 0:7e32b01354d3 | 22 | |
uasenden | 0:7e32b01354d3 | 23 | #include "PCF8575.h" |
uasenden | 0:7e32b01354d3 | 24 | #include "mbed.h" |
uasenden | 0:7e32b01354d3 | 25 | |
uasenden | 0:7e32b01354d3 | 26 | PCF8575::PCF8575(PinName sda, PinName scl, int address) |
uasenden | 0:7e32b01354d3 | 27 | : _i2c(sda, scl) { |
uasenden | 0:7e32b01354d3 | 28 | _address = address; |
uasenden | 0:7e32b01354d3 | 29 | } |
uasenden | 0:7e32b01354d3 | 30 | |
uasenden | 0:7e32b01354d3 | 31 | int PCF8575::read() { |
uasenden | 0:7e32b01354d3 | 32 | char foo[2]; |
uasenden | 0:7e32b01354d3 | 33 | _i2c.read(_address, foo, 2); |
uasenden | 0:7e32b01354d3 | 34 | return (foo[0] << 8) | foo[1]; |
uasenden | 0:7e32b01354d3 | 35 | } |
uasenden | 0:7e32b01354d3 | 36 | |
uasenden | 0:7e32b01354d3 | 37 | void PCF8575::write(int data) { |
uasenden | 0:7e32b01354d3 | 38 | char foo[2]; |
uasenden | 0:7e32b01354d3 | 39 | foo[0]=data; |
uasenden | 0:7e32b01354d3 | 40 | foo[1]=data>>8; |
uasenden | 0:7e32b01354d3 | 41 | _i2c.write(_address, foo, 2); |
uasenden | 0:7e32b01354d3 | 42 | } |