void timer1_fast_pwm_init() {
// Set PB1 (OC1A) as output
DDRB |= (1 << PB1);
// Configure Timer1 for Fast PWM
TCCR1A = (1 << WGM11) | (1 << WGM10) | (1 << COM1A1); // Fast PWM, Clear OC1A on Compare Match
TCCR1B = (1 << WGM12) | (1 << CS11); // Fast PWM, Prescaler = 8
// Set the TOP value for 10-bit Fast PWM
ICR1 = 1023;
// Set initial duty cycle to 50% (OCR1A = TOP/2)
OCR1A = 512;
// Enable Timer1 overflow interrupt
TIMSK1 = (1 << TOIE1);
//TCNT1 = 256; // Start from quarter-phase
sei();
}
int main() {
timer1_fast_pwm_init();
while (1) {
// Main loop runs while the interrupt handles the PWM logic
}
return 0;
}
// Timer1 Overflow Interrupt Service Routine
ISR(TIMER1_OVF_vect) {
// Dynamically adjust TCNT1 for phase shifting
if (TCNT1 > 900) {
TCNT1 = 0; // Reset to quarter-phase dynamically
}
}