How to Measure 2 or more digital inputs a the same time


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: How to Measure 2 or more digital inputs a the same time

    Thanks Henrik , the timer approach looks possible as a starting point , accuracy of upto 5us is required , as a change between pins can be small as 5us , do you have any examples how best to pole the pins to ensure i dont miss the change ? , i am starting to think i may need some asm in here again lol

    Cheers

    Sheldon

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


    Did you find this post helpful? Yes | No

    Default Re: How to Measure 2 or more digital inputs a the same time

    Hi,
    I'm not sure this will give enough performance to guarantee a granularity of 5us but it MIGHT. It's not tested but it does compile, perhaps you can use it as a starting point.
    Code:
    T1CON = %00000000	'TMR1 clocked by FOsc/4, prescaler 1:1 = 125ns per tick
    
    @Timer1 = TMR1L	' Create alias to TMR1 in order to access it as a WORD
    Timer1 VAR WORD EXT	    ' Let PBP know of its existance
    
    TMR1IF	VAR PIR1.0	  ' Alias to TMR1 Interrupt flag, set on overlfow
    
    Pulse1 VAR WORD		  ' Holding variables for pulsewidths
    Pulse2 VAR WORD
    Pulse3 VAR WORD
    Pulse4 VAR WORD
    
    PulseCount VAR BYTE	   ' Keep track of how many pulses we've grabbed.
    
    '--------------------------------------------------------------------------
    Setup:
    	Pulse1 = 0		   ' Clear all pulsewidhts
    	Pulse2 = 0
    	Pulse3 = 0
    	Pulse4 = 0
    	PulseCount = 0	   ' Clear pulsecounter
    	T1CON.0 = 0		   ' Timer 1 OFF
    	Timer1 = 0		   ' Clear TMR1
    	TMR1IF = 0		   ' Clear interrupt flag
    
    '--------------------------------------------------------------------------
    WaitForLeadingEdge:
    	While !PortB.0 : WEND	  ' Wait for rising edge on PortB.0
    	T1CON.0 = 1		          ' Start TMR1
    
    '--------------------------------------------------------------------------
    WaitForTrailingEdge:
    	IF !PortB.0 THEN	' First input is low
    		Pulse1 = Timer1	' Grab timer value
    		PulseCount = PulseCount + 1
    	ENDIF
    
    	IF !PortB.1 THEN	' Second input is low
    		Pulse2 = Timer1	' Grab timer value
    		PulseCount = PulseCount + 1
    	ENDIF
    
    	IF !PortB.2 THEN	' Third input is low
    		Pulse3 = Timer1	' Grab timer value
    		PulseCount = PulseCount + 1
    	ENDIF
    
    	IF !PortB.3 THEN	' Fourth input is low
    		Pulse4 = Timer1	' Grab timer value
    		PulseCount = PulseCount + 1
    	ENDIF
    
    
    	' Now check if we've got all four pulses.
    	IF PulseCount = 3 THEN WeAreDone
    
    	' Verify that the timer hasn't overflowed. At 125ns per tick the
    	' timer overflows after 8.192ms.
    	IF TMR1IF THEN ABORT
    
    	' Not all pulses grabbed and no overflow, continue waiting.
    	Goto WaitForTrailingEdge
    '--------------------------------------------------------------------------
    
    WeAreDone:
    	HSEROUT["Done.",13]
    	HSEROUT["Pulsewidth 1: ", #Pulse1,13]
    	HSEROUT["Pulsewidth 2: ", #Pulse2,13] 
    	HSEROUT["Pulsewidth 3: ", #Pulse3,13] 
    	HSEROUT["Pulsewidth 4: ", #Pulse4,13]
    Goto Setup
    
    Abort:
    	HSEROUT["ERROR! Timer overflow.",13]
    GOTO Setup
    /Henrik.

    EDIT: Just saw your second post, don't know I understand the D/A approach. If you mean that you're going to convert the the pulsewidth to analog voltage and read that with the ADC I don't think it's going to help because the ADC in the PIC can only sample one input at the time anyway. (Some PICs have dual channel S/H circuits but most don't). In that case you might as well measure pulse 1 then measure pulse 2 then pulse 3 and so on.

Similar Threads

  1. how to measure RPM and SPEED at the same time
    By teeeeee in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 30th July 2012, 08:34
  2. Measure time in mS between two pulses
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 7th March 2011, 07:20
  3. PIC 18f4680 DIGITAL INPUTS
    By MegaADY in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th July 2008, 07:34
  4. Setting up digital inputs
    By quester in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd March 2008, 22:14
  5. Digital inputs
    By Christopher4187 in forum General
    Replies: 5
    Last Post: - 1st August 2007, 23:14

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