Hi,
I need to create 10 different PWM duyt cycle with a MEGA board. As the controller doesn't have so many different PWM duyt cycles I tried to do that by reading the TIMER1 register. I used the onboard LED so everyone reading this topic can try it easily.
I got avery uneven response and the LED is not having a conxtant PWM but having some light flickerings visible...

That shouldn't happen, right?
Here is my code
#include <avr/io.h>
#include <avr/interrupt.h>
void setup() {
// initialize the digital pin as an output.
DDRB= B11111111; // set PORTB (digital 13-8) to outputs
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCCR1B |= (1 << CS10); // Set CS10 bit only for prescaler 1
sei(); // enable global interrupts:
}
void loop() {
if (TCNT1 < 5){
PORTB=B10000000;
}
if (TCNT1 > 20000){
PORTB=B00000000;
}
}