STM32F407的定时器1之7路PWM输出

时间:2022-03-12 23:30:58

实验现象:输出频率为10K

/****************************************************************************************
*函 数 名:bsp_InitTimer1GPIO
*函数功能:初始化定时器1相关IO
*形    参:无
*返 回 值:无
*****************************************************************************************/


void bsp_InitTimer1GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);/*使能GPIOE的时钟*/

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;/*复用*/
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;/*推挽输出*/
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;/**/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;/**/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_Init(GPIOE,&GPIO_InitStructure);/*初始化IO*/

GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_TIM1);/*复用*/
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource10,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource12,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_TIM1);
}


/****************************************************************************************
*函 数 名:
*函数功能:
*形    参:
*返 回 值:
*****************************************************************************************/
void bsp_Inittimer1(void)
{
uint16_t arr,ccr1,ccr2,ccr3,ccr4;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrecture;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);/*使能定时器1的时钟*/


arr = SystemCoreClock/10000;/*10K*/
ccr1 = arr/5;/*占空比20%*/
ccr2 = arr/4;/*占空比25%*/
ccr3 = arr/3;/*占空比33%*/
ccr4 = arr/2;/*占空比50%*/

bsp_InitTimer1GPIO();/*初始化IO口*/

TIM_TimeBaseInitStrecture.TIM_Period = arr;/*设置自动重装*/
TIM_TimeBaseInitStrecture.TIM_Prescaler = 0;/*不分频*/
TIM_TimeBaseInitStrecture.TIM_ClockDivision = TIM_CKD_DIV1;/**/
TIM_TimeBaseInitStrecture.TIM_CounterMode = TIM_CounterMode_Up;/*向上计数*/
TIM_TimeBaseInitStrecture.TIM_RepetitionCounter = 0;/**/
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStrecture);/*初始化*/



TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;/*设置PWM模式*/
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;/*起始输出为低*/
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;/*互补通道使能*/
TIM_OCInitStructure.TIM_OutputNState  = TIM_OutputNState_Enable;/*互补通道使能*/
TIM_OCInitStructure.TIM_Pulse = ccr1;/*比较寄存器设置*/
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;/**/
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;/*输出空闲状态1*/
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;/**/

TIM_OC1Init(TIM1,&TIM_OCInitStructure);/*初始化通道1*/

TIM_OCInitStructure.TIM_Pulse = ccr2;/*设置比较寄存器*/
TIM_OC2Init(TIM1,&TIM_OCInitStructure);/*初始化通道2*/

TIM_OCInitStructure.TIM_Pulse = ccr3;/*设置比较寄存器*/
TIM_OC3Init(TIM1,&TIM_OCInitStructure);/*初始化通道3*/

TIM_OCInitStructure.TIM_Pulse = ccr4;/*设置比较寄存器*/
TIM_OC4Init(TIM1,&TIM_OCInitStructure);/*初始化通道4*/

TIM_Cmd(TIM1,ENABLE);/*计数使能*/
TIM_CtrlPWMOutputs(TIM1,ENABLE);/*输出使能*/

}

/*
*********************************************************************************************************
* 函 数 名: main
* 功能说明: c程序入口
* 形    参:无
* 返 回 值: 错误代码(无需处理)
*********************************************************************************************************
*/
int main(void)
{
/*
ST固件库中的启动文件已经执行了 SystemInit() 函数,该函数在 system_stm32f4xx.c 文件,主要功能是
配置CPU系统的时钟,内部Flash访问时序,配置FSMC用于外部SRAM
*/
bsp_Init();//初始化定时器
/* 进入主程序循环体 */
while (1)
{

}
}