This is fixed sequence, and I want PBP to return "3", since in 15ms period it gets 3 pulses.
This is fixed sequence, and I want PBP to return "3", since in 15ms period it gets 3 pulses.
How often is this pattern repeating? Constantly?
Count will start counting immediately and then continue to count for 15ms. It won't sit there waiting for the first pulse and THEN count for 15ms.
What if the start of the command occurs in the middle of your 3 pulse sequence? Or in between two of those 3 pulse patterns?
The fact that always seem to get MORE than 3 counts indicates that your pin isn't driven to both states. Verify that you have well defined logic states for both '1' and '0' on the input.
/Henrik.
Above code is launched, when signal on input pin arrives, I just missed that line here.
I discarded the COUNT statement, and wrote software substitute, which accurately counts amount of pulses:
I'm interested, why count never works as it should.Code:UIDEDA: if pulsi=1 then FOR N=0 TO 300 'this is length of capture period, 300 for 16F870 @ 4mhz means approx 15ms. IF PULSI=1 THEN A=A+c c=0 d=1 ELSE B=B+d d=0 c=1 ENDIF NEXT IF A<>0 AND B<>0 THEN lcdout $FE,2, "A=",#A, " " lcdout $FE,$C0,"B=",#B, " " ENDIF A=0 B=0 endif GOTO UIDEDA
Sounds like noise to me.... Try a schmidt trigger between what you are counting and the pic.
Dave Purola,
N8NTA
EN82fn
If it was noise issue, why my code works perfectly?
The manual says with a 4MHz oscillator as you have, the pin is sampled every 20us which is way too slow for the fractional pulse durations you stated.
The different results can easily be explained since any low or high period could be entirely missed.
Your code is still pretty slow.
The fastest I can think of right now in PBP is this:
Then Count will be the number of pin changes.Code:newbitstate var bit oldbitstate var bit counter var word count var word duration var word newbitstate = 0 oldbitstate = 0 duration = 9000 ‘ set duration to sample the input count = 0 ‘ reset count for counter = 0 to duration newbitstate = portb.0 if newbitstate != oldbitstate then count = count + 1 endif oldbitstate = newbitstate next duration
To get the pulse count, rather than check in the routine and slow it down,
just divide Count by 2 at the end, to get the number of rising edges,
and if the remainder is 1, the duration ended while the port pin was high.
0.8ms=800us, so it fits very well![]()
Bookmarks