Hi there,
Does anyone have example code that shows how to read PWM signal in 4.3 framework and measure duty cycle and pulse high time ?
Thanks
Hi there,
Does anyone have example code that shows how to read PWM signal in 4.3 framework and measure duty cycle and pulse high time ?
Thanks
I am interested as well. PWM support has changed. My code is crashing when the freq and duty are set to zero. I guess I’ll try to turn off the port instead, but an example would be great.
Below is example of PWM in 4.3 framework using signal capture to measure distance by ultrasonic sensor.
class PWM
{
public double final_distance;
public uint[] signal;
public SignalCapture pin;
public PWM()
{
signal = new uint[sample_rate];
pin = new SignalCapture(GHI.Pins.G120.P1_5, Port.ResistorMode.Disabled);
}
public void distance_measurement()
{
pin.Read(false, signal);
final_distance = median(signal, sample_rate);
Debug.Print("Distance: " + final_distance.ToString());
}
}
I have a working pwm methods in 4.1, but when I make minor changes so that it will compile with 4.3 the pwm class does not work anymore.
Here are the methods:
public void Grab()
{
TurnOff();
pwmA.Set( 20000, 100 );
}
public void Release()
{
TurnOff();
pwmB.Set( 20000, 8 );
}
internal void TurnOff()
{
pwmA.Set(0, 0);
pwmB.Set(0, 0);
}
I have updated it to this for netmf 4.3
public void Grab()
{
TurnOff();
pwmA.DutyCycle(100);
pwmA.Frequency(20000);
pwmA.Start;
}
public void Release()
{
TurnOff();
pwmB.DutyCycle( 8 );
pwmB.Frequency( 20000 );
pwmB.Start;
}
internal void TurnOff()
{
pwmA.Stop;
pwmB.Stop;
}
Any ideas why the 4.3 code is not working? I guess I will need to put a scope on it to see what looks different about them.
What is the equivalent call to pwm.set under the new implementation for 4.3?
https://msdn.microsoft.com/en-us/library/jj610646(v=vs.102).aspx has all the details you need - and you should have a link to that in your kitbag of tools, for sure. PWM specifics https://msdn.microsoft.com/en-us/library/microsoft.spot.hardware.pwm(v=vs.102).aspx
Thanks for the links. They are nice, but seem to me limited to what intellisense in visual studio provides. I need to know why code that works in 4.1 does not work in 4.3.
Thankfully in GHI Code Share ChrisO has posted a replacement class for PWM
https://www.ghielectronics.com/community/codeshare/entry/932
and the description of the Duty Cycle is as follows
‘Duty Cycle expressed in Percentage EG: 0.5 gives 50% Duty Cycle of the Frequency’
This led me to realize that the Duty Cycle threshold changed from 0 to 100 in 4.1 to 0 to 1 in 4.3. Ah the smokin guns appears!
It was pretty cool to see the 4.1 pwm signal via an oscilliscope and after I realized the duty cycle change I was able to reproduce them in the 4.3 code.
Thanks for the feedback it kept me going.
I am using the PWM output of the EMX to grab and release a magnet. I have the solution, but would like to know if it can be explained.
When the following runs a short pulse is generated. This is not expected. I would expect a transition from low to high and the signal to stay high, since the duty cycle is 1.
public void Grab()
{
pwm.Stop();
pwm.Frequency = 20000;
pwm.DutyCycle = 1.0;
pwm.Start();
}
When I watch the signal with an O-Scope, and step through the code. When I hit the pwm.Frequency = 20000; line the pulse rises and then when I step through the pwm.Start(); line, the signal goes back low. So I commented out the pwm.Start(); line and now the signal stays high.
Any Explanations???
I assume you use actual PWM elsewhere ? Otherwise, PWM duty=1 is just a digital output and you should use it that way…
One other test you might try is not stopping the PWM and just changing the duty, as a stop/start will always tend to give you an unexpected pulse length that could cause issues (I’ve not ever tested this though, give it a whirl and check if it works)
I don’t think we should use the PWM as a digital output either, but I am just updating code to 4.3 now.
Just changing the duty cycle is a good idea, I will give that a try, I will need to build a small circuit with an led so I don’t have to keep setting up the o-scope.
thanks
I’d always want to make sure the 'scope didn’t show a glitch as well