Hi Premelec,MCP41050 is SPI interface and 255 taps - MCP4021 only 64 taps and UP/Down - you need to settle on just what you need...
HIGH C.6
main0:
LET b1=0
LET b2=0
main1:
HIGH C.7
pulsout C.6, 200
pause 100
LET b1=b1+1
IF b1>10 THEN GOTO main2
goto main1
main2:
LOW C.7
pulsout C.6, 200
pause 100
LET b2=b2+1
IF b2>10 THEN GOTO main0
goto main2
You can try the following code tested with an 18m2 and a MCP4231. The code is a stripped down version of westaust55's code. I hope he doesn't mind.Does anyone have a simple example of a few lines of code for the 18M2 to set the MCP41050 to wiper position 100 (or any single position)?
[color=Green]'stripped down version of following program
; =================================================
; File....... MCP41010 digital Potentiometer test program
; Purpose....
; Author..... Westaust55
; E-mail.....
; Started.... 18-12-2009
; Updated....
; =================================================
;
;
'modified by Rick100 to write a single value to pot and end[/color]
[color=Navy]#picaxe [/color][color=Black]18m2[/color]
[color=Navy]#terminal 4800[/color]
[color=Green]; Define the PICAXE Digital Outputs[/color]
[color=Blue]SYMBOL CS [/color][color=DarkCyan]= [/color][color=Blue]B.4 [/color][color=Green]; the MCP41050 CS pin connects to PICAXE output[/color]
[color=Blue]SYMBOL SClk [/color][color=DarkCyan]= [/color][color=Blue]B.5 [/color][color=Green]; the MCP41050 SCK pin connects to PICAXE output[/color]
[color=Blue]SYMBOL SData [/color][color=DarkCyan]= [/color][color=Blue]B.6 [/color][color=Green]; the MCP41050 SI pin connects to PICAXE output
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -[/color]
[color=Blue]SYMBOL [/color][color=Purple]wiper [/color][color=DarkCyan]= [/color][color=Purple]b1 [/color][color=Green]; step/position of the digital pot wiper[/color]
[color=Blue]SYMBOL [/color][color=Purple]index [/color][color=DarkCyan]= [/color][color=Purple]b2[/color]
[color=Blue]SYMBOL [/color][color=Purple]mask [/color][color=DarkCyan]= [/color][color=Purple]b3[/color]
[color=Blue]SYMBOL [/color][color=Purple]datbyte [/color][color=DarkCyan]= [/color][color=Purple]b4 [/color][color=Green]; digital pot data byte (low byte of w2)[/color]
[color=Blue]SYMBOL [/color][color=Purple]control [/color][color=DarkCyan]= [/color][color=Purple]b5 [/color][color=Green]; digital pot command byte (high byte of w2)[/color]
[color=Blue]SYMBOL [/color][color=Purple]varout [/color][color=DarkCyan]= [/color][color=Purple]w2 [/color][color=Green]; command + data bytes as a word to the digital pot
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Define constants[/color]
[color=Blue]SYMBOL writedata [/color][color=DarkCyan]= [/color][color=Navy]%00010001 [/color][color=Green];command byte for writing a value to pot 0
[/color][color=Blue]HIGH CS [/color][color=Green]; set the chip select high to disable serial control
[/color][color=Blue]LOW SClk
PAUSE [/color][color=Navy]2000
[/color][color=Purple]wiper [/color][color=DarkCyan]= [/color][color=Navy]100 [/color][color=Green]'pot position
[/color][color=Blue]sertxd([/color][color=Black]#[/color][color=Purple]wiper[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue]) [/color][color=Green]'write it to terminal window
[/color][color=Purple]control [/color][color=DarkCyan]= [/color][color=Blue]writedata [/color][color=Green];put control byte into high byte of word variable varout
[/color][color=Purple]datbyte [/color][color=DarkCyan]= [/color][color=Purple]wiper [/color][color=Green]'put data byte into low byte of word variable varout
[/color][color=Blue]GOSUB [/color][color=Black]ShiftOutMSBFirst [/color][color=Green];send it to the chip[/color]
[color=Black]waitHere:
[/color][color=Blue]goto [/color][color=Black]waitHere[/color]
[color=Green];
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
; Subroutine to shift the 16 bits out to the digital potentiometer[/color]
[color=Black]ShiftOutMSBFirst:
[/color][color=Blue]LOW CS [/color][color=Green]; set the Chip Select line low to enable the digital pot
[/color][color=Blue]FOR [/color][color=Purple]index [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]TO [/color][color=Navy]15 [/color][color=Green]; number of bits to send
[/color][color=Purple]mask [/color][color=DarkCyan]= [/color][color=Purple]control [/color][color=DarkCyan]AND [/color][color=Navy]$80 [/color][color=Green]; set the mask flag bit based on data bit to be sent
[/color][color=Blue]LOW SData [/color][color=Green]; set the serial data line low
[/color][color=Blue]IF [/color][color=Purple]mask [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]THEN [/color][color=Black]skiponlow [/color][color=Green]; if data bit = 0 then leave data line low
[/color][color=Blue]HIGH SData [/color][color=Green]; if data bit = 1 then set serial data line high[/color]
[color=Black]Skiponlow: [/color][color=Blue]PULSOUT SClk[/color][color=Black],[/color][color=Navy]1 [/color][color=Green]; pulse the serial clock line to tranfer data bit
[/color][color=Purple]varout [/color][color=DarkCyan]= [/color][color=Purple]varout [/color][color=DarkCyan]* [/color][color=Navy]$02 [/color][color=Green]; move data one bit (left) toward MSB - datbytes is pushed thru control byte[/color]
[color=Blue]NEXT [/color][color=Purple]index [/color][color=Green]; loop back for next bit
[/color][color=Blue]HIGH CS [/color][color=Green]; Set the CS line high to transfer data into the digital pot register
[/color][color=Blue]RETURN [/color][color=Green]; return to calling program[/color]
Hi Rick,You can try the following code tested with an 18m2 and a MCP4231. The code is a stripped down version of westaust55's code. I hope he doesn't mind.
I don't have a 41050 to test it on. I just looked at the datasheet. Maybe it will work.
Good luck,
Rick
#picaxe 18m2
setfreq m8
; Define the PICAXE Digital Outputs
SYMBOL CS = B.4 ; the MCP41050 CS pin connects to PICAXE output
SYMBOL SClk = B.5 ; the MCP41050 SCK pin connects to PICAXE output
SYMBOL SData = B.6 ; the MCP41050 SI pin connects to PICAXE output
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SYMBOL position = b0 ; serin from remote app
SYMBOL wiper = b1 ; step/position of the digital pot wiper
SYMBOL index = b2
SYMBOL mask = b3
SYMBOL datbyte = b4 ; digital pot data byte (low byte of w2)
SYMBOL control = b5 ; digital pot command byte (high byte of w2)
SYMBOL varout = w2 ; command + data bytes as a word to the digital pot
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Define constants
SYMBOL writedata = %00010001 ;command byte for writing a value to pot 0
waitHere:
serin C.0, T9600_8,b0 ; value from serin from remote
HIGH CS ; set the chip select high to disable serial control
LOW SClk
wiper = position 'pot position
sertxd(#wiper,13,10) 'write it to terminal window
control = writedata ;put control byte into high byte of word variable varout
datbyte = wiper 'put data byte into low byte of word variable varout
IF b0<>0 THEN GOSUB ShiftOutMSBFirst:
goto waitHere
;
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
; 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
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
'stripped down version of following program
; =================================================
; File....... MCP41010 digital Potentiometer test program
; Purpose....
; Author..... Westaust55
; E-mail.....
; Started.... 18-12-2009
; Updated....
; =================================================
;
;
'modified by Rick100 to write a single value to pot and end
'modified by Joe Paul Rampolla for Lionel train controller circuit and HC-06 transceiver
#picaxe 18m2
setfreq m8
; Define the PICAXE Digital Outputs
SYMBOL CS = B.4 ; the MCP41050 CS pin connects to PICAXE output
SYMBOL SClk = B.5 ; the MCP41050 SCK pin connects to PICAXE output
SYMBOL SData = B.6 ; the MCP41050 SI pin connects to PICAXE output
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SYMBOL wiper = b1 ; step/position of the digital pot wiper
SYMBOL index = b2
SYMBOL mask = b3
SYMBOL datbyte = b4 ; digital pot data byte (low byte of w2)
SYMBOL control = b5 ; digital pot command byte (high byte of w2)
SYMBOL varout = w2 ; command + data bytes as a word to the digital pot
;
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Define constants
SYMBOL writedata = %00010001 ;command byte for writing a value to pot 0
waitHere:
serin C.0, T9600_8,b0,b6 ; value from remote, 10 preset pot positions
if b6 = 48 then let b1=1 :goto outtopot :end if
if b6 = 49 then let b1=81 :goto outtopot :end if
if b6 = 50 then let b1=94 :goto outtopot :end if
if b6 = 51 then let b1=107 :goto outtopot :end if
if b6 = 52 then let b1=120 :goto outtopot :end if
if b6 = 53 then let b1=133 :goto outtopot :end if
if b6 = 54 then let b1=147 :goto outtopot :end if
if b6 = 55 then let b1=160 :goto outtopot :end if
if b6 = 56 then let b1=173 :goto outtopot :end if
if b6 = 57 then let b1=186 :goto outtopot :end if
outtopot:
HIGH CS ; set the chip select high to disable serial control
LOW SClk
sertxd(#wiper,13,10) 'write it to terminal window
control = writedata ;put control byte into high byte of word variable varout
datbyte = wiper 'put data byte into low byte of word variable varout
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
; 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
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
goto waitHere
Your welcome. I enjoyed your video but I'm curious about how your getting reverse with just a pot connection.Many thanks!!!!!!!!!!!]
Hi Rick,Your welcome. I enjoyed your video but I'm curious about how your getting reverse with just a pot connection.
Good luck,
Rick
serin [500, outtopot], C.0, T9600_8,b0,b6
Hi Rick,Hello Joe,
Thanks for the explanation of the reversing mechanism. In your video, I didn't see a decoupling cap across the power supply pins of the digital pot. You could try adding a .1 microFarad capacitor.
Good luck,
Rick