Shop OBEX P1 Docs P2 Docs Learn Events
Pulse Measurement (PASM2) — Parallax Forums

Pulse Measurement (PASM2)

JonnyMacJonnyMac Posts: 9,107
edited 2020-11-05 01:36 in Propeller 2
I still haven't been able to wrap my head around measuring a low-going pulse with P2 smart pins, so I thought I'd give brute-force another try. I have working code now, but it's a bit much -- this is simpler. The goal is to measure a low-going pulse. The code will wait tous (timeout microseconds) for the pulse to arrive, which can be up to tous microseconds wide. For the moment I am testing with inline PASM2, but this will be folded into a PASM cog/cpu. I've been feeling a bit janky for a couple days (sleep cycle is off), so if I've done something really silly, please go easy. :)

FWIW, this code does work. I connected an IR sensor and measured the leader pulse from a Sony remote. The result comes back near 2400 microseconds which is what it is supposed to be.
pub pulse0_in(pin, tous) : result | check, t1, t2
 
  org
                testp     pin                           wc      ' scan input
    if_c        mov       check, #1
    if_nc       mov       check, #0


' wait for up to timeout microseconds (tous) for high-to-low transition

.wait           getct     t1                                    ' sync with sys counter
                mov       t2, #0                                ' reset timeout

.loop1          addct1    t1, #US_001                           ' wait 1us
                waitct1
                testp     pin                           wc      ' scan input
                rcl       check, #1                             ' move into lsb of check
                and       check, #$0F                           ' isolate 5 scans
                cmp       check, #%1110                 wz      ' clean falling edge?
  if_e          jmp       #.loop0                               ' yes, measure pulse

                incmod    t2, tous                      wc      ' increment with roll-over
  if_nc         jmp       #.loop1

                jmp       #.done                                ' timeout, return 0


' measure low for up to tous

.loop0          addct1    t1, #US_001                           ' wait 1us
                waitct1
                incmod    result, tous                  wc      ' update pulse width 
  if_c          jmp       #.done                                ' - abort on time out
                testp     pin                           wc      ' rescan pin
  if_nc         jmp       #.loop0                               ' continue if still low

.done
  end

Comments

  • evanhevanh Posts: 15,934
    The smartpin mode to use is %10001 = Time A-input high states. And use inverted A input selection, %AAAA = %1000, to make it measure pulse low time.

    I tested it by also using %TT = %01 to enable the output. Then I could use OUT to toggle the pin, with the smartpin measuring it. This is my test code, minus the debug wrapper. The wrapper has a bunch of constants for things like smartpin modes, as well as the comport print routines.
    		flth	#tpin
    		wrpin	##SPM_TIM_PULSE | AF_NOT | SP_OUT, #tpin
    		dirh	#tpin				'start smartpin, sees pin high as first pulse completion
    		call	#readpulse			'consume the short reading
    
    .loop
    		outl	#tpin				'simulate an external low going pulse
    		waitx	##(2_400 * XMUL)		'2400 usec, plus six ticks for the instructions
    		outh	#tpin				'pulse end
    
    		call	#readpulse			'collect reading
    
    		qdiv	pa, #XMUL			'convert ticks to microseconds, XMUL is in megahertz
    		getqx	pa
    		getqy	misc1				'remainder
    		call	#itod				'emit measurement in usec
    		mov	pb, #"."			'a decimal point
    		call	#putch
    		mov	bcdlen, #2 | $100		'two decimal places with leading zeros
    		mov	pa, misc1
    		call	#itod
    		mov	bcdlen, #8
    		jmp	#.loop
    
    
    readpulse						'returns a reading in regiter PA, measured in sysclock ticks
    .wait		testp	#tpin		wc
    	if_nc	jmp	#.wait
    	_ret_	rdpin	pa, #tpin
    
  • Thanks for the suggestion, Evan. I really appreciate your feedback. I will give it a try.
Sign In or Register to comment.