提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
使用PWM调光控制LED灯板的亮度。
一、PWM是什么?
PWM是“脉宽调制”(Pulse Width Modulation)的缩写,它是一种对模拟信号进行数字控制的技术。在PWM中,一个周期性的信号(通常是方波)的占空比被调节,以控制输出电压或电流的平均值。这使得PWM广泛应用于各种电子设备和系统中,用于精确控制功率、电机速度、灯光亮度等。
二、使用步骤
1.配置设备树
&pwm5 {
sd,playback-num-chan = <2>;
#pwm-cells = <3>;
status = "okay";
};
2.配置信息如下:
3.PWM的一些参数概念
ls /sys/class/pwm/ pwmchip(n) #确定
pwm(n)所对应的chip
echo 1 > /sys/class/pwm/pwmchip0/export # 导出
pwm(n)的通道目录 ,echo 值代表通道号
echo 1000 > /sys/class/pwm/pwmchip0/pwm1/period #设置pwm周期
echo 500 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle #设置pwm占空比
echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable #使能pwm输出
echo 0 > /sys/class/pwm/pwmchip0/pwm1/enable #disabled pwm输出
4.编写shell脚本进行测试
比如我这里制作一个脚本pwm_config_light.sh进行测试,代码如下:
#!/bin/bash
echo 0 > /sys/class/pwm/pwmchip0/export
echo 1000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 500 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
cat /sys/class/pwm/pwmchip0/pwm0/enable
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
sleep 3
echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
sleep 3
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
while true
do
echo 400 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 300 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 200 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 100 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 50 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 30 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 10 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 30 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 50 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 100 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 200 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 300 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 400 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
sleep 1
echo 500 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
done
运行测试的效果还是不错的,后面可以增加一个视频进行说明。
总结
本次使用PWM控制LED灯板的亮度,使用自动化的脚本进行测试实际的亮度。