calculate time between pulses


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    OK, here you go.


    The interrupt period is 500uSec.

    All transitions in .5 seconds are added, since FanClock has a count value
    of 1000. 500E-9 X 1000 = .5 seconds.






    '------------------ Timer 0 interrupt handler-----------------------------------

    Asm
    ReadTachs

    movlw 0xEC ; Reload TMR0
    movwf TMR0H
    movlw 0x7E
    movwf TMR0L

    bcf INTCON,2 ; Clear the TIMER0 interrupt flag

    infsnz MasterClock
    incf MasterClock + 1 ; 16 bit Master system clock

    btfss TPE,0 ; Fan counter active gate
    bra CheckFans
    bra DoneForNow

    CheckFans
    infsnz FanClock
    incf FanClock + 1 ; 16 bit loop counter

    movlw 0x03 - 1 ; 1000 = 0x3E8, but must have one less
    cpfsgt FanClock + 1 ; to compare with greater than
    bra FanRoutine
    movlw 0xE8 - 1 ; Again, subtract one
    cpfsgt FanClock
    bra FanRoutine
    bsf TPE,0 ; Set the bit if completed 1000 loops
    clrf FanClock
    clrf FanClock + 1
    bra DoneForNow


    FanRoutine

    movf PORTB,0,0 ; Read the port bits all at once
    movwf Temp,0 ; Copy port into temp register
    xorwf OldPortB,0,0 ; XOR bits to see which ones have changed
    movwf changedB,0 ; Bits of changed ports will be set to "1"
    movff Temp,OldPortB ; Save the register for next pass

    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


    Fan1
    btfss changedB,0
    bra Fan2
    infsnz Fan1Counter
    incf Fan1Counter+1

    Fan2
    btfss changedB,1
    bra Fan3
    infsnz Fan2Counter
    incf Fan2Counter+1

    Fan3
    btfss changedB,2
    bra Fan4
    infsnz Fan3Counter
    incf Fan3Counter+1
    Fan4
    btfss changedB,3
    bra Fan5
    infsnz Fan4Counter
    incf Fan4Counter+1

    Fan5
    btfss changedB,4
    bra Fan6
    infsnz Fan5Counter
    incf Fan5Counter+1

    Fan6
    btfss changedB,5
    bra Fan7
    infsnz Fan6Counter
    incf Fan6Counter+1

    Fan7
    btfss changedC,0
    bra Fan8
    infsnz Fan7Counter
    incf Fan7Counter+1

    Fan8
    btfss changedC,1
    bra Fan9
    infsnz Fan8Counter
    incf Fan8Counter+1

    Fan9
    btfss changedC,5
    bra Fan10
    infsnz Fan9Counter
    incf Fan9Counter+1

    Fan10
    btfss changedC,4
    bra DoneForNow
    infsnz Fan10Counter
    incf Fan10Counter+1

    DoneForNow
    INT_RETURN ; Return from the interrupt
    ENDASM

    ;---------------------- End of Timer 0 Int handler -----------------------------
    Charles Linquist

  2. #2


    Did you find this post helpful? Yes | No

    Smile ooh more to learn!

    Thanks for posting that!

  3. #3
    Join Date
    Jun 2006
    Posts
    37


    Did you find this post helpful? Yes | No

    Talking thanks

    thanks!
    can nay body covert this to pic basic pro.

  4. #4


    Did you find this post helpful? Yes | No

    Default am i understanding this?

    as far as i can tell this 1. can read a max of 1khz 2. reads or counts for half a second. and 3. was going to have more inputs on port D?

  5. #5
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Yes, you are correct in assuming this version reads a max of 1KHz - and that is if the input is a square wave, less if the duty cycle is not 50%.

    I have used this routine successfully to read fan RPMs over 15,000.

    To increase the maximum frequency you can count, you have to increase the interrupt rate. Right now, the period is 500uSec, but could be easily decreased to less than 100uSec by changing the TMR preload (use Mr. E's calculator). I chose to run my routine for 1000 counts ( .5 seconds) so that I could get acceptable resolution, but there is no "magic". You do have to run it for a defined amount of time to come up with the answer you need.

    There is no limit to the number of pins you can read with this method. Theoretically, every pin on the pic could be an input.
    Charles Linquist

  6. #6


    Did you find this post helpful? Yes | No

    Talking Thanks!

    Thanks again for posting that. i actually learned a bit from it, and have a use for it! I mentioned the portd for expansion as it was read and compared while there doesn't appear to be a fan assigned to it. this is finally starting to make sense.

  7. #7
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    You are right about reading PortD and not using the result. I use this routine across several products. Some use PortD input, some do not. Because I support so many products, I sometimes have trouble with "cross pollination". This is one of those cases. I'll comment the port read out and save a few processor cycles. I have plenty of speed, since I always use 18F8722's at 40MHz.
    Charles Linquist

Similar Threads

  1. Count pulses between VARIABLE TIME
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 12:44
  2. Timing input pulses and re-outputting them
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th February 2007, 01:50
  3. Replies: 1
    Last Post: - 18th April 2006, 19:11
  4. How to calculate machine time in basic code?
    By chai98a in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd February 2006, 23:44
  5. anyone knows how to calculate muslim prayer time?
    By luqman83salleh in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 6th September 2004, 09:54

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts