Shop OBEX P1 Docs P2 Docs Learn Events
help using SEROUT to control SPI digital potentiometer — Parallax Forums

help using SEROUT to control SPI digital potentiometer

byocbyoc Posts: 17
edited 2015-05-15 10:05 in BASIC Stamp
I have some MCP4231-104 digital pots with SPI interface that I'd like to use. I'm assuming the SEROUT command is what I want to use here. Only problem is I'm not familiar with SPI or the SEROUT command. I mean, the data sheets me how to connect it and the reference manual shows me the syntax, but I don't understand the data that I'm suppose to communicate to the digipot. Any links to practice projects that can help wrap my head around these concepts....like the classic example of making an LED get brighter or dimmer or something that I can extrapolate from to make these kinds of digipots do what I want?

Comments

  • tomcrawfordtomcrawford Posts: 1,126
    edited 2015-05-13 10:44
    I'm pretty sure you want to use SHIFTOUT.

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [Value\7]
    HIGH ChipSelectPin

    Maybe value\8 if you have the 256-level version
  • davejamesdavejames Posts: 4,047
    edited 2015-05-13 11:07
    byoc - see attached.

    Some of the earlier Stamp-oriented manuals will have useful information.....except, I can't find them on the Parallax or Learn sites. Anybody know where the Stamps In Class and such manuals are stashed?


    MCP3208 Demo.bs2
  • byocbyoc Posts: 17
    edited 2015-05-13 11:27
    Thanks! That makes a lot more sense. I'm still a little fuzzy, but getting closer. This one is a 7 bit device, so [value\7] is where I feed the data to move the wiper in the 128 resistor network. Can you give me an example of what "value" would be for full turn counter clockwise (0 ohm resistance between P0A and P0W, half way (50k ohm resistance between P0A and P0W), and full turn clockwise (100k ohm resistance between P0A and P0W)? Is it as simple as writing 0, 64, and 128 in 7-bit highest-bit-first binary?

    edit: or just binary and the MODE will take care of which bit comes first I guess.
  • byocbyoc Posts: 17
    edited 2015-05-13 11:48
    davejames wrote: »
    byoc - see attached.

    Some of the earlier Stamp-oriented manuals will have useful information.....except, I can't find them on the Parallax or Learn sites. Anybody know where the Stamps In Class and such manuals are stashed?


    MCP3208 Demo.bs2

    Thanks, davejames. That helps as well. I didn't realize the same command/concept could be applied to the digital pot. That is actually half of the larger ultimate project I'm working on. I'm trying to use a single "real" pot and the ADC (that part I've already got working) to control a quad gang digital potentiometer to adjust the cross over frequency of a 24dB per octave linkwitz-riley filter.

    edit: I meant 8-gang pot. Yikes!
  • tomcrawfordtomcrawford Posts: 1,126
    edited 2015-05-13 12:38
    byoc wrote: »
    Is it as simple as writing 0, 64, and 128 in 7-bit highest-bit-first binary?

    edit: or just binary and the MODE will take care of which bit comes first I guess.

    It's just that simple. Typically, you would end up putting the code in a subroutine and passing the value in a variable.

    Edit: If you are planning to control multiple pots and have to control them individually (set them to different values), you will need separate chip selects (obviously). You can either spend a BS-2 pin on each or use an external decoder (74hc138 or so). In either case, you would pass the chip select pin number (or pattern) to the subroutine as well as the value (obviously).
    endedit
  • byocbyoc Posts: 17
    edited 2015-05-13 14:33
    Thank you so much. I'll give this a try and report back.
  • byocbyoc Posts: 17
    edited 2015-05-14 11:25
    I'm not having much luck. I'm starting off small by trying to make an LED turn off, half bright, full bright, half bright, loop. ChipSelectPin should connect to PIN1 of the MCP4231? SDO should connect to PIN13 of the MCP4231? And I'm assuming you meant SCK and not SCL, but I'm using SCL as the variable anyways, which should be PIN2 of the MCP4231? I'm using DEC because it tells me I have a "malformed binary integer" if I write value as a 7 bit binary.

    DO

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [0\7]
    HIGH ChipSelectPin
    PAUSE 100

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [64\7]
    HIGH ChipSelectPin
    PAUSE 100

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [128\7]
    HIGH ChipSelectPin
    PAUSE 100

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [64\7]
    HIGH ChipSelectPin
    PAUSE 100

    LOOP
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-05-14 11:44
    davejames wrote: »
    Some of the earlier Stamp-oriented manuals will have useful information.....except, I can't find them on the Parallax or Learn sites. Anybody know where the Stamps In Class and such manuals are stashed?

    Dave,

    Such manuals are on their respective product pages on our website. For example, the What's a Microcontroller manual can be found here: https://www.parallax.com/product/28123

    Always check under Downloads & Documentation. Was there something more specific?
  • davejamesdavejames Posts: 4,047
    edited 2015-05-14 14:12
    Was there something more specific?

    You betcha:
    - Applied Sensors manual
    - Basic Analog and Digital manual
    - Stamps in Class manual
    - and all the N&V articles

    Granted, they're old but still have great information.
  • SapphireSapphire Posts: 496
    edited 2015-05-14 14:27
    Values must remain below 128. [128\7] = 0, so use [127\7] instead. Also [63\7] is half way.
  • tomcrawfordtomcrawford Posts: 1,126
    edited 2015-05-14 14:50
    byoc wrote: »
    I'm not having much luck. I'm starting off small by trying to make an LED turn off, half bright, full bright, half bright, loop. ChipSelectPin should connect to PIN1 of the MCP4231? SDO should connect to PIN13 of the MCP4231? And I'm assuming you meant SCK and not SCL, but I'm using SCL as the variable anyways, which should be PIN2 of the MCP4231? I'm using DEC because it tells me I have a "malformed binary integer" if I write value as a 7 bit binary.

    DO

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [0\7]
    HIGH ChipSelectPin
    PAUSE 100

    LOW ChipSelectPin
    SHIFTOUT SDO, SCL, MSBFIRST, [64\7]
    HIGH ChipSelectPin
    PAUSE 100

    LOOP

    It's always best to show your whole program, including pin assignments and variables.

    One thing I see right away is you need to connect the SDO pin of your BS2 to SDI pin (pin 3) of the 4231. Using pin 13 makes for a bus crash.

    I looked at the data sheet and it is a whole lot more complicated than just writing a wiper position. Sorry, I was making assumptions and may have led you astray.

    Take a look beginning on page 33. Table 4-1 is the list of register addresses. Register %0000 is for wiper 0 and %0001 is for wiper 1. Now look on page 47. You will need to send 16 bits including a 4-bit register address, 2-bit command, and 10 bit data value.

    SHIFTOUT SDO, SCL, MSBFIRST, [0\4, 0\2, 0\3, WiperValue\7] 'I think

    I would be careful in driving a LED with one of these. First, the wiper resistance (that is, the minimum resistance value to either end) seems to be maybe 75 ohms which is pretty low. Second, since the resistance options are 5K minimum, the LED will get pretty dim pretty fast.

    Note you can also increment or decrement either wiper with an 8-bit command.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-05-14 16:00
    davejames wrote: »
    You betcha:
    - Applied Sensors manual
    - Basic Analog and Digital manual
    - Stamps in Class manual
    - and all the N&V articles

    Granted, they're old but still have great information.

    Ah yes...some of those are discontinued and so if someone requests them we can provide them, but they're not on the current site because they were discontinued before this site went up. You can still find Applied Sensors via a Google search.

    http://www.digikey.com/Web%20Export/Supplier%20Content/Parallax_149/PDF/Parallax_AppliedSensorsGuide.pdf?redirected=1

    You can probably do the same for the rest, however the N&V articles are on the current website. For example, try this link: https://www.parallax.com/search?search_api_views_fulltext=Nuts+and+Volts
  • byocbyoc Posts: 17
    edited 2015-05-14 16:01
    Thanks, Tom!

    OH! So I can control each pot separately? That's cool. I thought they were ganged. Sending the data to SDI makes a little more sense. I'm using a transistor to actually drive the LED, so there's no worries there. I'm also using the 100k version, but I suppose that doesn't matter if I'm just using it as a voltage divide with the transistor.

    I'm a little confused by the COMMAND BITS. Do you use the INCREMENT and DECREMENT commands if you only want to move the wiper 1 position.....like if you wanted to use pulses....like with the AD5220 digital pot? Do I want to READ or WRITE to move the wiper, i.e., when I'm sending data from the MCU am I writing to the digipot, or is the digipot reading from the MCU?

    Once I understand that, I think I understand the memory and command bits, but what makes up the data bits? Is this the location of the wiper in READ/WRITE or how many positions the wiper moves in INCR/DECR?
  • tomcrawfordtomcrawford Posts: 1,126
    edited 2015-05-14 16:14
    I'm just going by the data sheet; I've never actually used one of these. That being said,

    You want to WRITE (command %00) to move (force) the wiper. You can read the wiper position register back if you lose track of where it is (look at Figure 6-5).

    When you use Increment or Decrement command, you don't actually send a data byte, so it is just one position up or down.
  • byocbyoc Posts: 17
    edited 2015-05-14 16:38
    Ok. Got it working.

    DO

    LOW ChipSelectPin
    SHIFTOUT SDI, SCk, MSBFIRST, [%00010001, 0] 'this controls 1st pot. Wiper is full turn counter clockwise
    SHIFTOUT SDI, SCk, MSBFIRST, [%00000001, 255] 'this controls 2nd pot. Wiper is full turn clockwise
    HIGH ChipSelectPin
    PAUSE 100

    LOW ChipSelectPin
    SHIFTOUT SDI, SCL, MSBFIRST, [%00010001, 63] ' 1st pot. wiper is in the middle
    SHIFTOUT SDI, SCk, MSBFIRST, [%00000001, 63] 'this controls 2nd pot. Wiper is in the middle
    HIGH ChipSelectPin
    PAUSE 100

    LOW ChipSelectPin
    SHIFTOUT SDI, SCL, MSBFIRST, [%00010001, 255] ' 1st pot, wiper full turn clockwise
    SHIFTOUT SDI, SCk, MSBFIRST, [%00000001, 0] 'this controls 2nd pot. Wiper is in full turn counter clockwise
    HIGH ChipSelectPin
    PAUSE 100

    LOOP

    Thank you so much, Tom.
  • davejamesdavejames Posts: 4,047
    edited 2015-05-14 23:37

    Thanks Mr. Savage. :thumb:


    @byoc - Apologies, it appeared I was hijacking your thread. My intent was to locate some of these old manuals and articles so you could glean bits of knowledge from them.

    Now that we know the Nuts & Volts articles are still around, I'd recommend you peruse through the library. There is a ton 'o stuff in them, and I'm sure you'll find information pertinent to your efforts.
  • byocbyoc Posts: 17
    edited 2015-05-15 10:05
    Not a problem, davejames. I got the information I was seeking. And a little extra as well!
Sign In or Register to comment.