#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
// Volatile keyword is important
volatile char count=0;
unsigned int degrees;
// We use this ISR to leave only every sixth pulse and remove the rest.
// This reduces the PWM frequency to ~56Hz.
ISR(TIMER2_COMP_vect) {
if (count){ // Disconnect the OC2 to reduce pulse rate
TCCR2 &= ~(1<<COM21);
count--;
}
else { // Connect the OC2 (PD7) for this pulse
TCCR2 |= (1<<COM21);
count=6;
}
}
int main(void) {
DDRD|=(1<<PD7); // selcet OC0 as output pin
// Fast PWM, TOP = 255, Prescale = 128, F_CPU = 11.0592Mhz
TCCR2 |= (0<<FOC2)|(1<<WGM20)|(1<<WGM21)|(0<<COM20)|(1<<COM21)|(1<<CS20)|(0<<CS21)|(1<<CS22);
// This gives you ~1ms pulse. (86.4 is accurate value).
OCR2=0;
// Setup interrupt so we can remove pulses from the pulse train
TIMSK |= OCIE2;
sei();
while(1)
{
_delay_ms(1000);
OCR2 = 65 + ((130 * degrees) / 180);
degrees=degrees+1;
if(degrees==180)
degrees =0;
}
}