// Capture1.C // // Capture the time of a rising edge on RC2 // // Interrupts // Timer0: Time measured to 100ns // Timer1: Time measured to 100ns // Capture1: Rising edge on RC2 // #include // Global Variables unsigned long int TIME, TIME1; const unsigned char MSG0[21] = "T1 Capture1 "; // Interrupt Service Routine void interrupt IntServe(void) { if (TMR0IF) { TMR0 = -10000; RC0 = !RC0; TMR0IF = 0; } if (TMR1IF) { TIME = TIME + 0x10000; TMR1IF = 0; } if (CCP1IF) { TIME1 = TIME + CCPR1; CCP1IF = 0; } } // Subroutines #include "lcd_portd.c" // Main Routine void main(void) { unsigned int i; TRISA = 0; TRISB = 0xFF; TRISC = 0x04; // capture every rising edge TRISD = 0; ADCON1 = 0x0F; // set up Timer0 for PS = 1 T0CS = 0; T0CON = 0x88; TMR0ON = 1; TMR0IE = 1; TMR0IP = 1; PEIE = 1; // set up Timer1 for PS = 1 TMR1CS = 0; T1CON = 0x81; TMR1ON = 1; TMR1IE = 1; TMR1IP = 1; PEIE = 1; // set up Capture1 for rising edges TRISC2 = 1; CCP1CON = 0x05; CCP1IE = 1; PEIE = 1; LCD_Init(); LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]); Wait_ms(1000); TIME = 0; // turn on all interrupts GIE = 1; while(1) { LCD_Move(0,0); LCD_Out(TIME + TMR1, 10, 7); LCD_Move(1,0); LCD_Out(TIME1, 10, 7); } }