详解STM32互补PWM输出中带死区时间的设置方法

1.互补PWM输出简介

  • 在使用stm32输出PWM波形时,笔者所用的stm32f103zet6中的高级定时器TIM1和TIM8可以输出互补的PWM波形,使用互补的PWM波常见与一些半桥电路和全桥电路控制中,使用MCU自带的高级时钟可以简便的解决互补PWM输出问题。
    笔者在控制半桥电路中应用了MCU自带的TIM1时钟CH_1和CH_1N的输出带死区时间的互补PWM波形,输出稳定。
  • 2.互补PWM波输出设置

    /**
     * @brief 	互补的PWM输出初始化.
     * @param  	arr:计数总数;
    			psc:预分频;
    			ccr1_val:计数ccr1后翻转电平;
    			deadtime:死区时间.
     * @retval 	None.
     * @msg	   	PWM输出频率为72MHz/(arr+1)*(psc+1)此处设置PWM频率为100KHz;
    			死区时间设置为16*1/72us=222.2ns(0x10).
     */
    void TIM1_PWM1_DeadtimeInit(u16 arr,u16 psc,u16 CCRx_val,u16 deadtime)
    {
        GPIO_InitTypeDef GPIO_InitSturcture;
        TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
        TIM_OCInitTypeDef TIM_OCInitStructure;
        TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
    	
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
    
        GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_8 ;
        GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitSturcture);
    	
    	GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_13 ;
        GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitSturcture);
     
        TIM_TimeBaseInitStructure.TIM_Period = arr;      	  					/*设置在自动重装载周期值*/
        TIM_TimeBaseInitStructure.TIM_Prescaler = psc;	 	  					/*设置预分频值*/
        TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;			/*向上计数模式*/
        TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;				/*死区、输入滤波、输入采样不分频*/
        TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;    				/*重复计数器设置为0*/
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
    		
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;			 			/*PWM1输出方式*/
        TIM_OCInitStructure.TIM_Pulse = CCRx_val;								/*计数到CCRx_val电平反转,即设置CCRx的值*/
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;				/*输出通道电平极性配置,决定输出的有效电平*/
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;				/*互补输出通道电平极性配置,决定输出的有效电平*/
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 			/*输出使能*/
        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;			/*互补输出使能*/
        TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;			/*输出通道空闲电平极性配置(刹车后为空闲状态)*/
        TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;  			/*互补输出通道空闲电平极性配置*/
    	
        TIM_OC1Init(TIM1, &TIM_OCInitStructure);								/*根据TIM指定的参数初始化外设TIM1 OC1*/
    	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);						/*使能TIM1在CCR1上的预装载寄存器*/
    		
        /*刹车和死区设置*/
        TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; 			/*OSSR:运行模式下“关闭状态”选择*/
        TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; 			/*OSSI:空闲模式下“关闭状态”选择*/
        TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; 					/*锁定设置,级别为1*/
        TIM_BDTRInitStructure.TIM_DeadTime = deadtime;							/*死区延时时间*/
        TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;					/*禁止刹车输入*/
        TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; 		/*刹车输入极性*/
        TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; /*自动输出使能*/
        TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
    	
    	TIM_ARRPreloadConfig(TIM1,ENABLE);										/*设置ARR值(周期值)在当前周期即改变*/
    	
    	TIM_Cmd(TIM1, ENABLE);
        TIM_CtrlPWMOutputs(TIM1, ENABLE);										/*主输出使能,高级定时器专用*/							
    
    	TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE); 								/*TIM1 DMA请求使能,注:TIM1_Update请求对应DMA通道为DMA1_chanel5*/
    }
    
  • 此处是笔者所初始化的一段互补PWM波形输出,通过此段初始化代码可以简单的看到互补PWM输出只需要通过相应的设计即可完成,不需要额外配置其他硬件电路,将两个3.3v的互补PWM接到相应的硬件电路与即可完成。
  • 关于死区时间计算可以仔细研读一下stm32的手册
  • 死区时间设置

    物联沃分享整理
    物联沃-IOTWORD物联网 » 详解STM32互补PWM输出中带死区时间的设置方法

    发表评论