G120 Hardware PWM Example

Looking for the method to do hardware PWM…to be exact i want to control the brightness of the LCD backlight…mmm…might be an overkill?

Can PWN pins have different frequencies?

SignalGenerator is great for that purpose and work on any pin, not just PWM.

Is this a namespace that i need to use? But is it hardware or software pwm?

Many MCUs have a feature called Output Compare.

In fact the SignalGenerator class in 4.2 used to be OutputCompare class in 4.1

https://www.ghielectronics.com/downloads/man/Library_Documentation_v4.2/Premium/Index.html

It is a Premium feature, although @ taylorza has implemented it for Hydra as well.

This is how you will control brightness for example


        using  GHI.Premium.Hardware;
        
        ...
        private SignalGenerator backLight;

        ...
        backLight = new SignalGenerator(backLightPin, true, 2);
        ...

        public void SetBrightness(int percentage)
        {
            if (percentage < 0 || percentage > 100) 
                throw new ArgumentException(); 
            if (percentage == 0) 
                backLight.Set(false); 
            else if (percentage == 100) 
                backLight.Set(true); 
            else 
            { 
                const int PERIOD = 10000; 
                int highTime = PERIOD * percentage / 100; 
                backLight.Set(true, new uint[2] { (uint)highTime, (uint)(PERIOD - highTime) }, 0, 2, true); 
            }
        }


I just use the PWM class

static PWM brightness = new PWM(Cpu.PWMChannel.PWM_6, 2000, 1900, PWM.ScaleFactor.Nanoseconds, false);

brightness.Start();

This works for me on the G120HDR.

1 Like

I have connected a passive buzzer to pwm6…i have noticed that it on be default. Can this be changed?

static PWM brightness = new PWM(Cpu.PWMChannel.PWM_6, 2000, 1900, PWM.ScaleFactor.Nanoseconds, false);

brightness.Start();

All pins are pulled high by default. Add a pull down resistor.

Thanks Gus…problem solved