// Freq.C // // Measure the frequency of a signal on RC0 // Measuring as cycles per second // Output a 1kHz square wave on RC1 as a test signal // Timer0 interrupts every 1 second // Timer1 is a counter. Counts edges on RC0 // Global Variables const unsigned char MSG0[21] = "Freq.C "; const unsigned char MSG1[21] = "Hz "; unsigned int T0, T1, T2; // Subroutine Declarations #include // Subroutines #include "lcd_portd.c" // High-priority service void interrupt IntServe(void) { if (TMR0IF) { // interrupt every 1 second TMR0 = -39250; T0 += 1; T2 = T1; T1 = TMR1; TMR0IF = 0; } if (TMR1IF) { TMR1IF = 0; } if (TMR2IF) { RC1 = !RC1; TMR2IF = 0; } if (TMR3IF) { TMR3IF = 0; } } // Main Routine void main(void) { unsigned char i; unsigned int Hz; TRISA = 0; TRISB = 0; 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 = 257 T0CS = 0; T0CON = 0x87; TMR0ON = 1; TMR0IE = 1; TMR0IP = 1; PEIE = 1; // set up Timer1 for PS = 1 T1CON = 0x81; TMR1ON = 1; TMR1IE = 0; TMR1IP = 0; PEIE = 0; 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; TMR0 = 0; TMR1 = 0; T0 = 0; T1 = 0; T2 = 0; while(1) { Hz = T1 - T2; LCD_Move(0,8); LCD_Out(T0, 5, 0); LCD_Move(1,8); LCD_Out(Hz, 5, 0); } }