#include <avr/io.h>
#include <util/delay.h>
void setup_pwm() {
// Set PB1 (OC1A) and PB2 (OC1B) as outputs
DDRB |= (1 << PB5) | (1 << PB6);
// Configure Timer1 for Fast PWM mode
TCCR1A |= (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10); // Fast PWM, non-inverted
TCCR1B |= (1 << WGM12) | (1 << CS11); // Prescaler = 8
}
int main() {
setup_pwm(); // Initialize PWM
while (1) {
// Gradually increase brightness on OC1A and decrease on OC1B
for (uint8_t duty = 0; duty <= 255; duty++) {
OCR1A = duty; // Set duty cycle for LED1
OCR1B = 255 - duty; // Set duty cycle for LED2 (opposite)
_delay_ms(50); // Delay to visualize the change
}
// Gradually decrease brightness on OC1A and increase on OC1B
for (uint8_t duty = 255; duty > 0; duty--) {
OCR1A = duty; // Set duty cycle for LED1
OCR1B = 255 - duty; // Set duty cycle for LED2 (opposite)
_delay_ms(50); // Delay to visualize the change
}
}
return 0;
}