PDA

View Full Version : Counter B



Dug
01-09-2008, 05:35 AM
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[20]:=1··················· (disables·pin 20·from·counting the inbound signal until ready to perform a count)
·· dira[15]:=1··················· (sets up·pin 15 to·drive an LED to indicate final count result)
·· dira[19]:=0····················(enables pin 19 to read the X reference signal)
·· ctrb[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[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[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[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[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?



·

Paul Baker
01-09-2008, 05:47 AM
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 (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Dug
01-09-2008, 06:24 AM
Sorry Paul. I accidently left out this line in my post which does follow the line:dira[19]:=0 in my program:
ctrb[14..9]:=20

deSilva
01-09-2008, 06:45 AM
It is the PINA you need, not the PINB http://forums.parallax.com/images/smilies/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.

Dug
01-09-2008, 07:10 AM
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[5..0], are the Apin bits and ctra[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[5..0]:=19 and ctrb[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 Baker
01-09-2008, 07:16 AM
Yes you now understand correctly.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Dug
01-09-2008, 08:12 AM
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 Stabler
01-09-2008, 08:21 AM
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

deSilva
01-09-2008, 08:29 AM
Well, the most important - and very comprehensive!- source of information ist just this diagram. Study it! http://forums.parallax.com/images/smilies/smile.gif

Graham Stabler
01-09-2008, 08:35 AM
My problem was that when I read there was a PWM mode I sort of expected it to produce a PWM signal.

Paul Baker
01-09-2008, 08:37 AM
Yeah, thats why we've moved away from calling it PWM and started calling it just NCO.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

deSilva
01-09-2008, 08:45 AM
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 http://forums.parallax.com/images/smilies/smile.gif

Paul Baker
01-09-2008, 09:06 AM
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 (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

deSilva
01-09-2008, 10:25 AM
Hmm, so you want to avoid all redundancy http://forums.parallax.com/images/smilies/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...