Skip to main content
deleted 5 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Arduino AnalogWrite Source Code ExplanationanalogWrite source code explanation

The source code for the analogWriteanalogWrite 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?

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

Datasheet

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

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 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?

added 3 characters in body
Source Link
Ember
  • 83
  • 2
  • 12

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

Datasheet

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

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?

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

Datasheet

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

deleted 333 characters in body
Source Link
Ember
  • 83
  • 2
  • 12

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], TCCRnB.WGM02, and TIMSKnTCCRnB.OCIExWGM0 all need to be set as well.

  • TCCRnA.COM0x[1:0], TCCRnA.WGM0[1:0], and TCCRnB.WGM02 set the output mode and whether the signal is inverted.

  • TIMSKn.OCIEx enables the Compare Match Interrupt for the pin, which is required for the Waveform Generator to function, as stated in section 19.5 of the datasheet, referenced below.

Datasheet

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

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], TCCRnB.WGM02, and TIMSKn.OCIEx all need to be set as well.

  • TCCRnA.COM0x[1:0], TCCRnA.WGM0[1:0], and TCCRnB.WGM02 set the output mode and whether the signal is inverted.

  • TIMSKn.OCIEx enables the Compare Match Interrupt for the pin, which is required for the Waveform Generator to function, as stated in section 19.5 of the datasheet, referenced below.

Datasheet

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

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?

Source Link
Ember
  • 83
  • 2
  • 12
Loading