资深版主
- 积分
- 10541
- 金钱
- 10541
- 注册时间
- 2017-2-18
- 在线时间
- 1908 小时
|
本帖最后由 peng1554 于 2019-8-21 21:33 编辑
有用户发帖说2.8寸MCU屏无法设置背光亮度:http://www.openedv.com/forum.php ... 12&page=1#pid896957,所以移植了这个程序测试程序基于TFTLCD实验例程修改,战舰V3和精英板子的PB0接的是LCD的背光信号,而PB0可以复用为TIM3_CH3输出PWM
只要控制PWM的占空比就能控制液晶亮度,所以要使用PWM输出需要用户熟悉下PWM输出实验。在熟悉PWM的情况下再去
设置TIM3_CH3输出PWM
整个程序最重要的LCD_PWM_Init()和LCD_SSD_BackLightSet(),LCD_PWM_Init()
设置TIM3_CH3PWM输出,LCD_SSD_BackLightSet()设置7寸V2MCU屏液晶亮度
LCD_PWM_Init()如下:
[mw_shl_code=c,true]//控制BL信号PWM初始化TIM3_CH3(PB0)
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void LCD_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能GPIO外设时钟
//设置该引脚为复用输出功能,输出TIM3 CH3的PWM脉冲波形 GPIOB.0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //TIM_CH3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3 Channel3 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR3上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIM3
}[/mw_shl_code]
LCD_SSD_BackLightSet()如下:
[mw_shl_code=c,true]//SSD1963 背光设置
//pwm:背光等级,0~100.越大越亮.
void LCD_SSD_BackLightSet(u8 pwm)
{
LCD_WR_REG(0xBE); //配置PWM输出
LCD_WR_DATA(0x05); //1设置PWM频率
LCD_WR_DATA(pwm*2.55);//2设置PWM占空比
LCD_WR_DATA(0x01); //3设置C
LCD_WR_DATA(0xFF); //4设置D
LCD_WR_DATA(0x00); //5设置E
LCD_WR_DATA(0x00); //6设置F
}[/mw_shl_code]
EN_MCU_SSD1963宏来确定控制7寸V2屏的亮度还是控制 2.8/3.5/4.3寸MCU屏亮度,main.c程序如下[mw_shl_code=c,true]#define EN_MCU_SSD1963 1 //使能7寸V2MCU屏,注释掉即使能2.8/3.5/4.3MCU屏
int main(void)
{
u16 lcdpwmval_1,lcdpwmval_2=0;// lcdpwmval_1,lcdpwmval_2
u8 dir_1,dir_2=1;
u8 lcd_id[12]; //存放LCD ID字符串
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LCD_PWM_Init(899,0);//LCD PWM控制频率为80KHZ
LCD_Init();
POINT_COLOR=RED;
sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);//将LCD ID打印到lcd_id数组。
LCD_Clear(WHITE);
LCD_ShowString(30,40,210,24,24,"WarShip STM32 ^_^");
LCD_ShowString(30,70,200,16,16,"TFTLCD TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
while(1)
{
#ifdef EN_MCU_SSD1963 //控制TIM3_CH3输出PWM的占空比
delay_ms(10);
if(dir_1)lcdpwmval_2++;
else lcdpwmval_2--;
if(lcdpwmval_2>100)dir_1=0;
if(lcdpwmval_2==0)dir_1=1;
LCD_SSD_BackLightSet(lcdpwmval_2);
#else
delay_ms(10);
if(dir_1)lcdpwmval_1++;
else lcdpwmval_1--;
if(lcdpwmval_1>800)dir_1=0;
if(lcdpwmval_1==0)dir_1=1;
TIM_SetCompare3(TIM3,lcdpwmval_1);
#endif
}
}[/mw_shl_code]
|
|