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