Digital Potentiometers 101

#1
Digital Potentiometers
Digital potentiometers can in general be used in place of mechanical potentiometers.
Applications are wide and varied and include amplifier gain/volume control, variable oscillator control and even a for of DAC (digital to analogue) to, for example, achieve an analogue output in a PICAXE circuit.
Many IC manufacturers including Microchip, Maxim and Analog Devices all manufacture digital potentiometers.

The PICAXE range of chips include digital inputs, digital outputs, analogue inputs but no analogue outputs.
By using a digital potentiometer, a basic analogue output can be achieved.

Microchip offerings:
Microchip manufacture a range of digital potentiometers. Some of the prime considerations are:
- Overall resistance (typical ohmic values are 2.1k, 5k, 10k, 50k and 100k)
- Number of steps/taps over the total resistance range( common values are 64 and 256)
- Method of control (up/down or serial data via SPI and occasionally via i2c comms)
- Number of potentiometers in a single package
- Whether the Digital Pots have volatile or non-volatile memory (to remember the setting after power removal).

Below are notes on a few of the different offerings from Microchip including the method of control. All of the devices covered below are of the volatile type and so lose their settings when the power is removed.


MCP4011
The MCP4011 is an 8-pin SOIC package with a single digit potentiometer which is controlled using the up/down approach with an operating voltage range of 2.7V to 5.5Vdc.
The MCP4011 is one of a series (MCP4011/2/3/4) and can in fact be configured externally to implement the configurations of the other three digital pots in the series.
Note that the absolute maximum current through the potentiometer or thought any on of the pot pins is 2.5mA.
The three potentiometer pins (A, B and W) are not polarity sensitive but must be between Vss and Vdd.
Internally, the wiper has a resistance (Rw) of 75 ohms

When the MC4011 is powered-up, the default setting is the mid-point tap/setting. This has an internal wiper setting value of $1F (dec 31).
Two pins are used to control the MCP4011 chip, being CS and U/D.

CS Pin
The chip select pin, CS, is used to control whether the chip accepts serial commands.
When the CS pin is high, the digital potentiometer does not respond to commands.
When the CS pin is low, the digital pot responds to step commands on the U/D pin

U/D Pin
The U/D pin is used firstly to set the increment or decrement mode and secondly to subsequently step the pot wiper on subsequent rising edges of the signal to the U/D pin.
The MCP4011 also has a high voltage serial command mode for compatibility with the MCP402x range of digital pots but this mode is not covered here.


Operation
When the CS pin is taken low to commence a change in pot wiper position, the voltage level on the U/D pin at the time the CS pin is taken low determines whether the increment or decrement mode is selected.
The increment/Decrement protocol enables the potentiometer to move one step at a time through the range of possible resistance values. The pot wiper value does not roll over, underflow or overflow.

If the U/D pin is high when the CS pin is taken low, then this sets the “direction” for step but does not cause a step of the pot wiper. If the CS pin is then retained in a low state, subsequent rising edges on the U/D pin cause the pot wiper to increment. This moves the wiper one step towards terminal A. The maximum wiper setting value when the wiper is at terminal A is $3F. Once the internal pot wiper value reaches $3F, subsequent increment pulses are ignored.
If the U/D pin is low when the CS pin is taken low, then subsequent rising edges on the U/D pin cause the pot wiper to decrement. This moves the wiper one step towards terminal B. The minimum wiper setting value when the wiper is at terminal B is $00. Once the internal pot wiper value reaches $00, subsequent decrement pulses are ignored.

If one reads the datasheet it will be noted that:
1. There is differential error between increment and decrement modes – this means you may not achieve the same resistance between the end terminals and the wiper at a given wiper position when you reach that position when stepping from the A and B terminals to that position.
2. There is some non-linearity between the resistance steps. With a 5.5V supply this is typically 0.25 of the LSb.
3.The wiper resistance can vary significantly with voltage and temperature.

Timing considerations
For the MCP4011, the minimum duration for most characteristics in the timing waveforms is 500nsec.
With a PICAXE at 4MHz or 8MHz operation, this duration will typically not require any further delays. I have tested at both 4MHz and 8Mhz and not needed any delays or longer pulses. However, if the PICAXE is operated at higher clock speeds, it may be found necessary to incorporate a brief pause (for example PAUSE 1 or PAUSEUS 50) to ensure the required timing waveforms are achieved.

A PICAXE program:
Enough theory on the MCP4011 so lets use a small program to control the MCP4011 and step it up and down through the 64 wiper positions.
Firstly, I have assumed that the MCP4011:
- is the 10 kOhm resistance rating (ie MCP4011-103)
- has the “A” terminal is connected to 5Vdc (=Vdd) and the B terminal is connected to 0Vdc (=Vss). In this way, the increment mode will result in an increasing voltage at the wiper terminal as the program increments each step.

Based upon the above assumptions, the wiper step number and the nominal output voltage at the wiper terminal are also displayed via the SERTXD command on the PC's terminal screen (use F8 in the programming Editor to access the Terminal screen and set it to 4800 baud)

Code:
; =================================================
;   File....... MCP4011 digital Potentiometer test program
;   Purpose.... 
;   Author..... Westaust55
;   E-mail.....
;   Started.... 17-12-2009
;   Updated.... 
; =================================================
;
; -----[ Program Description ]---------------------------------------------
;
; A program to demonstrate the PICAXE control of the MCP4011 digital potentiometer
; the program starts at the default mid position, steps down to the minimum/step 0
; position and then repeatedly cycles stepping to the max step ($3F) and back down
; to the mining step ($00) positions. 
; 
;
; -----[ Revision History ]------------------------------------------------
;
; 
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; 
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
; Define the PICAXE Digital Outputs
SYMBOL LCD    = 0			; assumes a serial LCD display is connected to PICAXE output pin0
SYMBOL CS     = 1			; the MCP4011 CS pin connects to PICAXE output pin 1
SYMBOL UD     = 2			; the MCP4011 U/D pin connects to PICAXE output pin 2
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Define variable alias names
SYMBOL wiper  = b1
SYMBOL mvolts = w1		; using a word for 0 to 5,000 millivolts 
SYMBOL axefact = b4		; factor to prevent 16-bit maths overflow and improve accuracy
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Define constants
SYMBOL minpos  = $00 		; = 0  = setting for the min position at B terminal
SYMBOL maxpos  = $3F 		; = 63 = setting for the max position at A terminal
SYMBOL default = $1F 		; = 31 = default power up wiper position is at midpoint
SYMBOL magic   = $08		; magic number to prevent math overflow and improve accuracy
SYMBOL voltage = $05		; Power is 5Vdc regulated
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Define the initial conditions
Init:
	HIGH CS			;	set the chip select high to disable serial control.
	wiper = default

Main:
; decrement to the lowest resistance step = Terminal B
	LOW UD 			; set U/D pin to low state to indicate decrement mode
	LOW CS 			; set CS pin low to enable serial control of the MCP4011
	DO
		PULSOUT UD, 1		; 1 msec pulse on the UD line to step down
		wiper = wiper - 1 	;
		GOSUB CalcVolts		; calculate and display voltage
		PAUSE 2000		; wait 2 seconds before moving to the next step
	LOOP UNTIL wiper = $00	; loop until MCP4011 wiper is at B terminal
	HIGH CS	; Finished stepping down so must set CS pin high before can select a new mode.
;
; increment to the maximum resistance step = Terminal A
	HIGH UD ; set U/D pin to high state to indicate increment mode
	LOW CS ; set CS pin low to enable serial control of the MCP4011
	LOW UD ; this is not essential as the PULSOUT command actually toggles the pin involved
	DO
		PULSOUT UD, 1	; 1 msec pulse on the UD line to step up
		wiper = wiper + 1 ;
		GOSUB CalcVolts 	; calculate and display voltage
		PAUSE 2000		; wait 2 seconds before moving to the next step
	LOOP UNTIL wiper = $3F	; loop until MCP4011 wiper is at A terminal
	HIGH CS			; Finished stepping down so must set CS pin high before can select a new mode.
GOTO Main
;
;
; Subroutine to calculate voltage when there is 5V across the potentiometer "A" to "B" terminals
CalcVolts:
		axefact = 512
		IF wiper >0 THEN
			axefact = maxpos + 1 * magic / wiper
		ENDIF
		mvolts = wiper * 1000 / magic * axefact  / maxpos * voltage * magic / axefact
		
		;SEROUT LCD, N2400, (#mvolts, "mVolts")
		SERTXD ("step = ", #wiper,"  " ,#mvolts, "mVolts", CR, LF)
		RETURN
For indicative purposes, I used a 45mm analogue meter with a scale of 0 to 50uA (coil resistance 3000 Ohms).
By using a 100K Ohm and 3.3 MOhm resistor in parallel (equates to ~97kOhm) all in series with the meter, the meter is effectively calibrates 0 to 5 Volts.
Thus the scale 0 to 50 uA represents 0 to 5.0 Volts. :)

If the pauses in the above program example are removed, the output appears to slowly but smoothly swing back and forth between 0V to 5V and back to 0V ad infinitum at a rate of ~6 seconds per cycle with a clock speed of 4MHz
 
Last edited:
#2
Digital Potentiometer - A tutorial continued

MCP41xxx
The MCP41xxx series are 8-pin devices available in DIP and SOIC packages with a single digital potentiometer which is controlled using the up/down approach with an operating voltage range of 2.7V to 5.5Vdc. The potentiometers for these devices have 256 taps/steps.

The MCP41010 is one of a series (MCP41010, MCP41050 and MCP41100) where the last three digits indicate the Ohmic value of the potentiometer between the “A” and “B” terminals in kOhms. Note that the potentiometer maximum wiper current is 1.0mA.
At Vdd = 5.5V, the wiper resistance is 52 Ohms for the MCP41010 and 125 Ohms for the other devices in the series. The wiper resistance increase as the voltage drops and is 40% greater at Vdd = 2.7V compared to the values at Vdd = 5.5V.

The three potentiometer pins (A, B and W) are not polarity sensitive but must be between Vss and Vdd.
Internally, the wiper has a resistance (Rw) of 75 ohms

When the MC41XXX devices are powered-up, the default setting is the mid-point tap/setting. This has an internal wiper setting value of $80 (dec 128).
Three pins are used to control the MCP41xxx series chips, being CS, SCK and SI.


CS Pin
The chip select pin, CS, is used to select the chip into which new data will be entered to control the digital potentiometer.
When the CS pin is taken low, new command and data information will be clocked into the MCP41xxx devices.
When the CS pin rises from a low (0) to a high (1) state, the command information is acted upon and the data information is transferred from the shift register to the data register to change the potentiometer position.
Note that unlike the MCP4011 where the CS pin is held low while a sequence of potentiometer step changes are made, with the MCP41xxx series devices, the CS pin must return to the high state after each new pair of command/data bytes are entered.

SCK pin
This is the SPI interface clock pin. A Schmidt Trigger (ST) input is used within the MCP41xxx. Each bit of data is clocked into the MCP41xxx shift register on the rising edge of the clock pulse. If the CS pin is in a high state, then the SCK pin is ignored.

SI pin
This is the SPI interface serial data input pin. A Schmidt Trigger (ST) input is used within the MCP41xxx.


Operation
When the CS pin is taken low to commence a change in pot wiper position, the SPI interface pins (SCK and SI) are enabled
Two bytes of data must be transferred to the MCP41xxx for each potentiometer position change or control mode change. The first byte is the command byte and the second is the data byte.
Each byte must be transferred with the most significant bit (MSb) clocked first.

Command byte
Only three bits of the command bytes are utilized by the MCP41XXX series digital potentiometers. The format is as follows:
Bit # _7 _6 _5 _4 _3 _2 _1 _0
Fctn _X _X C1 C0 .X _X _X P0
The bits C1 and C0 form the command as follows:
C1 = 0, C0 = 0 => No operation is performed
C1 = 0, C0 = 1 => Transfer the data from the data byte to the potentiometer data register
C1 = 1, C0 = 0 => Potentiometer software shutdown – see comment below
C1 = 1, C0 = 1 => No operation is performed

For the bit P0,
P0 = 0 => No operation is performed
P0 = 1 = > The data is transferred to the potentiometer.

While the need for the P0 bit may seem a little strange as in the MCP41xxx series devices nothing is achieved that cannot be achieved with the C1 and C0 bits, the implementation of the P0 bit provides consistency with the dual digital potentiometers in the MCP42xxx series covered below.

Software Shutdown function
The Shutdown feature disconnects the “A” terminal from the resistor stack and simultaneously connects the wiper (terminal “W”) to the “B” terminal.
The data byte must still be send when this command is used but the data byte is ignored.
The potentiometer remains in shutdown mode until new data is written to the MCP41xxx data register.

If one reads the datasheet it will be noted that:
1. There is differential error between increment and decrement modes – this means you may not achieve the same resistance between the end terminals and the wiper at a given wiper position when you reach that position when stepping from the A and B terminals to that position.
2. There is some non-linearity between the resistance steps. With a 5.5V supply this is typically 0.25 of the LSb.

Timing considerations
For the MCP41xxx series devices, the minimum duration for most characteristics in the timing waveforms is around 40 nsec. These durations are an order of magnitude faster than the MCP4011 discussed above.
With a PICAXE at 4MHz or 8MHz operation, this duration will typically not require any further delays. I have tested at both 4MHz and 8Mhz and not needed any delays or longer pulses.

Not tested by myself but with these relatively short minimum times, I believe that even at higher speeds of which the X2 PICAXE chips are capable, there will not be any problems with timing. However, if the user does find problems using a PICAXE at higher clock speeds, the solution will likely be incorporate a brief pause (for example PAUSE 1 or PAUSEUS 50) to ensure the required timing waveforms are achieved.

An advantage of the MCP41xxx series over the MCP4011 type is that the user does not always need to know/remember the current tap/step position as the actual required step/tap value is sent to the digital potentiometer for each and every adjustment.

Text and program code size combined exceeds the allowable number of chars in a single post so I have attached a small PICAXE program with controls the MCP41010 digital potentiometer. By virtue of the PICAXE pins used, it will run on any PICAXE.
This program "assumes" a default status at PICAXE power-up and then
- jumps to the maximum step ($FF) closest to "A" terminal.
- steps down one step at a time to the min step ($00) closest the "B" terminal
- steps up one step at a time to the max step ($FF) closest the "B" terminal
- goes into software shutdown mode for a period (nominal 2 seconds) so output is set/connected to the "B" terminal
- returns from shutdown mode and jumps to the default/mid setting ($80) and pauses for a period (nominal 2 seconds)

The above cycle then repeats indefinitely


MCP42xxx
The MCP42xxx series of digital potentiometers are almost identical to the MCP41xxx chips except they have two electrically independent potentiometers in one 14 pin package.


In addition to the three pins for the second potentiometer, the added pins also provide two added control pins, RS and SHDN.


The RS pin is active when pulled low and returns the two potentiometers to their default/mid-scale ($80) position. The pin must return to a high state (1) before the potentiometers can be further changed.


The SHDN pin when pulled low performs the same action as a software shutdown to both potentiometer simultaneously. The pin must return to a high state (1) before the potentiometers can be returned to an active state


Control of the two potentiometers is done in the same manner as the MCP41xxx series using the command byte. With the MCP42xxx series one addition bit is used within the command byte.

The command byte format is as follows:
Bit # _7 _6 _5 _4 _3 _2 _1 _0
Fctn _X _X C1 C0 .X _X P1 P0
The bits C1 and C0 form the command as follows:
C1 = 0, C0 = 0 => No operation is performed
C1 = 0, C0 = 1 => Transfer the data from the data byte to the data register for the nominated potentiometer(s)
C1 = 1, C0 = 0 => Potentiometer software shutdown for the nominated potentiometer(s)
– see comment below
C1 = 1, C0 = 1 => No operation is performed

For the bits P1 and P0,
P1 = 0, P1 = 0 => No operation is performed with either potentiometer
P1 = 0, P0 = 1 = > The data is transferred to potentiometer No 0.
P1 = 1, P0 = 0 = > The data is transferred to the potentiometer No 1
P1 = 1, P0 = 1 = > The same data is transferred to both potentiometers


The attached program will also work for Potentiometer No 0 of a MCP42xxx series digital potentiometer.
It only requires some minor changes to the program code to control potentiometer No 1 or both potentiometers.
 

Attachments

Last edited:
#3
Looking foward to it :).

You've written a good guide - it might be useful to also have Using a digipot vs smoothed PWM. As far as I can see (for a DAC), the only advantage of a digipot is that it is smoother (no ripple). However, digipots are good for other applications - eg it can be used in any configuration requiring a pot (ie op-amp gain setting).

A
 
#4
This is great

Westy:

Summary:

Do you plan a follow-up discussion on DigiPots that use the I2C interface?
With non-volatile memory?

For consideration, the MCP45X1, only 50 cents more.
http://ww1.microchip.com/downloads/en/DeviceDoc/22107a.pdf

Perhaps the discussion of the SPI devices will be sufficient?


Detail:

Thanks to Dr. Acula’s op-amp tutorial, I have been able to breadboard a CPAP monitoring device.

CPAP -
http://en.wikipedia.org/wiki/Positive_airway_pressure#Continuous_pressure_devices

The design uses a pressure sensor to monitor air flow and signal the user if the mask has fallen off during sleep. The design improves upon the rather simple alarming in the CPAP unit itself.

Over time the user will change masks, air hose lengths, etc. Those changes effect the sensor’s voltage and require an adjustment to the pots in the op-amp subassembly as noted above. It will be easier to make those adjustments via SW once the board is enclosed.

Many thanks for the post.

Terry
 

ValueAdd

Senior Member
#5
@westy,

great intro to digital pots. Looking forward to reading about the others you intend to write about.

@Andrew/@TAMeyer,
rather than wait for westy, why not research and write some similar words on the aspects you both raise for the picaxe community as DP's Part 3?
 
#6
Westy:

Summary:

Do you plan a follow-up discussion on DigiPots that use the I2C interface?
With non-volatile memory?

For consideration, the MCP45X1, only 50 cents more.
http://ww1.microchip.com/downloads/en/DeviceDoc/22107a.pdf

Terry
Maybe another day.
While I certainly know that i2c digital pots exist, I prefer to play and do some testing first before a post a tutorial. At this time I do not have any i2c digital pots.
 

fernando_g

Senior Member
#7
Westy;
thanks a lot for your tutorial. I know it takes substantial time and effort to: 1) understand how a device works and 2) then writing a tutorial which pre-digests some of the more arcane datasheet details for all to understand.
Thanks again!

I have been thinking for a while to experiment on potentiometers which have non-volatile settings, as a mean of "digital trimpots" ...... hopefully I can contribute to the subject matter afterwards.

Hey, another New Year's resolution! Please remind me again in December 2010.;)
 
#8
@ValueAdd

@Andrew/@TAMeyer,
rather than wait for westy, why not research and write some similar words on the aspects you both raise for the picaxe community as DP's Part 3?
It would be an honor to give something back to this forum.

In my original note, you can see that I am just beginning to comprehend Op-Amps. Tonight I am struggling with interrupts using PortC masks.

So with regards to your suggestion, the ball is in my court.

It just might be there for a few months.

Many thanks to all.

Terry
 
Last edited:
#11
Good stuff, West...
For anyone that wants to have this in PDF format until westaust55 produces an official version, here it is (with editable DOC in the zip)

View attachment 11523 View attachment 11524
Great - thanks!

What I can't understand is how the variable "varout" is being used and, why does control = writedata why can't
mask = control AND $80
just be
mask = writedata AND $80

If I set SYMBOL writedata = %00000001 (when it's currently set at SYMBOL writedata = %00010001) then it kind of works for me but, I don't understand why :O))

Is there a simple way of setting a variable in the range of 0 - 255 (decimal) and the wiper reflecting this rather than the loop:
DO
control = writedata
datbyte = wiper
GOSUB ShiftOutMSBFirst
PAUSE 5
wiper = wiper - $01
LOOP UNTIL wiper = $00

I can see that the main body work is:

; Subroutine to shift the 16 bits out to the digital potentiometer
ShiftOutMSBFirst:

LOW CS ; set the Chip Select line low to enable the digital pot

FOR index = 0 TO 15 ; number of bits to send
mask = control AND $80 ; set the mask flag bit based on data bit to be sent
LOW SData ; set the serial data line low
IF mask = 0 THEN skiponlow ; if data bit = 0 then leave data line low
HIGH SData ; if data bit = 1 then set serial data line high

' debug

Skiponlow:
PULSOUT SClk,1 ; pulse the serial clock line to tranfer data bit
' varout = varout * $02 ; move data one bit (left) toward MSB - datbytes is pushed thru control byte
NEXT index ; loop back for next bit

HIGH CS ; Set the CS line high to transfer data into the digital pot register

RETURN ; return to calling program

I'm not too sure about this just yet, still learning!

Thanks to anyone who can help.
 
#12
Hi,

Can anyone help. I have this (below) so far, I'm hoping to control a MCP413 (digital pot). http://pdf1.alldatasheet.net/datasheet-pdf/view/305864/MICROCHIP/MCP4131-503I/MF.html

Not sure what to do next to be honest! I'm guessing it should be ok to use the command 'spiout' for this device?

#picaxe 20x2

SYMBOL CS = C.2
SYMBOL SClk = C.3
SYMBOL SData = C.4

SYMBOL Wiper = w1 ' hopefully this will end up containing a value 0 - 255 which gets passed to spiout


high CS
spiout SClk, SData, MSBFIRST_L, (b1 / 8) ‘ clock 8 bits from b1
low CS

'pause 500

Thanks.
 

pbaker21

Senior Member
#13
Hi folks,

I've bought myself a DS1806 (Digital Sextet Potentiometer). http://www.datasheetcatalog.org/datasheet/maxim/DS1806.pdf

I'm now unsure how to control this due to this line in the datasheet; "Once RST has transitioned from the low to the high state, the serial port becomes active. When active, data is loaded into the I/O shift register on the low-to-high transition of the CLK."

Am I to see this as (effectively) CS, chip select ?

Any thoughts/code would be great.


Thanks.
 

pbaker21

Senior Member
#15
That seems to be the way it reads. Time to try it and see !
Yeah, the second (Pot1) LED seems to be flickering, not sure why the first one isn't doing anything.
To be honest hippy I'm really unsure of the "48–BIT I/O SHIFT REGISTER Figure 3" datasheet (page 3), on how to select the individual Pots. Any heads-up will be grateful. :D

Thanks.
 

nick12ab

Senior Member
#16
Yeah, the second (Pot1) LED seems to be flickering, not sure why the first one isn't doing anything.
To be honest hippy I'm really unsure of the "48–BIT I/O SHIFT REGISTER Figure 3" datasheet (page 3), on how to select the individual Pots. Any heads-up will be grateful. :D

Thanks.
Data is transmitted in order of LSB first. Potentiometers are designated from 1 through 6 and the value
for potentiometer-1 will be the first data entered into the shift register, followed by that of potentiometer-
2 and so forth.

ADDED: A don't change feature is provided so that you don't need to remember the values for all the pots to send each time you want to update one.
Each wiper has an 8-bit register which is used for setting the position of the wiper on the resistor array.
Because the DS1806 is a 64-position potentiometer, only six bits of information are needed to set wiper
position. The remaining two bits of information are used to provide a “don’t change” feature. Wiper
position is controlled by bit positions 0 through 5 of each register. The “don’t change” feature is
controlled by bits 6 and 7 of each register. When bits 6 and 7 have value “11 xxxxxx,” wiper position will
not change regardless of the states of bits 0 through 5. If bits 6 and 7 are set to any other value, bits 0
through 5 will be used as the new wiper position. The “don’t change” feature allows the user to change
the value of any potentiometer of the DS1806 without affecting or having to remember the remaining
positions of the potentiometer wipers. Figure 2 provides the format for a wiper’s register.
 

hippy

Technical Support
Staff member
#17
This would be my first try at it, b1-b6 for wiper 1-6 ...

Code:
#Picaxe 20X2

Symbol RST = pinC.0 : Symbol RST_DIR = dirC.0
Symbol DAT = pinC.1 : Symbol DAT_DIR = dirC.1
Symbol CLK = pinC.2 : Symbol CLK_DIR = dirC.2

RST_DIR = 1
DAT_DIR = 1
CLK_DIR = 1

Do
  b1 = %000001
  b2 = %000011
  b3 = %000111
  b4 = %001111
  b5 = %011111
  b6 = %111111
  Gosub WriteWipers
  Pause 1000
Loop

WriteWipers:
  b0 = b1 : Gosub WriteWiper
  b0 = b2 : Gosub WriteWiper
  b0 = b3 : Gosub WriteWiper
  b0 = b4 : Gosub WriteWiper
  b0 = b5 : Gosub WriteWiper
  b0 = b6 : Gosub WriteWiper
  Return

WriteWiper:
               RST = 0 : RST = 1
  DAT = bit0 : CLK = 1 : CLK = 0
  DAT = bit1 : CLK = 1 : CLK = 0
  DAT = bit2 : CLK = 1 : CLK = 0
  DAT = bit3 : CLK = 1 : CLK = 0
  DAT = bit4 : CLK = 1 : CLK = 0
  DAT = bit5 : CLK = 1 : CLK = 0
  DAT = bit6 : CLK = 1 : CLK = 0
  DAT = 0    : CLK = 1 : CLK = 0
  DAT = 0    : CLK = 1 : CLK = 0
               RST = 0
  Return
If that works then hopefully this will too ...

Code:
#Picaxe 20X2

Symbol RST = C.0
Symbol DAT = C.1
Symbol CLK = C.2

Low RST
Low DAT
Low CLK

Do
  b1 = %000001
  b2 = %000011
  b3 = %000111
  b4 = %001111
  b5 = %011111
  b6 = %111111
  Gosub WriteWipers
  Pause 1000
Loop

WriteWipers:
  High RST
  SpiOut CLK, DAT, LSBfirst_L, ( b1, b2, b3, b4, b5, b6 )
  Low RST
  Return
 
#18
MCP41xxx
The MCP41xxx series are 8-pin devices available in DIP and SOIC packages with a single digital potentiometer which is controlled using the up/down approach with an operating voltage range of 2.7V to 5.5Vdc. The potentiometers for these devices have 256 taps/steps.
...............................
Command byte
Only three bits of the command bytes are utilized by the MCP41XXX series digital potentiometers. The format is as follows:
Bit # _7 _6 _5 _4 _3 _2 _1 _0
Fctn _X _X C1 C0 .X _X _X P0
The bits C1 and C0 form the command as follows:
C1 = 0, C0 = 0 => No operation is performed
C1 = 0, C0 = 1 => Transfer the data from the data byte to the potentiometer data register
C1 = 1, C0 = 0 => Potentiometer software shutdown – see comment below
C1 = 1, C0 = 1 => No operation is performed

For the bit P0,
P0 = 0 => No operation is performed
P0 = 1 = > The data is transferred to the potentiometer.
........................


MCP42xxx
The MCP42xxx series of digital potentiometers are almost identical to the MCP41xxx chips except they have two electrically independent potentiometers in one 14 pin package.


In addition to the three pins for the second potentiometer, the added pins also provide two added control pins, RS and SHDN.


The RS pin is active when pulled low and returns the two potentiometers to their default/mid-scale ($80) position. The pin must return to a high state (1) before the potentiometers can be further changed.


The SHDN pin when pulled low performs the same action as a software shutdown to both potentiometer simultaneously. The pin must return to a high state (1) before the potentiometers can be returned to an active state


Control of the two potentiometers is done in the same manner as the MCP41xxx series using the command byte. With the MCP42xxx series one addition bit is used within the command byte.

The command byte format is as follows:
Bit # _7 _6 _5 _4 _3 _2 _1 _0
Fctn _X _X C1 C0 .X _X P1 P0
The bits C1 and C0 form the command as follows:
C1 = 0, C0 = 0 => No operation is performed
C1 = 0, C0 = 1 => Transfer the data from the data byte to the data register for the nominated potentiometer(s)
C1 = 1, C0 = 0 => Potentiometer software shutdown for the nominated potentiometer(s)
– see comment below
C1 = 1, C0 = 1 => No operation is performed

For the bits P1 and P0,
P1 = 0, P1 = 0 => No operation is performed with either potentiometer
P1 = 0, P0 = 1 = > The data is transferred to potentiometer No 0.
P1 = 1, P0 = 0 = > The data is transferred to the potentiometer No 1
P1 = 1, P0 = 1 = > The same data is transferred to both potentiometers


The attached program will also work for Potentiometer No 0 of a MCP42xxx series digital potentiometer.
It only requires some minor changes to the program code to control potentiometer No 1 or both potentiometers.

Hi, is it okay if I reference this information in a piece of school work? Thank you :)
 

pbaker21

Senior Member
#19
This would be my first try at it, b1-b6 for wiper 1-6 ...

Code:
#Picaxe 20X2

Symbol RST = pinC.0 : Symbol RST_DIR = dirC.0
Symbol DAT = pinC.1 : Symbol DAT_DIR = dirC.1
Symbol CLK = pinC.2 : Symbol CLK_DIR = dirC.2

RST_DIR = 1
DAT_DIR = 1
CLK_DIR = 1

Do
  b1 = %000001
  b2 = %000011
  b3 = %000111
  b4 = %001111
  b5 = %011111
  b6 = %111111
  Gosub WriteWipers
  Pause 1000
Loop

WriteWipers:
  b0 = b1 : Gosub WriteWiper
  b0 = b2 : Gosub WriteWiper
  b0 = b3 : Gosub WriteWiper
  b0 = b4 : Gosub WriteWiper
  b0 = b5 : Gosub WriteWiper
  b0 = b6 : Gosub WriteWiper
  Return

WriteWiper:
               RST = 0 : RST = 1
  DAT = bit0 : CLK = 1 : CLK = 0
  DAT = bit1 : CLK = 1 : CLK = 0
  DAT = bit2 : CLK = 1 : CLK = 0
  DAT = bit3 : CLK = 1 : CLK = 0
  DAT = bit4 : CLK = 1 : CLK = 0
  DAT = bit5 : CLK = 1 : CLK = 0
  DAT = bit6 : CLK = 1 : CLK = 0
  DAT = 0    : CLK = 1 : CLK = 0
  DAT = 0    : CLK = 1 : CLK = 0
               RST = 0
  Return
If that works then hopefully this will too ...

Code:
#Picaxe 20X2

Symbol RST = C.0
Symbol DAT = C.1
Symbol CLK = C.2

Low RST
Low DAT
Low CLK

Do
  b1 = %000001
  b2 = %000011
  b3 = %000111
  b4 = %001111
  b5 = %011111
  b6 = %111111
  Gosub WriteWipers
  Pause 1000
Loop

WriteWipers:
  High RST
  SpiOut CLK, DAT, LSBfirst_L, ( b1, b2, b3, b4, b5, b6 )
  Low RST
  Return

Thanks for the coding example, appreciated. Sadly it's not giving me anything much, besides a dimly lit led on pot 2.
 

pbaker21

Senior Member
#20
Thanks for your feedback.

Data is transmitted in order of LSB first. Potentiometers are designated from 1 through 6 and the value
for potentiometer-1 will be the first data entered into the shift register, followed by that of potentiometer-
2 and so forth.

ADDED: A don't change feature is provided so that you don't need to remember the values for all the pots to send each time you want to update one.
 

pbaker21

Senior Member
#22
Hippy, I found a silly (darn silly) mistake in my circuit, your code is working - many thanks ;)


This would be my first try at it, b1-b6 for wiper 1-6 ...

Code:
#Picaxe 20X2

Symbol RST = pinC.0 : Symbol RST_DIR = dirC.0
Symbol DAT = pinC.1 : Symbol DAT_DIR = dirC.1
Symbol CLK = pinC.2 : Symbol CLK_DIR = dirC.2

RST_DIR = 1
DAT_DIR = 1
CLK_DIR = 1

Do
  b1 = %000001
  b2 = %000011
  b3 = %000111
  b4 = %001111
  b5 = %011111
  b6 = %111111
  Gosub WriteWipers
  Pause 1000
Loop

WriteWipers:
  b0 = b1 : Gosub WriteWiper
  b0 = b2 : Gosub WriteWiper
  b0 = b3 : Gosub WriteWiper
  b0 = b4 : Gosub WriteWiper
  b0 = b5 : Gosub WriteWiper
  b0 = b6 : Gosub WriteWiper
  Return

WriteWiper:
               RST = 0 : RST = 1
  DAT = bit0 : CLK = 1 : CLK = 0
  DAT = bit1 : CLK = 1 : CLK = 0
  DAT = bit2 : CLK = 1 : CLK = 0
  DAT = bit3 : CLK = 1 : CLK = 0
  DAT = bit4 : CLK = 1 : CLK = 0
  DAT = bit5 : CLK = 1 : CLK = 0
  DAT = bit6 : CLK = 1 : CLK = 0
  DAT = 0    : CLK = 1 : CLK = 0
  DAT = 0    : CLK = 1 : CLK = 0
               RST = 0
  Return
If that works then hopefully this will too ...

Code:
#Picaxe 20X2

Symbol RST = C.0
Symbol DAT = C.1
Symbol CLK = C.2

Low RST
Low DAT
Low CLK

Do
  b1 = %000001
  b2 = %000011
  b3 = %000111
  b4 = %001111
  b5 = %011111
  b6 = %111111
  Gosub WriteWipers
  Pause 1000
Loop

WriteWipers:
  High RST
  SpiOut CLK, DAT, LSBfirst_L, ( b1, b2, b3, b4, b5, b6 )
  Low RST
  Return
 
#23
Hi, is it okay if I reference this information in a piece of school work? Thank you :)
Thank you for asking.

Yes, feel free to utilise the information provided. I would only ask that you do reference the source, as you propose to do.
Purely from curiosity, might I ask what your project is?
 

saunj

Senior Member
#24
Dig pot advantages

For me these were the advantages of a digital pot over PWM:
No bulky analog filter caps
Independent of the supply voltage
Floating.
These thumbnails show the circuit and pot driver for a meter driver I used recently.
 

Attachments

saunj

Senior Member
#26
Correction

For those who may be interested, here is the link to John's Multimeter project webpage(s)
http://www.worsleyassociates.com/Projects/DigitalMeter/index.html
giving the full code and a number of photos of the project.
Thank you for your interest westaust55. On the "Outputs" circuit the value of the 6040 ohm resistor should be 2700.
6040 gave insufficient current through the zener.

I must confess that this project is a solution in search of a problem. However the pot code and meter circuit are good.
I'm surprised this discussion is now - I originally wrote the code in 2008, and did not change it for the recent upgrade.
The meter is quite accurate, I don't think any of the error is due to the pot.

Note: Since I am retired all this stuff in my web site is in the public domain.
 
#27
Mcp4131

Hi,

Does anyone have some basic code using a Picaxe (20x2 in particular) in conjunction with a MCP4131 Digital POT?

Thanks.
I realise this is an old post but I've just been playing around with this myself. The code already posted can do the task, you just need to know the differences in commands. There is an 8 bit code to increment or decrement (you need to adjust the ShiftOutMSBFirst function to cater for 8 bits insteadh of 16).

Symbol Increment = %00000100
Symbol Decrement = %00001000

Alternatively you can write data with the 16 bit code as follows:
0000 00nn nnnn nnnn

Where the leading zeros are the write command and the "n" values are the wipe register. Note that on the MCP4131 there are 129 steps.

So far I've only played around with the increment/decrement. If I end up using the "read" and "write" ability I'll post the code in this thread.
 
#28
Mcp4131-103

So I found this looking for digital potentiometer and picaxe in google.
I had recently purchased a couple of MCP4131-103 chips and was going to use them to replace a thermistor in a thermostat and convert it to a digital thermostat using a picaxe 08M2 and a DS18B20.

Finding this thread allowed me to get going much quicker. So thank you to the ones who contributed to this thread.

The one major difference I found working with these newer chips is that the command to write data is:
SYMBOL writedata = %00000000

instead of

SYMBOL writedata = %00010001

Once I fixed this, I was able to write the command byte and data byte to the pot and get the wiper to move.
I will say that the bit banging SPI in the bottom of the code had me scratching my head for a bit, why it worked. :confused:
 
Last edited:
Top