I am not getting any errors, but the wiper position will not change. I suspect I am either missing something, or doing something wrong. I would appreciate your help VERY much!
I haven't used this chip but looking at the data sheet you need to get the data you send it into two bytes. The second byte is D7 to D0 of the data you need to write. The first byte consists of :-
LSB = D8
D9
C0
C1
C2
C3
0
MSB = 0
So you need to shift the first byte two places to the right over what you are doing now and add in the two most significant bits of the second byte (your data) into the least significant bits of the first byte.
Well if you would like to know the juicy details, this is a modification on a previously working sketch. The project is now surface-mounted and I needed to swap the potentiometer IC for one with a 10 bit resolution. The sketch works. The bit about interfacing with the potentiometer, however, does not. That is why I only posted the relevant bits.
Here's a crudely chopped down version. Pardon me if there are any unrelated parts left over.
/********************************/
/* Biomed Project */
/* ATMEGA328 */
/********************************/
/*****Libraries*****/
#include <SPI.h> //Serial Protocol Interface for AD5293
/*******************/
/*Current and Voltage Sensors*/
const byte Current = A0; //Measures load current
int CurrentRead = 0; //Current Measurement
byte CurrentCounter = 0;
const byte CurrentPeriod = 10;
byte SensorTimer;
const byte SensorPeriod = 10;
int Resistance = 0x00;
/*****************************/
/**AD5293 Digital Potentiometer**/
const int slaveSelectPin = 10;
/********************************/
/*************User Settings***************/
int DesiredCurrent; //Up to 1A, in 50mA steps
/*****************************************/
/*************Pulse Timing*************/
unsigned long TimeCounter; //Dual-purpose timer; will count from 0 up to PulseWidthLow, then from 0 to PulseWidthHigh (can count up to 50 days)
/**************************************/
/***************Setup***************/
void setup() {
pinMode(slaveSelectPin, OUTPUT);
SPI.setDataMode(SPI_MODE1); //CPOL=0, CPHA=1
SPI.setBitOrder(MSBFIRST); //MSB First
SPI.begin();
digitalWrite(slaveSelectPin, LOW);
SPI.transfer(0b00000110); //Command 4
SPI.transfer(0b00000010); // |
digitalWrite(slaveSelectPin, HIGH);
CurrentCounter = 0; //Timer for periodically setting current
SensorTimer = 0;
Resistance = 0x00;
ResistanceWrite(Resistance);
}
/***********************************/
/*************************************Main*******************************************/
void loop() { // Main flow: Write LCD Screen, Read Buttons, check/increment pulse timer
/************Measure Sensors***********/
if (SensorTimer == SensorPeriod){
SensorTimer = 0;
CurrentRead = GetCurrent();
}
else if (SensorTimer < SensorPeriod){
SensorTimer = SensorTimer + 1;
}
/**************************************/
/*********Current Control**********/
if (CurrentCounter == CurrentPeriod) {
CurrentRead = GetCurrent();
CurrentCounter = 0;
if ((CurrentRead>(DesiredCurrent+10))&&(Resistance<0x3FF)) {
do{
Resistance = Resistance + 1;
ResistanceWrite(Resistance);
CurrentRead = GetCurrent();
}while((CurrentRead>DesiredCurrent)&&(Resistance<0x3FF));
}
if ((CurrentRead<DesiredCurrent)&&(Resistance>0x00)) {
do{
Resistance = Resistance - 1;
ResistanceWrite(Resistance);
CurrentRead = GetCurrent();
}while((CurrentRead<DesiredCurrent)&&(Resistance>0x00));
}
}
else if (CurrentCounter < CurrentPeriod) {
CurrentCounter = CurrentCounter + 1;
}
/**********************************/
}//end loop()
/************************************************************************************/
/***********************************Functions****************************************/
/************************************************************************************/
/******GetCurrent********/
int GetCurrent() {
return map((analogRead(Current)), 0, 1024, 0, 1024); //Fit 10-bit current value
}//end GetCurrent()
/************************/
/****ResistanceWrite****/
void ResistanceWrite(int Resistance) {
byte ResistanceLowerByte = Resistance;
digitalWrite(slaveSelectPin, LOW);
SPI.transfer((0b00000100)|(Resistance>>8)); //Command 1
SPI.transfer(ResistanceLowerByte); //Data
digitalWrite(slaveSelectPin, HIGH);
}//end ResistanceWrite()
/***********************/
hi, i'm about to start working with this chip and have been doing some research on it. just wondering if you ever got this sketch working, and if you could share the results? thanks.
Most of it looks good... but the command needs to be left shifted by 2 (command to enable 0bXX0110DD then your data to write to the command reg 0bXXXXX01X), you are writing to a 16 bit register, 10 of which is data 4 are for commands 2 do not care. Old thread i know, just thought i'd give a heads up for those reading now.
I'm studying to use it in my project but it doesn't work and I don't find the problem. I use the same code as you does, but the potentiometer doesn't takes the commands.