Dug
01-09-2008, 04: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?
·
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?
·