Can i measure 2 secs with pulsin?

# Thread: Can i measure 2 secs with pulsin?

1. ## Can i measure 2 secs with pulsin?

just like that, Can I measure 2 seconds with pulsin? or what is the max value that i can store in a variable with pulsin?

what i want to do is, hold a button just when i turn on the pic, and if the button is 2 seconds-hold or more do something

And another question,

if i want to store more tan one byte with serin this will work?

serin porta.1,N2400,["A"], var1, var2, var3 'for 3 vars???
Last edited by lugo.p; - 9th March 2010 at 00:23. Reason: correcting

2. No, 2 seconds is too long for pulsin.

According to the manual:
"The resolution of PULSIN is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the pulse width is returned in 10us increments. If a 20MHz oscillator is used, the pulse width will have a 2us resolution. "

Since the pulsin will return a WORD, the max pulse length is 65535 x 10uS (for a 4MHz clock) which is only 0.655 seconds.

For a 20MHz osc it would only be 0.131 seconds.

steve

3. thanks steve, can you tell me how i can measure a 2 seconds button-pressed?

4. I'll give you a hint... how many 1/2 seconds in 2 seconds? How many 1/10 of a second in 2 seconds? That's if you want to do it in the "foreground"... hmmm...

5. MyVar = 0 ' zero on entry
WHILE INPUTPIN = desired state
PAUSE 500 ' 1/2 second delay
MyVar = MyVar +1
WEND

When it exits just check the value in MyVar.

if i want to store more tan one byte with serin this will work?

serin porta.1,N2400,["A"], var1, var2, var3 'for 3 vars???
Yep.

6. Originally Posted by Byte_Butcher
No, 2 seconds is too long for pulsin.

According to the manual:
"The resolution of PULSIN is dependent upon the oscillator frequency. If a 4MHz oscillator is used, the pulse width is returned in 10us increments. If a 20MHz oscillator is used, the pulse width will have a 2us resolution. "

Since the pulsin will return a WORD, the max pulse length is 65535 x 10uS (for a 4MHz clock) which is only 0.655 seconds.

For a 20MHz osc it would only be 0.131 seconds.

steve
And what would be the problem with using a 1MHz clock?

YES--Of COURSE you can use Pulsin for a 2 second pulse! Most PIC's have internal clocks that are capable of it. Heck, if you need a faster clock for other stuff, just slow down (i.e. change osccon) right before the pulsin and put it back after.

7. Originally Posted by tenaja
And what would be the problem with using a 1MHz clock?
Well heck... I never thought of just slowing the clock down for the pulsin measurement!

Good idea!

steve

8. Thanks a lot Bruce and Tenaja, both ideas are great. Will tray both

Bruce one thing, your while...wend statemet, will be a inifinite loop if I left the button pressed, doesn't? I did the next code, will work?

Code:
```        if PORTA.0 = 1 THEN
PAUSE 2000
if PORTA.0 = 1 THEN
gosub PROGRAMAR
endif```
Last edited by lugo.p; - 9th March 2010 at 16:24.

9. Originally Posted by tenaja
And what would be the problem with using a 1MHz clock?

YES--Of COURSE you can use Pulsin for a 2 second pulse! Most PIC's have internal clocks that are capable of it. Heck, if you need a faster clock for other stuff, just slow down (i.e. change osccon) right before the pulsin and put it back after.
Tenaja there is a problem, when i code this:

Code:
```      OPTION_REG = \$07
ANSEL      = \$00     'para hacer funcionar todas las E/S como digitales
TRISA      = \$07     'Configura PORTA <5:3> salidas y PORTA <2:0> salidas
TRISC      = \$00     'Configura PORTC <5:0> todas salidas
DEFINE OSC 1         '8MHz

inicio:
pulsout PORTC.5,25000
pause 100
goto inicio```
i get an Error:

Error c:\picmicro\mcstud~1\pbp246\pbppic14.lib 6152 : [255] Unidefined Sybol 'PAUSEUSL'

looking in pbppic14.lib it seems that the lowest frecuency that can be handled by PBP is 3.58MHz, but pic16f684 can handle since frecuency from 31KHz.

But inclusive having that error I sumulate the HEX, and works, my question is, this error will give me headache????

--EDIT--

I took away the line

DEFINE OSC 1 'for 1MHz oscillator

and code was compiled just fine, the only thing is that PAUSE increments are bigger

with DEFINE OSC 1

PAUSE 100 'has a duration of 5mS

without DEFINE OSC 1

PAUSE 100 'has a duration of 400mS
Last edited by lugo.p; - 9th March 2010 at 17:07. Reason: correcting

10. Hi,

For pulsin,

You also can play with the internal oscillator ... if your chip, like the 16F88 has a configurable internal osc ...

use the " classic " 4 or 8 Mhz speed, and switch, on the fly, to 31 Khz or 125 Khz ... just changing osccon IRCF bits, when needed ...

note the BUTTON command can be configured to have a 250ms debounce time ( DEFINEs ...) ... 8 x 250 ms make 2 seconds.
which is much, much simpler !!! ( matches Bruce's idea ...)

Alain
Last edited by Acetronics2; - 9th March 2010 at 18:17.

11. Acetronics,

i really don't understand very well how it's works BUTTON, but, it will work if I use BUTTON just when PIC is turned on.

By example the following flow,

1. turn on PIC and if button is pressed 2 seconds GOTO X, or

2. turn on PIC and if button is not pressed run normally.

12. Hi, Lugo

let's suppose your button B is active LOW

Code:
```DEFINE BUTTON_PAUSE 250

.
.
.

FOR I = 1 to 8

Delay = 0                                ' reset check time
BUTTON B,0,255,0,Delay,1, Incr          ' Check if button pressed for 250 ms , IF yes, increment I and recheck

Incr:
NEXT I                                    ' If checked 8 times we enter Setup routine

Setup_routine :
.
.
.
.
Main :
.
.```
Note there's NO DELAY to enter main routine if button hasn't been pressed ... either test will be left as soon as button is released ...

Alain