Trying to measure time between two pulses on different pins.


Closed Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    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.

  2. #2
    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

  3. #3
    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.

  4. #4
    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.

  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.

    Ok,

    Just focusing on getting 60Hz out of the thing. kind of left the other code in there knowing it probably won't work completely until I rewrite it, but....

    I fixed ADCON1 (= 7) and TRISB to output on B.3 and now I have a pulse! The only problem is that it seems to be 38.1Hz

    Not knowing what "TMR0 = TMR0 + 94" for me is doing exactly (maybe preloading timer 0 so it overflows earlier or something?) and it has a nice integer there to play with, I changed it to various things, but no difference in the 38.1Hz.

    Should I be playing with the prescaler value or something? I figure as long as the oscillator is still 20MHz, it should be the same I know pretty much nothing when it comes to our timer friends.

    My next plan is to maybe get the working code for the 16F648A ported over and seeing if that will work on an 18F with the 18F DTII stuff and go from there.

    Thanks for taking the time to help me out!

  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 fixed ADCON1 (= 7) and TRISB to output on B.3 and now I have a pulse! The only problem is that it seems to be 38.1Hz
    That 38.1Hz tells me that your interrupt rate is 76.2(939)Hz, which is exactly what you get when you have 16bit timer free-running at 5MHz. (20Mhz / 4 / 2^16).

    On the 16F648 TMR0 is 8 bits wide with an 8bit prescaler shared with the WDT....
    On the 18F2320 TMR0 is configurable to be either 8 bit OR 16 bit wide with its own dedicated 8 bit prescaler.

    Going by your description alone and not seeing the actual code my guess is you've got the timer configured in 16 bit mode. That'll work too but the code needs to be changed to use the full 16 bit timer instead of the 8bit timer AND 8bit prescaler of the 16F648.

    Not knowing what "TMR0 = TMR0 + 94" for me is doing exactly (maybe preloading timer 0 so it overflows earlier or something?) and it has a nice integer there to play with, I changed it to various things, but no difference in the 38.1Hz.
    Yes exactly. In the original code where we had a 8 bit timer, "restarting" the timer at value of 94 each interrupt (which isn't REALLY what happens but it's easier to understand) will cause it to overflow in 162 ticks (256-94) instead 256 ticks. With a a 16bit timer running at 5MHz, "restarting" the timer at 94 to in the ISR will only change the interrupt rate by approximately 0.11Hz which is probably why you think you don't see it change.

    /Henrik.
    Last edited by HenrikOlsson; - 3rd September 2014 at 21:09.

  7. #7
    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.

    I have TMR0 set to 8 bit mode actually T0CON = %11000111.

    Changing the prescaler to either 128 or 64 seemed to speed it up. I can't seem to get to 60Hz no matter what I do.

    I tried porting over your working code from the 16F648A and using DT_INTS-18 instead of 14, but get a whole bunch of compiler errors.

    I notice that the code Bruce wrote using DT_INTS-18 didn't include the wsave varibles that the DT_INTS-14 looks to require?

    I know I have workable code and should just leave it alone, but I want to learn how to make this work with CPP on the 18F2320.

    As always, thank you!

    Haven't tried this yet, but maybe it will work?:

    Code:
    INCLUDE "DT_INTS-18.bas"       ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"    ; Include if using PBP interrupts
    
    DEFINE OSC 20                       ' Running with a 20MHz x-tal
    
    ' #CONFIG - doing this manually at the moment via programmer
    
    ADCON1 = 7
    
    TRISB = %11110111                   ' Set PortB direction.
    
    T0CON = %11000111                 ' Prescaler assigned to TMR0, 1:256 ratio
    
    TrigOut VAR PortB.3                   ' Pin for 60Hz output
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    TMR0_INT,  _ISR,   PBP,  yes
        endm
        INT_CREATE                      ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE TMR0_INT         ; Enable the 120Hz interrupt for the square wave generator.
    
    Main:
        Pause 2000
                 HSEROUT["Is This Working???",10,13]
    Goto Main
    
    ISR:
    If TrigOut = 0 THEN                 ' If trig output is low....
       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
    '-------------------------------------------------------------------------------

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 : 1

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