-
Notifications
You must be signed in to change notification settings - Fork 227
PWM module
Jaume Olivé Petrus edited this page Jun 20, 2017
·
13 revisions
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(...)
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(pwm.PWM0, 0, 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(pwm.PWM0, 0, 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(pwm.PWM0, 0, 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(pwm.PWM0, 0, 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(pwm.PWM0, 0, 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()