user30200: The source code for the analogWrite function looks like this:
void analogWrite(uint8_t pin, int val) {
    ...
    sbi(TCCRnA, COM0x);
    OCRnx = val;
    ...
}

Where n is which timer (0, 1, 2...) and x is which pin is controlled by that timer (A, B). 
This doesn't make sense to me, because the ATmega328P datasheet says that the bits TCCRnA.WGM0[1:0], and TCCRnB.WGM0 need to be set up as well.
Datasheet
Obviously the above code works, but how, with the missing bit settings?

user33871: Look at the init() function in wiring.c.  All of the timers get set up there depending on which chip is selected.  I don't know if some might also be done by initVariant().  But all it leaves for analogWrite to do is set the bit in the COM register to enable the PWM.  

30200: <p>The source code for the <code>analogWrite</code> function looks like this:</p>

<pre><code>void analogWrite(uint8_t pin, int val) {
    ...
    sbi(TCCRnA, COM0x);
    OCRnx = val;
    ...
}
</code></pre>

<p>Where n is which timer (0, 1, 2...) and x is which pin is controlled by that timer (A, B). </p>

<p>This doesn't make sense to me, because the ATmega328P datasheet says that the bits <code>TCCRnA.WGM0[1:0]</code>, and <code>TCCRnB.WGM0</code> need to be set up as well.</p>

<p><a href="http://www.atmel.com/Images/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf" rel="nofollow noreferrer">Datasheet</a></p>

<p>Obviously the above code works, but how, with the missing bit settings?</p>
 33871: <p>Look at the init() function in wiring.c.  All of the timers get set up there depending on which chip is selected.  I don't know if some might also be done by initVariant().  But all it leaves for analogWrite to do is set the bit in the COM register to enable the PWM.  </p>
