Welcome, Guest

Author Topic: PWM parameter for duty cycle  (Read 1204 times)

mikewax

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
PWM parameter for duty cycle
« on: December 12, 2016, 07:57:11 AM »
Hello
can someone look at my code and figure out why the duty cycle parameter isn't working here? i'm using timer1 on the Atmega 328 with code i copied from the net https://sites.google.com/site/qeewiki/books/avr-guide/pwm-on-the-atmega328

Code: [Select]
// BLINK the yellow (line D9) LED with period 2s, brightness grows and cycles every 8 periods
#include <avr/io.h>

int x = 0;
void setup() {
// setup fast PWM, timer1 rolls over every 32 units
ICR1 = 32;
TCCR1A |= (1 << COM1A1)|(1 << WGM11);
TCCR1B |= (1 << WGM12)|(1 << WGM13);
}

void loop() {

  x += 4;
  OCR1A = x%32; // Duty cycle = (x mod 32)/32
  DDRB |= (1 << DDB1);  // set pin D9 to output
  TCCR1B |= (1 << CS10);  // enable timer1
  delay(1000);
     
  DDRB &= ~(1 << DDB1); // D9 = input
  TCCR1B &= ~(1 << CS10); // disble timer1
  delay(1000);
}

when i load and run the program, the LED goes full on, full off, 100%, 0%, with no gradation in between. OCR1A is supposed to be the duty cycle to fade the LED up and down, but it doesn't.