[syntax=cpp]
#include <SPI.h>
void setup(){
pinMode(10, OUTPUT); //CS
pinMode(11, OUTPUT); //MOSI
pinMode(12, INPUT); //MISO
pinMode(13, OUTPUT); //SCK
Serial.begin(115200);
writeCommand;
}
void loop(){
unsigned int val;
for (val = 0; val <= 1023; val++) {
writeDigipots(val);
//delay(10);
}
for (val = 1023; val > 0; val--) {
writeDigipots(val);
//delay(10);
}
}
void writeDigipots(unsigned int val){
SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE1));
digitalWrite(10, LOW);
//SPI.transfer(0x18); //send command byte
//SPI.transfer(0x03);
byte highbyte = val >> 8 + 0x04; //high wiper byte + command
byte lowbyte = val & 0xFF;
SPI.transfer(highbyte); //send wiper data high byte
SPI.transfer(lowbyte); //send wiper data low byte
digitalWrite(10, HIGH);
SPI.endTransaction();
}
void writeCommand() {
SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE1));
digitalWrite(10, LOW);
SPI.transfer(0x18); //send command high byte
SPI.transfer(0x03); //send command low byte
digitalWrite(10, HIGH);
SPI.endTransaction();
}
[/syntax]