I think I've got it working well now. FWIW, tweaking the CONFIG or registers didn't really help much. Neither did setting the ADC sample rate to ridiculously long times. What seems to have fixed it was adding a "buffer" or tolerance limit. Subroutine movedigipot sets xprevious to the ADC reading that was used to last move the digital pot. Subroutine checkpot checks to see if the current ADC reading is within +/- 2.56% of xprevious. If it is, it doesn't allow the digital pot to move. I made it so that x could never equal zero because that causes trouble further down the road. There's no obvious reason at the moment.
Now it's time to implement the interrupt.....ugh.
Code:#config __CONFIG _CP_OFF & _WDTE_OFF & _BOREN_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _IOSCFS_8MHZ #endconfig ADCON0 = %00000000 ADCON1 = %00110000 ANSEL = %00000110 define ADC_SAMPLEUS 50 define ADC_BITS 8 CM1CON0 = %00000000 CM2CON0 = %00000000 TRISA = %00001111 TRISC = %00000000 ' ' Hardware connection ' =================== CS VAR PORTA.5 SCK VAR PORTC.2 SDI VAR PORTC.1 tempoLED var PORTC.0 tempobutton var PORTA.0 bypassbutton var PORTA.3 bypassLED var PORTC.4 fetA var PORTC.5 fetB var PORTA.4 fetC var PORTC.3 ' ' 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 word trailsmode var byte trailsmodecounter var word LEDon var byte LEDoff var byte LEDon = 1 LEDoff = 0 ' ' begin in bypass ' =============== tempoLED = 0 fetA = 0 fetB = 0 fetC = 1 bypassLED = LEDoff trailsmode = 0 gosub readpot gosub movepot if xprevious <= 0 then xprevious = 1 endif main: if bypassBUTTON = 0 then gosub bypasspress gosub bypassbuttonrelease ENDif gosub potcheck goto main ' subroutine that occurs when bypass button is pressed bypasspress: trailsmodecounter = 0 ' waits to see if you want to switch between normal or trails mode do until trailsmodecounter = 400 if bypassbutton = 1 then exit pause 10 trailsmodecounter = trailsmodecounter + 1 loop if trailsmodecounter = 400 then goto trailmodechange endif if trailsmode = 1 then gosub trailsbypass elseif trailsmode = 0 then gosub normalbypass endif return trailmodechange: ' subroutine that occurs when bypass button is held long enough to change between normal or trails mode if trailsmode = 1 then trailsmode = 0 elseif trailsmode = 0 then trailsmode = 1 endif for z = 1 to 5 tempoLED = 1 pause 100 tempoLED = 0 pause 100 next z goto main normalbypass: ' subroutine for normal bypass if bypassLED = 0 then fetA = 1 fetB = 1 fetC = 0 bypassLED = LEDon elseif bypassLED = 1 then fetA = 0 fetB = 0 fetC = 1 bypassLED = 0 endif return trailsbypass: 'subroutine for trails bypass if bypassLED = 0 then fetA = 1 fetB = 1 fetC = 0 bypassLED = LEDon elseif bypassLED = 1 then fetA = 0 fetB = 1 fetC = 1 bypassLED = LEDoff endif return bypassbuttonrelease: ' debounce subroutine when bypass button is pressed do until bypassbutton = 1 pause 10 loop return potcheck: gosub readpot if x <= 0 then x = 1 endif if x >= xprevious + 5 or x <= xprevious - 5 then gosub movepot endif return movepot: CS = 0 shiftout SDI, SCK, 1, [00000000,x] shiftout SDI, SCK, 1, [00010000,x] CS = 1 xprevious = x return readpot adcin 2,x pause 100 return end




Bookmarks