PDA

View Full Version : 16F876 ADC problem



Douwe
- 6th June 2006, 00:35
Hallo,

I have an problem with measure two values with the 16F876.
First i measure current (stroom) with 3,50 Volt on AN3, the value
then is correct and works fine.
Then i program thereafter basic for reading the voltage of an solar panel.
(zonnepaneel)
I use now the 5 Volt as Vref (no use of AN3)
The reading of the current from the first part is now different and not correct,
it changes to about 10% to 15 % higher.......
I have inserted pauses on different places etc. but have the same problem.
What are i doing wrong ???

this is de code i wroted:


================================================
' Connect analog input to channel-0 (RA0)
' Meet de stroom.

meten: ADCON1 = %01000001 ' Set PORTA analog and LEFT justify result
' and make Vref on AN3 = 3.50 Volt.
ADCON0 = %11000001 ' Configure and turn on A/D Module RA0
ADCON0.2 = 1 ' Start conversion (bit 2 = 1)
Pause 50 ' Wait .5 second
If ADCON0.2 = 1 Then meten ' Wait for low on bit-2 of ADCON0, conversion finished
adval = ADRESH ' Move high byte of result to adval
dummy = adval * 240 ' scaling
adval = div32 100 ' scaling
st1 = abs adval ' max. waarde is 600 (600 mA)
goto display ' Display current.

' Meet de spanning van zonnepaneel.

meetpnl: ADCON1 = %01000000 ' Reset AN3 Vref. (is nu weer 5 Volt)
ADCON0 = %11001001 ' Configure and turn on A/D Module RA1
ADCON0.2 = 1 ' Start conversion (bit 2 = 1)
Pause 50 ' Wait .5 second
If ADCON0.2 = 1 Then meetpnl ' Wait for low on bit-2 of ADCON0, conversion finished
adval = ADRESH ' Move high byte of result to adval
dummy = adval * 976 ' x 400 en 32 bits result in dummy lees bldz. 33
adval = div32 1000 ' : 255 en 32 bits divide scaling 150/255
adval = abs adval ' max. waarde is 400 (40 Volt)
pnl1 = adval /10 ' eerste digit scaling
pnl2 = adval // 10 ' de rest.
goto display ' Display Solar Voltage.

Douwe
- 7th June 2006, 23:18
Hallo,

I have an problem with measure two values with the 16F876.
First i measure current (stroom) with 3,50 Volt on AN3, the value
then is correct and works fine.
Then i program thereafter basic for reading the voltage of an solar panel.
(zonnepaneel)
I use now the 5 Volt as Vref (no use of AN3)
The reading of the current from the first part is now different and not correct,
it changes to about 10% to 15 % higher.......
I have inserted pauses on different places etc. but have the same problem.
What are i doing wrong ???

this is de code i wroted:


================================================
' Connect analog input to channel-0 (RA0)
' Meet de stroom.

meten: ADCON1 = %01000001 ' Set PORTA analog and LEFT justify result
' and make Vref on AN3 = 3.50 Volt.
ADCON0 = %11000001 ' Configure and turn on A/D Module RA0
ADCON0.2 = 1 ' Start conversion (bit 2 = 1)
Pause 50 ' Wait .5 second
If ADCON0.2 = 1 Then meten ' Wait for low on bit-2 of ADCON0, conversion finished
adval = ADRESH ' Move high byte of result to adval
dummy = adval * 240 ' scaling
adval = div32 100 ' scaling
st1 = abs adval ' max. waarde is 600 (600 mA)
goto display ' Display current.

' Meet de spanning van zonnepaneel.

meetpnl: ADCON1 = %01000000 ' Reset AN3 Vref. (is nu weer 5 Volt)
ADCON0 = %11001001 ' Configure and turn on A/D Module RA1
ADCON0.2 = 1 ' Start conversion (bit 2 = 1)
Pause 50 ' Wait .5 second
If ADCON0.2 = 1 Then meetpnl ' Wait for low on bit-2 of ADCON0, conversion finished
adval = ADRESH ' Move high byte of result to adval
dummy = adval * 976 ' x 400 en 32 bits result in dummy lees bldz. 33
adval = div32 1000 ' : 255 en 32 bits divide scaling 150/255
adval = abs adval ' max. waarde is 400 (40 Volt)
pnl1 = adval /10 ' eerste digit scaling
pnl2 = adval // 10 ' de rest.
goto display ' Display Solar Voltage.


In the program i checked the ADCON0.2 bit for low and if it is not low, jump to
label meten: thats the problem.
label meten: starts with initialize the AD converter again etc. thats no good, i can't explane why, but now i jump to ADCON0.2 = 1 (start conversion) and it works fine.

------------------------------------------------------------------------
summary, wrong code:


meten: ADCON1 = %01000001
ADCON0 = %11000001
ADCON0.2 = 1
Pause 50
If ADCON0.2 = 1 Then meten
etc.
etc.


Now, with good results:

meten: ADCON1 = %01000001
ADCON0 = %11000001
Again: ADCON0.2 = 1
Pause 50
If ADCON0.2 = 1 Then again
etc.
etc.
----------------------------------------------------------------------

Douwe (The Netherlands)

Melanie
- 8th June 2006, 00:31
It would be better like so...



meten:
ADCON0=%01000001 ' Set Fosc, Select Channel, Turn-On A/D
Pauseus 50 ' Wait for channel to setup
ADCON0.2 = 1 ' Start conversion
While ADCON0.2=1:Wend ' Wait for conversion

Douwe
- 8th June 2006, 19:38
It would be better like so...



meten:
ADCON0=%01000001 ' Set Fosc, Select Channel, Turn-On A/D
Pauseus 50 ' Wait for channel to setup
ADCON0.2 = 1 ' Start conversion
While ADCON0.2=1:Wend ' Wait for conversion


O.K. Melanie,
nice programming, and it works o.k. thanks for the tip.

Douwe.