Please Explain
electromanj
Posts: 270
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[noparse][[/noparse]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!····
·
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[noparse][[/noparse]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!····
·
Comments
CTRA is the control register for counter A (each cog has two, A and some of the higher bits set the mode the counter is in, the lower bits effect which pins are used (if any).
The line
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.
Graham
p.s. To put code in a box surround it with these tags [noparse][[/noparse]code ] your code here [noparse][[/noparse]/code ] (but take out the spaces inside the square brackets)