Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp II SPI to MicroChip 4131 Digital Potentiometer — Parallax Forums

Basic Stamp II SPI to MicroChip 4131 Digital Potentiometer

xanaduxanadu Posts: 3,347
edited 2015-05-15 15:58 in BASIC Stamp
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.
'{$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

  • RusmanRusman Posts: 2
    edited 2013-07-22 19:46
    Hi,
    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-22 20:16
    I don't know of any digital pots that send back anything. SPI has the potential to be a "two-way" road. It's not always used.
  • RusmanRusman Posts: 2
    edited 2013-07-22 23:05
    Thanks, Mike,
    That was a part of a confusion with SPI. Now, I can continue with my Rover project.
  • AntJadAntJad Posts: 3
    edited 2013-07-28 13:49
    Hello, I tried using the code you posted, however, it didn't work on my MCP 41010. I thought it would work because they had the same corresponding pins. What is the difference between my model and yours? I'm fairly new at this and I'd appreciate it if you could help me out by posting a similar code which will work on my MCP 41010. Thank you.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-28 16:06
    Of course it didn't work on your MCP41010. That's a different chip and works differently. One of the first things you have to learn is to look at the datasheets. You may not understand all the detail, but there are overview descriptions and that will often get you started. In particular, look at page 18 which shows you the 16-bit value that has to be sent to the 41010. To set pot #0, you have to add the value $1100 or %0001000100000000 to the value for the pot like this:

    SHIFTOUT Datapin, clock, MSBFIRST, [$1100+value\16]
  • xanaduxanadu Posts: 3,347
    edited 2013-07-28 20:43
    Sorry I did not notice any responses to this thread.

    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!
  • AntJadAntJad Posts: 3
    edited 2013-07-29 17:08
    Mike Green wrote: »
    Of course it didn't work on your MCP41010. That's a different chip and works differently. One of the first things you have to learn is to look at the datasheets. You may not understand all the detail, but there are overview descriptions and that will often get you started. In particular, look at page 18 which shows you the 16-bit value that has to be sent to the 41010. To set pot #0, you have to add the value $1100 or 01000100000000 to the value for the pot like this:

    SHIFTOUT Datapin, clock, MSBFIRST, [$1100+value\16]

    I appreciate your quick response. This will help me move forward with my projects, thank you.
  • iotaprimeiotaprime Posts: 1
    edited 2015-05-15 11:48
    Just wanted to thank you for posting this demo program.
    Saved me a lot of time fussing.
  • xanaduxanadu Posts: 3,347
    edited 2015-05-15 15:58
    Welcome to the resistance!

    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.
Sign In or Register to comment.