View Full Version : Please Explain

12-12-2009, 08:06 AM
Hello to All!

I am have some trouble understanding exactly whats going on in this method. Any and all explanations would be helpful.
This is the PUB COUNT from the BS2_Functions Object.

PUB COUNT (Pin, Duration) : Value
и Counts rising-edge pulses on Pin for Duration in mS
и Maximum count is around 30MHz
и Example Code:
иии x := BS2.count(5,100)иииииииииииииии ' Measure count for 100 mSec
иии BS2.Debug_Dec(x)ииииииииииииииииииии ' DEBUG value
иии BS2.Debug_Char(13)ииииииииииииииииии ' CR
ииииии dira[PIN]~иииииииииииииииииииииииииииииииииииииииии ' Set as input
ииииии ctra := 0ииииииииииииииииииииииииииииииииииииииииии ' Clear any value in ctrb
ииииииииииииииииииииииииииииииииииииииииииииииииии ииииииии ' set up counter, pos edge trigger
ииииии ctra := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN)и
ииииии frqa := 1ииииииииииииииииииииииииииииииииииииииииии ' 1 count/trigger
ииииии phsa := 0ииииииииииииииииииииииииииииииииииииииииии ' Clear phase - holds accumulated count
ииииии pause(duration)иииииииииииииииииииииииииииииииииии и ' Allow to count for duration in mS
ииииии Value := phsaииииииииииииииииииииииииииииииииииииии ' Return total count
ииииии ctra := 0ииииииииииииииииииииииииииииииииииииииииии ' clear counter mode

The main question is what is going on in the
ctra:=(%01010<<26 | (%001<<23 | (0<<9)| (PIN)
Also the
pause (duration)
I assume the Value in Value := phsaиwould be a predefined var and could be anyиname.

Sorry about the cluttered post I don't know how toиpost code with the white boxes and blue letters.
Thanks for any help!ииии


Graham Stabler
12-12-2009, 08:20 AM
OK this method is using the hardware counters in each cog counters, the best way to learn about these is using the application note which can be downloaded from the documentation area on Parallax's website. But I'll give you the gist.

CTRA is the control register for counter A (each cog has two, A and B) some of the higher bits set the mode the counter is in, the lower bits effect which pins are used (if any).

The line

ctra := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN)

Is building the 32 bit number CTRA out of portions of binary. %01010 is the mode for example and occupies the most significant bits. Check out the app note (and the manual for more details). In this case the left shift command << is being used to put the binary snippets in the right location, then they are combined by bitwise ORing them.

The pause is to provide a period of time in which the pulses are counted, the counting of pulses is done by the hardware counter, every time a pulse comes in on pin FRQA (which is set to one) is added to PHSA (which was cleared to zero) and so after the delay the count is in PHSA and this is copied into value before any more pulses are counted.


p.s. To put code in a box surround it with these tags [code ] your code here [/code ] (but take out the spaces inside the square brackets)

12-12-2009, 08:55 AM
Thanks. Need to study what you said. Thanks for the code in the box details. I'll try it next post.