Syntax
void analogWriteAdvance(
    uint32_t pin, 
    uint32_t sourceClock, 
    uint32_t clockDivider, 
    uint32_t cycle, 
    uint32_t dutyCycle
);
ContentsIndex
PreviousUpNext
analogWriteAdvance
Example
Syntax
void analogWriteAdvance(
    uint32_t pin, 
    uint32_t sourceClock, 
    uint32_t clockDivider, 
    uint32_t cycle, 
    uint32_t dutyCycle
);
Description

Outputs analog value (PWM wave) to the assigned analog output pin.

Parameters
Parameters 
Description 
ulPin 
[IN] analog output pin number; 3, 9
 
ulClock 
[IN] define base clock frequency, can be PWM_SOURCE_CLOCK_13MHZ or PWM_SOURCE_CLOCK_32KHZ
 
ulDiv 
[IN] define clock frequency divider, can be PWM_CLOCK_DIV1, PWM_CLOCK_DIV2, PWM_CLOCK_DIV4 or PWM_CLOCK_DIV8
 
ulCycle 
[IN] the cycle, between 0 and 8191
 
ulDuty 
[IN] the duty cycle,between 0 and 8191, but must be smaller than cycle 
Example
uint32_t pwmPin = 9;    // use Digital pin D9
 
int cycle = 9;                      // Divide output into 9+1 = 10 portions
int sourceClock = PWM_SOURCE_CLOCK_13MHZ;   //
int divider = PWM_CLOCK_DIV8;       // The PWM frequency will be 13MHz / 8 / 10 = 162.5KHz
 
int duty = 0;
int offset = 1;
 
void setup()
{
    pinMode(pwmPin, OUTPUT);
}
 
void loop()
{
    analogWriteAdvance(pwmPin, sourceClock, divider, cycle, duty);
 
    duty += offset;
    if (duty == cycle)
    {
        offset = -1;
    }
    else if(duty == 0)
    {
        offset = 1;
    }
    delay(1000);
}
File

wiring_analog.h

Notes

Only the analog output pins D3 and D9 support analog output. 

You can define your own PWM output wave accuracy (max. 13-bit) and frequency in this function. 

The wave accuracy and frequency can be setup by following below steps: 

  1. Decide accuracy first. If you need 0 ~ N levels, set cycle to N.
  2. Next, decide frequency. If cycle = N, sourceClock = PWM_SOURCE_CLOCK_13MHZ and clockDivider = WM_CLOCK_DIV1, the frequency will be 13MHz/(N+1). if cycle = N, sourceClock = PWM_SOURCE_CLOCK_32KHZ and clockDivider = PWM_CLOCK_DIV8, the frequency will be 32kHz / (8 * (N+1))
  3. If the frequency is too high or too low, adjust sourceClock and clockDivider adjust the frequency.
 

Group