Help in learning CMCON registers on PIC16f877A

Discussion in 'Embedded Systems and Microcontrollers' started by lloydi12345, Jan 2, 2011.

  1. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Hi, I would like to convert my experiment from more of electronics to more of computer engineering. I'm using external comparators and this time I would like to use internal comparators but the resources across the web is so limited for pic16f877a in C. Can I ask for a simple program or any guide so that I will base my program from it and modify it for my own? I'm using mikroC PRO as my PIC language.
     
    Last edited: Jan 2, 2011
  2. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    Here's a sample code I wrote. This makes use of the internal voltage reference of the PIC and checks the voltage at RA0 against this voltage.
    Code ( (Unknown Language)):
    1. //Compare a voltage at RA0 against internal voltage reference of 2.50V
    2.  
    3. void main() {
    4.      TRISA = 0xFF; //PORTA all inputs
    5.      TRISB = 0; //PORTB all outputs
    6.      PORTB = 0;
    7.      CMCON = 0b00000110; //Use internal voltage reference at "+" input
    8.                         //"-" input of C1 connected to RA0
    9.                         //Mode : 110
    10. /*Can also be written as:
    11.       CMCON.CIS = 0; //"-" input of C1 connects to RA0
    12.       CMCON.CM2 = 1; //Mode 110
    13.       CMCON.CM1 = 1;
    14.       CMCON.CM0 = 0;          
    15. */
    16.  
    17.       CVRCON = 0b10001000; //2.5v internal voltage reference
    18. /*Can also be written as:
    19.       CVRCON.CVREN = 1; //Enable internal voltage reference module
    20.       CVRCON.CVROE = 0; //Do not output reference voltage on RA2
    21.       CVRCON.CVRR = 0;
    22.       CVRCON.CVR3 = 1; //Set at 2.50v
    23.       CVRCON.CVR2 = 0;
    24.       CVRCON.CVR1 = 0;
    25.       CVRCON.CVR0 = 0;
    26. */
    27.  
    28.       delay_ms(500);
    29.      
    30.       while (1){
    31.             if (CMCON.C1OUT == 1) //Voltage < 2.50v
    32.                PORTB.F0 = 0; //Showing by clearing LED at RB0
    33.             if (CMCON.C1OUT == 0) //Voltage > 2.50v
    34.                PORTB.F0 = 1; //Showing by lighting LED at RB0
    35.       }
    36. }
    This is a table showing the reference voltage settings at VDD = 5.0v
    [​IMG]

    To fully understand the code, go through pages 137-144 of the datasheet.
    There are many modes for comparator setting. This is one. If you can set this, then you should be successful in setting the rest.

    Hope this helps.
    Tahmid.
     
    iccool and lloydi12345 like this.
  3. lloydi12345

    Thread Starter Member

    Aug 15, 2010
    103
    0
    Thanks Tahmid! You're so kind. Now I could start my program. I'll try this one in a while. :)
     
    Last edited: Jan 2, 2011
Loading...