In this circuit, we have connected an LED to each of the 6 wipers of the AD5206, with the A pins connected to ground and the B pins connected to +5VDC, exactly the same way the potentiometer is wired to be a voltage divider in the potentiometer analogue input tutorial. In this code example, we cycle through each of the 6 built in potentiometers and flash the LEDs quickly ON and slowly OFF to demonstrate the potentiometer function of the AD5206.
Code: Select all
//use SPI to control the AD5206 Digital Potentiometer
//with LEDs connected to each potentiometer channel (6)
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
byte pot=0;
byte resistance=0;
byte waitVariable = 0;
///////////////////////////////////////////////////////////////////
//spi transfer function (from ATmega168 datasheet)
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void setup()
{
byte i;
byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
///////////////////////////////////////////////////////////////////
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
///////////////////////////////////////////////////////////////////
for (i=0;i<6;i++) //set value of each pot to 0, turns off LEDS
{
write_pot(i,255);
}
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
byte write_pot(int potAddress, int potValue) //dig pot data transfer function
{
digitalWrite(SLAVESELECT,LOW); //digital pot chip select is active low
//2 byte data transfer to digital pot
spi_transfer(potAddress);
spi_transfer(potValue);
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void loop()
{
waitVariable = 50; //delay variable begins at 50ms
for (resistance = 0; resistance <50; resistance++) //0 to 50 is most effective range for LED brightness
{
write_pot(pot,resistance); //send SPI data to digital potentiometer
delay(waitVariable); //wait, leave pot at current setting
waitVariable--; //increase fade rate each time through for loop
}
write_pot(pot, 255); //set resistance to 0 to turn off LED completely
pot++; //cycle through each of the 6 pots in the 5206 chip
if (pot==6)
{
pot=0;
}
}