PIC18F comparator problem

Discussion in 'General Electronics Chat' started by Dritech, Apr 26, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

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

    This is the configuration code:

    Code ( (Unknown Language)):
    1.   CMCONbits.CM0      = 1;
    2.   CMCONbits.CM1      = 0;
    3.   CMCONbits.CM2      = 0;
    4.   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?
     
  2. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    761
    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?
     
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi,

    Thanks for the reply.

    I googled errate PIC18F4550 and found this PDF.

    http://ww1.microchip.com/downloads/en/DeviceDoc/80248A.pdf

    Apparently the issue is with the Capture/Compare and not with the comparator.
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    Is the TRIS bit set to '0' for output? See '*' at the bottom of Fig 22.1 COMPARATOR I/O Modes.

    Good luck.
     
    Last edited: Apr 26, 2014
    Dritech likes this.
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the reply.

    Since the inputs of the comparator are analogue, will the comparator still work?
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    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: Apr 26, 2014
  7. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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?
     
  8. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    761
    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).
     
  9. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
    Dritech likes this.
Loading...