0
\$\begingroup\$

I'm trying to start PWM at ATmega2560, but I don't know why isn't working

That's what my code looks like:

#include <avr/io.h>
#include <util/delay.h>

void set_pwm_duty_cycle(char duty);

int main()
{
  int dutycycle;

  while(1)
  {
    for(dutycycle = 0; dutycycle < 200; dutycycle++)
    {
      set_pwm_duty_cycle(dutycycle);
      _delay_ms(50);
    }
  }
}

void set_pwm_duty_cycle(char duty)
{
  TCCR2A |= _BV(WGM21) | _BV(WGM20); // Fast PWM mode
  TCCR2A |= _BV(COM2A1); // Clear OC2A on Compare Match, set OC2A at BOTTOM
  OCR2A = duty;
  DDRB |= _BV(PB4); // set PB4 as output
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Did you enable the clock to the timer? What is the prescaler? \$\endgroup\$ – filo Mar 2 '18 at 16:00
  • \$\begingroup\$ My mistake, forgot prescaler. \$\endgroup\$ – P47 R1ck Mar 3 '18 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.