Hi All,
I'm trying to count the number of cycles of a square wave (about 1 KHz) and decrement a word variable to zero. I have tried the following code (and small variants):
Counter = 1000
Pulsecount:
Do While High PORTA.4
Loop
Counter = Counter - 1
Do While Low PORTA.4
Loop
If Counter > 0 then goto Pulsecount
However, it appears that it never exits. Is the input too fast, or is there an error in this code?
Thanks for any ideas!
Terry
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't think it is recognizing the condition you are trying to create.
Do[{While|Until}condition]
loop
DoWhileHighPORTA.4Loop
I think you need to form the condiont using an '=' sign.
Do while PortA.4 = High
Or
Do while PortA.4 = 1
I personally prefer the second one because it says what you are looking for. Where as 'high' is defined as 1 but as you change languages it may not be defined or be a keyword.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Plus, if the program has #option explicit it would not have compiled.
#chipmega328p,20'chosen as guess..#option Explicit Dim Counter as Word Dir PORTA.4 In Counter = 1000 Pulsecount: Do While High PORTA.4 Loop Counter = Counter - 1 Do While Low PORTA.4 Loop If Counter > 0 then goto Pulsecount
This source failed to compile with the warning
Variable HIGHPORTA was not explicitly declared
Variable LOWPORTA was not explicitly declared
Last edit: Anobium 2023-01-01
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The previous use of Counter = Counter - 1 and If Counter > 0 is logical code but will be slow. Adding the [WORD]Counter +1 and then checking the condition [WORD]Counter > 1 uses 26 words more and calls to subroutines.
The Repeat loop simply sets a SysRepeatTemp variable and use DECrement to test it is not zero - a lot faster.
Enjoy.
Last edit: Anobium 2023-01-01
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi mmote and Anobium, Many thanks for the suggestions. I'm pleased to report that using the faster loop has resolved the issue. The loop now exits correctly!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Counting Pulses can also be done using a Timer. In this case Timer1 works nicely. The code below should be self-explantory. Timer1 is being used in Counter Mode ( default)
Hi All,
I'm trying to count the number of cycles of a square wave (about 1 KHz) and decrement a word variable to zero. I have tried the following code (and small variants):
Counter = 1000
Pulsecount:
Do While High PORTA.4
Loop
Counter = Counter - 1
Do While Low PORTA.4
Loop
If Counter > 0 then goto Pulsecount
However, it appears that it never exits. Is the input too fast, or is there an error in this code?
Thanks for any ideas!
Terry
I don't think it is recognizing the condition you are trying to create.
I think you need to form the condiont using an '=' sign.
Do while PortA.4 = High
Or
Do while PortA.4 = 1
I personally prefer the second one because it says what you are looking for. Where as 'high' is defined as 1 but as you change languages it may not be defined or be a keyword.
MMOTTE is spot on.
Plus, if the program has
#option explicit
it would not have compiled.This source failed to compile with the warning
Last edit: Anobium 2023-01-01
And, if you are looking for speed, consider the code below.
The previous use of
Counter = Counter - 1
andIf Counter > 0
is logical code but will be slow. Adding the [WORD]Counter +1 and then checking the condition [WORD]Counter > 1 uses 26 words more and calls to subroutines.The Repeat loop simply sets a SysRepeatTemp variable and use DECrement to test it is not zero - a lot faster.
Enjoy.
Last edit: Anobium 2023-01-01
Hi mmote and Anobium, Many thanks for the suggestions. I'm pleased to report that using the faster loop has resolved the issue. The loop now exits correctly!
Pleasure. HNY.
Mr Kent posted a 328 frequencycounter years ago. I got the code.
Hi,
Counting Pulses can also be done using a Timer. In this case Timer1 works nicely. The code below should be self-explantory. Timer1 is being used in Counter Mode ( default)
Last edit: William Roth 2023-01-05
A nice example,
How could this be modified to use an interrupt to remove the
wait until...
blocking ? Then, thedo loop
could be free running.Yes.
Would the setup be the same for the 16F1829 ? As the 16f18026 requires PPS.
Same setup except there is No PPS for 16F1829.
The T1CKI Pin is fixed at RA5 ( Pin2) on the 16F1829. So connect the 1KHz signal to Pin 2 ( RA5)
Last edit: William Roth 2023-01-05