PDA

View Full Version : 12F683 ADC problems



andywpg
- 5th April 2024, 16:46
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:


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:


duty = adval >> 2

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.


;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.

andywpg
- 5th April 2024, 20:17
Figured it out.

The problem was "define ADC_BITS 8". Not sure why I did that. Changed it to 10 and it works fine now. :smile:

Acetronics2
- 9th April 2024, 22:28
2.15.1 DEFINEs
Since many of the DEFINEs in PBP pass data directly to the assembler, they should
be considered case-sensitive. All defined parameters should be written in uppercase:


DEFINE OSC 20 ' Correct
DEFINE osc 20 ' Incorrect, and will not generate an error message




just " RTFM " ;)

9656

Alain

HenrikOlsson
- 10th April 2024, 16:03
Yes Alain, go RTFM and come back with another comic ;-)
This has been covered almost as many times as "disable analog functions when using pins as digital" but since it continues to create false statements here we go again.

It's not the word define that's case sensitive, it's what comes after it.
DeFinE ADC_BITS 8 will work, define ADC_BITS 8 will work, Define ADC_BITS 8 (as it was written in the original code) will work and DEFINE ADC_BITS 8 will work. However, DEFINE adc_bits 8 will not because adc_bits is not written in upper case. Or, to state it even more correct, it's not written with the same case as it was originally defined. Had the PBP authours written it as adc_bits then adc_bits would be correct and ADC_BITS would not.

So, again, it's not the word DEFINE that's case sensetive but what comes after it.

My suspicion with the original code is that left/right justification setting was wrong for the 8bit result (I always get that wrong).

/Henrik.

Acetronics2
- 15th April 2024, 16:34
Hi, Henrik

was indeed a "RTFM" issue ... ;) ... and you were also right !



The result should be configured as left-justified if you are performing an 8-bit
conversion. It should be right-justified if more than 8-bit precision is required. This
is also a register setting and will be found in the datasheet section on the Analog
Converter (ADC).


manual p 116 ...

BTW, it looks to be what to do with other Pics too ...