air103#
This chapter describes how to use the pwm library for LuatOS
Introduction#
Pulse width modulation (PWM), is the English “Pulse Width Modulation” abbreviation, referred to as pulse width modulation, is the use of microprocessor digital output to control the analog circuit of a very effective technology. Simply, it is the control of pulse width.
According to Air103_MCU Design Manual V1.2.pdf, Air103 has a total of 20 PWM channels.
We can adjust the brightness of the LED small light by pwm different duty cycle output
Hardware preparation#
Air103 Development Board
LED small lamp
Wiring Schematic
PWM00 ------ +
Air103 LED
GND ------ -
Software part#
Interface documentation can be referred to:pwm library
Turn on pwm output#
Turn on the pwm output of the PWM00 channel, set different duty cycles, the small lights will have different brightness
PROJECT = "pwm"
VERSION = "1.0.0"
sys = require("sys")
sys.taskInit(function()
while true do
-- Turn on pwm channel 00, set pulse frequency to 1kHz, frequency division accuracy to 100, duty cycle to 100 = 10% continuous output
pwm.open(0, 1000, 10, 0, 100) -- The small lamp glows slightly
sys.wait(1000)
-- Turn on pwm channel 00, set pulse frequency to 1kHz, frequency division accuracy to 100, duty cycle to 100 = 50% continuous output
pwm.open(0, 1000, 50, 0, 100) -- Small light medium brightness
sys.wait(1000)
-- Turn on pwm channel 00, set pulse frequency to 1kHz, frequency division accuracy to 100, duty cycle to 100/100=100 continuous output
pwm.open(0, 1000, 100, 0, 100) -- Small lamp with high brightness
sys.wait(1000)
end
end)
sys.run()
Breathing light effect#
The effect of breathing light is the effect that the small lamp is lit up little by little and extinguished little by little when it is never lit. We can continuously increase the duty cycle to make the small lamp light up little by little and then continuously decrease the duty cycle to make the small lamp extinguish little by little.
The code is as follows
PROJECT = "pwm"
VERSION = "1.0.0"
sys = require("sys")
sys.taskInit(function()
while true do
-- The duty cycle continuously improves the brightness of small lamps from 0% to 50%
for i = 0, 50 do
pwm.open(0, 1000, i, 0, 100)
sys.wait(10)
end
sys.wait(1000)
-- Duty cycle continuously reduces the brightness of small lamps from 50% to 0%
for i = 50, 0, -1 do
pwm.open(0, 1000, i, 0, 100)
sys.wait(10)
end
sys.wait(1000)
end
end)
sys.run()