How to Use Analog Comparators in PIC16F877A (Mode-6)

The following code demonstrate how to use analog comparator module in mode-6. In this mode comparator-1 and comparator-2 positive (+) inputs are connected to the comparator voltage reference module. The external inputs are applied on the negative (-) input of comparators. You can map C1OUT and C2OUT on any general purpose IOs. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.





Code in mikroC

// comparator voltage reference formula
// when CVRR = 1
// CVREF = (VR<3:0>/24) x (CVRSRC)
// Four Inputs Multiplexed to Two Comparators
#define CMP_MODE    6
// user assign comparator o/p signals
sbit CMP1_Dir at TRISC.B0;
sbit CMP2_Dir at TRISC.B1;
sbit CMP1_Out at PORTC.B0;
sbit CMP2_Out at PORTC.B1;
// function proto-type
void CMP_Setting(void);
void main(void)
{
CMP1_Dir = 0;
CMP2_Dir = 0;
// init value
CMP1_Out = 0;
CMP2_Out = 0;
CMP_Setting();
while(1)
{
// user assign comparators o/p signals
CMP1_Out = CMCON.C1OUT;
CMP2_Out = CMCON.C2OUT;
}
}
void CMP_Setting(void)
{
//Comparators Reset
#if CMP_MODE == 0
CMCON.CM2 = 0;
CMCON.CM1 = 0;
CMCON.CM0 = 0;
// One Independent Comparator with Output
#elif CMP_MODE == 1
CMCON.CM2 = 0;
CMCON.CM1 = 0;
CMCON.CM0 = 1;
// Two Independent Comparators
#elif CMP_MODE == 2
CMCON.CM2 = 0;
CMCON.CM1 = 1;
CMCON.CM0 = 0;
// Two Independent Comparators with Outputs
#elif CMP_MODE == 3
CMCON.CM2 = 0;
CMCON.CM1 = 1;
CMCON.CM0 = 1;
// Two Common Reference Comparators
#elif CMP_MODE == 4
CMCON.CM2 = 1;
CMCON.CM1 = 0;
CMCON.CM0 = 0;
// Two Common Reference Comparators with Outputs
#elif CMP_MODE == 5
CMCON.CM2 = 1;
CMCON.CM1 = 0;
CMCON.CM0 = 1;
// Four Inputs Multiplexed to Two Comparators
#elif CMP_MODE == 6
CMCON.CM2 = 1;
CMCON.CM1 = 1;
CMCON.CM0 = 0;
// Comparators Off
#elif CMP_MODE == 7
CMCON.CM2 = 1;
CMCON.CM1 = 1;
CMCON.CM0 = 1;
#endif
// Comparator Input Switch bit
// 1 = C1 VIN- connects to RA3/AN3
//     C2 VIN- connects to RA2/AN2
// 0 = C1 VIN- connects to RA0/AN0
//     C2 VIN- connects to RA1/AN1
CMCON.CIS = 0;

// Comparator 1 Output Inversion bit
// 1 = C1 output inverted
// 0 = C1 output not inverted
CMCON.C1INV = 0;

// Comparator 2 Output Inversion bit
// 1 = C2 output inverted
// 0 = C2 output not inverted
CMCON.C2INV = 0;

// Comparator Voltage Reference Generator
// Comparator Voltage Reference Enable bit
// 1 = CVREF circuit powered on
// 0 = CVREF circuit powered down
CVRCON.CVREN = 1;
// Comparator VREF Output Enable bit
// 1 = CVREF is output on RA2/AN2/VREF-/CVREF pin
// 0 = CVREF is disconnected from RA2/AN2/VREF-/CVREF pin
CVRCON.CVROE = 1;
// Comparator VREF Range Selection bit
// 1 = 0 to 0.75 CVRSRC, with CVRSRC/24 step size
// 0 = 0.25 CVRSRC to 0.75 CVRSRC, with CVRSRC/32 step size
CVRCON.CVRR = 1;
// Comparator VREF Value Selection bits 0 . VR3:VR0 . 15
// When CVRR = 1:
// CVREF = (VR<3:0>/24) x (CVRSRC)
// When CVRR = 0:
// CVREF = (1/4) x (CVRSRC) + (VR3:VR0/32) x (CVRSRC)
CVRCON.CVR3 = 1;
CVRCON.CVR2 = 0;
CVRCON.CVR1 = 1;
CVRCON.CVR0 = 0;
}

Download Files

For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.

Comments

Popular posts from this blog

How to Program Interrupts in PIC16F877A

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)