Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Spin wait timer for .NET Micro Framework

, 9 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Spin wait timer for .NET Micro Framework.

I'm working on getting a piece of hardware interfaced to a computer using a Netduino. There are some places in the documentation for this hardware that specify delays for which the available .NET Micro Framework functions just won't work for me. For this application spin waits are fine. So I've implemented a class to take care of the delays for me. When the program is first started I call the Calibrate method on this class. Calibrate will perform a spin wait for a predetermined number of cycles timing how long it takes to complete. The amount of time it takes to complete is then used to calculate the ratio between the cycles and the timing. That value is used to know how many cycles to wait to delay for an approximate amount of time. 

I've found that this works perfectly fine for my needs. Something to keep in mind when extremely short delays are needed is that there's some amount of overhead with the function call and preparing for the loop. For many applications this may not matter.  If the spin wait is interrupted (such as by another thread taking control or a hardware interrupt being triggered) then the timing could get thrown off. So this solution isn't for every one.

I've pre-populated the variable for cycles per second with the value that the hardware that I am using came up with. The hardware I am using is a Netduino Plus 2. The needed value will likely be different on other hardware and possibly on future firmware updates of the same hardware.

public class SpinWaitTimer
{
    double _cyclesPerSecond = 112262.2255516001;

    public double CyclesPerSecond
    {
        get { return _cyclesPerSecond; }
        set { _cyclesPerSecond = value; }
    }

    public SpinWaitTimer()
    {
    }

    public void Calibrate()
    {
        const int CYCLE_COUNT = 1048576;
        int dummyValue = 0;
        DateTime startTime = DateTime.Now;
        for (int i = 0; i < CYCLE_COUNT; ++i)
        {
            ++dummyValue;
        }
        DateTime endTime = DateTime.Now;

        TimeSpan timeDifference = endTime.Subtract(startTime);

        _cyclesPerSecond = ((double)CYCLE_COUNT / (double)timeDifference.Ticks)*10000000d;

    }

    public void WaitSeconds(double sec)
    {
        int cycleCount = (int)((sec * CyclesPerSecond));
        int dummyValue = 0;
        for (int i = 0; i < cycleCount; ++i)
        {
            ++dummyValue;
        }
    }

    public void WaitMilliseconds(double microseconds)
    {
        int cycleCount = (int)(_cyclesPerSecond * CyclesPerSecond / 1000d);
        int dummyValue = 0;
        for (int i = 0; i < cycleCount; ++i)
        {
            ++dummyValue;
        }
    }

    public void WaitMicroseconds(double microseconds)
    {
        int cycleCount = (int)(_cyclesPerSecond * CyclesPerSecond / 1000000d);
        int dummyValue = 0;
        for (int i = 0; i < cycleCount; ++i)
        {
            ++dummyValue;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.
 
For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.
 
While most of my CodeProject.com articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.
 
Twitter:@J2iNet
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionCalibrate... possible problem. PinmemberPaulo Zemek11-Apr-13 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 9 Apr 2013
Article Copyright 2013 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid