
Comparator Peripheral Labs
EXAMPLE 4-5:
INITIALIZE CODE FOR COMPARATOR LAB 3
//Configure RC0,RC1,RC2 and RC3 as digital outputs
ANSEL = 0b00001111;
PORTC = 0;
TRISC0 = 0;
TRISC1 = 0;
TRISC2 = 0;
TRISC3 = 0;
//Initialize PORTA pin connected to C12IN0-
TRISA1 = 1;
//Make C1OUT pin an output
TRISA2 = 0;
//Configure the Comparator 1 as follows:
//Turn on comparator 1
C1ON = 1;
//Make C1OUT available externally
C1OE = 1;
//Connect the non-inverting reference to CVREF
C1R = 1;
//Connect the inverting reference to C12IN0-
C1CH0 = 0;
C1CH1 = 0;
//Configure the CVREF as follows:
//Route CVREF output to Comparator 1 non-inverting reference
VRCON = 0;
//Enable the 0.6V fixed reference voltage
VP6EN = 1;
//Configure Timer1 as follows
//Make T1CKI and input
TRISA5 = 1;
T1CON = 0;
//Select T1CKI as Timer1's clock source
TMR1CS = 1;
//Initialize the 16-bit Timer1 register pair to 0
TMR1H = 0;
TMR1L = 0;
//Turn on Timer1
TMR1ON = 1;
//Set up Timer0 as follows:
//Use FOSC/4 for Timer0 Clock Source
OPTION = 0;
T0CS = 0;
//assign the prescaler to TMR0
PSA = 0;
//set up prescaler for 1:256
PS0 = 1;
PS1 = 1;
PS2 = 1;
//Enable Timer0 Interrupts
T0IE = 1;
//Clear the Timer0 overflow interrupt flag
T0IF = 0;
//Preload TMR0 with 10 to keep overflow period
//less than Timer1 overflow period
TMR0 = 10;
//Enable global interrupts
GIE = 1;
? 2009 Microchip Technology Inc.
DS41369A-page 73