PWMOut

public final class PWMOut

The PWMOut class is used to vary the output voltage by controlling the duration of high output in the time periodCycles on the pin.

Attention

The PWM pins with same number are paired, like PWM3A and PWM3B. They can only share the same frequency.

Example: Light a LED

import SwiftIO

// Initiate a PWMOut to Pin PWM0A.
let led = PWMOut(Id.PWM0A)

// Set the brightness of the LED by setting the duty cycle.
while true {
    led.setDutycycle(0.5)
}
  • The max frequency of PWM output.

    Declaration

    Swift

    public var maxFrequency: Int { get }
  • The min frequency of PWM output.

    Declaration

    Swift

    public var minFrequency: Int { get }
  • Initialize a PWM output on a specified pin.

    Usage Example

    // The most simple way of initiating a pin PWM0A, with all other parameters set to default.
    let pin = PWMOut(Id.PWM0A)
    
    // Initialize the pin PWM0A with the frequency set to 2000hz.
    let pin = PWMOut(Id.PWM0A, frequency: 2000)
    
    // Initialize the pin PWM0A with the frequency set to 2000hz and the dutycycle set to 0.5.
    let pin = PWMOut(Id.PWM0A, frequency: 2000, dutycycle: 0.5)
    

    Declaration

    Swift

    public init(_ id: IdName,
                frequency: Int = 1000,
                dutycycle: Float = 0.0)

    Parameters

    id

    REQUIRED The name of PWMOut pin. See Id for reference.

    frequency

    OPTIONAL The frequency of the PWM signal.

    dutycycle

    OPTIONAL The duration of high output in the time period from 0.0 to 1.0.

  • Set the frequency and the dutycycle of a PWM output signal. The value of the dutycycle should be a float between 0.0 and 1.0.

    Declaration

    Swift

    public func set(frequency: Int, dutycycle: Float)

    Parameters

    frequency

    The frequency of the PWM signal.

    dutycycle

    The duration of high output in the time period from 0.0 to 1.0.

  • Set the period and pulse width of a PWM output signal.

    Declaration

    Swift

    public func set(period: Int, pulse: Int)

    Parameters

    period

    The period of the PWM ouput signal in microsecond.

    pulse

    The pulse width in the PWM period. This time can’t be longer than the period.

  • Set the duty cycle of a PWM output signal, that’s to say, set the duration of the on-state of a signal. The value should be a float between 0.0 and 1.0.

    Declaration

    Swift

    @inline(__always)
    public func setDutycycle(_ dutycycle: Float)

    Parameters

    dutycycle

    The duration of high output in the time period from 0.0 to 1.0.

  • Stop the PWM output.

    Declaration

    Swift

    public func suspend()
  • Continue the PWM output.

    Declaration

    Swift

    public func resume()