PDA

View Full Version : Can someone check my code? Serially Controlled Digital Pot



TonyA
- 24th January 2007, 15:42
Hi,

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:
http://www.picbasic.co.uk/forum/showthread.php?p=21450&posted=1#post21450

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
' ======================

'DEFINITIONS'
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

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

' ' Hardware connection
' ===================
CS VAR PORTB.0
CLK VAR PORTB.1
SI VAR PORTB.2

' ' 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 Byte:(see 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

Start:

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

next

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

malc-c
- 24th January 2007, 17:59
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. ?

TonyA
- 24th January 2007, 19:05
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
endif

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

malc-c
- 24th January 2007, 22:01
Tony,

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 !

TonyA
- 24th January 2007, 22:20
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