我已经下载并成功编译了用于与AD5270数字电位器Controlling AD5270 10-bit Potentiometer with Arduino通信的代码 该代码是滚动类型的缓冲区,可连续写入RDAC,因此电阻值是周期性的。这对于演示目的非常有用,但对于“设置并忘记它”的电阻值则不切实际。
硬件:Arduino Nano; AD5270连接在分线板(MSSOP)上;欧姆表连接在端子A和W之间; + 5v连接到Nano的+5和GND。
问题是我试图修改代码以对通过串行输入提供的值做出反应,但没有任何反应。例如,我将发送0x0464
之类的信息。我的设置工作正常,因为当我使用下面的代码时,我可以看到欧姆表的变化值。但它始终以相同的960欧姆电阻值着陆。
老实说,我在理解数据表时遇到了麻烦。这里有一些问题:
我每次发送命令时都必须发送0x1C02
还是可以执行一次,并且RDAC将保持启用状态直到更改它?
我要左移还是右移代码?右上方链接的代码移动。如果MSB优先,那不应该是一个左移吗?
#include <SPI.h>
const int csPinCWF = 10;
const byte enableUpdateMSB = 0x1C; //B00011100
const byte enableUpdateLSB = 0x02; //B00000010
const byte command = 0x04; //B00000100
void setup() {
Serial.begin(9600);
Serial.println("Ready");
SPI.begin();
Serial.println("SPI Begin");
SPI.setBitOrder(MSBFIRST); //We know this from the Data Sheet
SPI.setDataMode(SPI_MODE1); //Pg.7:Fig.3 states CPOL=0, CPHA=1 --> MODE1
pinMode(csPinCWF,OUTPUT);
Serial.println("Output Set");
digitalWrite(csPinCWF, HIGH);
Serial.println("Set pin to HIGH");
enablePotWrite(csPinCWF);
}
void loop()
{
// Serial listener that sets the wiper to the specified value.
while (Serial.available() > 0)
{
char inChar = Serial.read();
digitalPotWrite(csPinCWF, inChar);
delay(10);
Serial.println(" ");
}
}
void digitalPotWrite(int csPin, int value) {
Serial.println("In digitalPotWrite Now");
digitalWrite(csPin, LOW); //select slave
Serial.println("Set csPin to LOW");
Serial.print("Command Byte is: ");
Serial.println(command, BIN);
byte shiftedValue = (value >> 8);
Serial.print("Shifted bit value is: ");
Serial.println(shiftedValue, BIN);
byte byte1 = (command | shiftedValue);
Serial.print("Byte1 is: ");
Serial.println(byte1, BIN);
byte byte0 = (value & 0xFF); //0xFF = B11111111 trunicates value to 8 bits
Serial.print("Byte0 is: ");
Serial.println(byte0, BIN);
//Write to the RDAC Register to move the wiper
SPI.transfer(byte1);
SPI.transfer(byte0);
Serial.print("Transfered: ");
Serial.print(byte1, BIN);
Serial.print(" ");
Serial.println(byte0, BIN);
digitalWrite(csPin, HIGH); //de-select slave
Serial.println("Set csPin back to HIGH, end of digitalPotWrite");
}
void enablePotWrite(int csPin)
{ //Enable Update of the Wiper position through the digital interface
digitalWrite(csPin, LOW); //select slave
Serial.print("Enable byte is: ");
Serial.print(enableUpdateMSB, BIN);
Serial.print(" ");
Serial.println(enableUpdateLSB, BIN);
SPI.transfer(enableUpdateMSB);
SPI.transfer(enableUpdateLSB);
digitalWrite(csPin, HIGH); //de-select slave
}