PIC18F comparator problem

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi all,

I am testing the PIC18F internal comparator which is set as "one independent comparator with output".

This is the configuration code:

Rich (BB code):
  CMCONbits.CM0      = 1;
  CMCONbits.CM1      = 0;
  CMCONbits.CM2      = 0;
  CMCONbits.C1INV    = 0;
I connected Vin+ to ground and the Vin- to 5V. The output was approximately 250-350mV. When inverting the inputs (Vin+ to 5V and Vin- to ground) the output remained at the same voltage.

What am I oing wrong please? am I missing something in the code?
 

atferrari

Joined Jan 6, 2004
4,764
Look at the errata for your particular chip. I recall at least the 18F4520 having an issue with one of the configurations.

Could you post the outcome?
 

JohnInTX

Joined Jun 26, 2012
4,787
Thanks for the reply.
Since the inputs of the comparator are analogue, will the comparator still work?
Oops, sorry. You are correct. The comparator inputs are analog. The output IS digital but RA4 is not mapped to any ADCON1 bits. Set TRISA.4 to output for the comparator out and it should be good to go.
 
Last edited:

Thread Starter

Dritech

Joined Sep 21, 2011
901
Hi again,

In figure 22-1 from the PIC datasheet, it states that when selecting "one independent comparator with output" mode, pins AN1 and AN2 are set as digital. Does that mean these pins cannot be used for the ADC module?
 

atferrari

Joined Jan 6, 2004
4,764
Hi again,

In figure 22-1 from the PIC datasheet, it states that when selecting "one independent comparator with output" mode, pins AN1 and AN2 are set as digital. Does that mean these pins cannot be used for the ADC module?
I solve these questions looking at two places:

First, the description of the peripheric/s involved. Check all details.

Second, the description of what each pin does in the port description.

Do not reverse the order. That way, everything will be more evident to you.

It happened to me recently, (there is a post of mine somewhere around) where I was in doubt about one pin used with SPI. If used to receive (IN), it worked for the peripheric otherwise could be used as OUT for general purpose. (Maybe it was the other way round but hope you get the idea).
 

THE_RB

Joined Feb 11, 2008
5,438
...
In figure 22-1 from the PIC datasheet, it states that when selecting "one independent comparator with output" mode, pins AN1 and AN2 are set as digital. Does that mean these pins cannot be used for the ADC module?
No.

Assigning the ADC to a pin WILL measure the voltage on that physical pin with the ADC.

If the pin is a digital output (set by TRISx) it just reads the pin voltage while the pin is an output.

If the pin is a digital input (set by TRISx) it still reads the pin voltage.

If the pin is an input (set by TRISx) AND you set it to be an analogue pin (set by ANSEL etc) then it disables the digital input circuit. What that means is that the pin will consume less current if the voltage is somewhere undefined, like in the middle of 0-5v. Reducing the chance of digital latch up or other issues.

So you can set those pins to analogue, and use them properly with the ADC.

Remeber some PIC models won't give you individual control over which pins are set to analogue, you have to set them in a group. Later PICs you can individually set them.
 
Top