You can setup one single ended comparator with RA1 & RA2, then use
RA0 or RA3 for A/D inputs.
Code:
@ 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.
Bookmarks