البرنامج :
كود:
/*
Project: Contactless digital tachometer
Description: MCU: PIC16F877A- 4MHz clock
TIMER0 module is used as a 8-bit counter to
count external pulses arriving at RA4/T0CKI input
Eng.F.ABDELAZIZ
http://www.eeecb.com/vb/index.php
*/
// Define LCD module connections.
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connection definition
unsigned long RPM_Value;
#define RPM_SWITCH PORTD.B0
#define ON 1
#define OFF 0
// Define Messages
char message1[] = "Tachometer";
char *RPM = "00000 RPM";
void Display_RPM(unsigned long num){
RPM[0] = num/10000 + 48;
RPM[1] = (num/1000)%10 + 48;
RPM[2] = (num/100)%10 + 48;
RPM[3] = (num/10)%10 + 48;
RPM[4] = num%10 + 48;
Lcd_Out(2,4,RPM);
}
void main() {
TRISA.B4 = 1; // Inut at RA4/T0CKI
PORTA.B4=0;
TRISD.B0=0; //RPM Switch Output
PORTD.B0=0;
OPTION_REG=0b00101000; //Prescaler(1:1) , T0CKI=1 for counter mode
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,4,message1); // Write message1 in 1st row
do {
/* The counter will be active for 1 sec and
the number of pulses arrived during this interval will be recorded,
and multiplied by 60 to get the RPM . */
TMR0=0; //The counter will be active for 1 sec, start
Delay_ms(1000); // Wait for 1 sec
RPM_Value = TMR0*60;
Display_RPM(RPM_Value);
if(RPM_Value > 600) RPM_SWITCH = ON ; else RPM_SWITCH = OFF;
} while(1); // Infinite Loop
}