PIC timing issue

Discussion in 'Embedded Systems and Microcontrollers' started by seantownsend, Mar 26, 2009.

  1. seantownsend

    Thread Starter New Member

    Mar 26, 2009
    i am new to the forum and was hoping to get some advice on how i can get a tricky timing issue resolved on a PIC project i have.

    I have a rotary shaft with a flag on it which is picked up by a sensor, basically i need to work out the rpm of the shaft. One way is to count how many pulses there are on the given port input in a set amount of time, the problem with this method is i don't get a true value, the resolution isn't high enough.

    A better way is to work out the time period between 2 pulses and use that to calculate the true rpm at that point in time. The only problem i have with this method is i don't know how to accurately measure the time period between the 2 pulses.

    I am using just a 16F627 for this project which has plenty of I/O for what i need.

    Any suggestions/info would be much appreciated.

  2. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    Maybe as follows:-

    Configure a timer - presumably with it's own interrupt service routine to keep track of timer overflow(s) - depends on how long the between the arrival of successive pulses.

    Shaft Pulse directed to any of PORTB RB<7:4> configured as input. These have an interrupt on change feature.

    At the PORT change interrupt service :

    1. Read the (previously configured) timer value - including overflow counts
    2. Store the relevant values in registers
    3. Reset / restart the timer (& overflow counts)
    4. Back to Main

    The very first shaft pulse recognition after PIC power up will have no history but thereafter the time differences would be correct.

    Do other stuff in Main to process / display measured time difference & or shaft RPM.

    How accurate do you need the RPM? At only one pulse per revolution the resolution may not be that fantastic if the shaft speed is varying significantly during any particular single rotation interval.

    I'm sure you'll get many much better ideas from other AAC members!
  3. seantownsend

    Thread Starter New Member

    Mar 26, 2009
    Thanks for the prompt reply,

    i was considering setting the shaft input to trigger interrupts, but i still have an issue with how exactly to work out the time difference between the 2 pulses, under a system such as linux (which is my usual programming environment) it would be as easy as ready the time of the first pulse then the time of the second pulse and subtracting. On the PIC i am using i don't have the luxury of time related functions, do i have to build in some kind of RTC into my circuit?

  4. nanovate

    Distinguished Member

    May 7, 2007
    Some PICs have an input capture feature on the timer (ECCP)--- you might try that to time the pulses. I think the PIC16F627A has this feature