PDA

View Full Version : ADC question



schu4647
- 27th May 2006, 23:38
I am trying to read a voltage using the 16F88. The signal I am reading is a 180Hz PWM signal that is TTL level. No matter what I set my sample time to, I either get the voltage as 0 or 1024. I have tried adjusting the sample time at 50, 1111, and 5555 all with the same results. My thinking was 180 Hz would be about 5.5 ms. 5555 would be 5.5 ms. I even tried using an array to pull in 10 samples and take an average which didn't work either. Here is a couple programs I tried. Any suggestion would be appreciated. thanks.

Here was the array attempt:
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 1111 ' Set sampling time in uS
DEFINE OSC 4 ' 4MHz crystal
INCLUDE "modedefs.bas"
TRISA = %11111111 ' Set register A as input
ADCON1 = %10000010 ' Set up register output
fuel VAR WORD[10]
x VAR BYTE
average VAR WORD
average=0

loop:
average = 0
For x=1 TO 10
ADCIN 0,fuel[x] ' Read channel 0 to fuel
Next x

For x=1 TO 10
average=average+fuel[x]
Next x
average=average/10


SerOut PORTB.2,n9600,[1,#average" "]
GoTo loop

Here was not using the array:
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 1111 ' Set sampling time in uS
DEFINE OSC 4 ' 4MHz crystal
INCLUDE "modedefs.bas"
TRISA = %11111111 ' Set register A as input
ADCON1 = %10000010 ' Set up register output
fuel VAR WORD

loop:
ADCIN 0,fuel ' Read channel 0 to fuel


SerOut PORTB.2,n9600,[1,#average," "]
Pause 200
GoTo loop

keithdoxey
- 28th May 2006, 11:22
A raw PWM signal is either ON or OFF for varying amounts of time so therefore with a 5v source you will read either 0V or 5v depending on where you sample the signal.

To read an anlogue voltage the PWM signal would need to filtered with a resistor and capacitor to smooth it to an analogue voltage.

schu4647
- 28th May 2006, 14:15
That was kind of what I was thinking, but it has been 5 years since college now and I have forgotten how to do all that. I actually found a better way. I am using the pulsin command to measure the on time. Seems a lot more accurate and quicker. Thanks for the advice.