If the pin is already in the the state of the defined "pulse", it starts counting as soon as PULSIN is called and stops counting when the trailing edge is detected.
Sorry, but I respectfully dissagree with this.
Try this;
Code:
DEFINE OSC 20
DEFINE LOADER_USED 1
PTIME VAR WORD
PTIME2 VAR WORD
SYMBOL PIN_PIN = PORTB.0
TRISB = %11111101
PTIME = 0
MAIN:
WHILE PIN_PIN = 0 ' wait for PIN_PIN to go high
WEND
HIGH 1 ' indicate PULSIN start
PULSIN PIN_PIN,1,PTIME ' this reads the pulse after the pin is already high
LOW 1 ' indicate PULSIN complete
PULSIN PIN_PIN,1,PTIME2 ' this reads it after the rising edge
HSEROUT ["Pulse 1 width = ",DEC PTIME,13,10] ' result = 0
HSEROUT ["Pulse 2 width = ",DEC PTIME2,13,10] ' result = 25419
GOTO MAIN
I have a 12F675 programmed to output a train of pulses. 50mS high with a low period of
250mS between each high pulse.
See the analyzer capture below. P1A is the pulse train being read with PULSIN. P1B is RB1
going high just prior to the 1st PULSIN, and low when the 1st PULSIN completes.
Look at the A & B markers. Now look at the time A-->B: 181.9mS. 50mS of this period is
the 50mS pulse shown by P1A at the top. P1B is the high period RB1 stays high once the
1st PULSIN executes. Subtract 50mS. The remainder is the time-out period at 20MHz.
If PULSIN were not waiting for the leading edge, then it should always return the remainder
of the high time period in PTIME, but it doesn't. It returns zero.
Now if you shorten the low period between each high-going pulse to < the PULSIN time-out
period, it will record the 2nd high-going pulse. Not the first if started when the pin is in
the state to be measured.
So, what I'm seeing is - PULSIN does need to see the leading edge. It will however appear
to work, even if started after the leading edge, but will only record the time of the 2nd
pulse - if it happens prior to the time-out period.
I have tried several variations, and I just can't get any results if I start PULSIN after
a leading-edge transition. Except for it locking onto & recording a 2nd pulse that happens
before the time-out period expires.
Bookmarks