#include #include #include #include //X has to be between 1 and 129 void clock_prescale_set(uint8_t x) { if(x == 0) { return; } else if(x == 1)//Disable prescaler { XDIV = 0; } else if(x <= 129) { XDIV &= ~_BV(XDIVEN); XDIV = (129 - x) | _BV(XDIVEN); } } uint8_t clock_prescale_get(void) { if(bit_is_clear(XDIV, XDIVEN)) { return 1; } else { return (129 - (XDIV & 0x7F)); } } ISR(TIMER1_OVF_vect) { static int i = 0; static uint8_t prescaler = 1; uint8_t prescalerRead; PORTB ^= 0xFF; i++; if(i > 10) { i = 0; prescaler++; if(prescaler > 129) prescaler = 1; clock_prescale_set(prescaler); prescalerRead = clock_prescale_get(); if(prescalerRead != prescaler) asm("break"); } } int main(void) { DDRB = 0xFF; sei(); //Enable timer 1 TIMSK |= _BV(TOIE1); TCCR1B |= _BV(CS10) | _BV(CS11); for(;;) asm("nop"); return 0; }