STM32F4高级定时器产生六路互补PWM波

高级定时器TIM8产生六路互补PWM波

一、使用高级定时器TIM8;

二、使用引脚:CH1-PC6,  CH1N-PA5,  CH2-PC7, CH2N-PB0,CH3-PC8,CH3N-PB1;

三、使用的寄存器:TIMx_CCMRx:选择PWM模式

                               TIMx_ARR:PWM的周期

                               TIMx_CCRx:PWM占空比

四、结构体:GPIO_InitTypeDef :GPIO口复用

                     GPIO_Init();

                     TIM_TimeBaseInitTypeDef :基本定时结构

                     TIM_TimeBaseInit();

                     TIM_OCInitTypeDef :输出比较结构

                     TIM_OCxInit();

                     TIM_BDTRInitTypeDef :刹车和死区结构

                     TIM_BDTRConfig();

五、配置函数

//使能定时器时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC,ENABLE);

//GPIO配置:使能GPIO口并复用

       GPIO_InitStrcuture.GPIO_Pin = GPIO_Pin_5;

       GPIO_InitStrcuture.GPIO_Mode = GPIO_Mode_AF;       

       GPIO_InitStrcuture.GPIO_Speed = GPIO_Speed_100MHz;      

       GPIO_InitStrcuture.GPIO_OType = GPIO_OType_PP;     

       GPIO_InitStrcuture.GPIO_PuPd = GPIO_PuPd_UP;       

       GPIO_Init(GPIOA,&GPIO_InitStrcuture); 

   

       GPIO_InitStrcuture.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;

       GPIO_Init(GPIOB,&GPIO_InitStrcuture);

      

       GPIO_InitStrcuture.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;  

       GPIO_Init(GPIOC,&GPIO_InitStrcuture);

      

       GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM8);    

       GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_TIM8);

       GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_TIM8);

       GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM8);

       GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM8);

       GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8);

//TIM8配置

       TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分割    

       TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;// 向上计数模式

       TIM_TimeBaseInitStructure.TIM_Period=arr; //自动重装载寄存器周期的值

       TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //预分频值

       TIM_TimeBaseInitStructure.TIM_RepetitionCounter=1; //周期计数器值 

       TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStructure);  

//PWM配置

       TIM_OCInitStruct.TIM_OCIdleState=TIM_OCIdleState_Reset;//当MOE=0重置输出比较空闲状态

       TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1; //脉冲宽度调制模式1     

       TIM_OCInitStruct.TIM_OCNIdleState=TIM_OCNIdleState_Reset;//当MOE=0重置输出比较N空闲状态

       TIM_OCInitStruct.TIM_OCNPolarity=TIM_OCNPolarity_High;// //互补输出比较极性高

       TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//输出比较极性高

       TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//使能输出比较状态

       TIM_OCInitStruct.TIM_OutputNState=TIM_OutputNState_Enable;//使能互补输出比较状态

       TIM_OCInitStruct.TIM_Pulse=4000;//待装入捕获比较寄存器的脉冲值

       TIM_OC1Init(TIM8,&TIM_OCInitStruct);

       TIM_OC2Init(TIM8,&TIM_OCInitStruct);

       TIM_OC3Init(TIM8,&TIM_OCInitStruct);//根据TIM8_OCInitStruct中指定的参数初始化 TIM8通道x

//使能端的打开

       TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);  

       TIM_OC2PreloadConfig(TIM8,TIM_OCPreload_Enable);

       TIM_OC3PreloadConfig(TIM8,TIM_OCPreload_Enable);//使能TIM8在CCR上的预装载寄存器

//刹车死区配置

       TIM_BDTRStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;// 自动输出功能使能  

       TIM_BDTRStructure.TIM_Break=TIM_Break_Disable;//失能刹车输入

       TIM_BDTRStructure.TIM_BreakPolarity=TIM_BreakPolarity_High; //刹车输入管脚极性高 

       TIM_BDTRStructure.TIM_DeadTime=100; //输出打开和关闭状态之间的延时      

       TIM_BDTRStructure.TIM_LOCKLevel=TIM_LOCKLevel_OFF;// 锁电平参数: 不锁任何位

       TIM_BDTRStructure.TIM_OSSIState=TIM_OSSIState_Disable; //设置在运行模式下非工作状态选项

       TIM_BDTRStructure.TIM_OSSRState=TIM_OSSRState_Disable; //设置在运行模式下非工作状态选项

       TIM_BDTRConfig(TIM8,&TIM_BDTRStructure);

TIM_ARRPreloadConfig(TIM8,ENABLE); //使能TIM8在ARR上的预装载寄存器

TIM_CtrlPWMOutputs(TIM8,ENABLE);   //PWM使能输出

TIM_Cmd(TIM8,ENABLE);              //打开TIM8

 


你可能感兴趣的:(stm32f4)