Sunday, August 19, 2007

sto_mcp42xxx mini lib

This morning i decided to encapsulate my Arduino code for interfacing with an MCP42xxx digital potentiometer IC. The resulting "mini library" can be downloaded here: http://www.milkcrate.com.au/_other/arduino/sto_mcp42xxx.zip. To install, uncompress the file and add the folder sto_mcp42xxx to /lib/targets/libraries.

Please note that as i don't actually have my Arduino NG here with me at the moment, this code is untested.


Single Function Send
The "mini library" contains a single usable function, which takes the following form:

sto_mcp42xxx(slaveselect, clock, masterout, command, databyte)

slaveselect (int): Sets the digital output pin for the SPI slaveselect signal. The slave select pin should be set high upon sketch initialisation, as the slave IC is selected upon this signal going low.

clock (int): Sets the digital output pin for the SPI clock signal.

masterout (int): Sets the digital output pin for the SPI serial transfer output.

command (byte): Sets the command byte that is sent to the slave IC and determines where the data byte goes, as follows:
  • If "sto_mcp42xxxcmd0" is entered in this space, then the data byte is written to pot 0. This is equivalent to entering "B00010001".
  • If "sto_mcp42xxxcmd1" is entered in this space, then the data byte is written to pot 1. This is equivalent to entering "B00010010".
  • If "sto_mcp42xxxcmd2" is entered in this space, then the data byte is written to bothpots 0 and 1. This is equivalent to entering "B00010011".
  • If "sto_mcp42xxxcmd3" is entered in this space, then shutdown mode is selected. This is equivalent to entering "B00100000".
databyte (byte): Sets the actual data that is to be written to the potentiometer IC.


Code Example
Below is an example sketch that controls two pots on two 42XXX ICs independently.

/* SPI for a MCP42xxx digital potentiometer IC
by Sebastian Tomczak
19 August 2007
Adelaide, Australia
*/


/* INCLUDE */
#include <sto_mcp42xxx.h>

/* INITIALISATION */
int SS1 = 2; // set slave select 1 pin
int SS2 = 5; // set slave select 2 pin
int CLK = 3; // set clock pin
int MOUT = 4; // set master out, slave in pin

/* SETUP */
void setup() { // setup function begins here
pinMode(SS1, OUTPUT); // set CS pin to output
pinMode(SS2, OUTPUT); // set CS pin to output
pinMode(CLK, OUTPUT); // set SCK pin to output
pinMode(MOUT, OUTPUT); // set MOSI pin to output

digitalWrite(SS1, HIGH); // hold slave select 1 pin high, so that chip is not selected to begin with
digitalWrite(SS2, HIGH); // hold slave select 2 pin high, so that chip is not selected to begin with
}

/* MAIN PROGRAM */

void loop () {
sto_mcp42xxx(SS1, CLK, MOUT, sto_mcp42xxxcmd0, 31); // send out data to chip 1, pot 0
sto_mcp42xxx(SS1, CLK, MOUT, sto_mcp42xxxcmd1, 63); // send out data to chip 1, pot 1
sto_mcp42xxx(SS2, CLK, MOUT, sto_mcp42xxxcmd0, 127); // send out data to chip 2, pot 0
sto_mcp42xxx(SS2, CLK, MOUT, sto_mcp42xxxcmd1, 255); // send out data to chip 2, pot 1

}

1 comments:

nicksen782 said...

I am unable to download your mini library linked at:
http://www.milkcrate.com.au/_other/arduino/sto_mcp42xxx.zip

. Where/how can I obtain your code? It seems to be exactly what I am looking for on my project. Thanks!