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.

    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
    '-------------------------------------------------------------------------------

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

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

    For 60Hz.
    Set Timer0 to 16 Bit with a 1:8 prescale
    Add this to your variable section
    Code:
    PreLoad     VAR WORD
    PreLoad = 23868
    and in the ISR routine
    Code:
    TMR0L = PreLoad.LowByte  
    TMR0H = PreLoad.HighByte
    I think...
    Dave
    Always wear safety glasses while programming.

  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.

    Thanks Dave!

    This is what is actually working for me thus far! At least some success:

    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
    DEFINE HSER_BAUD 9600
    DEFINE HSER_TXSTA 20h
    
    @ __CONFIG    _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H & _IESO_OFF_1H
    @ __CONFIG    _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L
    @ __CONFIG    _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
    @ __CONFIG    _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
    @ __CONFIG    _CONFIG3H, _CCP2MX_OFF_3H & _PBAD_DIG_3H & _MCLRE_OFF_3H 
    
    ADCON1 = 7
    
    TRISB = %11110111                   ' Set PortB direction.
    
    T0CON = %10000001                  ' 16 bit Prescaler assigned to TMR0, 1:4 ratio
    
    TrigOut VAR PortB.3                    ' Pin for 60Hz output
    PreLoad     VAR WORD
    PreLoad = 52000
    
    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] ' The lights are on... is anyone home?
    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
    TMR0L = PreLoad.LowByte       ' TMR0 High and low bytes preloaded
    TMR0H = PreLoad.HighByte
    @ INT_RETURN
    GOTO main:
    Now to add to it

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

    Sorry to keep beating on this post! I just really want to figure this out and having support is nice. Also, this is what I do on my lunch break ha-ha... So I write everthing in notepad, post it here and then I can cut and paste to my IDE when I get home

    Anyone care to look and maybe do a little bug hunting? I'm probably doing something wrong here. Something feels funny about having interrupts enable other interrupts. Maybe something will go wrong when those interrupts return? I'm not used to "Main" sitting there twirling away and then having my TMR0 doing it's own thing, which also happens to the be the starting point for all the real work.

    But what i think should happen is: each time the PulseGen ISR fires off a rising edge, it will start by intializing everything and then it will start TMR1 ticking and look to see which CCP interrupt was last enabled, and if the first one wasn't, it will enable it and mask it so it doesn't get selected the next go around. When CCP1 gets a rising edge, it will stop TMR1, disable itself and TMR1 interrupts, get the count from TMR1 into a variable and return (uh-oh...)

    If all goes to plan, it will start over and do the same with CCP2 and unmask CCP1 for the next round.

    My worries are since all this is happening from the TMR0 PulseGen interrupt, its timing will be at the mercy of those CCP and TMR1 interrupts... Also where those interrupts are getting returned to. Is that a safe place? Will it continue to skip out of the IF THEN ELSE traps and allow PulseGen to continue on? Maybe a GOTO PulseGen: added to the return points???

    Interrupts are hard on the brain!

    Thanks again!!! I promise once I get this going as desired I will give a full disclosure as to what it's for - some of you might find a good use for it. Oh, and I'll shut up and go away for a while

    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
    DEFINE HSER_BAUD 9600
    DEFINE HSER_TXSTA 20h
    
    @ __CONFIG    _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H & _IESO_OFF_1H
    @ __CONFIG    _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L
    @ __CONFIG    _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
    @ __CONFIG    _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
    @ __CONFIG    _CONFIG3H, _CCP2MX_OFF_3H & _PBAD_DIG_3H & _MCLRE_OFF_3H 
    
    ADCON1 = 7				' Turn off analog inputs
    TRISB = %11110111                   	' Set PortB direction.
    T0CON = %10000001                  	' TMR0 16 bit Prescaler 1:4 ratio
    T1CON = %00000000			' TMR1 prescale 1:1 clock=Fosc/4, TMR1=off (200nS per count @20MHz)
    CCP1CON = %00000101  			' CCP1 Capture mode, capture on rising edge
    CCP2CON = %00000101  			' CCP2 Capture mode, capture on rising edge
    
    TrigOut    VAR PortB.3                  ' Pin for 60Hz output
    OverFlows  VAR BYTE 			' Timer1 overflow total
    Remainder1  VAR WORD 			' Remaining Timer1 ticks after first pulse capture
    Remainder2  VAR WORD 			' Remaining Timer1 ticks after second pulse capture
    FirstCapt  VAR BIT : FirstCapt = 0
    PreLoad    VAR WORD : PreLoad = 52000   ' Preload TMR0 for 120Hz interrupt rate
    
    
    ;----[High Priority Interrupts]-------------------------------------------------------------------
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    TMR0_INT,  _PulseGen, PBP,  yes
            INT_Handler    TMR1_INT,  _Timer1,   PBP,  yes
            INT_Handler    CCP1_INT,  _Capture1, PBP,  yes
            INT_Handler    CCP2_INT,  _Capture2, PBP,  yes
        endm
        INT_CREATE                          ' Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE TMR0_INT          		' Enable the 120Hz interrupt for the square wave generator.
    
    '-------------------------------------------------------------------------------------------------
    
    '---[Main Routine]--------------------------------------------------------------------------------
    
    Main:
         IF T1CON.0 = 0 THEN 		' If TMR1 done, show result
    	HSEROUT["Timer Overflows = ",DEC OverFlows,"Ticks 1 = ",DEC Remainder1,"Ticks 2 = ",DEC Remainder2,10,13] 
         ENDIF
    GOTO Main:
    
    '-------------------------------------------------------------------------------------------------
    
    '---[TMR0 - Interrupt handler / 60Hz Squarewave Trigger Pulse Generator]--------------------------
    
    PulseGen:
       IF TrigOut = 0 THEN			' If trig output is low....
         TrigOut = 1                        ' Set trig output high
         TMR1L = 0                     	' Clear Timer1 Low Byte count
         TMR1H = 0     			' Clear Timer1 High Byte counts
         T1CON.0 = 1                   	' Turn Timer1 on at rising edge capture
         OverFlows = 0                 	' Clear over flow counts
         Remainder = 0                 	' Clear remainder
         PIR1.0 = 0                    	' Clear Timer1 overflow flag before enable
    @    INT_ENABLE  TMR1_INT         	' Enable Timer 1 Interrupts 
         IF FirstCapt = 0 THEN              ' If first pulse hasn't been looked for yet...
            FirstCapt = 1                   ' Mark it as such and...
    @    INT_ENABLE  CCP1_INT               ' Start looking for 1st pulse
         ELSE
         FirstCapt = 0                      ' Reset first pulse search mask and... 
    @    INT_ENABLE  CCP2_INT               ' Start looking for 2st pulse
         ENDIF
    ELSE
         TrigOut = 0                        ' Trig output was high, set it low.
    ENDIF
    
    TMR0L = PreLoad.LowByte       		' TMR0 High and low bytes preloaded
    TMR0H = PreLoad.HighByte
    @ INT_RETURN
    
    '-------------------------------------------------------------------------------------------------
    
    '---[CCP1 - interrupt handler]--------------------------------------------------------------------
    
    Capture1:
         T1CON.0 = 0                   	' Stop Timer1
    @    INT_DISABLE  TMR1_INT    		' Disable Timer 1 Interrupts
    @    INT_DISABLE  CCP1_INT    		' Disable CCP1 Interrupts
         Remainder1.LowByte = TMR1L    	' Get remaining Timer1 counts
         Remainder1.HighByte = TMR1H
    @ INT_RETURN
    
    '-------------------------------------------------------------------------------------------------
    
    '---[CCP2 - interrupt handler]--------------------------------------------------------------------
    
    Capture2:
         T1CON.0 = 0                   	' Stop Timer1
    @    INT_DISABLE  TMR1_INT    	   	' Disable Timer 1 Interrupts
    @    INT_DISABLE  CCP2_INT         	' Disable CCP2 Interrupts
         Remainder2.LowByte = TMR1L    	' Get remaining Timer1 
         Remainder2.HighByte = TMR1H
    @ INT_RETURN
    
    '-------------------------------------------------------------------------------------------------
    
    '---[TMR1 - interrupt handler]--------------------------------------------------------------------
    Timer1:	
        OverFlows = OverFlows + 1      	' Count the number of times TMR1 overflows
    @ INT_RETURN
    
    '-------------------------------------------------------------------------------------------------
    
    GOTO Main:
    
    END

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

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

    A quick read through your code and I do not see a problem. So the big question...
    Does it work as expected?
    If so Then NoProblem...
    Dave
    Always wear safety glasses while programming.

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

    Nope,

    It didn't work. I did catch a few mistakes as soon as I posted that. Like needing to comment my assembly stuff with ";" for example. And never flipping "FirstCapt" back after looking for the second pulse. And I had to play with the TMR0prescaler some more. The code I posted before this was running at 120Hz instead of 60Hz.

    I think I would have to find a way to keep proper order and timing of when my interrupts fire. When I corrected the above mistakes and ran that code, my output varibles would lock onto a certain value and stay that way. Almost as if the CCPs were firing as soon as their interrupts were enabled. I tried all sorts of stuff and never had any luck with getting it to all work together.

    I ended up using what Henrik had come up with for the 16F648A, I just got it to work on the 18F2320 and rolled in capturing with the CCP modules instead of the port B pins. It samples both CCPs 5 times, averages the results and breaks one of them out into 16 discrete "levels" before outputing via HSER.

    I need to tweak some things yet. But I'll post what I came up with soon and explain what it's for.

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, 08:20
  2. time measurement between 2 pulses
    By xvladx in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 27th April 2010, 18:33
  3. calculate time between pulses
    By hell_pk in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 20th October 2007, 17:49
  4. Count pulses between VARIABLE TIME
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 13:44
  5. Timed pulses on 2 pins
    By Danie Joubert in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th March 2004, 08: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