Main Site Documentation

PWM spaghetti code


#1

Hi,

im trying to port my code from NET MF to TinyCLR and using my PWM outputs is a mess.

this is my old code:

static PWM channel1 = new PWM(GHI.Pins.G30.PwmOutput.PC8, hertz, light, false);
static PWM channel2 = new PWM(GHI.Pins.G30.PwmOutput.PC9, hertz, light, false);
static PWM channel3 = new PWM(GHI.Pins.G30.PwmOutput.PA0, hertz, light, false);
static PWM channel4 = new PWM(GHI.Pins.G30.PwmOutput.PA1, hertz, light, false);
static PWM channel5 = new PWM(GHI.Pins.G30.PwmOutput.PA2, hertz, light, false);
static PWM channel6 = new PWM(GHI.Pins.G30.PwmOutput.PA3, hertz, light, false);
static PWM channel7 = new PWM(GHI.Pins.G30.PwmOutput.PB9, hertz, light, false);
static PWM channel8 = new PWM(GHI.Pins.G30.PwmOutput.PB8, hertz, light, false);
static PWM channel9 = new PWM(GHI.Pins.G30.PwmOutput.PA8, hertz, light, false);

and this is the new one:

PwmController pwm1 = PwmController.FromName(G30.PwmChannel.Controller1.Id);
pwm1.SetDesiredFrequency(hertz);
PwmController pwm2 = PwmController.FromName(G30.PwmChannel.Controller2.Id);
pwm2.SetDesiredFrequency(hertz);
PwmController pwm3 = PwmController.FromName(G30.PwmChannel.Controller3.Id);
pwm3.SetDesiredFrequency(hertz);
PwmController pwm4 = PwmController.FromName(G30.PwmChannel.Controller4.Id);
pwm4.SetDesiredFrequency(hertz);

        PwmChannel channel1 = pwm3.OpenChannel(G30.PwmChannel.Controller3.PC8);
        channel1.SetActiveDutyCyclePercentage(light);
        PwmChannel channel2 = pwm3.OpenChannel(G30.PwmChannel.Controller3.PC9);
        channel2.SetActiveDutyCyclePercentage(light);
        PwmChannel channel3 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA0);
        channel3.SetActiveDutyCyclePercentage(light);
        PwmChannel channel4 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA1);
        channel4.SetActiveDutyCyclePercentage(light);
        PwmChannel channel5 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA2);
        channel5.SetActiveDutyCyclePercentage(light);
        PwmChannel channel6 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA3);
        channel6.SetActiveDutyCyclePercentage(light);
        PwmChannel channel7 = pwm4.OpenChannel(G30.PwmChannel.Controller4.PB9);
        channel7.SetActiveDutyCyclePercentage(light);
        PwmChannel channel8 = pwm4.OpenChannel(G30.PwmChannel.Controller4.PB8);
        channel8.SetActiveDutyCyclePercentage(light);
        PwmChannel channel9 = pwm1.OpenChannel(G30.PwmChannel.Controller1.PA8);

this has a high risk of messing up controllers and channels and it completely passes, no error checking

can this be written differently??


#2

NETMF did this completely wrong. Actually in NETMF there was no checking. If you change the frequency on a channel, it may or may not effect the other channels. It was horrible!

In TinyCLR, controllers are separate and so you are sure that changing the frequency on a specific channel would not effect the other, as long as they are on different controllers.

I would change your code to specify the controller in the name. For example channel1C4.