Download - 16f877 Tachometer WORKING

Transcript

/* * SIMPLE SERIAL FREQUENCY METER * * this program writes one time per second to the RS232 communication line, * the frequency of the input signal on the RB0 pin * * PIC16F84A * 16 Mhz crystal, HS clock * * PORTB.0, in : counter input * PORTB.1, out : RS232 tx * PORTB.2, in : RS232 rx * * Author : Bruno Gavand, november 2005 * see more details on www.micro-examples.com * */ /* * RAM variables */ sbit LCD_RS at RB5_bit; sbit LCD_EN at RB6_bit; sbit LCD_D4 at RB1_bit; sbit LCD_D5 at RB2_bit; sbit LCD_D6 at RB3_bit; sbit LCD_D7 at RB4_bit; sbit sbit sbit sbit sbit sbit LCD_RS_Direction LCD_EN_Direction LCD_D4_Direction LCD_D5_Direction LCD_D6_Direction LCD_D7_Direction at at at at at at TRISB5_bit; TRISB6_bit; TRISB1_bit; TRISB2_bit; TRISB3_bit; TRISB4_bit; // number of RB0 transition // number of timer0 overflowsunsigned long cntr ; unsigned int ovrflw ; char str[8] ; char alrm[8]; unsigned int rpm; unsigned int mino;/* * * * * * *interrupt routine called 4000000/256 times by seconds : the timer TMR0 is increased each 4 clock cycles (quartz frequency is 16 Mhz), and overflows when reseting from 255 to 0, calling the interrupt procedure with bit T0IF set also called on each RBO transition, with bit INTF set */voidinterrupt(void) { if(INTCON.INTF) {/* * RB0 interrupt */ cntr++; // inc. transition counter INTCON.INTF = 0 ; // clear interrupt flag to enable next c all } if(INTCON.T0IF) { /* * TIMER 0 overflow */ ovrflw++ ; // inc. overflow counter INTCON.T0IF = 0 ; // clear interrupt flag to enable next c all on overflow } } void alarm(){ Sound_Play(4000, 300); lcd_out(2,1," "); lcd_out(2,1,"Warning !!!"); delay_ms(70); sound_play(4000,300);} void main() { ADCON1=0x06; Lcd_Init(); Lcd_Cmd(_LCD_CURSOR_off); Lcd_Cmd(_LCD_CLEAR); Sound_Init(&PORTB, 7); TRISC=0; PORTC=0; TRISA=0b1111111; TRISB=0b00000001; PORTB.f7=0; // RB0 interrupt pin as input OPTION_REG = 0b11011000 ; // no prescaler Sound_Play(4000, 70); delay_ms(50); Sound_Play(4000, 70); lcd_out(1,1,"Welcome.."); delay_ms(200); lcd_out(2,1,"Ahmed M. Hassan"); delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); lcd_out(1,1,"ENGINE RPM"); lcd_out(2,1,"RPM ALARM"); mino=6000; inttostr(mino,alrm);ltrim(alrm);lcd_out(2,11,alrm);for(;;){ cntr = 0 ; ovrflw = 0 ; rpm=0;// clear counterswhile (PORTA.f0==0) { Sound_Play(3000, 60);mino=mino+100; if (m ino>6000) {mino=6000;}inttostr(mino,alrm);ltrim(alrm);lcd_out(2,11,alrm);delay_m s(200);} while (PORTA.f1==0) { Sound_Play(3000, 60);mino=mino-100;if (mi no=mino) {alarm();} else {PORTB.f7=0;lcd_out(2,1," ");lcd_out(2,1,"RP M ALARM");inttostr(mino,alrm);ltrim(alrm);lcd_out(2,11,alrm);} } }