Invert Pin - PWM on FEZ Hydra - VS2012, .NETMF 4.3, GHI NETMF v4.2

Hi All,

This is in part a continuation of this thread: https://www.ghielectronics.com/community/forum/topic?id=15557

I wonder if anyone has an answer to this already?

My Problem: Apparently the Invert Pin Boolean is not working in the Method PWM. Where ‘false’ indicates that the Pin is not inverted:



If I was to change this Boolean to true, it appears that it is not working.

[b]
My Code:
[/b]

```cs

using System;
using System.Threading;
using System.Collections;

// Microsoft Statements...
using Microsoft.SPOT;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Presentation.Controls;

// Gadgeteer Statements...
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

// Gadgeteer Statements...
using Gadgeteer.Networking;
using Gadgeteer.Modules.GHIElectronics;


namespace Hydra_PWM_TEST
{
public partial class Program
{
// ALLWAYS do Firmware Update...
// https://www.ghielectronics.com/docs/237/update-preparation-loader-tinybooter-and-firmware-tinyclr

// Config the PWM...
private PWM MyPWM1;
private PWM MyPWM2;

private uint Frequency = 900000;
private double Duty = 0.5;

// This method is run when the mainboard is powered up or reset.   
void ProgramStarted()
{
bool Decrement = true;

MyPWM1 = new PWM(Cpu.PWMChannel.PWM_1, Frequency, Duty, false);
MyPWM2 = new PWM(Cpu.PWMChannel.PWM_2, Frequency, Duty, false);

// Turn Off the Garbage Collector...
Debug.GC(false);

while (true)
{
if (Decrement)
{
Frequency = Frequency - GetPercentage(Frequency);
ConfigPWM();
                    
Thread.Sleep(10);

if (Frequency <= 100)
{
Decrement = false;
}
}
else
{
Frequency = Frequency + GetPercentage(Frequency);
ConfigPWM();

Thread.Sleep(10);

if (Frequency >= 900000)
{
Decrement = true;
}
}
}
}

private uint GetPercentage(uint Frequency)
{
uint F = 0;

F = ((Frequency  * 1) / 100);

return F;
}

private void ConfigPWM()
{
uint Harmonic = CalculateHarmonic(Frequency, 3);
MyPWM1.Frequency = Frequency;
MyPWM2.Frequency = Harmonic;

MyPWM1.DutyCycle = Duty;
MyPWM2.DutyCycle = Duty;

StartPWM();
}

private uint CalculateHarmonic(uint Frequency, int HarmonicInterval)
{
// Harmonic frequencies are 2f, 3f, 4f, 5f ...
uint Harmonic = ((uint)HarmonicInterval * Frequency);
return Harmonic;
}

private void StartPWM()
{
MyPWM1.Start();
MyPWM2.Start();
}


I did read a bit about this before in another post but it was not resolved? Any word on this issue?

All the Best

Chris

I don’t know about invert not working, but would a workaround be:

invertedDutyCycle = 1.0 - dutyCycle

1 Like

Hi Mike,

My Search: invertedDutyCycle site:www.ghielectronics.com

Returns nothing of use.

Where abouts does ‘invertedDutyCycle’ sit in the documentation? Its not in the PWM Class where I was expecting to find it.

Can anyone suggest a better way to follow the documentation, I seem to be having soo much trouble now. Since the 4.2 changes that is. Before that I was able to follow it nicely.

Thanks

All the Best

Chris

PWM is a core netmf capability, so the reference will be on MSDN. In your case, PWM Constructor | Microsoft Learn is the specific PWM class constructor.

Hi Brett,

Thanks for the info. I realised this is the case and already searched for ‘invertedDutyCycle’ in the Microsoft library’s.

[quote=“ChrisO”]
Its not in the PWM Class where I was expecting to find it.[/quote]

then I did the search on GHI to see it was listed on the site anywhere.

I cant find ‘invertedDutyCycle’ anywhere.

unless Mike meant:

All the Best

Chris

Mike was right, after my tired brain clicked, Mikes solution does work. 1+ Mike thanks.

@ All

Just thought I let everyone know. I ran Firmware update again and the Invert Boolean is now working. So to reiterate:


PWM MyPWM1 = new PWM(Cpu.PWMChannel.PWM_1, 1000, 0.5, false);

changing to this:


PWM MyPWM1 = new PWM(Cpu.PWMChannel.PWM_1, 1000, 0.5, true);

Does now invert the pin. Go figure re-applying the Firmware…

All the Best

Chris