Hi all.
I'm having an issue that is driving me crazy. I'm trying to use a potentiometer on GPIO4 (10K linear - wiper on GP4, ends on 5V and ground).
Originally, I tried to use:
Code:
duty = adval 'adval is the result of the ADC
It worked, but it appears that I'm only getting part of the way, as the led (on GP2/CCP1) only varies from off to about half brightness. I then discovered that the result is from 0-1025, not 255 as I had assumed.
Then I tried:
But the led never lights.
After some more reading, here is what I have now. It still results in the led never lighting, no matter the position of the pot.
Code:
;pic12f683
#CONFIG
cfg = _INTOSCIO
cfg&= _WDT_ON
cfg&= _PWRTE_OFF
cfg&= _MCLRE_ON
cfg&= _CP_OFF
cfg&= _CPD_OFF
cfg&= _BOD_ON
cfg&= _IESO_ON
cfg&= _FCMEN_ON
__CONFIG cfg
#ENDCONFIG
DEFINE OSC 8
' Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS
OSCCON=$70
CMCON0 = 7
TRISIO = %111011 ;gp2= output
ANSEL = %00111000 ' Set AN3 analog, rest digital
CMCON0 = 7 ' Analog comparators off
ADCON0.7 = 1
adval Var word ' Create adval to store result
duty var byte
result var word
mainloop:
ADCIN 3, adval ' Read channel 3 to adval
pauseus 50 'wait 50us for read to complete
result = adval >> 2 'right shift by 2 bits to divide by 4
if result > 255 then result = 255
duty = result.BYTE0
' hpwm Channel, Dutycycle, Frequency
hpwm 1,duty,600 ; set led brightness at 600hz
pause 50 ;Pause for 50 milliseconds
goto mainloop
Anyone got any ideas what I'm doing wrong? It's getting a little frustrating. I am planning to do a test for the GO/DONE bit in ADCON0 to make the delay after read less, but I want to get this part working first.
I would appreciate any help anyone can offer.
Bookmarks