Interface to Analog devices AD5258 digital I2C potentiometer
AD5258.cpp@2:8a71db02417b, 2013-11-12 (annotated)
- Committer:
- RodColeman
- Date:
- Tue Nov 12 12:57:07 2013 +0000
- Revision:
- 2:8a71db02417b
- Parent:
- 1:64570234d7b5
- Child:
- 3:f5b60d166896
v4.29: first release with RF Detect I2C trimmer (manual only)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RodColeman | 0:8920c7d857d8 | 1 | #include "AD5258.h" |
RodColeman | 0:8920c7d857d8 | 2 | #include "mbed.h" |
RodColeman | 0:8920c7d857d8 | 3 | |
RodColeman | 0:8920c7d857d8 | 4 | AD5258::AD5258(PinName sda, PinName scl, int address) |
RodColeman | 0:8920c7d857d8 | 5 | : _i2c(sda, scl) { |
RodColeman | 0:8920c7d857d8 | 6 | _address = address; |
RodColeman | 0:8920c7d857d8 | 7 | } |
RodColeman | 0:8920c7d857d8 | 8 | |
RodColeman | 1:64570234d7b5 | 9 | |
RodColeman | 1:64570234d7b5 | 10 | // RDAC ACCESS: values of 0 to 0x3F for full pot range. |
RodColeman | 1:64570234d7b5 | 11 | |
RodColeman | 0:8920c7d857d8 | 12 | int AD5258::read() { |
RodColeman | 0:8920c7d857d8 | 13 | char foo[1]; |
RodColeman | 1:64570234d7b5 | 14 | foo[0] = 00; // command to read RDAC |
RodColeman | 1:64570234d7b5 | 15 | _i2c.write(_address, foo, 1); |
RodColeman | 0:8920c7d857d8 | 16 | _i2c.read(_address, foo, 1); |
RodColeman | 0:8920c7d857d8 | 17 | return foo[0]; |
RodColeman | 0:8920c7d857d8 | 18 | } |
RodColeman | 0:8920c7d857d8 | 19 | |
RodColeman | 0:8920c7d857d8 | 20 | void AD5258::write(int data) { |
RodColeman | 0:8920c7d857d8 | 21 | char foo[2]; |
RodColeman | 0:8920c7d857d8 | 22 | foo[0] = 00; |
RodColeman | 0:8920c7d857d8 | 23 | foo[1] = data; |
RodColeman | 0:8920c7d857d8 | 24 | _i2c.write(_address, foo, 2); |
RodColeman | 0:8920c7d857d8 | 25 | } |
RodColeman | 1:64570234d7b5 | 26 | |
RodColeman | 1:64570234d7b5 | 27 | // EEPROM ACCESS |
RodColeman | 1:64570234d7b5 | 28 | |
RodColeman | 1:64570234d7b5 | 29 | int AD5258::readEE() { |
RodColeman | 1:64570234d7b5 | 30 | char foo[1]; |
RodColeman | 1:64570234d7b5 | 31 | foo[0] = 0x20; // command to read RDAC |
RodColeman | 1:64570234d7b5 | 32 | _i2c.write(_address, foo, 1); |
RodColeman | 1:64570234d7b5 | 33 | _i2c.read(_address, foo, 1); |
RodColeman | 1:64570234d7b5 | 34 | return foo[0]; |
RodColeman | 1:64570234d7b5 | 35 | } |
RodColeman | 1:64570234d7b5 | 36 | |
RodColeman | 1:64570234d7b5 | 37 | void AD5258::writeEE(int data) { |
RodColeman | 1:64570234d7b5 | 38 | char foo[2]; |
RodColeman | 1:64570234d7b5 | 39 | foo[0] = 0x20; |
RodColeman | 1:64570234d7b5 | 40 | foo[1] = data; |
RodColeman | 1:64570234d7b5 | 41 | _i2c.write(_address, foo, 2); |
RodColeman | 1:64570234d7b5 | 42 | } |
RodColeman | 1:64570234d7b5 | 43 | |
RodColeman | 1:64570234d7b5 | 44 | // Store RDAC value to EEPROM, for nonvol retention: |
RodColeman | 1:64570234d7b5 | 45 | void AD5258::store(void) { |
RodColeman | 1:64570234d7b5 | 46 | char foo[1]; |
RodColeman | 1:64570234d7b5 | 47 | foo[0] = 0xC0; // command to store RDAC to EE |
RodColeman | 1:64570234d7b5 | 48 | _i2c.write(_address, foo, 1); |
RodColeman | 1:64570234d7b5 | 49 | } |
RodColeman | 1:64570234d7b5 | 50 | |
RodColeman | 1:64570234d7b5 | 51 | // restore RDAC value from EEPROM |
RodColeman | 1:64570234d7b5 | 52 | void AD5258::restore(void) { |
RodColeman | 1:64570234d7b5 | 53 | char foo[2]; |
RodColeman | 1:64570234d7b5 | 54 | foo[0] = 0xA0; // command to store RDAC to EE |
RodColeman | 1:64570234d7b5 | 55 | foo[1] = 0x80; // NOP to restore low power mode |
RodColeman | 1:64570234d7b5 | 56 | _i2c.write(_address, foo, 1); |
RodColeman | 2:8a71db02417b | 57 | } |
RodColeman | 2:8a71db02417b | 58 | |
RodColeman | 2:8a71db02417b | 59 | // software write protect |
RodColeman | 2:8a71db02417b | 60 | void AD5258::writeProtect(bool enable) { |
RodColeman | 2:8a71db02417b | 61 | char foo[2]; |
RodColeman | 2:8a71db02417b | 62 | foo[0] = 0x40; // command to store RDAC to EE |
RodColeman | 2:8a71db02417b | 63 | if (enable) { |
RodColeman | 2:8a71db02417b | 64 | foo[1] = 0x01; // SET WP |
RodColeman | 2:8a71db02417b | 65 | } |
RodColeman | 2:8a71db02417b | 66 | else { |
RodColeman | 2:8a71db02417b | 67 | foo[1] = 0x010; // RESET WP (enable writes) |
RodColeman | 2:8a71db02417b | 68 | } |
RodColeman | 2:8a71db02417b | 69 | _i2c.write(_address, foo, 2); |
RodColeman | 1:64570234d7b5 | 70 | } |