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

How to generate a PWM signal with C#?


  • Please log in to reply
4 replies to this topic

#1 Leon

Leon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 27 March 2013 - 11:16 AM

I input the following code in C#  in order to generate a PWM signal in netduino D6 PIN ,but failed after  visual studio display:

1. 'Microsoft.SPOT.Hardware.PWM' does not contain a constructor that takes 1 arguments...

2.''Microsoft.SPOT.Hardware.PWM' does not contain a definition for 'Setdutycycle' and no extension method 'Setdutycycle' accepting a first argument  of type 'Microsoft.spot.hardware.PWM' could be found .

 

Is there somebody can help me? gratitude!

{   public class Program   {   public static void Main()   {   PWM PWM_R = new PWM(Pins.GPIO_PIN_D6);   int value = 0;   while (true)   {   value += 1;   if(value>=100) value = 0;   PWM_R.SetDutyCycle(value);   Thread.Sleep(20);   }     }   } }  

Attached Files



#2 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 27 March 2013 - 06:20 PM

This might inspire you if you have a Netduino 2 or Netduino 2 Plus.

 

namespace NP2.Examples_PWM{    public class Program    {        static PWM led1;        public static void Main()        {            led1 = new PWM(PWMChannels.PWM_ONBOARD_LED, 1000, .5, false);                        led1.Frequency = 1000;            led1.DutyCycle = .5;            led1.Start();                        while (true)            {                double startValue, endValue;                for (startValue = 4.712; startValue < 10.995; startValue = startValue + 0.1)                {                    endValue = System.Math.Sin(startValue) * .5 + .5;                    led1.DutyCycle = endValue;                    Thread.Sleep(5);                    if (startValue == 4.712)                    {                        Thread.Sleep(1000);                    }                }            }        }    }}


#3 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 28 March 2013 - 06:46 AM

I use a Netduino+ (Version 1).

 

This is my code:

using SecretLabs.NETMF.Hardware.NetduinoPlus;using SecretLabs.NETMF.Hardware;using Microsoft.SPOT.Hardware;...//Output ports with PWM functionallity	    static private PWM PWM5 = new PWM(Pins.GPIO_PIN_D5);	    static private PWM PWM6 = new PWM(Pins.GPIO_PIN_D6);	    static private PWM PWM9 = new PWM(Pins.GPIO_PIN_D9);	    static private PWM PWM10 = new PWM(Pins.GPIO_PIN_D10);    

 

Have a look at your usings, probably you are missing something.

 

Attached File  References.jpg   21.7KB   51 downloads

Also be sure you have all references (picture above). Watch out. there is a Microsoft.SPOT.Hardware AND a Microsoft.SPOT.Hardware.PWM.

Both must be referenced.

 

Greets, Markus


NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#4 Leon

Leon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 30 March 2013 - 11:08 AM

[color=#000080;]Thank you Lunddahl[/color] and NeonMika,  after these days study,I found in Netduino2 must use "led1 = new PWM(PWMChannels.PWM_ONBOARD_LED, 1000, .5, false);"  to generate a PWM,  and  "PWM PWM_R = new PWM(Pins.GPIO_PIN_D6);" is only suit for Netduino1,I guess. Do you agree with me? 



#5 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 31 March 2013 - 03:38 PM

You you are right, or not, the correct answer is:

 

It depends, and on more than one thing... :-)

 

1. There is more than one Netduino, and they don't all work alike, there is the Netduino Series 1, those includes the Netduino, Netduino Plus and the Netduino Mini, the have a Atmel Micro controller  And then there is the Netduino Series 2, those includes the Netduino 2, Netduino 2 Plus and the Netduino Go, thay have a STMicro STM32 Series Micro controller.

 

2. There is more than one PWM class, and while they do the same, they work differently: SecretLabs.NETMF.Hardware.PWM and Microsoft.SPOT.Hardware.PWM.

 

You will have to add a reference to one of them in your project, and often you also choose to use a using statement, and most code snippets on the forums does not specify witch one is used, because the first one is done in the GUI, and the second one is just often omitted when posting code.

 

While i don't know much about the Series 1 Netduinos, i think they only worked with the SecretLabs PWM class.

 

The Series 2 originally only worked with the Microsoft PWM class, but for the above reasons a lot of people was confused, so one of the resent updates made the Secret Labs class work again, this was part of a lot of things that was done to make code easier to port from the Series 1 Netduinos.

 

When you know the difference it easy to figure out what to do, there is one more thing that might make you life easier.

 

Starting the PWM class with values that "overloads" the MCU, makes it appear hanging, and you will have reset and stop the program to be able to deploy to it again, often that means a reflash, however there are other ways.

 

- Ulrik Lunddahl






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.