Interface to Analog devices AD5258 digital I2C potentiometer
AD5258.cpp@3:f5b60d166896, 2013-11-12 (annotated)
- Committer:
- RodColeman
- Date:
- Tue Nov 12 13:51:04 2013 +0000
- Revision:
- 3:f5b60d166896
- Parent:
- 2:8a71db02417b
add GPL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RodColeman | 3:f5b60d166896 | 1 | /* Copyright 2013 Rod Coleman |
RodColeman | 3:f5b60d166896 | 2 | |
RodColeman | 3:f5b60d166896 | 3 | This program is free software: you can redistribute it and/or modify |
RodColeman | 3:f5b60d166896 | 4 | it under the terms of the GNU General Public License as published by |
RodColeman | 3:f5b60d166896 | 5 | the Free Software Foundation, either version 3 of the License, or |
RodColeman | 3:f5b60d166896 | 6 | (at your option) any later version. |
RodColeman | 3:f5b60d166896 | 7 | |
RodColeman | 3:f5b60d166896 | 8 | This program is distributed in the hope that it will be useful, |
RodColeman | 3:f5b60d166896 | 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
RodColeman | 3:f5b60d166896 | 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
RodColeman | 3:f5b60d166896 | 11 | GNU General Public License for more details. |
RodColeman | 3:f5b60d166896 | 12 | |
RodColeman | 3:f5b60d166896 | 13 | You should have received a copy of the GNU General Public License |
RodColeman | 3:f5b60d166896 | 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
RodColeman | 3:f5b60d166896 | 15 | */ |
RodColeman | 3:f5b60d166896 | 16 | |
RodColeman | 0:8920c7d857d8 | 17 | #include "AD5258.h" |
RodColeman | 0:8920c7d857d8 | 18 | #include "mbed.h" |
RodColeman | 0:8920c7d857d8 | 19 | |
RodColeman | 0:8920c7d857d8 | 20 | AD5258::AD5258(PinName sda, PinName scl, int address) |
RodColeman | 0:8920c7d857d8 | 21 | : _i2c(sda, scl) { |
RodColeman | 0:8920c7d857d8 | 22 | _address = address; |
RodColeman | 0:8920c7d857d8 | 23 | } |
RodColeman | 0:8920c7d857d8 | 24 | |
RodColeman | 1:64570234d7b5 | 25 | |
RodColeman | 1:64570234d7b5 | 26 | // RDAC ACCESS: values of 0 to 0x3F for full pot range. |
RodColeman | 1:64570234d7b5 | 27 | |
RodColeman | 0:8920c7d857d8 | 28 | int AD5258::read() { |
RodColeman | 0:8920c7d857d8 | 29 | char foo[1]; |
RodColeman | 1:64570234d7b5 | 30 | foo[0] = 00; // command to read RDAC |
RodColeman | 1:64570234d7b5 | 31 | _i2c.write(_address, foo, 1); |
RodColeman | 0:8920c7d857d8 | 32 | _i2c.read(_address, foo, 1); |
RodColeman | 0:8920c7d857d8 | 33 | return foo[0]; |
RodColeman | 0:8920c7d857d8 | 34 | } |
RodColeman | 0:8920c7d857d8 | 35 | |
RodColeman | 0:8920c7d857d8 | 36 | void AD5258::write(int data) { |
RodColeman | 0:8920c7d857d8 | 37 | char foo[2]; |
RodColeman | 0:8920c7d857d8 | 38 | foo[0] = 00; |
RodColeman | 0:8920c7d857d8 | 39 | foo[1] = data; |
RodColeman | 0:8920c7d857d8 | 40 | _i2c.write(_address, foo, 2); |
RodColeman | 0:8920c7d857d8 | 41 | } |
RodColeman | 1:64570234d7b5 | 42 | |
RodColeman | 1:64570234d7b5 | 43 | // EEPROM ACCESS |
RodColeman | 1:64570234d7b5 | 44 | |
RodColeman | 1:64570234d7b5 | 45 | int AD5258::readEE() { |
RodColeman | 1:64570234d7b5 | 46 | char foo[1]; |
RodColeman | 1:64570234d7b5 | 47 | foo[0] = 0x20; // command to read RDAC |
RodColeman | 1:64570234d7b5 | 48 | _i2c.write(_address, foo, 1); |
RodColeman | 1:64570234d7b5 | 49 | _i2c.read(_address, foo, 1); |
RodColeman | 1:64570234d7b5 | 50 | return foo[0]; |
RodColeman | 1:64570234d7b5 | 51 | } |
RodColeman | 1:64570234d7b5 | 52 | |
RodColeman | 1:64570234d7b5 | 53 | void AD5258::writeEE(int data) { |
RodColeman | 1:64570234d7b5 | 54 | char foo[2]; |
RodColeman | 1:64570234d7b5 | 55 | foo[0] = 0x20; |
RodColeman | 1:64570234d7b5 | 56 | foo[1] = data; |
RodColeman | 1:64570234d7b5 | 57 | _i2c.write(_address, foo, 2); |
RodColeman | 1:64570234d7b5 | 58 | } |
RodColeman | 1:64570234d7b5 | 59 | |
RodColeman | 1:64570234d7b5 | 60 | // Store RDAC value to EEPROM, for nonvol retention: |
RodColeman | 1:64570234d7b5 | 61 | void AD5258::store(void) { |
RodColeman | 1:64570234d7b5 | 62 | char foo[1]; |
RodColeman | 1:64570234d7b5 | 63 | foo[0] = 0xC0; // command to store RDAC to EE |
RodColeman | 1:64570234d7b5 | 64 | _i2c.write(_address, foo, 1); |
RodColeman | 1:64570234d7b5 | 65 | } |
RodColeman | 1:64570234d7b5 | 66 | |
RodColeman | 1:64570234d7b5 | 67 | // restore RDAC value from EEPROM |
RodColeman | 1:64570234d7b5 | 68 | void AD5258::restore(void) { |
RodColeman | 1:64570234d7b5 | 69 | char foo[2]; |
RodColeman | 1:64570234d7b5 | 70 | foo[0] = 0xA0; // command to store RDAC to EE |
RodColeman | 1:64570234d7b5 | 71 | foo[1] = 0x80; // NOP to restore low power mode |
RodColeman | 1:64570234d7b5 | 72 | _i2c.write(_address, foo, 1); |
RodColeman | 2:8a71db02417b | 73 | } |
RodColeman | 2:8a71db02417b | 74 | |
RodColeman | 2:8a71db02417b | 75 | // software write protect |
RodColeman | 2:8a71db02417b | 76 | void AD5258::writeProtect(bool enable) { |
RodColeman | 2:8a71db02417b | 77 | char foo[2]; |
RodColeman | 2:8a71db02417b | 78 | foo[0] = 0x40; // command to store RDAC to EE |
RodColeman | 2:8a71db02417b | 79 | if (enable) { |
RodColeman | 2:8a71db02417b | 80 | foo[1] = 0x01; // SET WP |
RodColeman | 2:8a71db02417b | 81 | } |
RodColeman | 2:8a71db02417b | 82 | else { |
RodColeman | 2:8a71db02417b | 83 | foo[1] = 0x010; // RESET WP (enable writes) |
RodColeman | 2:8a71db02417b | 84 | } |
RodColeman | 2:8a71db02417b | 85 | _i2c.write(_address, foo, 2); |
RodColeman | 1:64570234d7b5 | 86 | } |