Help in learning CMCON registers on PIC16f877A

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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:

Tahmid

Joined Jul 2, 2008
343
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.
Rich (BB code):
//Compare a voltage at RA0 against internal voltage reference of 2.50V

void main() {
     TRISA = 0xFF; //PORTA all inputs
     TRISB = 0; //PORTB all outputs
     PORTB = 0;
     CMCON = 0b00000110; //Use internal voltage reference at "+" input
                        //"-" input of C1 connected to RA0
                        //Mode : 110
/*Can also be written as:
      CMCON.CIS = 0; //"-" input of C1 connects to RA0
      CMCON.CM2 = 1; //Mode 110
      CMCON.CM1 = 1;
      CMCON.CM0 = 0;          
*/

      CVRCON = 0b10001000; //2.5v internal voltage reference
/*Can also be written as:
      CVRCON.CVREN = 1; //Enable internal voltage reference module
      CVRCON.CVROE = 0; //Do not output reference voltage on RA2
      CVRCON.CVRR = 0;
      CVRCON.CVR3 = 1; //Set at 2.50v
      CVRCON.CVR2 = 0;
      CVRCON.CVR1 = 0;
      CVRCON.CVR0 = 0;
*/

      delay_ms(500);
      
      while (1){
            if (CMCON.C1OUT == 1) //Voltage < 2.50v
               PORTB.F0 = 0; //Showing by clearing LED at RB0
            if (CMCON.C1OUT == 0) //Voltage > 2.50v
               PORTB.F0 = 1; //Showing by lighting LED at RB0
      }
}
This is a table showing the reference voltage settings at VDD = 5.0v


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.
 

Thread Starter

lloydi12345

Joined Aug 15, 2010
103
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.

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.
Thanks Tahmid! You're so kind. Now I could start my program. I'll try this one in a while. :)
 
Last edited:
Top