void analogWriteAdvance( uint32_t pin, uint32_t sourceClock, uint32_t clockDivider, uint32_t cycle, uint32_t dutyCycle );
void analogWriteAdvance( uint32_t pin, uint32_t sourceClock, uint32_t clockDivider, uint32_t cycle, uint32_t dutyCycle );
Outputs analog value (PWM wave) to the assigned analog output pin.
|
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 |
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); }
wiring_analog.h
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: