PDA

View Full Version : Can i measure 2 secs with pulsin?



lugo.p
- 8th March 2010, 23:19
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???

Byte_Butcher
- 8th March 2010, 23:33
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

lugo.p
- 8th March 2010, 23:42
thanks steve, can you tell me how i can measure a 2 seconds button-pressed?

mtripoli
- 8th March 2010, 23:53
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...

Bruce
- 8th March 2010, 23:56
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.

tenaja
- 9th March 2010, 00:28
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.

Byte_Butcher
- 9th March 2010, 01:10
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

lugo.p
- 9th March 2010, 15:03
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?



if PORTA.0 = 1 THEN
PAUSE 2000
if PORTA.0 = 1 THEN
gosub PROGRAMAR
endif

lugo.p
- 9th March 2010, 15:56
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:



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
INTCON = $00 'Interrupciones deshabilitadas
OSCCON = $47 'Oscilador 8MHz
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

Acetronics2
- 9th March 2010, 17:15
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

lugo.p
- 9th March 2010, 20:59
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.

Acetronics2
- 10th March 2010, 16:04
Hi, Lugo

let's suppose your button B is active LOW




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
GOTO Main ' If not jump to main

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