Hi, Daniel
I generally use TMR1 and it's interrupt, reading Timer1 ( as free running timer ) ...
just read it @ each interrupt ... difference from previous read ... gives the period.
@ 20 Mhz max resolution is 200ns which is quite enough ...
Alain
Hi, Daniel
I generally use TMR1 and it's interrupt, reading Timer1 ( as free running timer ) ...
just read it @ each interrupt ... difference from previous read ... gives the period.
@ 20 Mhz max resolution is 200ns which is quite enough ...
Alain
Last edited by Acetronics2; - 17th November 2009 at 09:07.
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
Hi Daniel, you can also use the timer1 as an asynchronous counter, and connect your encoder directly to portB.6 (pin 12) and with two words variable one for the count one for the delay you will govern the counting process. You can also set (using DT interrupt) an overflow routine which will tell you when the setted delay time is too much.
All the time you will call the subroutine Get_Count the word Counter will contain the count from time0 and time0+delay. Setting delay appropriatly the counter could give you the speed without any additional maths.
Al.
Code:Counter var word Delay var word Get_Count: TCON1 = 6 TMR1L = 0 TMR1H = 0 TCON1 = 7 Pauseus Dalay TCON1 = 6 Counter.Byte0 = TMR1L Counter.byte1 = TMR1H Return
All progress began with an idea
I posted this at least once before, but here it is again;
Code:INCLUDE "DT_INTS-18.bas" ; Base Interrupt System ASM INT_LIST macro INT_Handler TMR0_INT, ReadTachs, ASM, yes endm INT_CREATE ENDASM Goto OverInt ;----------------------------------------------------------------- Asm ReadTachs movff PreloadH,TMR0H ; Preload depends on clk speed movff PreloadL,TMR0L infsnz MasterClock ; 16 bit timer used for housekeeping incf MasterClock + 1 btfsc TPE,0 ; Tells tach routine to stop bra DoneForNow ;CheckTach infsnz TachClock ; Tells tach counters how long to count 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 PORTB,0,0 movwf Temp,0 ; Save VAR so can't change between compare and save xorwf OldPortB,0,0 movwf changedB,0 movff Temp,OldPortB movf PORTC,0,0 movwf Temp,0 xorwf OldPortC,0,0 movwf changedC,0 movff Temp,OldPortC movf PORTD,0,0 movwf Temp,0 xorwf OldPortD,0,0 movwf changedD,0 movff Temp,OldPortD Tach1 btfss changedB,0 bra Tach2 infsnz Tach1Counter incf Tach1Counter+1 Tach2 btfss changedB,1 bra Tach3 infsnz Tach2Counter incf TachCounter+1 Tach3 btfss changedB,2 bra Tach4 infsnz TachCounter incf TachCounter+1 Tach4 btfss changedB,3 bra Tach5 infsnz Tach4Counter incf Tach4Counter+1 Tach5 btfss changedB,4 bra Tach6 infsnz Tach5Counter incf Tach5Counter+1 Tach6 btfss changedB,5 bra Tach7 infsnz Tach6Counter incf Tach6Counter+1 Tach7 btfss changedC,0 bra Tach8 infsnz TachCounter incf TachCounter+1 Tach8 btfss changedD,5 bra Tach9 infsnz Tach8Counter incf Tach8Counter+1 Tach9 btfss changedD,4 bra DoneForNow infsnz Tach9Counter incf Tach9Counter+1 DoneForNow bcf INTCON,2 ; Clear the TIMER0 interrupt flag INT_RETURN ENDASM
Set the PRELOAD value for the interrupt period (must be shorter than the shortest tach half-cycle). Set the TachCounter for how many interrupts you want to count over (currently 1000). Clear all the TachCounters and set TPE to 0. Periodically check to see if TPE is set. If so, copy the TachCounters to other variables, zero the variables and start over. All done in the background and does a nice job of averaging - something you will need if there is any kind of noise present.
Charles Linquist
Thank you guys!!! I'll try the examples you gave me... To tell you the truth, this is my first time with interrupts, so, wish me luck..
Daniel.
Bookmarks