Shop OBEX P1 Docs P2 Docs Learn Events
Prop1 Spin; best method for reflecting a signal — Parallax Forums

Prop1 Spin; best method for reflecting a signal

Under certain circumstances I wish to pass through, or reflect a signal unchanged from an input pin to an output pin.
The signal is a standard servo pulse signal from a RC receiver.
Obviously one method is a continuous read and write loop.
Are their any more efficient Spin coding methods to achieve this?

Comments

  • You could use a counter. Set CTRx to POS detect mode to measure the incoming pulse (value will be in system ticks in the PHSx register). When you've seen the 0-1-0 transition on the input pin, you can reconfigure the counter to NCO mode to create the outgoing pulse. Between the two processes you could modify the timing of the output pulse.

    If you want this to run autonomously, you could launch this method into a cog (with cognew). It uses CTRA to measure the incoming pulse, CTRB to create the outgoing pulse. I made a note where you can adjust the output pulse value. Since this is a Spin cog, you can use a global variable to drive the adjustment.

    Warning: I just woke up and this is not tested.
    pri reflect(in, out)                                            ' use cognew() to run
    
      ctra := (%01000 << 26) | in                                   ' POS detect (measure)    
      frqa := 1
      phsa := 0
    
      ctrb := (%00100 << 26) | out                                  ' NCO mode (pulse)
      frqb := 1
      phsb := 0
      dira[out] := 1
    
      repeat
        waitpne(|<in, |<in, 0)                                      ' wait for input low
        phsa := 0                                                   ' clear measurement
        waitpeq(|<in, |<in, 0)                                      ' wait for pulse
        waitpne(|<in, |<in, 0)
    
        { adjust phsa here if needed }
        
        phsb := -phsa                                               ' start output pulse
    
  • KeithEKeithE Posts: 957
    edited 2017-04-20 17:33
    I had asked what I think is a similar question here and Ariba posted a simple method which works well if you can dedicate 4 counters:

    http://forums.parallax.com/discussion/163764/simple-question-about-simpleide-and-fdserial-155200-baud#latest

    Edited: 4 counters was actually for 2 signals
  • It's two counters in one cog, and one extra pin, to pass a single pin servo signal through non-inverted. Delay is 3 clock cycles.
    POSFEEDBACK = %01001
    ...

    ctra := (POSFEEDBACK<<26)+(dummyPin<<9)+servoInPin
    ctrb := (POSFEEDBACK<<26)+(servoOutPin<<9)+dummyPin
    dira := |<dummyPin + |<servoOutPin
  • Duane DegnDuane Degn Posts: 10,588
    edited 2017-04-20 19:03
    Am I correct in assuming these techniques are limited to two input lines?

    FWIW, I use something like the code below with some of my quadcopter projects to pass through lines from a RC receiver.
    DAT                     org
    
    DAT enter               mov     statesToShift, inputMask
                            shl     statesToShift, shiftAmount
                            or      dira, statesToShift                
                          
    loop                    mov     statesToShift, ina            ' record states of all pins 
                            and     statesToShift, inputMask      ' ignore all but data pins               
                            shl     statesToShift, shiftAmount   ' shift input bits to output pins
                            mov     outa, statesToShift                
                            jmp     loop
    
    inputMask               long    %1111
    shiftAmount             long    17 ' use appropriate value for your setup
    statesToShift           res     1
    
                            fit
    

    This particular code requires the input pins to on lower numbered pins than the output pins but it would certainly be easy to modify the code to use any group of input and output pins.

    I think the above loop runs at 4MHz with an 80MHz clock.

    Edit: I had an "or" where a "mov" needed be.
  • Jon, Ariba, Tracy, Duane,
    Thank you for your input, much appreciated.
    I suspected there would be some elegant solutions to this task, and you have identified and documented them very clearly.
    The Prop 1 brains trust has delivered again!
Sign In or Register to comment.