Arduino Nano Every (ATmega4809) analogwrite at 50Hz

I'm trying to use analogwrite to generate PWM signal on pins 3, 6, 9, and 10, but I want it to be at 50Hz. I've managed to get it down to 60ish Hz with a prescaler on Timer0. I thoughts changing the period registry would allow me to get the rest of the way there, but that doesn't seem to effect the analogwrite period. I'd like to be able to independently control the duty cycle for each of the pins, but the frequency should always be 50Hz. I've read through the datasheet but I can't figure out why changing the period of the clock wouldn't effect the analogwrite function. Thanks for any help you can give me!

#define PIN               3

#define OFF               14  // 0 to 255
#define IDLE              15  // 0 to 255
#define PWM_1100          17  // 0 to 255

#define PWM_TIMER_PERIOD 100 // For frequency

void setup() {
  TCA0.SINGLE.CTRLA &= ~TCA_SINGLE_ENABLE_bm; // Turn off timer while we change parameters.
  TCA0.SINGLE.CTRLA &= ~TCA_SINGLE_CLKSEL_gm; // Clear all CLKSEL bits.
  TCA0.SINGLE.CTRLA |= 0b1110; // prescaler
  TCA0.SINGLE.CMP0 = PWM_TIMER_PERIOD;
  TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm; // Re-enable timer. 

  pinMode(PIN, OUTPUT);
  analogWrite(PIN, OFF);
}

About 60 Hz is the lowest frequency you can get with the 16MHz clock, 1024 prescaler, and an 8bit timer. ((16e6/1024)/256 = 61.035)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.