Shop OBEX P1 Docs P2 Docs Learn Events
pulse counting — Parallax Forums

pulse counting


Hi All, I would like to count low to high transitions on pin 2(I/O 1). Total number of pulse will be displayed on terminal.
Code below. Thanks

Pub Start | pulsecounter

pst.Start(115_200) 'this sets up terminal

CTRA := %01101000000000000000000000000010 ' - pos edge
FRQA := $01 'this is the count of events
PHSA := $01

dira[1] := 0 'make pin 2(P1) an input to receive pulses

repeat
pst.str(string(" CTRA Value = "))
pst.bin(CTRA,32)

pst.str(string(" PHSA Value = "))
pst.bin(PHSA,32)

pst.str(string(" Pulse Count = ")) 'show Pulse Count value
pst.bin(FRQA,32)
pst.newline

Comments

  • Here's a method that you can call from the main body of the program.
    pub count_edges(pin, ms) | t0                                   ' count positive edges for ms milliseconds
    
      ctra := (%01010 << 26) | pin                                  ' setup counter
      frqa := 1
      phsa := 0
      
      t0 := cnt                                                     ' sync timing
      repeat ms                                                     ' loop                                 
        waitcnt(t0 += (clkfreq / 1000))                             '  delay 1ms
    
      ctra := 0                                                     ' stop counting
    
      return phsa
    

    The great thing about the Propeller is that it can test itself. You can create an oscillator with the other counter and connect it to the same pin -- I just did this and it works as expected.
    pub freqout(pin, freq)
    
      ctrb := (%00100 << 26) | pin
      frqb := ($8000_0000 / (clkfreq / freq)) << 1
      phsa := 0
      dira[pin] := 1
    
  • Thanks JM, I will try it. Appreciate your help.
  • JM will low to high transition come in on pin 2
  • Those are methods to plop into your own test program. Call freqout() first to create test pulses on a pin, then call count_edges() with that same pin and the period (in milliseconds) that you want to count.

    Here's something you can drop into a program to see that the mechanisms work. You'll need to define an output to display the counts (hertz in this case).
    con
    
      TACH_PIN  = 2
    
      TEST_FREQ = 10000
    
    
    var
    
      long  hertz                                                         
                                                                     
                                                                     
    pub main | t                                                         
                                                              
      ctra := (%00100 << 26) | TACH_PIN                             ' generate test frequency
      frqa := ($8000_0000 / (clkfreq / TEST_FREQ)) << 1
      phsa := 0
      dira[TACH_PIN] := 1
    
      ' count 0->1 transitions
    
      ctrb := (%01010 << 26) | TACH_PIN                             ' setup counter
      frqb := 1
      phsb := 0
    
      t := cnt
      repeat
        waitcnt(t += clkfreq)
        hertz := phsb
        phsb := 0
    
        { display hertz here }
    
  • There are lots of ways to count pulses using a Propeller. The way JonnyMac showed you it probably the best option for this application but there are times when a loop running in assembly will produce a more accurate count if the input signal has noise on it.

    I discussed by confusion about differences in pulse counting techniques in this thread. The thread includes code I used to monitor pulses using three different strategies. You might find the code useful for your purposes (though the code JonnyMac posted should do the job just fine).
Sign In or Register to comment.