Library with address set to hardware pins
Dependents: Test2PCF8574 pcf8574-IO_expander
pcf8574.cpp@0:7913586369c2, 2012-09-08 (annotated)
- Committer:
- Bas
- Date:
- Sat Sep 08 22:11:31 2012 +0000
- Revision:
- 0:7913586369c2
changes in WriteByte. Array data added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bas | 0:7913586369c2 | 1 | /* |
Bas | 0:7913586369c2 | 2 | * PCF8574(A) Remote 8-bit I/O expander for I2C-bus |
Bas | 0:7913586369c2 | 3 | * Copyright (c) 2012 Bas van Drunen Littel |
Bas | 0:7913586369c2 | 4 | * |
Bas | 0:7913586369c2 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Bas | 0:7913586369c2 | 6 | * of this software and associated documentation files (the "Software"), to deal |
Bas | 0:7913586369c2 | 7 | * in the Software without restriction, including without limitation the rights |
Bas | 0:7913586369c2 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Bas | 0:7913586369c2 | 9 | * copies of the Software, and to permit persons to whom the Software is |
Bas | 0:7913586369c2 | 10 | * furnished to do so, subject to the following conditions: |
Bas | 0:7913586369c2 | 11 | * |
Bas | 0:7913586369c2 | 12 | * The above copyright notice and this permission notice shall be included in |
Bas | 0:7913586369c2 | 13 | * all copies or substantial portions of the Software. |
Bas | 0:7913586369c2 | 14 | * |
Bas | 0:7913586369c2 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Bas | 0:7913586369c2 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Bas | 0:7913586369c2 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Bas | 0:7913586369c2 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Bas | 0:7913586369c2 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Bas | 0:7913586369c2 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Bas | 0:7913586369c2 | 21 | * THE SOFTWARE. |
Bas | 0:7913586369c2 | 22 | */ |
Bas | 0:7913586369c2 | 23 | |
Bas | 0:7913586369c2 | 24 | #include "pcf8574.h" |
Bas | 0:7913586369c2 | 25 | |
Bas | 0:7913586369c2 | 26 | PCF8574::PCF8574(I2C* _interface, unsigned char _chipselect, bool _type) |
Bas | 0:7913586369c2 | 27 | : i2c(_interface) |
Bas | 0:7913586369c2 | 28 | { |
Bas | 0:7913586369c2 | 29 | if (_type == PCF8574_TYPE) { |
Bas | 0:7913586369c2 | 30 | address = (_chipselect << 1) | PCF8574_I2C_ADDRESS; |
Bas | 0:7913586369c2 | 31 | } else { |
Bas | 0:7913586369c2 | 32 | address = (_chipselect << 1) | PCF8574A_I2C_ADDRESS; |
Bas | 0:7913586369c2 | 33 | } |
Bas | 0:7913586369c2 | 34 | } |
Bas | 0:7913586369c2 | 35 | |
Bas | 0:7913586369c2 | 36 | PCF8574::~PCF8574() {} |
Bas | 0:7913586369c2 | 37 | |
Bas | 0:7913586369c2 | 38 | unsigned char PCF8574::ReadByte() |
Bas | 0:7913586369c2 | 39 | { |
Bas | 0:7913586369c2 | 40 | char data[1]; |
Bas | 0:7913586369c2 | 41 | i2c->read(address,data,1); |
Bas | 0:7913586369c2 | 42 | return data[0]; |
Bas | 0:7913586369c2 | 43 | } |
Bas | 0:7913586369c2 | 44 | |
Bas | 0:7913586369c2 | 45 | void PCF8574::WriteByte(unsigned char byte) |
Bas | 0:7913586369c2 | 46 | { |
Bas | 0:7913586369c2 | 47 | unsigned char data[1]; |
Bas | 0:7913586369c2 | 48 | |
Bas | 0:7913586369c2 | 49 | data[0]=byte; |
Bas | 0:7913586369c2 | 50 | i2c->write(address,(char *)data,1); |
Bas | 0:7913586369c2 | 51 | } |