Basic Stamp II SPI to MicroChip 4131 Digital Potentiometer
xanadu
Posts: 3,347
Awhile back I had purchased some of these Digital Potentiometers from sparkfun - https://www.sparkfun.com/products/10613
I couldn't find any Basic Stamp examples so I made one that will sweep the digital pot from lowest to highest resistance in a loop. This loop moves slow for DMMs to keep up. You would be better off with an analog ohms meter, although with the right pauses a DMM will work. You could also use an LED or some other means of monitoring the pins.
SPI is a beast for me to understand the inner workings, I had found lots of tutorials and video online but the Basic Stamp made it really easy to work with as usual.
If you have a COM-10613 or MicroChip 4131-103 you can use this code as a starting point. Please read the notes carefully as there are some variables. If anyone notices an issue with my code please let me know this is the most BS2 code I've written that didn't already exist hehe.
I couldn't find any Basic Stamp examples so I made one that will sweep the digital pot from lowest to highest resistance in a loop. This loop moves slow for DMMs to keep up. You would be better off with an analog ohms meter, although with the right pauses a DMM will work. You could also use an LED or some other means of monitoring the pins.
SPI is a beast for me to understand the inner workings, I had found lots of tutorials and video online but the Basic Stamp made it really easy to work with as usual.
If you have a COM-10613 or MicroChip 4131-103 you can use this code as a starting point. Please read the notes carefully as there are some variables. If anyone notices an issue with my code please let me know this is the most BS2 code I've written that didn't already exist hehe.
'{$STAMP BS2} '{$PBASIC 2.5} ' -----[ Program Description ]--------------------------------------------- ' Example code for popular Digital Potentiometer - 10K (MicroChip 4131-103) ' Please use at your own risk, be sure to check pinouts! ' This code will sweep the 4131 through its 129 (0-128) steps. Beginning at the highest ' resistance through lowest and back up, then loops the entire thing. ' **You must allow time for a digital multimeter to read the value, you will ' see three second pauses between commands for a digital multimeter. If you ' are not using a digital multimeter you may lower or omit the three second pauses.** ' See datasheet for schematic and SPI data details. ' -----[ I/O Definitions ]------------------------------------------------- CS PIN 2 ' (CS) Chip Select = Pin 2 Clock PIN 1 ' (SCK) Clock = Pin 1 Datapin PIN 0 ' (SDI) Data = Pin 0 **USE 1K RESISTOR** ' Other Pins: ' VSS = Pin 4 ' VDD = Pin 8 ' Wiper = Pin 6 ' P0A & P0B = Pins 5 & 7 ' Ohm Meter goes across Pins 6&7 (tied together) and Pin 5 ' -----[ Variables ]------------------------------------------------------- value VAR Word ' setup a variable for sending decimal value to 4131 ' -----[ Init Setup ]------------------------------------------------------ DEBUG CLS ' clear the debug terminal HIGH CS ' turn off program mode on 4131 value = 0 ' initialize the variable we use for each step of the 4131 ' -----[ Program Code ]---------------------------------------------------- DO ' start decrease and increase loops (main program) DO UNTIL value = 128 ' do this program until value = 128 (decrease resistance loop) LOW CS ' turn on program mode 4131 SHIFTOUT Datapin, clock, MSBFIRST, [value\16] ' send data (decimal 0 to 128) ("\16" = bits) to 4131 HIGH CS ' turn off program mode 4131 value = value + 1 ' increment send data value by +1 (decreases resistance) PAUSE 3000 ' pause for digital ohm meter to keep up DEBUG CLS ' clear the debug screen DEBUG DEC value ' debug the current send data value LOOP ' loop decrease resistance LOOP DO UNTIL value = 0 ' do this program until value = 0 (increase resistance loop) LOW CS ' turn on program mode 4131 SHIFTOUT Datapin, clock, MSBFIRST, [value\16] ' send data (decimal 128 to 0) ("\16" = bits) to 4131 HIGH CS ' turn off program mode 4131 value = value - 1 ' increment send data value by -1 (increases resistance) PAUSE 3000 ' pause for digital ohm meter to keep up DEBUG CLS ' clear the debug screen DEBUG DEC value ' debug the current send data value LOOP ' loop increase resistance loop LOOP ' loop (main program)
Comments
Thanks a lot for this code example. I was trying to figure out how to use the same dig.pot. for around 3 weeks. I have read a manual from Microchip, I have read other tutorials about SPI itself, but I still could not understand how to "activate" dig.pot. Where did you find that Microcontroller should send out MSB first? And what about reading back what ever dig.pot. sends back? As far as I have learned, SPI is a "two-way" road: it sends and receives simultaneously data.
That was a part of a confusion with SPI. Now, I can continue with my Rover project.
SHIFTOUT Datapin, clock, MSBFIRST, [$1100+value\16]
Thank you Mike that is exactly the hurdle that slowed me down. At first the datasheet is a bit daunting, in comparison to say, an LED haha. After one or two aha moments it gets real easy thanks the the Basic Stamp SHIFTOUT command!
I appreciate your quick response. This will help me move forward with my projects, thank you.
Saved me a lot of time fussing.
And the forum, there's a projection section here - http://forums.parallax.com/forumdisplay.php/61-Projects if you want to share what you're doing with it I'd love to see.