Vous êtes sur la page 1sur 2

**********************************************************************

************
'
**********************************************************************
************
' *****
' *****
MOTORCYCLE TACHOMETER by Warren Schroeder July 5, 2008
' *****
ACCURATE OUTPUT FROM 0 TO 65500 RPM WITH 1RPM
RESOLUTION
' *****
MikroBASIC 7.2
' *****
Tested On EasyPIC5 with PIC18F4431 @ 32MHz (8MHz x
4PLL)
' *****
'
**********************************************************************
*************
'
**********************************************************************
*************
' *****
' *****
RPM Display on 16x2 LCD
' *****
' *****
This TACHOMETER Code is developed for 60 input pulses per
engine revolution
' *****
Output Range is 0 to 62535 RPM in 1RPM Resolution with
updates every second
' *****
' *****
Timer0 is set up as a 16-bit counter with T0CK1 RC3 catching
rising edges
' *****
CCP1 is set up for 62500us interrupts using the special
event feature
' *****
which resets Timer1 on Compare-Match
' *****
16 CCP1 Interrupts @ 62500us each = 1 Second
' *****
'
**********************************************************************
*************
'
**********************************************************************
*************
Dim
Dim
Dim
Dim
Dim
Dim
Dim

CCPR
Tally
RPM
TRPML
TRPMH
TRPM
RPMStr

as
as
as
as
as
as
as

Word Absolute
Byte
Word
Byte Absolute
Byte Absolute
Word Absolute
String[5]

$FBE

' CCPR1L:CCPR1H Pair as Word

$30
$31
$30

sub procedure interrupt()


TRPML = TMR0L
register
TRPMH = TMR0H
Tally = inc(Tally) And 15
If Tally = 0 Then
TMR0H = 0
TMR0L = 0
RPM = TRPM
End If

' capture Count; store in temp


' Mod16 counter
' if 1 second
' clear Timer0 Counter
' save RPM value for display

PIR1.CCP1IF = 0
end sub
sub procedure
T0CON
=
prescaler
TMR0H
=
TMR0L
=
TRISC.3 =
end sub

' clear interrupt flag

Timer0_As_Counter()
40
' Off; 16Bit; Counter; rising edge; no
0
0
1

' Clear Timer0 Count


' RC3 is T0CK1 Counter Input

sub procedure CCP1_As_Timer()


CCP1CON = 11
' CCP1 Compare Match Special Event
T1CON
= 48
' Timer1 Off; Prescaler=8 = 1us ticks (125ns x

8)

TMR1H
= 0
TMR1L
= 0
CCPR
= 62500
PIE1.CCP1IE = 1
PIR1.CCP1IF = 0
INTCON
= 192
end sub

' Clear Timer1


'
'
'
'

interrupt every 62500us


enable CCP1 interrupt
clear CCP1 interrupt flag
enable GIE and PEIE

main:
ANSEL0
ANSEL1
TRISA
TRISB
TRISC
TRISD
LATA
LATB
LATC
LATD

=
=
=
=
=
=
=
=
=
=

0
0
0
0
0
0
0
0
0
0

' disable adc's

Timer0_As_Counter()
CCP1_As_Timer()
LCD_CONFIG(PORTB,3,2,1,0,PORTB,4,0,5)
LCD_CMD(LCD_CURSOR_OFF)
Tally = 0
T0CON.TMR0ON = 1
T1CON.TMR1ON = 1

' reset timing tally


' start Timer0 counter
' start Timer1

While 1=1
delay_ms(50)
WordToStr(RPM, RPMStr)
LCD_OUT(1,1,RPMStr)
LCD_OUT_CP(" RPM")
Wend
end.

Vous aimerez peut-être aussi