• AVR Freaks

Hot!Comparators XC8 PIC16F877A

Page: 12 > Showing page 1 of 2
Author
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
2019/05/18 18:24:24 (permalink)
0

Comparators XC8 PIC16F877A

Hello, Could you give some suggestions of how to learn to program comparators (Besides the PIC Datasheet) ? I don't understand exactly what they are and what applications may have

MPLAB X IDE v5.05
XC8 2.00
#1

24 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 9494
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/18 19:37:30 (permalink)
    +2 (2)
    delfindelfin
    Hello, Could you give some suggestions of how to learn to program comparators (Besides the PIC Datasheet) ? I don't understand exactly what they are and what applications may have

    https://en.wikipedia.org/wiki/Comparator
    #2
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/18 21:41:28 (permalink)
    0
    What does it mean POR in "POR Default Value"?

    MPLAB X IDE v5.05
    XC8 2.00
    #3
    1and0
    Access is Denied
    • Total Posts : 9494
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/18 21:58:05 (permalink)
    +1 (1)
    delfindelfin
    What does it mean POR in "POR Default Value"?

    Power On Reset.  POR default value is the initial value when the device power on.
    #4
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/18 22:03:38 (permalink)
    0
    So, technically, something simple like this should work: 

     
    // Including XC8 Compiler Library
    #include <xc.h>
     
    // Defining Oscillator Frequency
    #define _XTAL_FREQ 8000000
     
    // Setting Configuration Bits
    #pragma config FOSC = HS // Oscillator Selection bits (XT oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
    #pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits
    #pragma config CP = OFF // Flash Program Memory Code Protection bit
     
    void main(void) {

    //// Comparator Initialization //////////////////////////////////////////////

    TRISA0 = 1; // RA0 as Input PIN
    TRISA1 = 1; // RA1 as Intput PIN
    TRISA4 = 0; // RA4 as Output PIN

    CMCON = 0x00 ; // Setting CMCON as 0000 0000

    //// Main Function ///////////////////////////////////////////////////////////
    while(1)
    {


    }

    }
     

    But is not ..

    MPLAB X IDE v5.05
    XC8 2.00
    #5
    1and0
    Access is Denied
    • Total Posts : 9494
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/18 22:10:22 (permalink)
    +1 (1)
    Read your PIC datasheet to see what CMCON=0x00 does.  Hint: look at Figure 12-1.
    #6
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/19 04:30:46 (permalink)
    0
    I changed some things

     
    void main(void) {

    //// Comparator Initialization //////////////////////////////////////////////

    TRISA0 = 1; // RA0 as Input PIN (Vin-)
    TRISA3 = 1; // RA4 as Input PIN (Vin+)
    TRISA4 = 0; // RA4 as Output PIN (C1OUT)

    CMCON = 0x02 ; // Setting CMCON as 0000 0010
    ADCON1 = 0x04; // Selection of Analog and Digital Pins (0000 0100)

    //// Main Function ///////////////////////////////////////////////////////////
    while(1)
    {
    RA4 = C1OUT ; // LED ON


    }

    }
     

    post edited by delfindelfin - 2019/05/19 09:22:57

    MPLAB X IDE v5.05
    XC8 2.00
    #7
    DeutcheN
    Super Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2010/09/01 12:35:40
    • Location: DM43BH
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/05/20 09:31:06 (permalink)
    0
    This line is not necessary:
       RA4 = C1OUT;
     
     RA4 will change level as the output of the comparator changes.  You can test this per figure 12-2 in the datasheet.
     
     
    #8
    DeutcheN
    Super Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2010/09/01 12:35:40
    • Location: DM43BH
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/05/20 09:31:11 (permalink)
    0
     
     
     
    post edited by DeutcheN - 2019/05/20 09:32:41
    #9
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/07/15 17:50:50 (permalink)
    0
    Yes but I think the code is still wrong .. If you set CMCON to 0x02 you select Two independent comparators but without output .. That means the line RA4 = C1OUT it is necessary
     
    So if you want output in RA4 you need to use something like this 

     
     
     
    void main(void) {

    /// Comparator Initialization ///////////////////////////////////////////////
    TRISAbits.TRISA0 = 1; // RA0 as Input PIN (Vin-)
    TRISAbits.TRISA3 = 1; // RA3 as Input PIN (Vin+)
    TRISAbits.TRISA4 = 0; // RA4 as Output PIN (C1OUT)

    CMCONbits.CM2 = 0;
    CMCONbits.CM1 = 0;
    CMCONbits.CM0 = 1; // One Independent Comparator with Output

    ADCON1bits.PCFG3 = 0;
    ADCON1bits.PCFG2 = 1;
    ADCON1bits.PCFG1 = 0; // Selection of Analog and Digital Pins (0000 0100)
    ADCON1bits.PCFG0 = 0; // AN7,AN6,AN5,AN4,AN2 = D, AN3,AN1,AN0 = A

    /// Main Function ///////////////////////////////////////////////////////////
    while(1)
    {


    }
     
     
     

    Now I am selecting the mode "One Independent Comparator with Output", but the LED, doesn't turn on yet .. So, something continuous being wrong in the code 
    post edited by delfindelfin - 2019/07/15 17:55:04

    Attached Image(s)


    MPLAB X IDE v5.05
    XC8 2.00
    #10
    ric
    Super Member
    • Total Posts : 23207
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/07/15 18:10:23 (permalink)
    +1 (1)
    On a PIC16F877A, pin RA4 is an open collector output.
    It can only drive low, not high.
    Try turning your LED around, and connecting R1 to Vdd instead of ground.
    (This IS pointed out in the comparator section. Note 3 in the grey box.)

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    PStechPaul
    Super Member
    • Total Posts : 2348
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/07/16 01:53:42 (permalink)
    +1 (1)
    Probably a typo, but C1 and C2 are most likely 22 pF, not 22 uF.

     
    #12
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/07/16 11:43:28 (permalink)
    0
    PStechPaul
    Probably a typo, but C1 and C2 are most likely 22 pF, not 22 uF.


    Oh, yeah .. Although I think those values doesn't affect the simulation because the clock frequency is set inside "Edit Component"
     
    ric
    Try turning your LED around, and connecting R1 to Vdd instead of ground.

    Do I also need to set TRISAbits.TRISA4 as Input, right? Because I have already turned on my LED around, connected R1 to VDD and set TRISA4 as input, and the LED doens't turn on ..
    post edited by delfindelfin - 2019/07/16 12:15:10

    MPLAB X IDE v5.05
    XC8 2.00
    #13
    pcbbc
    Super Member
    • Total Posts : 1188
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/07/16 13:19:57 (permalink)
    +2 (2)
    delfindelfinDo I also need to set TRISAbits.TRISA4 as Input, right?

    Why in God’s name do you think you need to do that?
    If it’s driving an output signal (either high or low) it’s an OUTPUT.
    #14
    mbrowning
    Just a Member
    • Total Posts : 1458
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/07/16 13:38:02 (permalink)
    +1 (1)
    Why do you think you need to do that?

    Well, the arrow (diode symbol) is pointing IN to the PIC now, isn't it :)

    Oh well - there's always next year
    #15
    ric
    Super Member
    • Total Posts : 23207
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/07/16 13:38:49 (permalink)
    +1 (1)
    Confusion about data direction, and current direction...
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/07/16 17:19:59 (permalink)
    0
    Well, if I set it as an output, now it works fine, although I didn't understand very well the confusion between data direction and current direction .. Physically you can sink current, and still, it would be an output .. I guess it is because I don't know how Push-Pull Output works, neither Open Collector Outputs and how you make them inputs .. I guess I need to study that ..
     
    Edit.
    Another qustion, when I am using the mode "two comparators with output" .. I need to use  RA0, RA1, RA2, RA3 as analog inputs and RA4, RA5  as digital outputs .. But, I don't see a configuration that allows you to do that:
     
              AN7   AN6   AN5    AN4    AN3    AN2   AN1 AN0 VREF+ VREF-  C/R
    0000= A         A        A        A         A          A        A       A     VDD      VSS    8/0
    0001= A         A        A        A      VREF+   A        A       A     AN3       VSS    7/1
    0010= D         D        D       A          A         A         A       A    VDD       VSS    5/0
    0011= D         D        D       A       VREF+   A        A       A     AN3       VSS     4/1
    0100= D         D        D       D          A         D        A       A     VDD      VSS     3/0
    0101= D         D        A        A          A         A         A       A    VDD      VSS     6/0
    011x= D         D        D        D          D         D        D       D       -             -       0/0
    1000= A         A         A        A      VREF+ VREF-  A       A    AN3      AN2     6/2
    1001= D         D        A         A          A          A       A       A     VDD     VSS     6/0
    1011= D         D        A         A      VREF+ VREF-  A       A     AN3     AN2     4/2
    1100= D         D        D         A       VREF+ VREF- A       A     AN3     AN2     3/2
    1101= D         D        D         D      VREF+ VREF-  A      A      AN3    AN2     2/2
    1110= D         D        D         D          D          D        D      A    VDD     VSS     1/0
    1111= D         D        D         D       VREF+ VREF-  D      A     AN3     AN2     1/2


    post edited by delfindelfin - 2019/07/16 18:03:50

    MPLAB X IDE v5.05
    XC8 2.00
    #17
    ric
    Super Member
    • Total Posts : 23207
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/07/16 19:06:41 (permalink)
    +1 (1)
    delfindelfin
    Well, if I set it as an output, now it works fine, although I didn't understand very well the confusion between data direction and current direction ..

    If the PIC is driving the pin, it is an "output", regardless of which way the current is flowing.
    If something else is driving the PIC pin, then it is an "input", again regardless of which way the current is flowing.
    The TRIS setting controls if it is an input or an output.
    When it is an output (TRIS=0), then the LAT bit controls the voltage, and therefore the current direction.
    (High = out, Low = in)
     

    Physically you can sink current, and still, it would be an output ..

    Yes.

    I guess it is because I don't know how Push-Pull Output works, neither Open Collector Outputs and how you make them inputs .. I guess I need to study that ..

    "Push pull" means there are transistors to both Vss and Vdd, so the pin can be driven both high and low.
    "Open collector" (really should be called "open drain") means there is only a transistor to Vss, so the pin can only be driven down to ground, not up to supply.
     

    Edit.
    Another qustion, when I am using the mode "two comparators with output" .. I need to use  RA0, RA1, RA2, RA3 as analog inputs and RA4, RA5  as digital outputs .. But, I don't see a configuration that allows you to do that:
     

    You are confusing input and output again. That diagram is ONLY talking about inputs.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #18
    delfindelfin
    Super Member
    • Total Posts : 374
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/07/16 21:08:56 (permalink)
    0
    But, What does it mean driving? I don't understand that term .. Why Open Drain should be called Open Collector? The difference I see is that one uses a NPN transistor, the other a MOSFET
     
     
    post edited by delfindelfin - 2019/07/16 21:13:58

    Attached Image(s)


    MPLAB X IDE v5.05
    XC8 2.00
    #19
    ric
    Super Member
    • Total Posts : 23207
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Comparators XC8 PIC16F877A 2019/07/16 21:16:35 (permalink)
    +1 (1)
    "Driving" means the same thing as "controlling" or "outputting".
    It is the opposite of "Receiving", "inputting", "being driven by".
    This really is the most basic of electronic terms.
    I understand that English is not your native tongue, but you must get your head around this.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5