// Compare2.C // // Use Timer1 compare to produce a 10ms pulse on RC0 // whenever RB0 goes high #include // Global Variables unsigned long int TIME; const unsigned char MSG0[21] = "Comapre2.C "; unsigned int EDGES; // Interrupt Service Routine // Interrupt Service Routine void interrupt IntServe(void) { if (INT0IF) { RC0 = 1; EDGES += 1; CCPR1 = TMR1 + 12500; // 10ms with PS = 8 INT0IF = 0; } if (TMR1IF) { TIME = TIME + 0x10000; TMR1IF = 0; } if (CCP1IF) { RC0 = 0; CCP1IF = 0; } } // Subroutines #include "lcd_portd.c" // Main Routine void main(void) { unsigned int i; TRISA = 0; TRISB = 0xFF; TRISC = 0; TRISD = 0; ADCON1 = 0x0F; LCD_Init(); LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]); Wait_ms(1000); TIME = 0; // set up INT0 for rising edge INTEDG0 = 1; INT0IE = 1; TRISB0 = 1; // set up Timer1 for PS = 8 TMR1CS = 0; T1CON = 0xB1; TMR1ON = 1; TMR1IE = 1; TMR1IP = 1; PEIE = 1; // set up Timer1 Compare for no change CCP1CON = 0x0A; CCP1IE = 1; PEIE = 1; // turn on all interrupts GIE = 1; while(1) { LCD_Move(0,0); LCD_Out(TIME + TMR1, 10, 7); LCD_Move(1,0); LCD_Out(EDGES, 4, 0); } }