//* PWM generation with glitch-free changing of PWM rate *

static volatile uint8_t pwm_rate = 25; // <-- this variable is global and is controlled externally static uint8_t pwm_current = 0; static uint8_t pwm_count = 0; if ( 0 == pwm_count) { pwm_count = 100; pwm_current = pwm_rate; } else { pwm_count--; } if ( pwm_count>pwm_current ) { setPin(1); } else { setPin(0); } // Note: if you need a lower resolution than 1% then you do not need to count to 100 // Using a lower count number results in lower interrupt rate for the same PWM frequency