Netduino+2 PWM for high resolution timing - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Netduino+2 PWM for high resolution timing


  • Please log in to reply
3 replies to this topic

#1 kejensen

kejensen

    New Member

  • Members
  • Pip
  • 2 posts

Posted 22 December 2014 - 02:34 PM

Hi

I'm tryying to generate data pulses on one of the outputs of my Netduino plus 2 board which have to have pulse width of 150µs, 300µs and 450µs.

Sine the lowest resolution of the timer in the micro framework is 1 ms, I need another approach

I would like to have it event based, since it should be able to receive IR commands from a remote control in parallel to this, so looping in a loop until ticks indicates that 150µs has passed is not an option, since I guess it will block all other execution

I have tried to use the PWM functionality on pin 10, and generate pulses each 150µs and define pin 11 as an interrupt port, and then connect pin 10 to pin 11.

I have tried the code below, but both the board and Visual Studio 2013 seems to be unstable when I try to execute the code.
Often I get the errormessage: An unhandled exception of type 'System.OutOfMemoryException' occurred in Microsoft.SPOT.IO.dll and often the board stops responding.

If I change the frequency of the PWN from 3.333 Hz to 1.000 Hz, then it works more often, but still not every time.

Until now the eventhandler for the interrupt port does not contain any code at all.

I'm using SW version Version 4.3.1 on my Netduino plus 2 board

 

I hope anybody can help on this

 

Thanks

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplicationPwmInterrupt
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print("Trace #1");

            OutputPort led = new OutputPort(Pins.GPIO_PIN_D5, false);

            InterruptPort myInterruptPort = new InterruptPort(Pins.GPIO_PIN_D11, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            myInterruptPort.OnInterrupt += new NativeEventHandler(port_OnInterrupt); // Create an event handler

            
            PWM led1 = new PWM(PWMChannels.PWM_PIN_D10, 3333, .5, false); //period time = 150 uS when triggering on both edges
            led1.Start();


            while (true)
            {
                led.Write(true);
                Thread.Sleep(10);
                led.Write(false);
                Thread.Sleep(400);
            }
        }


        private static void port_OnInterrupt(uint port, uint data, DateTime time)
        {
            //var myVar = Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks;
        }

    }
}
 



#2 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 23 December 2014 - 12:03 AM

For starters, an interrupt in a managed framework is not the same as an interrupt on a raw device.

 

Rather than look at MachineTime Ticks, look at the value of the DateTime timestamp argument passed to you.

 

But that is for future reference, it is not causing the errors you see.

 

I suspect you are just driving interrupts faster than the framework can handle.  As far as I know, the Netduino is NOT a good device for reading high speed signals, unless there was something added that I missed.

 

Even if your code is not doing anything for the interrupt, again, it is NOT like interrupts on an Arduino. It IS doing a lot of stuff under the covers. For one, it is passing you a timestamp. You can be sure that has to go through memory allocation at a minimum. Memory fragmented? Maybe the garbage collector HAD to run.  Then it gets placed in an event Queue, that might need to be expanded .... more memory management. Then it calls your code, that does nothing ... but the stack needs to be cleaned up, and unreferenced objects garbage collected.

 

Managed code is a different way of doing things. It has many advantages, and a few disadvantages.



#3 kejensen

kejensen

    New Member

  • Members
  • Pip
  • 2 posts

Posted 23 December 2014 - 06:03 AM

Thanks a lot, Spiked

 

So it seems like Netduino is not a good choice for doing that :( and I have to find another way for doing this

 

>>Rather than look at MachineTime Ticks, look at the value of the DateTime timestamp argument passed to you.

The reason for doing it that way is that MachineTime.Ticks has a resolution of 100 nanoSeconds, where I originally thought that dateTime.Ticks only had a resolution of 1 millisecond even though it returns a lot more digits.

 

I have now testet it with the DateTime timestamp argument passed in the eventhandler, and it seems like it does have a high resolution



#4 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 23 December 2014 - 10:24 AM

The biggest difference is one is the time now, the other is the time when the interrupt occurred (or closer to it), and the two are not related.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.