PWM with inverted output not working at extremes

hardillb

New member
Hi,

I'm working on a system that needs to invert one of the channels on a PWM time so I can drive 2 outputs in a way that means neither are on at the same time (as long as the duty cycle for the 2 outputs adds to less than 1 and I invert the output). To do this I'm using the following to invert the channel:

Code:
// invert pin 6 (FMT0_CH4)
FTM0_POL |= ( 1 << 4);

analogWriteResolution(12);

analogWrite(6, map(value, 0,100,0,4095));

This works fine for most values apart from when value = 0 (which since the channel is inverted should be 100% on). Looking at the code I think it's because the code just drives the pin high/low if we are at the extremes of the analogWriteResolution ranges and doesn't take the inversion into account.

Code:
...
	max = 1 << analog_write_res;
	if (val <= 0) {
		digitalWrite(pin, LOW);
		pinMode(pin, OUTPUT);	// TODO: implement OUTPUT_LOW
		return;
	} else if (val >= max) {
		digitalWrite(pin, HIGH);
		pinMode(pin, OUTPUT);	// TODO: implement OUTPUT_HIGH
		return;
	}
...

I'm running this code on a Teensy 3.2
 
Back
Top