Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 538
John K Avatar asked Dec 01 '10 02:12

John K


3 Answers

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.

like image 111
superfro Avatar answered Nov 02 '22 17:11

superfro


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;
}
like image 35
Seidleroni Avatar answered Nov 02 '22 18:11

Seidleroni


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

like image 36
Phill Avatar answered Nov 02 '22 18:11

Phill