Can someone check my code? Serially Controlled Digital Pot

Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2006

    Post Can someone check my code? Serially Controlled Digital Pot


    I am trying to use a serially controlled digital pot MCP42100 with Pic 18F452.

    What I'm doing is receiving an analog input (a pot) on portA and then converting its reading to operate the digital pot.

    (Note this is a continuation of an old thread located here:

    This code was from Mister E (thank you)

    My question is should I just replace the For Next loop with my adc pot reading and replace the variable "Wiper" with the pot reading results?

    ' ' Hardware configuration
    ' ======================

    DEFINE OSC 20 'set for external high speed powered ocsillator
    DEFINE ADC_BITS 10 'set number of bits in result
    DEFINE ADC_CLOCK 3 'set internal Pic timer as RC
    DEFINE ADC_SAMPLEUS 15 'Set sampling time in uS

    'serial registers for midi out option'
    DEFINE HSER_RCSTA 90h 'receiving definition for hardware serout (Midi)
    DEFINE HSER_TXSTA 20h 'transmitting difinition for harware serout
    DEFINE HSER_BAUD 31250 'Baud rate for midi

    TRISA = %11111111 'Port A as inputs
    TRISB=0 'Port B as outputs
    ADCON1 = %10000010 'Set PORTA analog conversion and right justify result

    ' ' Hardware connection
    ' ===================

    ' ' Variables definition
    ' ===================
    Wiper var byte

    ' ' Constants definition
    ' ====================
    MSBFIRST CON 1 'this is part of the shiftout function of the Pic
    WriteOnPot0 con %00010001 'this is a command byte needed by the digital pot

    'WriteOnPot0 = Command Bytesee fig. 5-2 of data sheet)

    'EXPLAINATION OF THE COMMAND BYTE(see fig. 5-2 of data sheet);
    'Pot Selection Bits: Starting from the left 01 means the command is
    'executed on Pot 0
    '(there are 2; pots 0 and 1)

    'Command Selection Bits:
    'At bit 4 and 5, the 01 means write data to the pot selected by the first 2 bits,
    'which in this case is Pot 0

    ' ' Software/Hardware initialisation
    ' ================================

    cs = 1 ' Make CS pin on dig pot high = disable MCP

    PAUSE 100 ' safe settle time


    for wiper = 0 to 255 'For demo purposes this will automatically toggle the pot
    'up from 0-255 steps

    cs=0 'CS pin low = Enable

    shiftout si,clk, MSBFIRST,[WriteOnPot0, WIPER] ' Write to Digital Pot

    cs = 1 ' CS Pin high = Disable

    pause 10


    goto start

    I thought this might be good for others trying to use serial controlled digital pots, so I commented everything, which helps me too.

    Thanks for any input,
    Tony A

  2. #2
    malc-c's Avatar
    malc-c Guest

    Did you find this post helpful? Yes | No


    Ok Tony I have to ask... why ?

    If I understand your post you are using a digital pot to do the same as an analogue pot ?? Or is the digital pot remote from the analogue pot, like in the remote controlls for a TV or audio amp. ?

  3. #3
    Join Date
    Apr 2006

    Did you find this post helpful? Yes | No


    Good question, well the "pot" I'm speaking of is really a voltage divider strip, like one of those force sensitive resistor strips, however made as a voltage divider rather than a variable resistor. I said "pot" just to simplify the explaination of my example since in this scenario it really can be any kind of analog sensor input to the Pic.
    However, this strip I'm speaking of is essentially a voltage divider used as a ribbon controller for electronic musical instruments. It can be wired to output a resistance, but not enough OHMS to control my analog oscillator circuit.

    The problem is I want to use this voltage divider strip (ribbon controller)
    to control an analog sound generating circuit (an oscillator). The voltage divider strip won't do the job if connected directly to the analog ocsillator circuit. So I would like to connect this voltage divider strip to the pic and have the pic control the digital pot, and the digital pot control the analog oscillator.

    In addition, I'll also be able to do some things with the digital pot (like switching it on and off to create stacatto effects, etc.) that an analog sensor wired directly to the analog circuit couldn't do.

    So essentially I can also do some tricks by having the pic control the pot on my analog oscillator circuit.

    I'm thinking if I just remove the section of code where Mister-E wrote in the above posted code:

    for wiper = 0 to 255 'wiper is the variable written to the pot in the shiftout command

    with instead:

    ADCin 1, ADCVar 'Read voltage divider strip on pin 3
    WIPER = ADCVar / x 'scale the voltage divider strip to get 0-255; "x" is a number I'll use to scale the value for "Wiper"
    if wiper > 255 then 'if the reading goes above 255 look for new reading
    goto Start

    and then do this:

    shiftout si,clk, MSBFIRST,[WriteOnPot0, WIPER] ' Write to Digital Pot

    I'm thinking this might be a starting point, but haven't tried it yet.

    Thanks again,
    Tony A
    Last edited by TonyA; - 24th January 2007 at 19:17.

  4. #4
    malc-c's Avatar
    malc-c Guest

    Did you find this post helpful? Yes | No



    Thanks for the explanation, and for not taking my comment as being an attack on your sanity

    Hopefully some of the more knowledgable guys here can help, as I'm afraid its way over my head !

  5. #5
    Join Date
    Apr 2006

    Did you find this post helpful? Yes | No


    Hey, no problem man. One of the reasons I posted this was to also help others who might have similar questions. Thanks for your input.

    Tony A

Similar Threads

  1. decoding quadrature encoders
    By ice in forum mel PIC BASIC Pro
    Replies: 93
    Last Post: - 28th February 2017, 09:02
  2. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  3. Writing code for battery operated projects
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th June 2005, 03:39
  4. Code check (sorry)
    By barkerben in forum General
    Replies: 5
    Last Post: - 30th November 2004, 15:54
  5. DS1868 Digital Pot
    By ero in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 31st August 2004, 13:22

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