Counter B
Dug
Posts: 11
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
·· 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?
·
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
·· 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 Baker
Propeller Applications Engineer
Parallax, Inc.
ctrb[noparse][[/noparse]14..9]:=20
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.
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 Baker
Propeller Applications Engineer
Parallax, Inc.
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
The duty cycle mode however should never be called PWM mode, please
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
I think is would be good marketing to include the term PWM with timer operations somehow.. A slash can be interpreted in many ways...