Calculating time delay between two inputs?


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2010
    Posts
    12

    Post Calculating time delay between two inputs?

    Hi everyone,
    i'm new at pic programming though i know basic logic of c and qbasic programming. i have a project that i have to calculate time between PORTA.0 and PORTA.1 inputs. (like i'm applying +5 volt for PORTA.0 and after 3ms apply +5 volt for PORTA.1). i dont have any problems about basic statements and loops and i need codes for; when PORTA.0=1 timer will start and it will end when PORTA.1=1 and 5ms would be enough for maximum value.
    I've read about ON INTERRUPT and did not understand it well, and if there's no way around ON INTERRUPT i'd appreciate if you can explain it simple.

    Thnx a lot.

    i'm using XTAL 4 MHZ and pic16f84 but i can use other pics if i have to.

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    Oh dear, Does this '84 have 7 lives?

    Anyway, what about:

    Code:
    while !porta.0:wend
    start_your_timer_here
    while !porta.1:wend
    stop__your_timer_here
    get_timer_value

  3. #3
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    thnks for your concern, i want excatly that algorythm but i need codes for
    start_your_timer_here
    stop__your_timer_here
    get_timer_value

    but as i know there's no command for them at picbasic. Could you share if you have those commands?

    Quote Originally Posted by Ioannis View Post
    Oh dear, Does this '84 have 7 lives?

    Anyway, what about:

    Code:
    while !porta.0:wend
    start_your_timer_here
    while !porta.1:wend
    stop__your_timer_here
    get_timer_value

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Well, the 'F84 certanly isn't the most flexible device.... Here's an utested piece of code. Bring out the datasheet, look specifically at the TMR0 section. Go back and forth between it and the code below and see if you can understand it.
    Code:
    'We need the WDT postscaler for TMR0. Make sure to disable the WDT in CONFIGs
    DEFINE NO_CLRWDT 1  
     
    myValue VAR WORD  'Variable to store the count.
     
    Option_Reg.5 = 1  'Set TMR0 to external clock, Make sure to tie PortA.4 LOW.
    Option_Reg.3 = 0  'Assign prescaler to TMR0
     
    'We need to time up to 5ms, that's 5000 ticks with a 4Mhz clock.
    'TMR0 is only 8 bits wide meaning it will overflow in ~0.25ms
    'therefor we need to prescale the clock. We select 1:32 which will
    'allow us to time 8.192ms with a resolution of 32us
     
     
    Option_Reg.2 = 1  'Set prescaler to 1:32
    Option_Reg.1 = 0
    Option_Reg.0 = 0
     
    TRISA.0 = 1   'Make PortA.0 an input
    TRISA.1 = 1   'Make PortA.1 an input
     
    TMR0 = 0   'Clear the TMR0 register.
     
    Main:
    WHILE !PortA.0 : WEND  'Wait for PortA.0 to go high
    Option_Reg.5 = 0  'Switch TMR0 to internal clock (start counting)
    While !PortA.1 : WEND  'Wait for PortA.1 to go high
    Option_Reg.5 = 1  'Switch back TMR0 to external clock. (stop counting)
    myValye = TMR0 * 32  'myValue now contains the time in us (5000us=5ms)
     
    'Now do whatever with the value.
     
    TMR0 = 0   'Reset count in preparation for next round.
    Goto Main   'And do it again.
    Like I said, untested code but the general idea is there and you should be able to figure it out.

    /Henrik.

  5. #5
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    i am really greatful you for spending your time for my problem HenrikOlsson. I have understood your explanations and logic behind the codes. These codes helped me out.

    Thanks a lot.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    I was hopping that eventually grimmjow would use a more recent chip with a timer switch to set it on or off.

    Anyway, nice trick Henrik.

    Your code has a typo in the line:

    Code:
    myValye = TMR0 * 32  'myValue now contains the time in us (5000us=5ms)
    it should be

    Code:
    myValue = TMR0 * 32  'myValue now contains the time in us (5000us=5ms)
    Also while TMR0 is zeroed before new cycle, the Prescaller still holds the previous value.

    A chip with 16 bit timer would be more precise here.

    Ioannis

  7. #7
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    Yes i do not have obligation to use f84, and i know f877 has Timer1(16bit). Let us check this;

    i will use 4MHZ xtal and f877, and it has 16 bit timer ~= 66ms is more than enough for me.

    So i do not need to use watchdog timer prescaler commands and i just need to use,

    OPTION_REG.5=1 and OPTION_REG.5=0 commands for timer right?
    Last edited by grimmjow; - 15th October 2010 at 21:52. Reason: typing error

  8. #8
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    i've looked up f877 datasheet and TMR1 has its own control register (we can enable and disable it) and it's two paired (TMR1H and TRM1L)

    i am not sure but this is algorythm as i understand, if TMR1L=255 then TRM1H=TMR1H+1

    and if i sum them up, timepassed=TRM1H*255+TMR1L i can find time in useconds.

    Thanks for your helps.

    Quote Originally Posted by grimmjow View Post
    Yes i do not have obligation to use f84, and i know f877 has Timer1(16bit). Let us check this;

    i will use 4MHZ xtal and f877, and it has 16 bit timer ~= 66ms is more than enough for me.

    So i do not need to use watchdog timer prescaler commands and i just need to use,

    OPTION_REG.5=1 and OPTION_REG.5=0 commands for timer right?

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default

    Ioannis,
    Thanks for catching the typo! As for the prescaler not clearing, I actually thought about that but the datasheet says that writing to TMR0 does clear the prescaler (when it is assigned to TMR0) so it shouldn't be an issue. Now, with the '877 it's of little importance anyway.

    grimmjow,
    Yes, TMR1 on the 'F877 is controlled thru its own control register and you get its 16bit value almost like you shown:

    timepassed = TMR1H * 256 + TMR1L

    Make sure you stop the timer before reading it or you MAY get weird results if the low byte happens to roll over between you reading it and the high byte.

    And no, you don't need to use any prescaler since the 16bit wide timer allows you to measure 65535us which is far more than you need.

    Good luck!
    /Henrik.

  10. #10
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    Thanks a lot for your helps, HenrikOlsson and Ioannis.

    It is great for beginners to ask pros like you. i will try these codes and im pretty sure that it'll work. I'll let you know if i have problems about some parts again.

    Have a nice day.

  11. #11
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    Henrik: I missed that part about the prescaler on F84. Good one!

    grimmjow:

    remember to declare timepassed as a word variable.

    Given that you will still be using 4MHz crystal, the timer step will be 1usec.

    Ioannis

  12. #12
    Join Date
    Oct 2010
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    Henrik: I missed that part about the prescaler on F84. Good one!

    grimmjow:

    remember to declare timepassed as a word variable.

    Given that you will still be using 4MHz crystal, the timer step will be 1usec.

    Ioannis
    Thank you Ioannis, i'm using it as word.

    Btw circuit works fine thnx for your efforrs both Ioannis and HenrikOlsson

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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