pinMode(pin, OUTPUT);
analogWrite(pin,value);
//stuff
digitalWrite(pin,HIGH);
pinMode(pin, OUTPUT);
analogWrite(pin,value);
//stuff
pinMode(pin, OUTPUT);
digitalWrite(pin,HIGH);
Yes, analogWrite is putting the pin into a different mode, which digitalWrite can't use. I'll work on digitalWrite next week.
if (pwm_level >= (max_pwm)){
pinMode(PWM_PIN, OUTPUT); // this is a weakness of the PWM pin, this step is required. AnalogWrite misbehaves at full amplitude.
digitalWrite(PWM_PIN, HIGH);
} else{
analogWrite(PWM_PIN, pwm_level);
}
That is correct, because PWM by design has on and off cycles. The hardware doesn't allow either cycle to be 0 length.After a little testing, the spike is identical regardless of the PWM frequency, it is the same at 1 MHz and 4 kHz.
I think it is likely that analogWrite is failing to keep the pin HIGH for a single clock when the PWM duty cycle is 100%
Vales 0 and 256 are meant to correspond to logic low and high. However, the PWM hardware may not be capable of truly 100% low or high. A very short pulse may occur between each PWM cycle where the pin is driven to begin a new cycle, and then very quickly changed because the setting attempts to keep the pin always low or always high. To avoid any small glitch pulses, the pin may taken out of PWM mode using pinMode() and then controlled by digitalWrite().