Shop OBEX P1 Docs P2 Docs Learn Events
Please Explain — Parallax Forums

Please Explain

electromanjelectromanj Posts: 270
edited 2009-12-12 01:55 in Propeller 1
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!····


·

Comments

  • Graham StablerGraham Stabler Posts: 2,510
    edited 2009-12-12 01:20
    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.

    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)
  • electromanjelectromanj Posts: 270
    edited 2009-12-12 01:55
    Thanks. Need to study what you said. Thanks for the code in the box details. I'll try it next post.
Sign In or Register to comment.