Skip to main content
2 of 4
deleted 333 characters in body
Ember
  • 83
  • 2
  • 12

Arduino AnalogWrite Source Code Explanation

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 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 as well.

Datasheet

Obviously the above code works, but how, with the missing bit settings?

Ember
  • 83
  • 2
  • 12