pulse counting
in Propeller 1
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
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 phsaThe 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.
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 }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).