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
Bookmarks