Main Site Documentation

Pulse In Function


#1

Do the FEZ controllers have a Pulsein function, similar to arduino. I want to use a particular accelerometer and it uses a variable width pulse output to represent the value.


#2

You maybe able to use interruptPort for that. What size the pulses are? Part number of the device you are using?


#3

The documentation says 25 milliseconds at 25 deg C. Here is a link to the documentation, http://www.radioshack.com/graphics/uc/rsk/Support/ProductManuals/2760029_PM_EN.pdf.


#4

I have that same accl.

Pulsein is a pretty necessary function for an MCU like the FEZ. It measures the time the signal on a specified pin is high.


#5

This can be easily done in C#, the code is below, not tested though…

public static long PulseIn(InputPort pin, bool value)
{
    // wait for pin state
    while (pin.Read() != value)
        ;

    DateTime start = DateTime.Now;

    // wait for pin to change state
    while (pin.Read() == value)
        ;

    DateTime end = DateTime.Now;
    
    // return time in micro seconds
    return (end - start).Ticks / (TimeSpan.TicksPerMillisecond / 1000);
}

#6

The code worked really well except for one thing. It took me a long time to suspect it and I finally confirmed my suspicion when I found that Arduino had the same problem which they have corrected in a similar fashion. If the pulse is already in the state defined by value when the function is called then the start time doesn’t catch the leading edge of the pulse and will be too short. I got around this by adding another “while loop” at the beginning of the function that waits if the pin is in the value condition. This causes the function to wait for the next pulse if it was already in the value state when checking began. My version looks like this:

public static long PulseIn(InputPort pin, bool value)
{
//check to see if the pin is already in the value state, if so, the pulse has already begun and the function needs to wait for the next one to "catch" the leading edge. 
while(pin.Read() == value);
// wait for pin state
while (pin.Read() != value)
;

DateTime start = DateTime.Now;

// wait for pin to change state
while (pin.Read() == value)
;

DateTime end = DateTime.Now;

// return time in micro seconds
return (end - start).Ticks / (TimeSpan.TicksPerMillisecond / 1000);
}

Without that I had a terrible hysterisis.

Also, Why does your code convert ticks to milliseconds. I dropped the conversion and got a much finer resolution by just working with ticks. The micro-framework documentation says that ticks are about 100 nanoseconds. Is the USBizi timer that accurate?


#7

The conversion to milliseconds was done to make it easier to understand. It is better to just use 100ns…no need to scale and you get better resolution.

Yes the timer is 100ns on USBizi…you are already seeing the numbers :slight_smile:
I think this 100ns was used so your code is compatible with the full .NET framework on PCs.