Interface to Analog devices AD5258 digital I2C potentiometer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AD5258.cpp Source File

AD5258.cpp

00001 /*  Copyright 2013 Rod Coleman
00002 
00003     This program is free software: you can redistribute it and/or modify
00004     it under the terms of the GNU General Public License as published by
00005     the Free Software Foundation, either version 3 of the License, or
00006     (at your option) any later version.
00007 
00008     This program is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011     GNU General Public License for more details.
00012 
00013     You should have received a copy of the GNU General Public License
00014     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 */    
00016 
00017 #include "AD5258.h"
00018 #include "mbed.h"
00019  
00020 AD5258::AD5258(PinName sda, PinName scl, int address)
00021         : _i2c(sda, scl) {
00022     _address = address;
00023 }
00024  
00025  
00026  // RDAC ACCESS: values of 0 to 0x3F for full pot range.
00027  
00028 int AD5258::read() {
00029     char foo[1];
00030     foo[0] = 00;                       // command to read RDAC
00031     _i2c.write(_address, foo, 1);  
00032     _i2c.read(_address, foo, 1);
00033     return foo[0];
00034 }
00035  
00036 void AD5258::write(int data) {
00037     char foo[2];
00038     foo[0] = 00;
00039     foo[1] = data;
00040     _i2c.write(_address, foo, 2);
00041 }
00042 
00043 // EEPROM ACCESS
00044 
00045 int AD5258::readEE() {
00046     char foo[1];
00047     foo[0] = 0x20;                       // command to read RDAC
00048     _i2c.write(_address, foo, 1);  
00049     _i2c.read(_address, foo, 1);
00050     return foo[0];
00051 }
00052  
00053 void AD5258::writeEE(int data) {
00054     char foo[2];
00055     foo[0] = 0x20;
00056     foo[1] = data;
00057     _i2c.write(_address, foo, 2);
00058 }
00059  
00060  // Store RDAC value to EEPROM, for nonvol retention:
00061  void AD5258::store(void)  {
00062     char foo[1];
00063     foo[0] = 0xC0;                       // command to store RDAC to EE
00064     _i2c.write(_address, foo, 1);      
00065  }
00066  
00067   // restore RDAC value from EEPROM
00068  void AD5258::restore(void)  {
00069     char foo[2];
00070     foo[0] = 0xA0;                       // command to store RDAC to EE
00071     foo[1] = 0x80;                       // NOP to restore low power mode
00072     _i2c.write(_address, foo, 1);      
00073   }
00074   
00075    // software write protect
00076  void AD5258::writeProtect(bool enable)  {
00077     char foo[2];
00078     foo[0] = 0x40;                       // command to store RDAC to EE
00079     if (enable) {
00080         foo[1] = 0x01;                       // SET WP
00081     }
00082     else {
00083         foo[1] = 0x010;                       // RESET WP (enable writes)
00084     }
00085     _i2c.write(_address, foo, 2);      
00086   }