Component: PWM (Outputs: General)
Author | Matrix Ltd |
Version | 1.5 (Release) |
Category | Outputs: General |
Contents
PWM 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.
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
Downloadable macro reference
ChangePeriod
Sets the overall period and prescaler of the output PWM signal.
Parameters
- BYTE Period
- The maximum number that will represent 100% on, 0-255
- INT Prescaler
- The scaler used to divide the system clock speed down to the PWM rate.
Return value
- This call does not return a value
Disable
Disables a PWM channel and allows the default output / input state to be resumed
Parameters
- This macro has no parameters
Return value
- This call does not return a value
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.
Parameters
- BYTE Duty
- 8-bit PWM duty 0-255
Return value
- This call does not return a value
Enable
Enables a PWM channel as an output overriding the default output pin state.
Parameters
- This macro has no parameters
Return value
- This call does not return a value
SetDutyCycle10Bit
Sets the 10-bit PWM duty cycle in terms of on/off based on the current period setting.
E.g. if period = 255 then Duty of 512 is equal to 50% on and 50% off.
Parameters
- UINT Duty
- 10-bit PWM duty 0-1023
Return value
- This call does not return a value
Simulation macro reference
This component does not contain any simulation macros
Property reference
Channel
This property is of type Fixed list of ints and can be referenced with the variable name channel.
Selects which PWM channel the component is connected to.
Alternative pin
This property is of type True or false and can be referenced with the variable name altpin.
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
This property is of type Single digital pin and can be referenced with the variable name pwm_pin.
Specifies the pin assigned to the PWM channel selected
Period Overflow
This property is of type Fixed list of ints and can be referenced with the variable name period.
Allows the user to change the number of counts for the whole PWM period. 0 - 255.
Prescaler
This property is of type Fixed list of ints and can be referenced with the variable name prescale.
Allows the user to change the number of program cycles per PWM cycle count.
Period (us)
This property is of type Floating point and can be referenced with the variable name period_calc.
Displays the length of time to complete one PWM cycle.
Frequency (KHz)
This property is of type Floating point and can be referenced with the variable name frequency_calc.
Displays the frequency of PWM cycles per second.
Representation
This property is of type Fixed list of ints and can be referenced with the variable name SimRep.
Edits how the PWM is shown on the simulation panel.
Displayed Periods
This property is of type Fixed list of ints and can be referenced with the variable name DispPeriods.
No additional information