PDA

View Full Version : ADC problem



NacNud
- 14th December 2004, 19:40
Hello,

First I want to say that this is a nice forum finding solutions for PBP.

Here is my queston about an Analog to digital conversion.
I want to put an conversion in the variable word_temp_int, but this ain't seem to work. Every result of the AD conversion is 0.
Using a PIC16F870.

This code is in my interrupt routine, the routine is 500 ms. The varaiable word_wachttijd_aantal is decreased with every interrupt.



if (word_wachttijd_aantal == 12) and (byte_status == 1) then
adcon0.0 = 1 ' AD converter on
endif
if (word_wachttijd_aantal == 11) and (byte_status == 1) then
word_pot_adc = 0 ' reset value potmeter
adcon0.2 = 1 ' start AD conversion
endif
if (word_wachttijd_aantal < 11) and (byte_status == 1) then
if adcon0.2 == 0 then ' test AD conversion ready
word_temp_int.highbyte = ADRESH ' store AD value
word_temp_int.lowbyte = ADRESL ' store AD value
word_pot_adc = word_pot_adc * 9 ' some calculations
word_pot_adc = word_pot_adc + word_temp_int
word_pot_adc = word_pot_adc / 10
endif
adcon0.2 = 1 ' start new AD conversion
endif


The AD converter is put on when word_wachttijd_aantal is equal to 12.
500 ms later the AD converter is started when word_wachttijd_aantal is equal to 11.
500 ms later the AD converter is stored in word_pot_adc because word_wachttijd_aantal is less than 11 and adscon0.2 is equal to 0 a long time ago.
After the AD value is stored in word_pot_adc a new AD conversion is started.

If I put de code untherneath here at the same place it works fine.


test:
adcon0.0 = 1 ' AD converter on
pause 500 ' wait 500ms is
adcon0.2 = 1 ' start AD conversion
ADC:
pause 5 ' wait 5ms aquisition time
if adcon0.2 = 1 Then ADC ' test if AD conversie is ready
word_pot_adc.highbyte = ADRESH ' store AD value
word_pot_adc.lowbyte = ADRESL
adcon0.0 = 0 ' AD converter off
goto test

mister_e
- 14th December 2004, 20:02
what about if you put an PAUSE 50 after starting the analog to digital conversion. This will give the PIC enough time to convert and store it to your var.

BTW wich PIC are you using ?

if nothing work, can you post/attach your whole code?

NacNud
- 14th December 2004, 21:07
I have found the answer myself. The if statements where more than 4 levels deep. Thx mister_e for your reply.

mister_e
- 15th December 2004, 02:27
I have found the answer myself. The if statements where more than 4 levels deep

kinda, but look a this part of your code really closer



if (word_wachttijd_aantal < 11) and (byte_status == 1) then
if adcon0.2 == 0 then ' test AD conversion ready
word_temp_int.highbyte = ADRESH ' store AD value
word_temp_int.lowbyte = ADRESL ' store AD value
word_pot_adc = word_pot_adc * 9 ' some calculations
word_pot_adc = word_pot_adc + word_temp_int
word_pot_adc = word_pot_adc / 10
endif
adcon0.2 = 1 ' start new AD conversion
endif


in clear if you A/D conversion is not ready you begin a new one...
Simple to modify...



if (word_wachttijd_aantal < 11) and (byte_status == 1) then
while ADCON0.2 'wait until AD conversion is ready
pause 5
Wend
word_temp_int.highbyte = ADRESH ' store AD value
word_temp_int.lowbyte = ADRESL ' store AD value
word_pot_adc = word_pot_adc * 9 ' some calculations
word_pot_adc = word_pot_adc + word_temp_int
word_pot_adc = word_pot_adc / 10
adcon0.2 = 1 ' start new AD conversion
endif


IF THEN statement don't use stacks... SO you can use as many level you want... SUBs use stacks