Here's the code I wrote to control 5 digital pots daisy chained together.
Code:
DEFINE LOADER_USED 1 ' Bootloader is being used
DEFINE OSC 16 ' Set oscillator frequency, 4MHz (HS_PLL x4)
DEFINE HSER_BAUD 9600 ' Baud rate for serial output
DEFINE HSER_CLROERR 1 ' Automatically clear over-run errors
DEFINE HSER_RCSTA 90h ' Enable USART receive
DEFINE HSER_TXSTA 24h ' TXSTA=%00100100. TX enable, BRGH=1 for high-speed
@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H
' Oscillator Switch Disable
' Oscillator Type HS PLL
@ __CONFIG _CONFIG2L, _BOR_ON_2L & _PWRT_ON_2L & _BORV_45_2L
' Brown-Out Reset Enabled
' Power-Up Timer Enable
' Brown-Out Reset Voltage 4.5V
@ __CONFIG _CONFIG2H, _WDT_ON_2H
' Watch Dog Timer Enable
@ __CONFIG _CONFIG4L, _STVR_ON_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
' Stack Over/Underflow Reset Enable
' Low Voltage ICSP Programming Disabled
' Background Debugger Disabled
ADCON1 = %0111 ' Set porta to digital mode
si var porte.2
sclk var porte.1
cs var porte.0
pot_value_0 var byte
pot_value_1 var byte
pot_value_2 var byte
pot_value_3 var byte
pot_value_4 var byte
cmd var byte
position var byte
low sclk ' Set clock pin low
high cs ' Set chip select low
clear
start:
gosub pot_5
gosub pot_4
gosub pot_3
gosub pot_2
gosub pot_1
goto start
pot_5:
low cs
cmd = %00010001 : position = pot_value_4 : gosub shout
cmd = 0 : position = 0 : gosub shout
cmd = 0 : position = 0 : gosub shout
high cs
return
pot_4:
low cs
cmd = %00010010 : position = pot_value_3 : gosub shout
cmd = 0 : position = 0 : gosub shout
high cs
return
pot_3:
low cs
cmd = %00010001 : position = pot_value_2 : gosub shout
cmd = 0 : position = 0 : gosub shout
high cs
return
pot_2:
low cs
cmd = %00010010 : position = pot_value_1 : gosub shout
high cs
return
pot_1:
low cs
cmd = %00010001 : position = pot_value_0 : gosub shout
high cs
return
shout:
shiftout si,sclk,1,[cmd]
shiftout si,sclk,1,[position]
return
Just set the pot_value_x to whatever you want between 0-255.
Bookmarks