PDA

View Full Version : Counting RPM's with Interrupt? Speed Limitations?



wdmagic
- 13th December 2012, 23:44
I have a PIC that controls a Geared Motor with the PWM output (Works great). It also has a LCD attached, I want to display a count for every RPM, then I also will have a AD setting for a RPM MAX COUNT, basically i can set how many rotations is the limit, it will run and count input pulses till it reaches the max limit then auto shut off motor. my question is can i use the interrupt to count pulses from a magnetic reed switch , max speed or RPM on motor is say 1500, so 1500 / 60 = Hertz
so 25hz, can the IRQ of a microprocessor handle 25 activations a second?

if not anyone have another way of doing this?

SteveB
- 14th December 2012, 01:43
Yes, a PIC is more than capable of interrupting at that frequency. The problem will be the reed switch.
If you use an un-bounced reed switch, you will likely catch a lot of bouncing, and the PIC will interrupt many times due to the bounce.
You might look into hall effect or light/optical methods of triggering the interrupt that would be less prone to bounce.

wdmagic
- 15th December 2012, 00:20
yea, i was going to use a debounce circuit, but i do have a surplus of IR emiter/detector interupters, I can use one of them. thanks

Charles Linquis
- 16th December 2012, 20:19
The following code can be used or modified to do what you want. Everything is done in the background.

The good thing about it, is that it can be used on any port - not just port B.
And it will watch a large number of tachs - not just 8 (of course, you have to expand the ASM section).
It does NOT need a debounce - as long as the sample interval is longer than the expected bounce time.

The sample interval is set by the TMR0 interrupt rate. The interrupt rate must be high enough to 'catch' every
HIGH and LOW of the input. So if the HIGH time of the input is 10 mSec and the LOW time of the input is 30 mSec,
then the interrupt must be faster than 10 mSec.

As written, the code will run for 1000 interrupt times, then quit. if you don't want it to stop, and only count trasitions, simply eliminate
the TACHCLOCK part.

(as written)

To start,

Clear the TachCounters
Clear TPE

In your main loop, check for TPE to be set. If it is,

Read the TachCounters
Clear the TachCounters
Clear TPE





INCLUDE "DT_INTS-18.bas"


ASM
INT_LIST macro
INT_Handler TMR0_INT, ReadTachs, ASM, yes

endm
INT_CREATE
ENDASM

Goto OverInt

'---[TMR0 interrupt handler]---------------------------------------------------

Asm
ReadTachs
movff PreloadH,TMR0H ; Preload depends on clk speed
movff PreloadL,TMR0L

btfsc TPE,0
bra DoneForNow


infsnz TachClock
incf TachClock + 1

movlw 0x03 - 1 ; Real value is 1000 = 0x3E8, but must have one less
cpfsgt TachClock + 1 ; to compare with greater than
bra TachRoutine
movlw 0xE8 - 1 ; Likewise, subtract one here, too.
cpfsgt TachClock
bra TachRoutine
bsf TPE,0

clrf TachClock
clrf TachClock + 1
bra DoneForNow

TachRoutine

movf PORTF,0,0
movwf Temp,0 ; Save VAR so can't change between compare and save
xorwf OldPortF,0,0
movwf changedF,0
movff Temp,OldPortF


Tach1
btfss changedF,0
bra Tach2
infsnz Tach1Counter
incf Tach1Counter+1

Tach2
btfss changedF,1
bra Tach3
infsnz Tach2Counter
incf Tach2Counter+1

Tach3
btfss changedF,2
bra Tach4
infsnz Tach3Counter
incf Tach3Counter+1
Tach4
btfss changedF,3
bra DoneForNow
infsnz Tach4Counter
incf Tach4Counter+1


DoneForNow

bcf INTCON,2 ; Clear the TIMER0 interrupt flag
INT_RETURN
ENDASM



OverInt:

TachCounter1=0:TachCounter2=0:TachCounter3=0:TachC ounter4=0

asm
movff PreloadH,TMR0H ; Preload depends on clk speed
movff PreloadL,TMR0L
bcf INTCON,2
endasm

TPE = 0

INT_ENABLE TMR0_INT

(your code here)

wdmagic
- 23rd December 2012, 03:10
Ok sorry thats way beyond me, fir right now i just need the single counter, as far as a tachometer, i can just use the pulsein and get a counter for that if i need it. thans everyone for there input, if anyone could show me how to enable those extra irq's, say I want to use all 3 INT's on a 18f4550 (portb 0,1,2)