#include // Global Variables unsigned long int TIME, TIME0, TIME1, dT; // Interrupt Service Routine void interrupt IntServe(void) { if (TMR1IF) { TIME = TIME + 0x10000; TMR1IF = 0; } if (CCP1IF) { TIME0 = TIME + CCPR1; CCP1IF = 0; } if (CCP2IF) { TIME1 = TIME + CCPR2; dT = TIME1 - TIME0; CCP2IF = 0; } } // Subroutines #include "lcd_portd.c" // Main Routine void main(void) { double time; double meters; TRISA = 0; TRISB = 0xFF; TRISC = 0x04; // capture every rising edge TRISD = 0; ADCON1 = 0x0F; // 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; // set up Capture2 for falling edges TRISC1 = 1; CCP2CON = 0x04; CCP2IE = 1; PEIE = 1; LCD_Init(); Wait_ms(100); TIME = 0; // turn on all interrupts GIE = 1; while(1) { time = 0.0000001 * dT; meters = 0.125 * 9.8 * time * time; LCD_Move(0,0); LCD_Out(dT, 10, 7); LCD_Move(1,0); LCD_Out(meters*1000000.0, 10, 6); } }