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

Super simple PWM tester/generator

PWM

  • Please log in to reply
5 replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 07 September 2013 - 09:27 PM

Hey guys, 

I'm having some issues with my program. I connected my scope to the output of the PWM, but I'm only getting a solid 3.3v. Anyone have a Netduino and a scope handy? I need a reality check here...

 

Thanks!

 

P.S. I disabled the adjustments to make life easier?

using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;// Uses .NETMF 4.1namespace PWM_Tester{    public class Program    {        public static void Main()        {            AnalogInput Frequency_Pot = new AnalogInput(Pins.GPIO_PIN_A0);            AnalogInput Duty_Pot = new AnalogInput(Pins.GPIO_PIN_A1);            PWM Test_PWM = new PWM(Pins.GPIO_PIN_D5);            while (true)            {                double Freq_Hz_Bits = Frequency_Pot.Read();                double Duty_Cycle_Bits = Duty_Pot.Read();                double Freq_Hz_Scale = Freq_Hz_Bits / 1023; // To scale from 0-1                double Duty_Cycle_Scale = Duty_Cycle_Bits / 1023; // To scale from 0-1                int Duty_Cycle = 50; //(int)(Duty_Cycle_Scale * 100); // To scale from 0-100%                uint PWM_Min = 10; // In Hz                uint PWM_Max = 400; //In Hz                int Freq_Hz = 100; //(int)(PWM_Min + (Freq_Hz_Scale * (PWM_Max - PWM_Min)));                uint period = (uint)((1 / Freq_Hz) * 1000 * 1000); // Period in usec (*microseconds*)                uint duration = (uint)(Duty_Cycle_Scale * period); // Duration in usec                Test_PWM.SetPulse(period, duration);                Debug.Print("Duty Cycle = " + Duty_Cycle + "%");                Debug.Print("Frequency = " + Freq_Hz + "Hz");                Thread.Sleep(100);             }        }    }}


#2 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 17 September 2013 - 11:20 AM

Anyone?? Any input would be nice...

 

Thanks.



#3 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 19 September 2013 - 05:10 PM

Hi,

 

I don't have any hardware to hand right now.

 

I would try calling Test_PWM.SetPulse() with hard coded integer values instead of calculated ones.

 

I am a little suspicious of your calculation (uint)((1 / Freq_Hz) * 1000 * 1000);

I think that since Freq_Hz is an integer, and "1" is an integer, the arithmetic may be integer arithmetic (1 / 100 = 0).

Try "1.0 / Freq_Hz", or make Freq_Hz a float.

 

Hope this helps a bit - Paul



#4 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 20 September 2013 - 07:00 PM

Paul, 

I did hard code it. I 'commented out' the calculations. I saw no change on the output. 

 

I will try playing around with the math. That might just be it. I'll report back here once I complete the test. 

BTW, do you know if the PWM needs a pulldown or a pullup? Is PWM a trigger HIGH or trigger LOW?

 

Thanks



#5 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 22 September 2013 - 03:11 PM

A few observations:

If hard coding the values does not work, then there is something other than the maths wrong.

My understanding (at least for the Netduino Plus v1) is that when you configure a pin as a PWM output, it will be a push/pull output, hence no pullup or pulldown should be required.

PWM is normally active high - so if you give it a pulse of 3/4 of the period, the output will be high for 3/4 of the time.

(If you need it to be active low, then you can change the pulse length in software to be pulse = (period - desired pulse), thus for OFF, you would set the pulse equal to the period, etc.)

 

Which hardware are you using? Netduino Plus v1 or v2

 

Assuming you have a Netduino Plus v1:

I cut this code down from the .netmf v4.1 version of my buggy project, I can't test it on hardware as I have upgraded my Netduino Plus v1's to .netmf v4.2, but it compiles OK, and it would be interesting to see what you get when you run it.

using System.Threading;                       // Thread.Sleepusing SecretLabs.NETMF.Hardware;              // PWM classusing SecretLabs.NETMF.Hardware.NetduinoPlus; // Pins enumerationnamespace PwmTestHarness{    public class Program    {        public static void Main()        {            PWM pwmPort = new PWM(Pins.GPIO_PIN_D5);            pwmPort.SetPulse(20000, 15000);  // period = 20ms - 50Hz, pulse 15ms            while (true)            {                Thread.Sleep(100); // Loop here forever            }        }    }}

Paul



#6 TinkeringTurtle

TinkeringTurtle

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 28 September 2013 - 05:33 PM

Here's what you get with Paul's sample in case you're still wondering...

Attached File  pwm.jpg   106.86KB   7 downloads

 

 







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.