I've been meaning to test out SPI on the LaunchPad for a while, and specifically this digital potentiometer.
Bill of materials:
Breadboard
MSP-EXP430F-5529LP or other LaunchPad
MCP41010 Digital Potentiometer
Jumpers
330 ohm resistor
LED
This is for the single channel version of the chip, there is also a dual channel version labelled the 42xxx series.  There should be enough information here to get that to work - I didn't have any problems with the single channel.  Here is the code and a description of the circuit:
/*
  Digital Pot Control
  
  This example controls Microchip MCP41010 I/P digital potentiometer with a 
  MSP-EXP430F5529LP.  The MCP41010 has 1 potentiometer channel with 256 taps.
   
  The MCP41010 is SPI-compatible,and to command it, you send two bytes, 
  one with the command selection bits (xxC1xxC2) and one with the data value
  (0-255).  Note there is only one channel on the MCP41010.
  
  C1  C0  Command  Command Summary
  --  --  -------  --------------------------------------------------------
   0   0  None     No command executed
   0   1  Write    Write the date contained in Data Byte to the pot
   1   0  Shutdown Enter shutdown mode.  Data Byte does not matter
   1   1  None     No command executed
   
  P1  P0  Potentiometer Selections
  --  --  ------------------------------------------------------------------
   0   0  Dummy Code - pot not affected
   0   1  Command executed on Pot 0
   1   0  Command executed on Pot 1
   1   1  Command executed on both Pots
  
  Note: Since the MCP41010 only has one channel, P1 above is ignored
  
  MCP41010  Connection
  --------  ----------------------------------------------------------
  1 (CS)    Pin 8 of MSP-EXP430F5529LP  (CS)
  2 (SCK)   Pin 7 of MSP-EXP430F5529LP  (SCK)
  3 (SI)    Pin 15 of MSP-EXP430F5529LP (MOSI)
  4 (GND)   GND 
  5 (PAO)   3.3V 
  6 (PWO)   330 ohm resistor to LED and GND
  7 (PBO)   GND 
  8 (Vdd)   3.3V
  created 10 Aug 2010 
  by Tom Igoe
  modified 2 May 2012 - changed SS pin
  by Rick Kimball 
  Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
   
  modified 1 April 2014 for EXP430F5529LP and MCP41010 I/P
  by Frank Milburn
*/
#include <SPI.h>
// set pin 8 as the slave select for the digital pot:
const int slaveSelectPin = 8;
void setup() 
{
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  SPI.begin(); 
}
void loop() {
  for (int level = 0; level < 255; level++)
  {
    digitalPotWrite(level);
    delay(20);
  }
  delay(100);             // wait a bit at the top
  digitalPotWrite(0);     // change the resistance from max to min:
  delay(10);   
}
int digitalPotWrite(int value) 
{
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, LOW);  
  // give the command to write to pot 1
  byte byte1 = B10001;
  SPI.transfer(byte1);
  // send the pot value
  SPI.transfer(value);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin, HIGH); 
}
 
No comments:
Post a Comment