Jump to content

Component: PWM (Internal) (General Output): Difference between revisions

From Flowcode Help
No edit summary
m Text replacement - "style="background-color:#EAE1EA;"" to "class="mtx-class-propfolder""
 
(5 intermediate revisions by the same user not shown)
Line 100: Line 100:


===ChangePeriod===
===ChangePeriod===
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ChangePeriod'''
| width="90%" class="mtx-class-macrohead" | '''ChangePeriod'''
|-
|-
| colspan="2" | Sets the overall period and prescaler of the output PWM signal. 
| colspan="2" | Sets the overall period and prescaler of the output PWM signal. 
Line 124: Line 124:


===Disable===
===Disable===
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Disable'''
| width="90%" class="mtx-class-macrohead" | '''Disable'''
|-
|-
| colspan="2" | Disables a PWM channel and allows the default output / input state to be resumed 
| colspan="2" | Disables a PWM channel and allows the default output / input state to be resumed 
Line 138: Line 138:


===Enable===
===Enable===
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Enable'''
| width="90%" class="mtx-class-macrohead" | '''Enable'''
|-
|-
| colspan="2" | Enables a PWM channel as an output overriding the default output pin state. 
| colspan="2" | Enables a PWM channel as an output overriding the default output pin state. 
Line 152: Line 152:


===SetDutyCycle===
===SetDutyCycle===
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetDutyCycle'''
| width="90%" class="mtx-class-macrohead" | '''SetDutyCycle'''
|-
|-
| colspan="2" | Sets the PWM duty cycle in terms of on/off based on the current period setting. E.g. if period = 255 then duty of 128 is equal to 50% on and 50% off. 16-bit PIC users should use the 10bit duty function to access the full range. 
| colspan="2" | Sets the PWM duty cycle in terms of on/off based on the current period setting. E.g. if period = 255 then duty of 128 is equal to 50% on and 50% off. 16-bit PIC users should use the 10bit duty function to access the full range. 
Line 171: Line 171:


===SetDutyCycle10Bit===
===SetDutyCycle10Bit===
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetDutyCycle10Bit'''
| width="90%" class="mtx-class-macrohead" | '''SetDutyCycle10Bit'''
|-
|-
| colspan="2" | PIC/AVR - Sets the full scale PWM duty cycle based on the current period setting. If period = 255 then Duty of 512 is equal to 50%.  16-bit PICs have a 16-bit period range available. If period = 65535 then Duty of 32768 is equal to 50%. 
| colspan="2" | PIC/AVR - Sets the full scale PWM duty cycle based on the current period setting. If period = 255 then Duty of 512 is equal to 50%.  16-bit PICs have a 16-bit period range available. If period = 65535 then Duty of 32768 is equal to 50%. 
Line 190: Line 190:


===SetFrequency===
===SetFrequency===
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetFrequency'''
| width="90%" class="mtx-class-macrohead" | '''SetFrequency'''
|-
|-
| colspan="2" | Sets a PWM of the specifed frequency at a duty of 50% Ideal use is a  frequency generator 
| colspan="2" | Sets a PWM of the specifed frequency at a duty of 50% Ideal use is a  frequency generator 
Line 210: Line 210:
==Property reference==
==Property reference==


{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" | [[File:Fc9-prop-icon.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-prop-icon.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Properties'''  
| width="90%" class="mtx-class-macrohead" | '''Properties'''  
|-
|-
|-
|-
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
| width="10%" align="center" class="mtx-class-propfolder" | [[File:Fc9-conn-icon.png]]
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Connections
| width="90%" class="mtx-class-propfolder" | Connections
|-
|-
|-
|-
Line 240: Line 240:
| colspan="2" | Allows the PWM hardware pin to be reassigned to another pin 
| colspan="2" | Allows the PWM hardware pin to be reassigned to another pin 
|-
|-
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
| width="10%" align="center" class="mtx-class-propfolder" | [[File:Fc9-conn-icon.png]]
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | PWM Frequency
| width="90%" class="mtx-class-propfolder" | PWM Frequency
|-
|-
|-
|-
Line 274: Line 274:
| colspan="2" | Displays the frequency of PWM cycles per second. 
| colspan="2" | Displays the frequency of PWM cycles per second. 
|-
|-
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
| width="10%" align="center" class="mtx-class-propfolder" | [[File:Fc9-conn-icon.png]]
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Calculations
| width="90%" class="mtx-class-propfolder" | Calculations
|-
|-
|-
|-
Line 303: Line 303:
| colspan="2" | Applies the calculated period and prescaler settings to the PWM component default settings. 
| colspan="2" | Applies the calculated period and prescaler settings to the PWM component default settings. 
|-
|-
| width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]]
| width="10%" align="center" class="mtx-class-propfolder" | [[File:Fc9-conn-icon.png]]
| width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Simulation
| width="90%" class="mtx-class-propfolder" | Simulation
|-
|-
|-
|-

Latest revision as of 14:36, 13 July 2026

Author Matrix Ltd
Version 12.0
Category General Output


PWM (Internal) component

Pulse Width Modulation, a versitile way of generating a digital pulse using mark / space modulation. Uses the capture compare peripherals onboard most Microcontrollers to generate accurate waveforms without any intervention from the processor. Useful for generating audio, controlling the speed of motors, brightness of LED etc.

Version information

Library Version, Component Version, Date, Author, Info
11, 11.0, 15-10-24, BR, Fixed change frequency and duty for ESP32
12, 12.0, 15-10-24, MW, Fixed Digital version as visually looks wrong


Detailed description

No detailed description exists yet for this component

Examples

Here is a basic example to control the PWM duty based on an ADC reading.

PWMDemo

Digital PWM trace


Here is another basic example which ramps the PWM duty cycle up and down. If the PWM output pin is connected to a LED and series resistor then the LED will smoothly transition between on bright and off.

PWM Fader


Macro reference

ChangePeriod

ChangePeriod
Sets the overall period and prescaler of the output PWM signal. 
- UINT Period
The maximum number that will represent 100% on, PIC/AVR: 0-255 16-bit PIC: 0-65535 
- INT Prescaler
The scaler used to divide the system clock speed down to the PWM rate. 
- VOID Return


Disable

Disable
Disables a PWM channel and allows the default output / input state to be resumed 
- VOID Return


Enable

Enable
Enables a PWM channel as an output overriding the default output pin state. 
- VOID Return


SetDutyCycle

SetDutyCycle
Sets the PWM duty cycle in terms of on/off based on the current period setting. E.g. if period = 255 then duty of 128 is equal to 50% on and 50% off. 16-bit PIC users should use the 10bit duty function to access the full range. 
- BYTE Duty
8-bit PWM duty 0-255 
- VOID Return


SetDutyCycle10Bit

SetDutyCycle10Bit
PIC/AVR - Sets the full scale PWM duty cycle based on the current period setting. If period = 255 then Duty of 512 is equal to 50%. 16-bit PICs have a 16-bit period range available. If period = 65535 then Duty of 32768 is equal to 50%. 
- UINT Duty
PWM duty PIC/AVR: 0-1023 16-bit PIC: 0-65535 
- VOID Return


SetFrequency

SetFrequency
Sets a PWM of the specifed frequency at a duty of 50% Ideal use is a frequency generator 
- ULONG Frequency
Enter frequency in Hz 
- VOID Return


Property reference

Properties
Connections
Channel
Selects which PWM channel the component is connected to. 
Alternative pin
Allows an alternate pin to be used if available. Note that on some devices you will also have to change this setting in the device configuration. 
PWM Pin
Specifies the pin assigned to the PWM channel selected 
Remap Pin
Allows the PWM hardware pin to be reassigned to another pin 
PWM Frequency
PWM Timer
Timer associated with PWM to drive the output. Warning - All PWM channels linked to the same timer will share the same period and prescaler settings. 
Period Overflow
Allows the user to change the number of counts for the whole PWM period. Note that all PWM channels based on the same timer will share the same period overflow. PIC/AVR Range: 0 - 255 16-bit PIC Range: 0 - 65535 
Prescaler
Allows the user to change the number of program cycles per PWM cycle count. Note that all PWM channels based on the same timer will share the same prescaler. 
Period (us)
Displays the length of time to complete one PWM cycle. 
Frequency (Hz)
 
Frequency (KHz)
Displays the frequency of PWM cycles per second. 
Calculations
Required Frequency (Hz)
Type in your required frequency in Hz and the component will automatically calculate the best prescaler and period to acheive the frequency with the maximum possible resolution. 
Calculated Frequency (Hz)
The frequency we have ended up acheiving using the calculated period and prescaler 
Calculated Period
The suggested period setting to use to acheive the calculated frequency. 
Calculated Prescaler
The suggested prescaler setting to use to acheive the calculated frequency. 
Apply To Defaults
Applies the calculated period and prescaler settings to the PWM component default settings. 
Simulation
Representation
Edits how the PWM is shown on the simulation panel. 

Component Source Code

Please click here to download the component source project: FC_Comp_Source_PWM_Output.fcfx

Please click here to view the component source code (Beta): FC_Comp_Source_PWM_Output.fcfx