Greater time resolution using .NET Micro Framework on Netduino board (for dimming an LED)?

StackOverflow https://stackoverflow.com/questions/4320572

문제

I'm programming a Netduino board using the .NET Micro Framework 4.1 and want to get a higher time resolution than milliseconds. This is because I'm attempting to dim an LED by blinking it really fast.

The issue is that the sample code uses Thread.Sleep(..) which takes a number of milliseconds.

Sample code from http://netduino.com/projects/ showing the issue in question:

OutputPort ledOnboard = new OutputPort(Pins.ONBOARD_LED, false);
while (true)
{
    ledOnboard.Write(true);
    Thread.Sleep(1); // << PROBLEM: Can only get as low as 1 millisecond

Even if there's another way to accomplish dimming by not using a greater time resolution, I'm game.

도움이 되었습니까?

해결책

This doesn't answer your question about getting a better time resolution, but it does solve your problem with changing the brightness on an LED. You should be using the PWM module for the Netduino.

Netduino Basics: Using Pulse Width Modulation (PWM) is a great article on how to use it.

다른 팁

I have had a similar problem in the past and used the following method to time in the microsecond range. The first line determines how many ticks are in a millisecond (its been a while since I used this, but I think 1 tick was 10 microseconds). The second line gets the amount of time the system has been on (in ticks). I hope this helps.

public const Int64 ticks_per_millisecond = System.TimeSpan.TicksPerMillisecond;

public static long GetCurrentTimeInTicks()
{
    return Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks;
}

You can use a timer to raise an event instead of using sleep.

The Interval property on a timer is a double so you can have less than a millisecond on it.

http://msdn.microsoft.com/en-us/library/0tcs6ww8(v=VS.90).aspx

In his comment to Seidleroni's answer BrainSlugs83 suggests "sit in a busy loop and wait for the desired number of ticks to elapse. See the function I added in the edit". But I cannot see the function added to the edit. I assume it would be something like this:

using System;
using Microsoft.SPOT.Hardware;

private static long _TicksPerMicroSecond = TimeSpan.TicksPerMillisecond/1000;

private void Wait(long microseconds)
{
    var then = Utility.GetMachineTime().Ticks;
    var ticksToWait = microseconds * _TicksPerNanoSecond;
    while (true)
    {
        var now = Utility.GetMachineTime().Ticks;
        if ((now - then) > ticksToWait) break;
    }
}

A point that you might not be thinking about is that your code is relying on the .NET System namespace, which is based on the real time clock in your PC. Notice that the answers rely on the timer in the device.

Moving forward, I would suggest that you take a moment to qualify the source of the information you are using in your code -- is it .NET proper (Which is fundamentally based on your PC), or the device the code is running on (Which will have a namespace other than System, for example)?

PWM is a good way to control DC current artificially (by varying the pulse width), but varying the PWM frequency will still be a function of time at the end of the day.

Rather than use delays....like Sleep....you might want to spawn a thread and have it manage the brightness. Using Sleep is still basically a straight line procedural method and your code will only be able to do this one thing if you use a single thread.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top