• AVR Freaks

Hot!Comparators XC8 PIC16F877A

Author
delfindelfin
Super Member
  • Total Posts : 272
  • 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

8 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 9200
    • 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 : 272
    • 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 : 9200
    • 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 : 272
    • 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 : 9200
    • 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 : 272
    • 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
    Senior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2010/09/01 12:35:40
    • Location: DM43BH
    • Status: offline
    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
    Senior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2010/09/01 12:35:40
    • Location: DM43BH
    • Status: offline
    Re: Comparators XC8 PIC16F877A 2019/05/20 09:31:11 (permalink)
    0
     
     
     
    post edited by DeutcheN - 2019/05/20 09:32:41
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5