// INT_Pulse_Width.C // // Measure the width of a pulse to 100ns // // Interrupts: // Timer0: Measure time to 100ns // INT0: Time of the rising edge // INT1: Time of the falling edge // Global Variables const unsigned char MSG0[21] = "INT_Pulse_Width "; const unsigned char MSG1[21] = " "; unsigned long int TIME, T0, T1, dT; // Subroutine Declarations #include // Subroutines #include "lcd_portd.c" void interrupt IntServe(void) { if (TMR0IF) { TIME += 0x10000; TMR0IF = 0; } if (INT0IF) { T0 = TIME + TMR0; INT0IF = 0; } if (INT1IF) { T1 = TIME + TMR0; dT = T1 - T0; INT1IF = 0; } } // Main Routine void main(void) { unsigned char i; unsigned int j; double sec, inch; TRISA = 0; TRISB = 0xFF; TRISC = 0; TRISD = 0; TRISE = 0; ADCON1 = 0x0F; LCD_Init(); // initialize the LCD LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]); LCD_Move(1,0); for (i=0; i<20; i++) LCD_Write(MSG1[i]); Wait_ms(2000); LCD_Inst(1); // set up Timer0 for PS = 1 T0CS = 0; T0CON = 0x88; TMR0ON = 1; TMR0IE = 1; TMR0IP = 1; PEIE = 1; // Turn on INT0 interrupt INT0IE = 1; TRISB0 = 1; INTEDG0 = 1; // Turn on INT1 interrupt INT1IE = 1; TRISB1 = 1; INTEDG1 = 0; // turn on all interrupts GIE = 1; TIME = 0; while(1) { LCD_Move(0,0); LCD_Out(T0/1000, 6, 4); LCD_Move(0,8); LCD_Out(T1/1000, 6, 4); LCD_Move(1,0); LCD_Out(dT, 10, 7); } }