// Tach.C // Measure the period of a square wave on RC0 // Interrupts // Timer0: Time accurate to 100ns // Timer1: Interrupt on rising edge of RC0 // Timer2: Output a 1kHz test signal on RC1 // Global Variables const unsigned char MSG0[21] = "Tach "; const unsigned char MSG1[21] = "sec "; unsigned long int TIME; unsigned long int T2, T1; unsigned long int PERIOD; // Subroutine Declarations #include // Subroutines #include "lcd_portd.c" // Interrupt Service Routine void interrupt IntServe(void) { if (TMR0IF) { TIME = TIME + 0x10000; TMR0IF = 0; } if (TMR1IF) { TMR1 = -1; T2 = T1; T1 = TIME + TMR0; PERIOD = T1 - T2; TMR1IF = 0; } if (TMR2IF) { RC1 = !RC1; TMR2IF = 0; } } // Main Routine void main(void) { unsigned char i; unsigned int Hz; 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(100); // set up Timer0 for PS = 1 T0CS = 0; T0CON = 0x88; TMR0ON = 1; TMR0IE = 1; TMR0IP = 1; PEIE = 1; // set up Timer1 for PS = 1, input = RC0 T1CON = 0x81; TMR1ON = 1; TMR1IE = 1; TMR1IP = 1; PEIE = 1; TMR1CS = 1; TRISC0 = 1; // set up Timer2 for 0.5ms T2CON = 0x4D; PR2 = 124; TMR2ON = 1; TMR2IE = 1; TMR2IP = 1; PEIE = 1; // turn on all interrupts GIE = 1; TIME = 0; while(1) { LCD_Move(0,5); LCD_Out(TIME + TMR1, 9, 7); LCD_Move(1,5); LCD_Out(PERIOD, 9, 7); } }