Calculating time delay between two inputs?


Closed Thread
Results 1 to 12 of 12

Hybrid View

  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,825


    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,523


    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,825


    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

Members who have read this thread : 0

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