PDA

View Full Version : PIC16LF819 A/D problem



gavo
- 25th January 2010, 17:56
Having a small problem with the PIC16LF819 trying to measure a 3-5V on RA0. I can't seem to get the program to run into loop4 the program keeps seeing it in loop 5 with the measurement of 4.995V it can't seem to change/read it when the voltage drops to 4.7V or 4.3V, or even below 4 and 3V, any ideas why?,

My code looks like this:

Define OSC 4 ' Set Xtal Frequency
' ** Setup the Debug Defines **

' ** Declare the Variables **


Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 1 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

AD_Raw VAR WORD' 10-bit result of A/D conversion
AD_Result VAR WORD' Quantasized ADC result
Average VAR WORD' Variable for building up the average result
Samples VAR BYTE' Amount of samples taken
Volts VAR BYTE' Holds the Volts part of the result (only for display purposes)
Millivolts VAR WORD' Holds the Millivolts part of the result (only for display purposes)
adval VAR WORD' Create adval to store result


Quanta Con 1250

Adcon1 = 7
TRISA = %00000001
pause 100

goto start

START:

high portb.0
low portb.1
low portb.2

Average=0 ' Clear the Average variable befor use
For Samples=0 TO 9 ' We will take 100 samples of the ADC
ADCIN 0,AD_Raw ' Place the conversion of channel0 into AD_RAW
Average=Average+AD_Raw ' Build up the Average result
Next ' Close the loop
Average=Average/10 ' Calculate the average by dividing by the number of samples taken
AD_Result=(Average) */ Quanta' Quantasize the result
Adval=AD_Result

volts = ad_result / 1000
millivolts = ad_result // 1000

if volts = 0 then goto loop2
if volts = 1 then goto loop2
if volts = 2 then goto loop2
if volts = 3 then goto loop2
if volts = 4 then goto loop3
goto start

loop2:

high portb.0
high portb.1
low portb.2
pause 500
goto start

loop3:

if millivolts < 995 then goto loop4
if millivolts > 995 then goto loop5
goto loop3

loop4:

high portb.0
high portb.1
low portb.2
pause 500
goto start

loop5:

high portb.0
low portb.1
high portb.2
pause 500
goto start



END