Shop OBEX P1 Docs P2 Docs Learn Events
Smart pin count mode %01100 w/o using adjacent pin — Parallax Forums

Smart pin count mode %01100 w/o using adjacent pin

I'm probably missing something simple here. I want to configure a smart pin to count rising edges. Easy, right? BUT I dont want to waste an adjacent pin. The manual says:

**Count A-Input Positive Edges When B-Input Is High (%01100)**
● X[31:0] establishes a measurement period in clock cycles.
● If zero is used for the period, the measurement operation will not be periodic, but continuous, like a
totalizer, and the current 32-bit high count can always be read via RDPIN / RQPIN.
● If a non-zero value is used for the period, events will be counted for that many clock cycles and then the
result will be placed in Z, while the accumulator will be set to the 0/1 value that would have otherwise
been added into it, beginning a new measurement. This way, all events get counted across
measurements. At the end of each period, IN will be raised and RDPIN / RQPIN can be used to retrieve the
32-bit measurement.
● During reset (DIR=0), IN is low and Z is set to the adder value (0/1).

This is exactly the functionality I want, BUT, I don't want/need that adjacent pin. The B-Input does nothing for me. I need an always-on totalizer. If it gets a pulse, it increments Z, regardless of anything the adjacent B-pin is doing. How do I do this?

Comments

  • JRoarkJRoark Posts: 1,215
    edited 2023-05-07 15:54

    And, I'm happy to report the dunce cap still fits! :)

    Count A-Input Rises (%01110) looks like it's what I should have been using.

  • evanhevanh Posts: 15,912

    Mode %01100 is what you'd use for performing Sinc1 filtering for a bitstream ADC. SmartA input is the clock pin and SmartB input is the bitstream pin.
    Of course, there is the Sinc2 and Sinc3 filters in the other specialised smartpin modes so probably this one will never get used.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2023-05-07 16:10

    This works:

    pub main() | x, t
    
      ' setup pulse generator, 10Hz
    
      x.word[0] := 1 #> ((clkfreq / 10) / 100_0) <# $FFFF            ' set unit timing
      x.word[1] := 100_0                                             ' set period (units)
      pinstart(PULSER, P_PWM_SAWTOOTH | P_OE, x, 50_0)               ' start pwm (sawtooth)
    
      ' setup edge counter
    
      pinstart(COUNTER, P_COUNT_RISES, 0, 0)
    
    
      t := getct()
      repeat
        waitct(t += CLK_FREQ)
        x := rdpin(COUNTER)
        debug(udec(x))
    

    Of course, you can reset the count like this if needed:

        pinf(COUNTER)
        pinl(COUNTER)
    
  • JRoarkJRoark Posts: 1,215
    edited 2023-05-07 18:31

    Many thanks to all. @Wuerfel_21 and I posted simultaneously. Happy to report its working nicely using @JonnyMac / Ada's approach.

    FWIW, my function generator goes up to 25 mhz, and the P2 at 160mhz has no problem keeping up with that. Pretty cool to just "set and forget" and let the hardware deal with counting. Screen cap at 25 mhz square wave input swinging rail to rail with a 1 sec sampling period:

    #       Z
    ===============
    1       37202                                                                   
    2       25047437                                                                
    3       50060577                                                                
    4       75073717                                                                
    5       100086857                                                               
    6       125101093                                                               
    7       150115328                                                               
    8       175129563                                                               
    9       200143798                                                               
    10      225158033                                                               
    

    @evanh I have to admit I'm not following you. The manual says the ADC stuff sits at %11000 and %11001. Your statements are always carefully measured, so I'm trying to figure-out what I missed.
    ADC Sample/Filter/Capture, Internally Clocked (%11000)
    ADC Sample/Filter/Capture, Externally Clocked (%11001)

  • RaymanRayman Posts: 14,633

    Just FYI, for low frequencies, I think the reciprocal counter mode is nice.
    https://forums.parallax.com/discussion/170882

  • @Rayman said:
    Just FYI, for low frequencies, I think the reciprocal counter mode is nice.
    https://forums.parallax.com/discussion/170882

    Gotta save that trick for my next project. 👍

  • evanhevanh Posts: 15,912

    @JRoark said:
    @evanh I have to admit I'm not following you. The manual says the ADC stuff sits at %11000 and %11001. Your statements are always carefully measured, so I'm trying to figure-out what I missed.
    ADC Sample/Filter/Capture, Internally Clocked (%11000)
    ADC Sample/Filter/Capture, Externally Clocked (%11001)

    Those are the specialised Sinc filters for handling Sigma-Delta ADC bitstreams. They each optionally perform Sinc2 or Sinc3 filtering, but not Sinc1.

    Mode %01100 isn't explicitly an ADC mode. It just happens to be how a Sinc1 filter works. So it's the Sinc1 version of the externally clocked mode %11001 above.
    Mode %01111 (with Y=0) would be the Sinc1 version of mode %11000 above.

  • evanhevanh Posts: 15,912

    BTW: Sigma-Delta bitstreams, both from an ADC and to a DAC, are a form of PDM (Pulse Density Modulation) that I've been raving about elsewhere. They'll get called a bitstream because of the matching synchronous clock that goes alongside.

Sign In or Register to comment.