Initialization of PWM for Mega boards

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
Lecostarius
Posts: 46
Joined: Mon Oct 20, 2014 11:29 pm

Initialization of PWM for Mega boards

Post by Lecostarius »

Everyone,

I stumbled over the initialization of the compare registers for timers 3 and 4 for the Atmel Mega controllers. This is used to set the PWM rate. In Output.cpp there is the following line:

ICR3 |= 0x3FFF; // TOP to 16383;

If ICR3 is set to 16383, the PWM frequency will be half of (16 MHz / 16383) or half of 976.6 Hz or 488.3 Hz - just below the 500 Hz theoretical limit. This is what is intended and I verified that the resulting PWM frequency on my board really is 488 Hz.

However why is this a "|=" and not a plain "="? The two highest order bits are left unaffected and might have any value during initialization. That would lead to PWM frequencies of less than 488 Hz - could be as low as 122 Hz. Although copters will still fly at that frequency, this is undesired (and unpredictable).

I consider the line above a bug. Am I missing something?

Best, Leco

User avatar
haydent
Posts: 583
Joined: Sun Jun 17, 2012 1:35 am
Location: NSW, AU

Re: Initialization of PWM for Mega boards

Post by haydent »

you could be onto something, though i know little about this topic, ive been looking at it recently and found other spots where higher bits weren't set and wondered why not ... ?

Post Reply