PDA

View Full Version : How long a variable has the same value?



DanPBP
- 22nd February 2008, 06:48
Hi Guys,

I'm reading a value from pulsin (pic12f675), I want to know if that value is greater than 150, but I also want to know if that value stays greater than 150 for over than 3 seconds.

--------------------
value var byte

loop:

PulsIn GPIO.0, 1, value

IF (value > 150) Then
'do something here
EndIF

GoTo loop

----------

The first part it's easy, the second part, well, still looking on how to do it.

I searched the forum, but no luck. Could someone give me a hand?

Thanks!

Daniel.

sayzer
- 22nd February 2008, 10:07
Have a look at this;

Modify as desired.


<font color="#000000">Value <font color="#000080"><b>VAR BYTE

</b></font>T1CON = <font color="#FF0000"><b>%00110100 </b></font><font color="#000080"><i>'TMR1GE enabled, Prescaller 1:8, LP OFF, No Sync, Internal Clk
</i></font>TMR1IF <font color="#000080"><b>VAR </b></font>PIR1.<font color="#FF0000"><b>0 </b></font><font color="#000080"><i>'Timer1 Overflow bit.
</i></font>TMR1ON <font color="#000080"><b>VAR </b></font>T1CON.<font color="#FF0000"><b>0 </b></font><font color="#000080"><i>'Timer1 Start/Stop bit.

</i></font>Pulsepin <font color="#000080"><b>VAR </b></font>GPIO.<font color="#FF0000"><b>0
</b></font>Limit <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>150 </b></font><font color="#000080"><i>' Desired Pulse limit.
</i></font>TimeDone <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>6 </b></font><font color="#000080"><i>' Desired Time. Each unit is 524mS. 6 = approx. 3 secs.
</i></font>CountTime <font color="#000080"><b>VAR BYTE

</b></font>Loop:

TMR1L = <font color="#FF0000"><b>0
</b></font>TMR1H = <font color="#FF0000"><b>0
</b></font>TMR1IF = <font color="#FF0000"><b>0
</b></font>TMR1ON = <font color="#FF0000"><b>0

</b></font><font color="#000080"><b>PULSIN </b></font>Pulsepin, <font color="#FF0000"><b>1</b></font>, Value

<font color="#000080"><b>IF </b></font>Pulsepin &gt; Limit <font color="#000080"><b>THEN </b><i>' Incoming pulse is greater then desired pulse limit.
</i></font>TMR1ON = <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Start Timer.
</i></font>CountTime = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Clear time variable.

</i><b>WHILE </b></font>Pulsepin &gt; Limit <font color="#000080"><i>'Loop while the incoming pulse is greater then limit.
</i><b>PULSIN </b></font>Pulsepin, <font color="#FF0000"><b>1</b></font>, Value

<font color="#000080"><b>WHILE </b></font>TMR1IF = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Wait about 524ms.
</i><b>PULSIN </b></font>Pulsepin, <font color="#FF0000"><b>1</b></font>, Value <font color="#000080"><i>' Keep reading.
</i><b>IF </b></font>Pulsepin =&lt; Limit <font color="#000080"><b>THEN </b></font>Jump <font color="#000080"><i>' In case Pulse changed then exit!
</i><b>WEND
</b></font>TMR1IF = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>'Clear timer1 overflow bit.
</i></font>CountTime = CountTime + <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Count Timer1 ticks,
</i><b>IF </b></font>CountTime = TimeDone <font color="#000080"><b>THEN </b></font>Dothings <font color="#000080"><i>' ...Exit loop if time is out.
'and do your stuff.

</i></font>Jump:

<font color="#000080"><b>WEND

ENDIF


GOTO </b></font>Loop


Dothings:


<font color="#000080"><i>' do somethings here.


</i><b>GOTO </b></font>Dothings

DanPBP
- 22nd February 2008, 23:54
Great!!! Thanks a lot!!!

I'll read it and test it this weekend.

Daniel.