// Define LCD module connections.
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
// End LCD module connection definition
// Define Messages
unsigned long noOfOverflows = 0, startTime = 0, endTime = 0, pulse_ticks = 0, rpm = 0, rps = 0, oldVal = 0;
char count = 0, display = 0, sendTime[23], sRPM[23], msg[] = "CPM = ";
double rpm2 = 0.0;
void Interrupt(){
if(TMR1IF_bit){
noOfOverflows++;
//counts 65536 before each overflow
TMR1IF_bit = 0; //Fosc = 8 MHz, Timer OSC = 8 MHz / 4 = 2 MHz //TMR1 T = 1 / 2 MHz = 0.5 us
//0.5 us = 1 tick, 32767.5 ticks in 1 sec
} //1966050 ticks in 60 sec
if(CCP1IF_bit){
count++;
if(count == 1){
CCP1IF_bit = 0;
startTime = CCPR1; //(65536 * noOfOverflows) + CCPR1;
noOfOverflows = 0;
}
if(count == 2){
CCP1IE_bit = 0;
CCP1IF_bit = 0;
display = 1;
}
}
}
void main() {
TRISC = 0xFF;
PORTC = 1;
T1CON = 0b10001000;
CCP1CON = 0b00000101;
INTCON = 0xC0;
TMR1IE_bit = 1;
TMR1ON_bit = 1;
CCP1IE_bit = 1;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF);
while(1) { // Cursor off
/*Lcd_Out(1,4,message1); // Write message1 in 1st row
Lcd_Out(2,1,message2); // Write message2 in 2nd row
Delay_ms(1000); // Wait for 1 sec
Lcd_Cmd(_LCD_CLEAR); // Clear display
Delay_ms(1000); // Wait for 1 sec*/
if(display){
endTime = (65536 * noOfOverflows) + CCPR1; //On second CCP1IF save time to endTime
endTime = endTime - startTime; //Take difference of time.
rpm = 120e6 / endTime;
rps = rpm / 60;
//rpm2 = (float)rpm;
noOfOverflows = 0;
if(oldVal != rpm) {
LongToStr(rpm, sRPM);
LCD_Out(2,7, " ");
LCD_Out(2,7, sRPM);
oldVal = rpm;
}
endTime = 0;
startTime = 0;
count = 0;
display = 0;
CCP1IE_bit = 1;
}
} // Infinite Loop
}