help using SHIFTOUT to control SPI digital potentiometer


Results 1 to 7 of 7

Threaded View

  1. #5


    Did you find this post helpful? Yes | No

    Default Re: help using SHIFTOUT to control SPI digital potentiometer

    I just used mister e's code essentially verbatim. My IC, the MCP4231-xxx is a dual digital potentiometer. At first I thought the two pots were ganged, but turns out, you can actually control them separately by changing the address in the command byte. So I added a second "WriteOnPot" constant.

    Code:
        '
        '    Hardware configuration
        '    ======================
        TRISC=0
        
        '                      
        '    Hardware connection
        '    ===================
        CS          VAR PORTC.0
        CLK         VAR PORTC.1
        SI          VAR PORTC.2
        
        '  
        '    Variables definition
        '    ===================
        Wiper       var byte
        
        '
        '    Constants definition
        '    ====================
        MSBFIRST    CON 1              
        WriteOnPot0 con 000001
        WriteOnPot1 con 010001
    
        '
        '    Software/Hardware initialisation
        '    ================================
        cs = 1      ' disable MCP
        PAUSE 100   ' safe settle time
        
    Start:
        for wiper = 0 to 255
            cs=0                                            ' enable
            shiftout si,clk, MSBFIRST,[WriteOnPot0, WIPER]  ' Write to MCP first address
            shiftout si,clk, MSBFIRST,[WriteOnPot1, WIPER]  ' Write to MCP second address
            cs = 1                                          ' Disable
            pause 10                                      
            next                                          
        goto start
    "WriteOnPot0 con 000001" Is the part of the command byte that tells it to WRITE on the first wiper address. I added "WriteOnPot1 con 010001" which allows you to write to the 2nd wiper address. So it's the first two bits that make up the wiper address. I'm not really sure in what situation you would want to use any of the functions other than write, but it's the last 4 bits that tell it to read, write, increment, or decrement. And then you just tell it where you want to wiper at with a decimal number between 0 - 255. This chip can use 7 or 8 bit resolution. If you wanted to use 7 bit, the WIPER variable would need to be a dec num between 0 - 127 and the syntax would need to be [WriteOnPot0, Wiper\7]. Once again, I'm not sure where the advantage of this would be over just keeping it 8 bit. I'm pointing these things out because it made figuring out how to use the chip more confusing than it needed to be....at least for me. The example above proves that you can SHIFTOUT to both addresses simultaneously...or at least within a few uS apart in the same CS enable/disable. The example below proves that you can operate both addresses independently of one another.

    Code:
    Start:
        for wiper = 0 to 255
            cs=0                                            ' enable
            shiftout si,clk, MSBFIRST,[WriteOnPot0, WIPER]  ' Write to MCP first address
            cs = 1                                          ' Disable
            pause 10                                      
            next 
    
            for wiper = 0 to 255
            cs=0                                            ' enable
            shiftout si,clk, MSBFIRST,[WriteOnPot1, WIPER]  ' Write to MCP second address
            cs = 1                                          ' Disable
            pause 10                                      
            next                                          
        goto start
    Last edited by keithv; - 15th May 2015 at 18:47.

Similar Threads

  1. Digital potentiometer ICs (common)
    By rajultra in forum mel PIC BASIC
    Replies: 3
    Last Post: - 25th February 2014, 00:59
  2. Using SPI and Shiftout together
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd May 2013, 10:19
  3. Replace a potentiometer by a digital pot
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th November 2007, 07:52
  4. Microchip MCP41xxx Digital Potentiometer Code Trouble
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 31st August 2005, 18:21
  5. Replies: 9
    Last Post: - 30th August 2005, 06:33

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts