PDA

View Full Version : 16F88 using comparators AND the adc



jpeakall
- 12th May 2005, 17:43
Hi All,

A brief outline of what I am working on:

The PIC will recieve a signal using the onboard comparators. I loop and look for the state of the comparator until it hits. Timer 1 is running and uses the intterupt for a timeout if the signal is not recieved.

Now what I want to do is use the ADC right after the comparator hits and measure the voltage on a cap to determine signal strength (its a 40kHz ultrasonic signal, amplified by an op amp and fed into the comparators) so as to position my sensor in the best location.

From reading the spec sheets and browsing the archive on the 'F88, I gather that there are issues using both the adc and the comparators at the same time. Before I pound my head silly, I thought I'd run it past y'all and see if this is what I should do:

init the comps
wait for signal
disable the comps
enable the adc
get the signal

Or, is it possible to have to comps turned on and the the adc turned on, using only of course the channels not used by the comps?

As always, thanks!

Jonathan

Bruce
- 12th May 2005, 22:49
You can setup one single ended comparator with RA1 & RA2, then use
RA0 or RA3 for A/D inputs.



@ DEVICE HS_OSC, CCPMX_ON, MCLR_OFF, LVP_OFF, WDT_OFF
' CCPMX_ON = PWM ON B.3, CCPMX_OFF = PWM ON B.0

DEFINE OSC 20
DEFINE HSER_BAUD 19200

Comp VAR BYTE
AD VAR WORD
Symbol COUT = CMCON.7 ' Comparator C2OUT bit

Init:
ANSEL = %00001111 ' RA0 to RA3 A/D function
ADCON0 = %10000001 ' Channel 0, A/D Frc osc, enable A/D module
ADCON1 = %10000000 ' Right justify, Vref = AVdd/AVss
TRISA = %00001111 ' All inputs
CMCON = %00100101 ' Single ended comparator inputs on RA1/RA2 C2OUT inverted

Main:
ADCON0.2 = 1 ' Start A/D conversion
While ADCON0.2 = 1 ' Wait for A/D conversion to complete
WEND
AD = (ADRESH << 8) | ADRESL
Comp = COUT
HSEROUT ["A/D in = ",DEC AD,13,10,"Comp out = " , DEC Comp,13,10]
PAUSE 1000
GOTO Main
END
For this example I fixed RA2 at 2.5V through a 1K voltage divider. RA1 is
the analog signal input to the comparator.

RA0 is configured as an A/D input. The analog input signal is wired to both
RA1 comparator & RA0 A/D input to display the analog value that flips C2OUT.

Used a 5K pot for the analog signal in.

jpeakall
- 13th May 2005, 01:08
Bruce,

Once again thanks for the help. Got it all working a treat. Now to get my interrupt code all smoothed out...

Jonathan