PDA

View Full Version : ADC in 12F683; puzzled



mikebike
- 21st September 2010, 13:06
I have started my second project, which is a temperature regulator.
My first project used one ADC channel, but I only used it as 8 bit ADC.
This time I thought that I needed 10 bits and specified that in the set up...

To test the first mock up, I set up a trip point of 512, which should be 2,5V.
-But it's not. By trying different numbers, I came to the conclusion that the ADC gives 13300 per Volt!??
What's up here? I'm totally lost!

Acetronics2
- 21st September 2010, 13:12
Hi, Mike

How do you want us to give you any idea ???

PLEASE ...

Your program With the Config Header ... as always !!!

Alain

mikebike
- 21st September 2010, 13:15
Your program With the Config Header ... as always !!!

Alain

Right, of cource...



'Define ADCIN parameters
ANSEL = %00000011 'GP0 = AN0, GP1 = AN1

DEFINE ADC_BITS 10 'Set number of bits in result
DEFINE ADC_CLOCK 3 'Set clock source
DEFINE ADC_SAMPLEUS 50 'Set sampling time in us

time1 VAR WORD 'First time in seconds
time2 VAR WORD 'Second time in seconds
time3 VAR WORD 'Third time in seconds
involt1 VAR word 'Value for ADC0 (temp)
involt2 VAR word 'Value for ADC1 (adjust)
ut4 var bit 'Status for LED 4
ut5 var bit 'Status for LED 5
trip1 var word
trip1 = 13300 'Default value
trip2 var word
trip2 = 13300 'Default value

TRISIO = %11001011 'Defining PORT
GPIO = %00111000 'Turn off all LEDs
time1 = 0 'Zero time 1
time2 = 0 'Zero time 2
time3 = 0 'Zero time 3
ut4 = 1 'LED 4 = off (inversed)
ut5 = 1 'LED 5 = off (inversed)

loop1:
ADCIN 0, involt1 'Read channel 0 to involt1
ADCIN 1, involt2 'Read channel 0 to involt2
pause 100 'Wait 100 ms
IF involt1 >= trip1 THEN
GPIO.4 = 0 'Turn on LED4
else
GPIO.4 = 1 'Turn off LED4
endif
IF involt2 >= trip2 THEN
GPIO.5 = 0 'Turn on LED5
else
GPIO.5 = 1 'Turn off LED5
endif
goto loop1 'Go back to loop1
END


trip1 = 13300 gives exactly 1V trip level. I don't mind having this kind of resolution, but it doesn't seem realistic.

cncmachineguy
- 21st September 2010, 13:22
Just guessing with your code, It looks like your result is left justified (or right) but in any event, you appear to be reading the the 2 LSB as bit 6,7 of your answer. since the 10 bit result is a word, you seem to have result high as valid 8 bits. but your result low is ddxxxxxx. So your LSB is really = dec128 and not 1!

How do i guess this? if you right shift your 13300 6 times, you get ~207. Assuming you are testing 0-5V you expect 1024/5=204 per volt. Pretty close.

If you need help understanding or fixing this, post code!

Sorry, we posted together

Acetronics2
- 21st September 2010, 13:31
Right, of cource...

trip1 = 13300 gives exactly 1V trip level. I don't mind having this kind of resolution, but it doesn't seem realistic.

Missing :

- Deactivate comparators
- ADC result right justified

Alain

mikebike
- 21st September 2010, 13:58
Missing :

- Deactivate comparators

Is this specific for 10 bit ADC, or should it be there for 8 bit conversion as well?
And how? -CMON0 something?


Missing :
- ADC result right justified
Alain
Hm, the example programs that I have (for 16F877A) do not have such an instruction, does that PIC not need it?

ADCON0.7 = 1 seems to have have cured the problem, thanks guys. :)

Acetronics2
- 21st September 2010, 14:16
Is this specific for 10 bit ADC, or should it be there for 8 bit conversion as well?
And how? -CMON0 something?


have a look to datasheet ... Comparator section !!!



Hm, the example programs that I have (for 16F877A) do not have such an instruction, does that PIC not need it?

some pins share the comparator and ADC functions ... you HAVE to verify which function is deactivated and which is to activate. ( one at a time ... )

Depends on the chip used - and the ports used ...



ADCON0.7 = 1 seems to have have cured the problem, thanks guys. :)


Full config done = NO suprise ...

sometimes, the pic defaults @ reset are the ones you want ... sometimes not. so you have to verify everything with the pic Datasheet !!! ;)

mikebike
- 21st September 2010, 14:37
have a look to datasheet ... Comparator section !!!
Done that, but didn't understand it (new to this stuff).


CMCON0 = %00000000 seems to be accepted by PBP and the chip (still) works.

mikebike
- 21st September 2010, 14:39
sometimes, the pic defaults @ reset are the ones you want ... sometimes not. so you have to verify everything with the pic Datasheet !!! ;)
Yes, I have had some interesting output signals on outputs that were not specified in the program!

Acetronics2
- 21st September 2010, 14:44
Done that, but didn't understand it (new to this stuff).


CMCON0 = %00000000 seems to be accepted by PBP and the chip (still) works.

Boooooooooooooo ....:eek: the chip is really gentle with you ...

look a bit closer at the drawings and the corresponding config @ $8 p 53 ...

Alain

mikebike
- 21st September 2010, 15:47
I said that I don't understand this kind of data sheet...:p

Well, I think it says that .....000 only turnes the comp output off, but ....111 turnes the comp off as well. It doesn't matter much for this project, but my first project is battery operated and saving some current is welcome.


I guess it's nice to have a comparator integrated, but right now I would need an OP-amp instead!

Acetronics2
- 21st September 2010, 16:36
I said that I don't understand this kind of data sheet...:p
!

I do promise you lots of fun ...

Swedish hot nights ... I believed it was a bit different :rolleyes:

Alain

HankMcSpank
- 23rd September 2010, 16:32
Not sure if you're sorted, but as someone who recently struggled with this very type of same thing, here are the 12F683 commands towards helping you out ...

If using 10 bit AtoD, then right justify is required**.....

ADCON0.7 = 1 (right justifies your 12F683 AtoD result)

.....whereas with 8 bit AtoD, left justify is required....

ADCON0.7 = 0 (left justifies your 12F683 AtoD result)

and finally this one....

CMCON0 = 0 (turns the pesky 12f683 comparator off - of course if you need the PIC's comparator, then it's not pesky at all & this command should not be used!)

Good luck!



** and no matter how many times I read the datasheet with respect to left/right justifying, I end feeling like an amoeba being wafted around the bottom of a refuse tip pond.

Dave
- 23rd September 2010, 20:27
mikebike, Here is something I thru together last year for quick motor control pwn controller. Try it... The A/D works just fine.. You can get rid of the 64 samples...

Dave Purola,
N8NTA