如何通过PWM占空比控制屏幕背光?

2. 准备工作
可正常运行的屏幕显示程序
屏幕背光调节范围20-100%,20k pwm频率
3. 实施
不管是采用那种gui框架开发,我们一般都需要对屏幕背光做控制,背光的控制是通用的,例如我们在emwin工程和touchgfx工程下,都是采用同一套背光可控制逻辑和代码,背光的最低亮度根据自己的实际应用做限制,例如我的工程是25%-100%背光可调节。以stm32h743控制800*480的显示屏为例,使用tim2的一个通道产生pwm来控制屏幕背光。要清除tim作为pwm运行的控制和几个计算以及寄存器的配置。
4. 代码实现
首先我们需要对外有一个初始化接口,有一个改变占空比的接口,
void bsp_bl_init(void)
{
/* compute the prescaler value to have tim1 counter clock equal to 20000000 hz */
uhprescalervalue = (uint32_t)(systemcoreclock / (2*20000000)) - 1;
timhandle.instance = timx;
timhandle.init.prescaler = uhprescalervalue;
timhandle.init.period = period_value;
timhandle.init.clockdivision = 0;
timhandle.init.countermode = tim_countermode_up;
timhandle.init.repetitioncounter = 0;
if (hal_tim_pwm_init(&timhandle) != hal_ok)
{
/* initialization error */
error_handler();
}
/ ##-2- configure the pwm channels ######################################### /
/* common configuration for all channels */
sconfig.ocmode = tim_ocmode_pwm1;
sconfig.ocpolarity = tim_ocpolarity_high;
sconfig.ocfastmode = tim_ocfast_disable;
sconfig.ocnpolarity = tim_ocnpolarity_high;
sconfig.ocnidlestate = tim_ocnidlestate_reset;
sconfig.ocidlestate = tim_ocidlestate_reset;
/* set the pulse value for channel 1 */
sconfig.pulse = 0;//1000-1;//10;//pulse1_value;
if (hal_tim_pwm_configchannel(&timhandle, &sconfig, tim_channel_1) != hal_ok)
{
/* configuration error */
error_handler();
}
if (hal_tim_pwm_start(&timhandle, tim_channel_1) != hal_ok)
{
/* pwm generation error */
error_handler();
}
}
初始化tim外设的底层驱动,配置时钟,通道,周期等。并开启pwm
对外的占空比改变接口主要是用来刷新pwm的占空比,达到调节背光的目的,
void bsp_vlcdblctrl(uint32_t pulsevalue)
{
if((pulsevalue >= 10) && (pulsevalue <= 1000))/**校验设置范围,**/{
sconfig.pulse = pulsevalue;}
else
{
sconfig.pulse = 500;/**50%**/}
if (hal_tim_pwm_configchannel(&timhandle, &sconfig, tim_channel_1) != hal_ok)/**配置通道**/ { /* configuration error */ error_handler(); } hal_tim_pwm_stop(&timhandle, tim_channel_1); if (hal_tim_pwm_start(&timhandle, tim_channel_1) != hal_ok)/**开启pwm***/ { /* pwm generation error */ error_handler(); }}
一般触摸屏我们固定周期没有触摸就会将背光调低,这时候屏幕会变暗,一旦有触摸,重新将背光调亮,
pwm也可以采用中断,这里没有采用中断,都可以实现。在三个项目上都是采用的这个背光控制逻辑和代码。

数字照度计的规格_数字照度计的特点
光伏专业术语全解
余压监控系统可保证消防通道压差维持在一个标准值
idea要配置java环境变量吗
融智于光,共创未来 | 华为持续引领行业高质量发展 加速光伏成为主力能源
如何通过PWM占空比控制屏幕背光?
基于89C52单片机的微电压信号源设计
2008年十大电动车电池品牌榜中榜/名牌电动车电池
介绍四种方法均可执行PCIe 2.0基本规格要求的PLL回路响应量测
三星Note8什么时候上市?三星Note8最新消息:三星Note8屏内指纹+4K无边框+IP68防水,设计图抢先看
物联网未来的光明浪潮
紫光集团巨资完成收购法国芯片组件商
X-MADIS综合反无人机系统
运行nginx所需的最低配置
三分钟看懂贴片大电流共模电感选型中封装尺寸的重要性
什么是PWM技术
神行超充电池加速“上车”,全民超充时代已来
采用低压差大电流集成可调稳压芯片实现半导体激光器驱动电路的设计
科普——什么是WiFi6
HP8656A百分百实惠HP8656A无线通讯设备HP8656A