Shop OBEX P1 Docs P2 Docs Learn Events
Counter B — Parallax Forums

Counter B

DugDug Posts: 11
edited 2008-01-09 02:25 in Propeller 1
I have two continuous square wave signals, both running at about 1 KHz and both are in phase.
Let's call them X and Y.
X·is connected to Pin 19 and·Y to Pin 20.
First I use·X for a reference and·count the number of clock pulses during·one single·positive duration of Y.
I use Counter A and store the results of the count in phsa·in·a variable.
When I do that, I have no problem.
I then switch to Counter B and use Y as a reference and store the count during a single positive duration of X in phsb and store that in a different variable.
However, I observe that Counter B does not count.
Here is the code I am using for the Counter B portion;

PUB CountB
·· pinX:=19
·· pinY:=20
·· dira[noparse][[/noparse]20]:=1··················· (disables·pin 20·from·counting the inbound signal until ready to perform a count)
·· dira[noparse][[/noparse]15]:=1··················· (sets up·pin 15 to·drive an LED to indicate final count result)
·· dira[noparse][[/noparse]19]:=0····················(enables pin 19 to read the X reference signal)
·· ctrb[noparse][[/noparse]30..26]:=%01000···· (sets up Counter B to count during positive level of BPIN)
·· frqb:=1·························(load "1" at each clock edge into the B counter accumulator)
·· waitpne(l<pinX,lpinX,0)····(wait until reference pin 19 goes Low)
···phsb~························ ·(clear the B counter accumulator)
·· dira[noparse][[/noparse]20]:=0···················(set pin 20 to input to count incoming signal)
·· waitpne(l<pinX,lpinX,0)·· ·(wait until counter has counted high duration of signal on pin 20 and·pin 20 has gone back low)
·· dira[noparse][[/noparse]20]:=1·················· (disables pin 20 from counting the incoming signal any further)
···B:=phsb······················· (moves count stored in B accumulator to variable B)
·· repeat························· (forms a loop out of the following "if" statement to keep the LED in its state)
·· if B==0
····· outa[noparse][[/noparse]15]:=1··············· (if statement says if the contents of the B accumulator = 0 turn on the LED. If the contents are anything else but 0,
·· else······························· keep the LED turned off)
····· outa[noparse][[/noparse]15]:=0

After running this program,·the contents of the B counter are always zero (the B counter does not count). Using the same code but setting it up to count with the A counter results in·expected normal counter operation (the A counter stores counts and A>0 results in the LED coming on).
Variable A and B are stated in a long·VAR statement.

The comments I placed in (....) have been added to this post for explanation purposes·but are not part of my actual Method.

Running the program in a different Cog results in the same problem so it is not a bad B counter in the Cog.
Any ideas?



·

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-08 21:47
    You are trying to count events on pin 20, but you never indicated to the counter which pin it should monitor (or rather you didn't specify it so it's counting pin 0). This information needs to be conveyed in the setting of ctrb.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • DugDug Posts: 11
    edited 2008-01-08 22:24
    Sorry Paul. I accidently left out this line in my post which does follow the line:dira[noparse][[/noparse]19]:=0 in my program:
    ctrb[noparse][[/noparse]14..9]:=20
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-08 22:45
    It is the PINA you need, not the PINB smile.gif
    THis indeed can be confusing. PINA and PIN B have nothing to do with counter A or counter B. Each counter has ITS OWN SET of PINA and PINB.

    There are two pins configurable for each timer/counter to allow a lot of very tricky operations, have a lopk in the modes the counters have for their logical combinations.
  • DugDug Posts: 11
    edited 2008-01-08 23:10
    Wow. This is confusing!

    In summary then, each Cog has two timers called Timer A and Timer B. Each of those timers can be configured to use up to two pins each, correct?

    In·Spin, ctra[noparse][[/noparse]5..0], are the Apin bits and ctra[noparse][[/noparse]14..9] are the bpin bits for each counter then correct?

    So in my example, to use Counter A and Counter B, and use both counters single ended and not differentially or logically (counting on a single pin to ground), I should configure·both Methods to use ctra[noparse][[/noparse]5..0]:=19 and ctrb[noparse][[/noparse]5..0]:=20 so the Apin 19·is used for Counter A counting and the Apin 20 is used for Counter B conting.

    Do I have this correct now?
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-08 23:16
    Yes you now understand correctly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • DugDug Posts: 11
    edited 2008-01-09 00:12
    Modified code and all is well now.

    Thank you for helping sort this out!

    It would have been helpful to call the two counters by a different name then their respective input pins.

    Perhaps Counter #1, input pins A and B and Counter #2, input pins A and B for each Cog.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-01-09 00:21
    Believe me the counters being called A and B and the pins being A and B is hardly anything to complain about, now that there is the application note on the counters life is good. I learnt about them before it came out, not fun.

    Graham
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-09 00:29
    Well, the most important - and very comprehensive!- source of information ist just this diagram. Study it! smile.gif
    650 x 586 - 33K
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-01-09 00:35
    My problem was that when I read there was a PWM mode I sort of expected it to produce a PWM signal.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-09 00:37
    Yeah, thats why we've moved away from calling it PWM and started calling it just NCO.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-09 00:45
    I shall elaborate a little bit more on that on the wiki page soon. Because of both uses the term "NCO/PWM" mode is still best!
    The duty cycle mode however should never be called PWM mode, please smile.gif
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-09 01:06
    Technially PWM is a subset of NCO, so it's not really accurate to elevate PWM to the same level as NCO. Another way of looking at it is NCO/PWM is redundant since NCO implies PWM.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-09 02:25
    Hmm, so you want to avoid all redundancy smile.gif
    I think is would be good marketing to include the term PWM with timer operations somehow.. A slash can be interpreted in many ways...
Sign In or Register to comment.