STM32Min开发板四路触屏可调PWM互补输出
在正点原子的开发例程上,结合PWM输出实验和触摸屏实验代码进行修改。 定义了几个全局变量存储定时器ARR值和占空比:
u32 F1=50;
u32 r1=50,r2=50;
u16 arr1=1439,comp1=719,comp2=719;
首先进行初始化,LCD和触摸屏初始化与例程相同。
TIM1_PWM_Init(arr1,0);
LCD_Init();
tp_dev.init();
定时器1初始化了四个通道,GPIO分别是PA8、9、10、11。
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
需要注意的是其中在开发板上PA9和PA10接到了串口,PA8接到了LED0。 然后是显示界面的设计:
POINT_COLOR=RED;
LCD_ShowString(30,30,200,16,16,"PWM1,PWM2 PWM3,PWM4");
LCD_ShowString(30,50,200,16,16,"F1: KHz ");
LCD_ShowString(30,70,200,16,16,"r1: % r2: %");
LCD_ShowString(20,120,200,16,16," F1+ F1-");
LCD_ShowString(20,170,200,16,16,"r1+ r1- r2+ r2-");
LCD_ShowString(30,250,200,16,16," PWM1:PA8 PWM2:PA9");
LCD_ShowString(30,270,200,16,16," PWM3:PA10 PWM4:PA11");
LCD_DrawLine(10,108,230,108);
LCD_DrawLine(10,148,230,148);
LCD_DrawLine(10,198,230,198);
LCD_DrawLine(10,108,10,198);
LCD_DrawLine(62,148,62,198);
LCD_DrawLine(118,108,118,198);
LCD_DrawLine(178,148,178,198);
LCD_DrawLine(230,108,230,198);
LCD_ShowNum(54,50,F1,2,16);
LCD_ShowNum(54,70,r1,2,16);
LCD_ShowNum(158,70,r2,2,16);
LCD_ShowString(100,220,200,16,16,"key:");
显示效果如下,比较简略: 主函数里边用while循环一直扫描触屏是否被按下,按下后在判断按下的区域,在执行对应的操作,改变定时器1的ARR或者CCR寄存器,从而改变PWM输出的频率和占空比。
while(1)
{
tp_dev.scan(0);
if(tp_dev.sta&TP_PRES_DOWN)
{
if((tp_dev.x[0]>10&&tp_dev.x[0]<230) &&(tp_dev.y[0]>108&&tp_dev.y[0]<198))
{
if((tp_dev.x[0]>10&&tp_dev.x[0]<118) &&(tp_dev.y[0]>108&&tp_dev.y[0]<148))
{
F1 += 5;
key = 1;
}
else if((tp_dev.x[0]>118&&tp_dev.x[0]<230) &&(tp_dev.y[0]>108&&tp_dev.y[0]<148))
{
F1 -= 5;
key = 2;
}
else if((tp_dev.x[0]>10&&tp_dev.x[0]<62) &&(tp_dev.y[0]>148&&tp_dev.y[0]<198))
{
r1 += 5;
key = 3;
}
else if((tp_dev.x[0]>62&&tp_dev.x[0]<118) &&(tp_dev.y[0]>148&&tp_dev.y[0]<198))
{
r1 -= 5;
key = 4;
}
else if((tp_dev.x[0]>118&&tp_dev.x[0]<178) &&(tp_dev.y[0]>148&&tp_dev.y[0]<198))
{
r2 += 5;
key = 5;
}
else if((tp_dev.x[0]>178&&tp_dev.x[0]<230) &&(tp_dev.y[0]>148&&tp_dev.y[0]<198))
{
r2 -= 5;
key = 6;
}
if(F1<20)
F1 = 20;
else if(F1>80)
F1 = 80;
if(r1>100)
r1 = 100;
if(r2>100)
r2 = 100;
arr1 = 72000000/(F1*1000);
comp1 = arr1*r1/100;
comp2 = arr1*r2/100;
TIM1->ARR = arr1;
TIM1->CCR1 = comp1;
TIM1->CCR2 = comp1;
TIM1->CCR3 = comp2;
TIM1->CCR4 = comp2;
LCD_ShowNum(54,50,F1,2,16);
LCD_ShowNum(54,70,r1,3,16);
LCD_ShowNum(158,70,r2,3,16);
LCD_ShowNum(135,220,key,1,16);
while(tp_dev.sta&TP_PRES_DOWN)
{
tp_dev.scan(0);
}
}
}else delay_ms(10);
}
完整工程: STM32Min开发板四路触屏可调PWM互补输出
|