-
Notifications
You must be signed in to change notification settings - Fork 0
PWM module
This module contains functions for accessing the CPU's PWM (Pulse Width Modulation).
To use this module you must take into consideration the following:
-
Attach a device to a PWM pin, using the pwm.attach function, and store the instance into a variable.
device = pwm.attach(.....)
-
Use the variable device for operate:
device:start(...) ... device:stop(...)
-
Detach the device if it is no longer used:
device:start(...) ... device:detach(...)
Attach a device to a PWM pin.
Arguments:
- pin: the GPIO identifier to use. Use pio.GPIOx defined for this purpose.
- frequency: pulse frequency, expressed in hertzs
- initial duty: initial duty value, a decimal number beetween 0 and 1, where 0 is 0%, and 1 is 100%.
Returns: the PWM instance or an exception. You must store this instance into a variable for further operations with it.
-- Attach a device to PWM pin at GPIO16, at 10 Khz, with initial duty value of 50%.
device = pwm.attach(pio.GPIO16, 10000, 0.5)
Start the PWM generation.
Returns: nothing, or an exception.
-- Attach a device to PWM pin at GPIO16, at 10 Khz, with initial duty value of 50%.
device = pwm.attach(pio.GPIO16, 10000, 0.5)
-- Start the PWM generation
device:start()
Stop the PWM generation.
Returns: nothing, or an exception.
-- Attach a device to PWM pin at GPIO16, at 10 Khz, with initial duty value of 50%.
device = pwm.attach(pio.GPIO16, 10000, 0.5)
-- Start the PWM generation
device:start()
....
-- Stop the PWM generation
device:stop()
Set a new duty value.
Arguments:
- duty: new duty value, a decimal number beetween 0 and 1, where 0 is 0%, and 1 is 100%.
Returns: nothing, or an exception.
-- Attach a device to PWM pin at GPIO16, at 10 Khz, with initial duty value of 50%.
device = pwm.attach(pio.GPIO16, 10000, 0.5)
-- Start the PWM generation
device:start()
....
-- Set duty to 25%
device:setduty(0.25)
....
-- Stop the PWM generation
device:stop()
Set a new frequency value.
Arguments:
- frequency: pulse frequency, expressed in hertzs
Returns: nothing, or an exception.
-- Attach a device to PWM pin at GPIO16, at 10 Khz, with initial duty value of 50%.
device = pwm.attach(pio.GPIO16, 10000, 0.5)
-- Start the PWM generation
device:start()
....
-- Set frequency to 5 khz
device:setfreq(5000)
....
-- Stop the PWM generation
device:stop()
Detach a device to a PWM pin. When detaching, the PWM generation is stopped. The device can be also detached if it is assigned to the nil value.
Arguments: nothing.
Returns: nothing.
-- Attach a device to PWM pin at GPIO16, at 10 Khz, with initial duty value of 50%.
device = pwm.attach(pio.GPIO16, 10000, 0.5)
-- Start the PWM generation
device:start()
....
-- Detach the device
device:detach()