Shop OBEX P1 Docs P2 Docs Learn Events
A little more help on smartpins — Parallax Forums

A little more help on smartpins

CRST1CRST1 Posts: 103
edited 2019-12-05 01:32 in Propeller 2
With your help I finally got the P2 counting on a smartpin.
The part of code that counts is:
PUB Main | tmr1, count1
  clkset(oscmode, freq)
  'start the SmartSerial object
  serial.Start(rx, tx, %0000, baud)                    
  coginit(7, RPMs, @RPMStack)  
 
  asm
    dirl #7                                            'disable pin
  endasm

    wrpin_(%0000_0000_000_0000000000000_00_10011_0,7)  'times rise to rise in clocks
    wxpin_(1,7)                                        'times one cycle
    wypin_(%0,7)                                       'rise to rise

  asm
    dirh #7                                            'enable pin
  endasm
 
repeat
    asm
      rdpin    tmr1, #7 {wc}
    endasm

  serial.str(string(" Tmr "))  
  serial.dec(tmr1)
  serial.str(string("     ")) 

PUB  RPMs
  dirb[24] :=1

  repeat
    outb[24] :=1
    waitcnt(cnt + 5_000_000)
    outb[24] :=0
    waitcnt(cnt + 5_000_000)


The pulses generated are 16hz which reads out as 10000032 which is 16hz at 160mhz clock.
All is good until I unplug the jumper and the count stays at 10000032.
How would I reset Z to 0 when the pulses stop?
I tried using dirl #7 and dirh #7 but then I get a fluctuating count.

Thanks again for any help.

Comments

  • AJLAJL Posts: 517
    edited 2019-12-05 02:17
    You aren't monitoring the IN bit for the smartpin in question.
    I think you'll find that with no edges to time, the Z register (which holds the result) will still give you the result of the last completed measurement, but the IN bit will be low.

    If you tested INA[7] before RDPIN you'd know whether the Z register contents was current or stale. Simply waiting for a new reading won't achieve what you want so you need to keep looping and detect valid new readings and act accordingly.

    As your loop could easily cycle more than once per pulse you need to keep track of this in a variable.

    It could be something as crude as
    repeat
        if ina[7] == 0                'new count available?
            stalecount++           'no 
        elseif
            stalecount := 0       'yes, clear stalecount and get it
            asm
              rdpin    tmr1, #7 {wc}
            endasm
    
        if stalecount > toolong  ' reading is too old, so pulse train has been lost
            tmr1 := 0
    
      serial.str(string(" Tmr "))  
      serial.dec(tmr1)
      serial.str(string("     ")) 
    
    

    Finding a value for toolong might need some trial and error, or some cycle counting based on the final code.

    EDIT: Of course, the whole status testing, stale count tracking, and pin reading could be done in asm, but the serial object timing is the timing wildcard.
  • CRST1CRST1 Posts: 103
    edited 2019-12-05 02:30
    Can you tell me what sets the ina to 0? Is it when read with rdpin?
    I take it it's set to 1 when it reads a complete pulse from rise to rise. then what sets it low again?

    The reason for this is to read a speed and tach pulse train for a vehicle.
  • evanhevanh Posts: 15,916
    edited 2019-12-05 02:44
    From the main google doc, at beginning of smartpins:
    When a mode-related event occurs in a smart pin, it raises its IN signal to alert the cog(s) that new data is ready, new data can be loaded, or some process has finished. A cog acknowledges a smart pin whenever it does a WRPIN, WXPIN, WYPIN, RDPIN or AKPIN on it. This causes the smart pin to lower its IN signal so that it can be raised again on the next event. Note that since the RQPIN instruction (read quiet) does not do an acknowledge, it can be used by any number of cogs, concurrently, to read a pin without bus conflict.

    And for mode %10011:
    Upon completion of X periods, the measurement is placed in Z, IN is raised, and a new measurement begins. RDPIN/RQPIN can then be used to retrieve the completed measurement. Z will be limited to $80000000.

    So, yep, your hunches are all correct.

  • Thanks. I guess I read that, I have those parts printed, it's just so much new to learn and I don't injest it when I'm just reading it. I will probably have more questions, especially when I get into the analog to digital stuff. I'll try the ina monitor and let you guys know how it comes out.
  • Update
    Yes that will work. I had tried similar before but was using clock values for the counter loop. It was way too long of loop and never reset to 0. Brought the number way down and now it works with the too long counter.
    It's been a long three days of studying but its getting there.
    Thank you
  • CRST1 wrote: »
    Update
    Yes that will work. I had tried similar before but was using clock values for the counter loop. It was way too long of loop and never reset to 0. Brought the number way down and now it works with the too long counter.
    It's been a long three days of studying but its getting there.
    Thank you

    Glad to be of help. Living is learning, or at least it should be.
Sign In or Register to comment.