Trying to measure time between two pulses on different pins.


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    Henrik,

    That looks awesome! I will try it this weekend when I get some time.

    I did get it to work on an 18F2320 by taking the first bit of code that Bruce posted in the link in my initial post and modifying it a bit. Basically I could only measure one circuits pulse in relation to the 60HZ signal though. Not both circuits.

    I did that by taking out his failing edge capture and replacing it with a rising edge capture of the second pulse via CCP2

    So CCP1 looks for the rising edge on the 60Hz signal, and then it switches to look for the rising edge of the second pulse on CCP2. The rest is the same as his code for the most part.

    I think its pretty close to your original thinking, except using CCP to capture instead of raw pins.

    I got some really nice, workable results right off the bat, so I was pretty happy I figured something out at least

    I can wait to try your code out, as it gets me even closer.

    Thanks so much to all of you for helping and lending your thoughts. I'll let you know how it goes!

    -Ryan

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    Hi Ryan,
    Yes, using a chip with two CCP modules will allow you to get the absolute best results.
    Basically, what we're doing here is the same thing as the CCP module is doing when operating in capture mode except that we are polling the input pins in software in order to determine when to "capture" the timers value while that's being done all in hardware when using the CCP module.

    Polling the inputs like in my example will always introduce some latency, jitter etc. You said your preferred hardware for this was the good old 628/648 (which only got one CCP module) so that's what I tried to do :-)

    /Henrik.

  3. #3
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    Ok,

    Super big thanks Henrik for helping me get this working on a 16F648A!

    It seems to do ok, but of course I can’t leave well enough alone.

    The 16F648A code does what I need, but has some “quarks” I assume are due to the polling. Like if I run the same signal into both capture pins, the times are different by a bit, which I would think comes from the fact one pin is getting polled later than the other, which is fine, I can live with it.

    But wanting to learn more about interrupts and timers, I tried taking the code for 18F chips using Darrel’s instant interrupts that Bruce did in the post here: http://www.picbasic.co.uk/forum/show...se+measurement and combining it with the code that Henrik provided.

    This is what I have so far for an 18F2320, 20MHz HS, 2.60C, All configs set properly (off , input pin, etc.):

    Code:
    DEFINE NO_CLRWDT 1
    DEFINE OSC 20
    DEFINE HSER_BAUD 9600
    DEFINE HSER_TXSTA 20h
    
    TrigOut VAR PortB.3
    
    Time1 VAR WORD                      ' Time between leading edge of Pulse1 and leading edge of Pulse2
    Time2 VAR WORD                      ' Time between leading edge of Pulse1 and leading edge of Pulse3
    Measure VAR BIT                     ' Semaphore to indicate when we want to start a measurment.
    OverFlows  VAR BYTE                 ' Timer1 overflow total
    
    
    INCLUDE "DT_INTS-18.bas"       ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"    ; Include if using PBP interrupts
    
    ;----[High Priority Interrupts]----------------------------------------
    ASM
    INT_LIST  macro    ; IntSource,   Label,   Type, ResetFlag?
            INT_Handler   TMR0_INT,  _SixtyHz,   PBP,  yes
            INT_Handler   CCP1_INT,  _Capture1,  PBP,  yes
            INT_Handler   CCP2_INT,  _Capture2,  PBP,  yes
    	    INT_Handler   TMR1_INT,  _Timer1,    PBP,  yes
        
    	endm
        INT_CREATE               ; Creates the High Priority interrupt processor
    ENDASM
    
    CCP1CON = %00000101  ' Capture1 mode, capture on rising edge
    CCP2CON = %00000101  ' Capture2 mode, capture on rising edge
    T0CON = %11000111    ' Prescaler assigned to TMR0, 1:256 ratio
    T1CON = 0            ' TMR1 prescale=1, clock=Fosc/4, TMR1=off (200nS per count @20MHz)
    
    @    INT_ENABLE TMR0_INT ; Enable the 120Hz interrupt for the square wave generator.
    
    Main:    
        Gosub Capture
        IF T1CON.0 = 0 THEN ' If done, show result
          hserout ["Timer Overflows = ",DEC OverFlows]
          hserout [" Remaining ticks = ",dec time1]
          hserout [" Remaining ticks = ",dec time2,13,10]
        ENDIF
        PAUSE 2000
        GOTO Main
    
    '-------------------------------------------------------------------------------
    Capture:
        Measure = 1
        While Measure = 1 : WEND        ' Wait until ISR has created the rising edge and started the timer
        @    INT_ENABLE  CCP1_INT       ; Start new capture
        Measure = 1
        While Measure = 1 : WEND        ' Wait until ISR has created the rising edge and started the timer
        @    INT_ENABLE  CCP2_INT       ; Start new capture
        RETURN
    '-------------------------------------------------------------------------------
    
    
    '---[CCP1 - interrupt handler]------------------------------------------
    Capture1:
         
         Time1.LowByte = TMR1L     ; Get remaining Timer1 counts 
         Time1.HighByte = TMR1H
    
    '---[CCP2 - interrupt handler]------------------------------------------
    Capture2:   
    
         Time2.LowByte = TMR1L     ; Get remaining Timer1 counts 
         Time2.HighByte = TMR1H
    @ INT_RETURN
    
    '---[TMR1 - interrupt handler]---------------------------------------------
    Timer1:
        OverFlows = OverFlows + 1
    @ INT_RETURN
    
    '-------------------------------------------------------------------------------
    SixtyHz:
    If TrigOut = 0 THEN                 ' If trig output is low....
        If Measure = 1 THEN             ' ...and we want to start a measurment
            T1CON.0 = 0 : PIR1.0 = 0    ' Stop timer and clear interrupt flag
            TMR1H = 0 : TMR1L = 0       ' Clear the time
            Time1 = 0 : Time2 = 0       ' Clear measurements
            T1CON.0 = 1                 ' And start the timer
            Measure = 0                 ' Let main program know we've set the ball in motion.
        ENDIF
        TrigOut = 1                     ' Set trig output high
    ELSE
        TrigOut = 0                     ' Trig output was high, set it low.
    ENDIF
       
    TMR0 = TMR0 + 94                    ' Reload TMR0 for 120Hz interrupt rate
    @ INT_RETURN
    '-------------------------------------------------------------------------------
    
    
         END
    It compiled the first time with an error about TMR0 not being defined, so I edited the .INC file and got around that, but of course it still doesn’t work. No 60Hz square wave out and no serial output.

    Anyone care to have a gander at my code and tell me what I’m doing wrong? Point and laugh?? Poke at me with a stick???

    Huge thanks as always!

    -Ryan

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


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    Hi Ryan,
    Like if I run the same signal into both capture pins, the times are different by a bit, which I would think comes from the fact one pin is getting polled later than the other, which is fine, I can live with it
    Yes, that's correct.

    I think you're trying to bite off a bit too much at a time with this new version.
    I'd start by getting the serial output going without any interrupts or anything, a simple Hello World kind of thing.
    Then I'd get a LED blinking on what will eventually be the 60Hz output, just so you know you have control over that pin.
    Then I'd get the TMR0 interrupt going.
    Then.... ah you get the picture ;-)

    For example, PortB.3 (your 60Hz output) is an ADC input and it "comes up" in analog mode, I don't see you setting it to digital (see ADCON1 register).
    You say "All configs set properly" but I don't see you setting any TRIS register in the code. All pins comes up as inputs, TRIS bit needs to be cleared for them to become outputs.

    /Henrik.

  5. #5
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    Alright, you caught me!

    I wrote that, or rather pieced it together when I should have been going to bed for the evening. It does have the typical “noob” mistakes of not turning the analog inputs off and setting TRIS for my output pins and probably a few more mistakes!

    I have a bad habit of tackling this stuff right before bed, figuring I can get things off my mind before trying to sleep with ideas bouncing around in there. Then I start racing the clock and trip myself up. I’m an electronics technician by profession, but it’s actually mostly high voltage and tubes for me. I don’t get to do as much programming unless it’s my own stuff, after work.

    Another problem is that, as you can see by my forum join date, I’ve been using PicBasic Pro a few years now. But I only work on PIC based projects once every few months, sometimes longer. And as they say: if you don’t use it…

    But that said I’ve done all sorts of complex projects with PicBasic Pro, from thermostats with one-wire sensors and LCD readouts to all kinds of PWM motor and lighting controllers, robots, etc. I’ve just always found ways around using scary interrupts and timers. But I guess that’s what separates the men from the boys.

    I tend to use SEROUT over HSEROUT because it doesn’t require outside help to communicate directly with the serial port on my PC. And I did remember that when trying to run your code the first time and gibberish was appearing in hyperterminal . I just inverted it with a 4049 and all was well. So a “hello world” I can handle.... I hope!

    I’ll start again and try to work on a bit at a time (smaller bytes ) and maybe get it working. But I think I’m having trouble wrapping my little brain around Interrupts. I mean I understand the basics of it. Like: when pin 1 sees signal X, jump to Y routine without regard to what the program was doing, and then return to that point in the program when the ISR is satisfied. But in the context of actual code, I’m clueless.
    Like:
    Code:
    Capture:
    	    Measure = 1
    	    While Measure = 1 : WEND        ' Wait until ISR has created the rising edge and started the timer
    	    @    INT_ENABLE  CCP1_INT       ; Start new capture
    	    Measure = 1
    	    While Measure = 1 : WEND        ' Wait until ISR has created the rising edge and started the timer
    	    @    INT_ENABLE  CCP2_INT       ; Start new capture
    	    RETURN
    Isn’t this the same as polling kind of sort of? Why would I want to do that? Am I even supposed to be doing that?! Does it really matter if CCP2 gets a read of the counter before CCP1? On and on…

    Anyway, I’ll play around with it until I either figure it out or get tired and go back to hiding from the hard stuff!

    Thanks Again!
    Ryan

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    Hi Ryan,
    I wrote that, or rather pieced it together when I should have been going to bed for the evening. It does have the typical “noob” mistakes of not turning the analog inputs off and setting TRIS for my output pins and probably a few more mistakes!

    I have a bad habit of tackling this stuff right before bed, figuring I can get things off my mind before trying to sleep with ideas bouncing around in there. Then I start racing the clock and trip myself up. <snip>
    Yes, I think we're all guilty of the above now and then ;-)

    Another problem is that, as you can see by my forum join date, I’ve been using PicBasic Pro a few years now. But I only work on PIC based projects once every few months, sometimes longer. And as they say: if you don’t use it…
    That's the situation in my case as well. It can go months without me working on a particular project. However, what I do is visit this forum (and the MELABS forum (though it's pretty silent most of the time)) and trying to help other members with their issues, discuss implementation ideas etc. Doing that helps me remember what I've learned and learn a lot of new stuff.

    But that said I’ve done all sorts of complex projects with PicBasic Pro, from thermostats with one-wire sensors and LCD readouts to all kinds of PWM motor and lighting controllers, robots, etc. I’ve just always found ways around using scary interrupts and timers. But I guess that’s what separates the men from the boys.
    Not at all. Everything is hard (more or less) until you know how to do it. Interrupts are no different but Darrel has really made a BIG difference with the DT-INTS routines. It makes it really quite easy without the drawbacks of ON INTERRUPTS, though it does have it drawbacks which you'll also get familiar with over time.

    I tend to use SEROUT over HSEROUT because it doesn’t require outside help to communicate directly with the serial port on my PC. And I did remember that when trying to run your code the first time and gibberish was appearing in hyperterminal .
    Nothing wrong with SEROUT but you really "should" get up to speed with using HSEROUT, especially so now when you start introducing interrupts because once you do your SEROUT will basically stop working (if you don't see to it that an interrupt will NOT fire while the PIC is executing the SEROUT). I've got a couple of different development boards and they all got level translators (MAX232 etc) and/or a FTDI USB<->RS232 chip on them so I usually don't need to do anything special. When breadboarding (as I did with this code) I've got this thing which makes it really easy.

    Code:
    Capture:
    	    Measure = 1
    	    While Measure = 1 : WEND        ' Wait until ISR has created the rising edge and started the timer
    	    @    INT_ENABLE  CCP1_INT       ; Start new capture
    	    Measure = 1
    	    While Measure = 1 : WEND        ' Wait until ISR has created the rising edge and started the timer
    	    @    INT_ENABLE  CCP2_INT       ; Start new capture
    	    RETURN
    Isn’t this the same as polling kind of sort of? Why would I want to do that? Am I even supposed to be doing that?! Does it really matter if CCP2 gets a read of the counter before CCP1? On and on…
    I'm not sure I understand. You're polling the Measure flag but you're not polling the actual input(s). However, the way you have it written above it will first measure the time between the trig pulse and the first pulse. Then it will sit there and wait for the next trig pulse and THEN measure the time between THAT trig pulse and the second pulse. What you probably want to do is enable the CCP interrupts from the SixtyHz interrupt routine and (again probably) get rid of the Capture subroutine alltogether.

    /Henrik.

  7. #7
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: Trying to measure time between two pulses on different pins.

    TrigOut VAR PortB.3
    TrigOut is your output but you have not set TRIS register as output for this pin?
    Last edited by EarlyBird2; - 29th August 2014 at 08:33.

Similar Threads

  1. Measure time in mS between two pulses
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 7th March 2011, 07:20
  2. time measurement between 2 pulses
    By xvladx in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 27th April 2010, 17:33
  3. calculate time between pulses
    By hell_pk in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 20th October 2007, 16:49
  4. Count pulses between VARIABLE TIME
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 12:44
  5. Timed pulses on 2 pins
    By Danie Joubert in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th March 2004, 07:38

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