Interface to Analog devices AD5258 digital I2C potentiometer

AD5258.cpp

Committer:
RodColeman
Date:
2013-11-12
Revision:
3:f5b60d166896
Parent:
2:8a71db02417b

File content as of revision 3:f5b60d166896:

/*  Copyright 2013 Rod Coleman

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/    

#include "AD5258.h"
#include "mbed.h"
 
AD5258::AD5258(PinName sda, PinName scl, int address)
        : _i2c(sda, scl) {
    _address = address;
}
 
 
 // RDAC ACCESS: values of 0 to 0x3F for full pot range.
 
int AD5258::read() {
    char foo[1];
    foo[0] = 00;                       // command to read RDAC
    _i2c.write(_address, foo, 1);  
    _i2c.read(_address, foo, 1);
    return foo[0];
}
 
void AD5258::write(int data) {
    char foo[2];
    foo[0] = 00;
    foo[1] = data;
    _i2c.write(_address, foo, 2);
}

// EEPROM ACCESS

int AD5258::readEE() {
    char foo[1];
    foo[0] = 0x20;                       // command to read RDAC
    _i2c.write(_address, foo, 1);  
    _i2c.read(_address, foo, 1);
    return foo[0];
}
 
void AD5258::writeEE(int data) {
    char foo[2];
    foo[0] = 0x20;
    foo[1] = data;
    _i2c.write(_address, foo, 2);
}
 
 // Store RDAC value to EEPROM, for nonvol retention:
 void AD5258::store(void)  {
    char foo[1];
    foo[0] = 0xC0;                       // command to store RDAC to EE
    _i2c.write(_address, foo, 1);      
 }
 
  // restore RDAC value from EEPROM
 void AD5258::restore(void)  {
    char foo[2];
    foo[0] = 0xA0;                       // command to store RDAC to EE
    foo[1] = 0x80;                       // NOP to restore low power mode
    _i2c.write(_address, foo, 1);      
  }
  
   // software write protect
 void AD5258::writeProtect(bool enable)  {
    char foo[2];
    foo[0] = 0x40;                       // command to store RDAC to EE
    if (enable) {
        foo[1] = 0x01;                       // SET WP
    }
    else {
        foo[1] = 0x010;                       // RESET WP (enable writes)
    }
    _i2c.write(_address, foo, 2);      
  }