Shop OBEX P1 Docs P2 Docs Learn Events
Using Counters for Ping? — Parallax Forums

Using Counters for Ping?

SRLMSRLM Posts: 5,045
edited 2014-03-21 06:54 in Propeller 1
Is it possible to use the Propeller counters to read the results from a PING? It's easy enough to bit bang the start pulse, but waiting and blocking for the response is valuable program time wasted. I'd much rather use a counter to get that duration.

Comments

  • mhm21mhm21 Posts: 14
    edited 2014-03-19 12:27
    I have done something similar using a little logic loop. Basically, setup the counter to auto increment when the input pin is high. Processing the read can then take place when the input pin is low and the PHSx register exceeds some threshold. This code is not complete as I cut it out from a larger program, but it shows the flow.
            mov             ctrb,#0                                 'Setup Counter B to monitor high state time of PW pin on MAXBOTIX ping sensor
            mov             ctrb,cntrstate
            mov             frqb,#1
    
     PING
                            mov     dummy1,ina              ' Sample pins
                            and     dummy1,PING_PIN         ' Limit to PW pin of MaxBotix ping sensor
                            cmp     dummy1,#0 wz            ' If ina==0, between pulses
                            cmp     min_cmp,phsb wc         ' If phsb>0, phsb contains pulse width of previous pulse  
            if_c_and_z      call    #PING_WRITE
    
    PING_RET                ret
    
    cntrstate               LONG    (%11010 << 26 ) | (%001 << 23) | (0 << 9) | (9) 'Last digit is pin declaration 
    
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-03-20 10:18
    That should be possible Cody. Here is a 'scope shot of the signal sequence, an 8us trigger pulse (orange), followed by a short setup delay, and then the echo timing pulse. In this case the echo is maximum, 20ms. There is time in between the trigger and the echo for Spin to reconfigure the counter from NCO mode to POSDET mode.
    Screen shot 2014-03-20 at 10.07.31 AM.png

    CON
      _clkmode = xtal1 + pll8x                '
      _xinfreq = 5_000_000
    
      NCOMODE = % 00100 << 26
      POSDETMODE = % 01000 << 26
    
    OBJ
      pst : "Parallax Serial Terminal"
    
    PUB PingCount(pin) | trigger
      frqa := 1
      trigger :=  -(clkfreq/1000000 * 8)  ' for 8us duration
      repeat
        ' send an 8us trigger pulse
        dira[pin]~~
        ctra := NCOMODE | pin
        phsa := trigger
        waitpeq(0, |<pin, 0)   ' until end of trigger pulse
    
    
        ' capture the return pulse with counter
        dira[pin]~  
        ctra := POSDETMODE | pin
        phsa := 0
          ' do other stuff here
          ' at least 20ms, but less than 26 seconds (at clkfreq=80MHz)
        pause(1000)   ' just a pause for example
        pst.dec(phsa)
        pst.newline
    
    PRI Pause(ms)
      waitcnt(clkfreq/1000*ms+cnt)
    
  • SRLMSRLM Posts: 5,045
    edited 2014-03-21 05:50
    Thanks for the code. I'll give it a shot and see if I can make this work in C++.
  • photomankcphotomankc Posts: 943
    edited 2014-03-21 06:54
    I'll see if I can access my C++ code. That's exactly how I measure the TOF from my sonar sensors.
Sign In or Register to comment.