#include <avr/io.h>
#include <util/delay.h>
#define PWM_PIN PB1
#define SWITCH_PIN PD2
int main(void) {
DDRB |= (1 << PWM_PIN); // Configure PWM pin as output
DDRD &= ~(1 << SWITCH_PIN); // Configure switch pin as input
PORTD |= (1 << SWITCH_PIN); // Enable internal pull-up resistor
// Configure Timer1 for PWM operation
TCCR1A |= (1 << COM1A1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 625;
uint8_t duty_cycle = 0;
while (1) {
// Read switch state
if (!(PIND & (1 << SWITCH_PIN))) {
// Switch is pressed, increase duty cycle
duty_cycle += 10;
if (duty_cycle > 100) {
duty_cycle = 100;
}
} else {
// Switch is not pressed, decrease duty cycle
duty_cycle -= 10;
if (duty_cycle < 0) {
duty_cycle = 0;
}
}
// Set duty cycle
OCR1A = duty_cycle * 6.25;
// Delay to allow the human eye to perceive the change
_delay_ms(50);
}
}