spin equivalent to arduino pulsin — Parallax Forums

# spin equivalent to arduino pulsin

Posts: 11
Dear forum users
Is ther a spin equivalent to the arduino comand pulsin?

With best regards Ben

• Posts: 7,706
edited 2016-02-27 05:35
There is no Spin command for pulseIn, but it's easy to duplicate in a method. Here's a simple version that measures a 0-1-0 pulse in microseconds.
```pub ez_pulse_in(pin) | ticks

waitpne(|<pin, |<pin, 0)                                      ' wait for pin to be low
waitpeq(|<pin, |<pin, 0)                                      ' wait for pin to go high
ticks := -cnt                                                 ' start timing
waitpne(|<pin, |<pin, 0)                                      ' wait for pin to go low
ticks += cnt                                                  ' stop timing

return ticks / (clkfreq / 1_000_000)                          ' return pulse width in microseconds
```

Note that this method doesn't have any tuning for command overhead. The great thing about the Propeller is that you could use another cog to create precision pulses on a pin, then use this method is a second cog to measure them to determine a tuning constant.
• Posts: 11
thanks for the info. now i have an other question. Is it posible to blow up the propellor mini if you conect a function generator and an oscilloscoop to an input pin?(my input signal was 3.3V puls signal)
• Posts: 7,706
Anything is possible. If any of your cogs makes a pin an output, it's an output. Accidents happen. Resistors are cheap insurance. Use a 4.7K or 10K until you know your code is working as expected; if your signal is 3.3 or lower they resistor won't matter; it's its higher than 3.3 it will limit the current through the pin's protective diodes.
• Posts: 1,440
edited 2016-02-26 20:37
That pulse width isn't microseconds, it's 80ths of a second (for clkfreq = 80MHz). Replace the last line with:
```  return ticks / constant(clkfreq / 1_000_000)
```
to make it microseconds.
• Posts: 7,706
edited 2016-02-27 05:34
Lowercase clkfreq is not a constant; it's a run-time system variable (and can be changed using the correct technique). That said, you're correct that I erred in my code. It's fixed now.

In my own programs I use this header (mod of something someone posted in these forums) to create compile-time constants for clock frequency, and ticks in milliseconds and microseconds.
```con { timing }

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000                                          ' use 5MHz crystal

CLK_FREQ = (_clkmode >> 6) * _xinfreq                         ' system freq as a constant
MS_001   = CLK_FREQ / 1_000                                   ' ticks in 1ms
US_001   = CLK_FREQ / 1_000_000                               ' ticks in 1us
```