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

InputPort class and PWM issues

Inputport PWM

  • Please log in to reply
4 replies to this topic

#1 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 28 August 2013 - 07:42 PM

Hey guys, I've been working on this code for roughly an hour, but have stumbled upon an issue with my InputPort class. I've used this MANY times, but for some reason my Visual C# is angry at me... :huh: Here are the errors, for both Frequency_Pot and Duty_Pot (haha duty pot... :P ). 

 

Error 1 The best overloaded method match for 'Microsoft.SPOT.Hardware.AnalogInput.AnalogInput(Microsoft.SPOT.Hardware.Cpu.AnalogChannel)' has some invalid arguments
 
Error 2 Argument 1: cannot convert from 'Microsoft.SPOT.Hardware.Cpu.Pin' to 'Microsoft.SPOT.Hardware.Cpu.AnalogChannel'
 
I have a feeling this has something to do with me switching from .NET 4.1 to .NET 4.2......... I'm using a Netduino Plus 1..BTW

 

 

Also, when the .SetPulse is written, and the program loops back to the top, will I have an issue with re-writing the .SetPulse and setting the output to High or Low over and over again, resulting in an infinite High or Low output? Or does it wait till the 'period' is completed before assigning the new 'period' and 'duration'? If so, how do I fix this?

 

Here's the code:

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;namespace 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_D13);            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 = (int)(Duty_Cycle_Scale * 100); // To scale from 0-100%            uint PWM_Min = 0; // In Hz            uint PWM_Max = 400; //In Hz            int Freq_Hz = (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);        }    }}

Thanks a lot!!



#2 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 28 August 2013 - 08:17 PM

BIn there done that!

I held out with v4.1 until I was forced to change to get the Ethernet to work.

 

Some of the IO classes and types are a bit different.

I can't remember everything I had to do, but looking at my code the following may help:

 

Try "SecretLabs.NETMF.Hardware.AnalogInput" instead of just "AnalogueInput"

I appear to be using "Pins.GPIO_PIN_A0" so I guess that's OK.

I think you also need a new reference "SecretLabs.NETMF.Hardware.AnalogueInput".

 

Should get you a bit further - Paul

 

EDIT: I am sure there was a wiki page or a forum post listing the changes, but I can't find it.



#3 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 28 August 2013 - 08:24 PM

Not sure if the hardware waits until the current pulse finishes before setting the next.

However I am fairly sure that the software is not made to wait for the next pulse, so your code should not hang even if the hardware waits.



#4 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 29 August 2013 - 02:57 PM

/*instead of this*/            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_D13);            /*type this*/            AnalogInput Frequency_Pot = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);            AnalogInput Duty_Pot = new AnalogInput(AnalogChannels.ANALOG_PIN_A1);            //Pin D13 is not PWM-enabled, you must choose another port.            //Adjust frequency, duty cycle, and invert arguments to your needs            PWM Test_PWM = new PWM(PWMChannels.PWM_PIN_D3, 1000, 0.5, false);


#5 ncsteinb

ncsteinb

    Member

  • Members
  • PipPip
  • 27 posts

Posted 29 August 2013 - 03:00 PM

Well, I am mostly concerned with the PWM output. Say if I request a PWM freq of 20 Hz with a duty cycle of 30%, the code will cycle through many times before even one PWM cycle is complete. I'm afraid that when the .SetPulse is written it may pull it HIGH for the duration of the pulse width, and then before the pulse can go LOW, .SetPulse is rewritten and the PWM is set to HIGH again. In other words, does .SetPulse set the 'clock' back every time it is written? 

 

Another method to fix this would be to add a Thread.Sleep(x), but for higher frequencies >~20Hz, I would run into resolution issues because .SetPulse's arguments are in microseconds, and Thread.Sleep's arguments are in milliseconds....

 

Maybe I should just throw a scope on the output...

 

Any ideas?

 

EDIT: Also, where can I find more information about a specific class such as .SetPulse, or any of the classes within Secretlabs.Netmf.Hardware? I haven't found anything explaining exactly the overloads for these...which can be very useful (necessary)

 

I said screw it with 4.2, my Netduino is still 4.1, so I'm just going to stick with it as long as I can... Thanks.







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.