try something like this
slightly altered for a pic16f1825
Code:
;pic16f1825
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _CP_OFF & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CLKOUTEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF
#ENDCONFIG
ADCON0 = %00000000
ADCON1 = %00110000
'ANSEL = %00000110 ;pic16f1825
ANSELA = %00000100 ;pot is on porta.2 an2
define ADC_SAMPLEUS 50
define ADC_BITS 8
DEFINE ADC_CLOCK 3
'CM1CON0 = %00000000 ;pic16f1825
'CM2CON0 = %00000000 ;pic16f1825
TRISA = %00001110 ;porta.0 used for debug
TRISC = %00000000
porta.0=1 ;porta.0 used for debug
DEFINE DEBUG_REG PORTA
DEFINE DEBUG_BIT 0 ; if not used for pwr
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 0
' Set TMR0 interrupt prescaler to 1:128
OPTION_REG = %10000110 ' Set TMR0 configuration and enable PORTB pullups
INTCON = %10100000 ' Enable TMR0 interrupts
osccon=$68 ;pic16f1825
On Interrupt Goto tickint
'
' Hardware connection
' ===================
CS VAR PORTA.5
SCK VAR PORTC.2
SDI VAR PORTC.1
tempoLED var PORTC.0
' tempobutton var PORTA.0 ;porta.0 used for debug
' bypassbutton var PORTA.3 ;mclr used on pic16f1825
bypassLED var PORTC.4
fetA var PORTC.5
fetB var PORTA.4
fetC var PORTC.3
;pot is on porta.2 an2
'
' Variables definition
' ===================
' w var byte ' division toggle switch
x var byte ' delay time knob
xprevious var byte
' z var byte
' ticks var word
tapcount var word
LEDcounter var word
' LEDcounterlimit var word
LEDrate var byte
' trailsmode var byte
' trailsmodecounter var word
' LEDon var byte
' LEDoff var byte
pause 2000
Debug "Start",13 ,10
'
' begin in bypass
' ===============
tempoLED = 0
fetA = 0
fetB = 0
fetC = 1
bypassLED = 0
' trailsmode = 0
gosub readpot
gosub movepot
if xprevious <= 0 then
xprevious = 1
endif
main:
if (LEDcounter // LEDrate) > (LEDrate>>1) then
tempoLED = 1
else
tempoLED = 0
endif
gosub potcheck
if ! LEDcounter//4==0 then
Debug 13 ,10 , #LEDcounter ,9,#(LEDcounter // LEDrate),9,#LEDrate
endif
goto main
potcheck:
gosub readpot
' if x <= 0 then why ??????????????????????????????????
' x = 1
' endif
;if x >= xprevious + 5 or x <= xprevious - 5 then ' analog potentiometer debounce tolerance
if abs ( xprevious-x) > 4 then
gosub movepot
endif
return
movepot:
CS = 0
shiftout SDI, SCK, 1, [00000000,x]
shiftout SDI, SCK, 1, [00010000,x]
CS = 1
xprevious = x
gosub tempoLEDadjust
return
readpot
adcin 2,x
x = 8 max x ;stop led from shutting down completely
' pause 10 why ??????
return
tempoLEDadjust: ' compensate for TMR0 not being 1:1 with actual delay time
LEDrate = x/2
' LEDcounterlimit = LEDrate + 1 'counterlimit must always be 1 greater than rate
' LEDcounter = 0 ' reset LED counter
return
Disable ' Disable interrupts during interrupt handler
tickint: ' Interrupt routine to handle each timer tick
LEDcounter = LEDcounter + 1 ' add 1 to LEDcounter on every interrupt
' if LEDcounter > LEDcounterlimit then ' prevent LEDcounter from overflowing
' LEDcounter = 0
' endif
tiexit:
INTCON.2 = 0 ' Reset timer interrupt flag
Resume
enable
end
Bookmarks