STM32F103VCT6 高级定时器的PWM输出

要求得到下列波形,死区时间1us,CH1和CH1之间的相位差事3us,频率50HZ。
STM32F103VCT6 高级定时器的PWM输出_第1张图片


1,To get TIM1 counter clock at 72MHz,the prescaler is computer as follows:
          Prescaler = (TIM1CLK / TIM1 counter clock) - 1
     To objective is to genterate PWM signal at 50KHz:
          -TIM1_Priod = (SystemCoreClock / 50000) - 1
     To get TIM1 output clock at 50KHz , the period (ARR) is computed as follows :
          ARR = (TIM1 counter clock / TIM1 output clock) - 1
     The Two Duty cycles are computed as the following description :  
            TIM1 Channel1 duty cycle = (TIM1_CCR1 / TIM1_ARR)*100 = 50%
            TIM1 Channel2 duty cycle = (TIM1_CCR1 / TIM1_ARR)*100 = 50% 
     The Timer pulse is calculated as follows : 
            TIM1_CCRx = (DutyCycle * TIM1_ARR) / 100
2,死区时间deadtime的计算方法
      UTG[7:0]:死区发生器装置,这些位定义了插入互补输出之间的死区持续时间,假设DT标示其持续时 间:
      DTG[7:0] = 0xx      =>      DT = DTG[7:0]× Tdtg , Tdtg = TDTS ;
      DTG[7:0] = 10x      =>      DT = (64 +  DTG[5:0])× Tdtg , Tdtg = 2×TDTS ; 
      DTG[7:0] = 110      =>      DT = (32 +  DTG[4:0])× Tdtg , Tdtg = 8×TDTS ;
      DTG[7:0] = 111      =>      DT = (32 +  DTG[4:0])× Tdtg , Tdtg = 16×TDTS ;      例子:若TDTS =  13.89ns(72MHz),可能的死区时间位:
      0到1764ns,若步长13.89ns;
      1777.9ns到3528.88ns;
      3555.84ns到7000.56ns;
      7111.68ns到14001.12ns;
如果 deadtime=1us,落在区间0到1764ns,所以公式选择& amp;nbsp;DTG[7:0] = 0xx      =>      DT = DTG[7:0]× Tdtg , Tdtg = TDTS ;
     1us = x 13.89ns,x=72,DTG[7:0]=0x72。假设是用第二个公式得到的30则DTG[5:0]=0b11110,加上DTG的前两位就是 DTG[7:          0]=0b1011110=0x5E;
3,Configure the break feature,active at High level,and using the automatic output enable feature
4,Use the Locking parameters level1
5,要求这两个通道的波形不对齐,那么TIM1_CCMR1的OC1M[2:0]必须设定为011,即 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;这样ARR走两趟才是一个周期。
6,要求PWM输出频率为50KHz。所以ARR=(72MHz/100000)-1=719
      因为ARR加1的时间为(1/72MHz)s,则(1/72MHz)X=3us。X=216即CCR1、 CCR2之间相隔216时,PWM的相位差是3us。

      程序如下:< /div>void TIM1_PWM_DeadtimeInit(uint16_t arr,uint16_t psc,uint16_t ccr1,uint16_t ccr2,uint16_t deadtime)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

    /*************************************************
    *采用寄存器模式  
    *TIM1的互补PWM,没有使用重映像,CH1-PA8,CH1-PB13< /div>    GPIOA->CRH&=0XFFFFFFF0;   //PA8输出
    GPIOA->CRH|=0X0000000B;   //复用功能输出 推挽式    
    GPIOA->ODR|=3<<8;
    
    GPIOB->CRH&=0XFF0FFFFF;   //PB13输出
    GPIOB->CRH|=0X00B00000;   //复用功能输出 推挽式    
    GPIOB->ODR|=3<<13;
    ***************************************************/
    /**************************************************/
    /*TIM1的互补PWM,没有使用重映像,CH1-PA8,CH1-PB13*/< /div>    GPIO_InitStructure.GPIO_Pin=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);
    
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 | GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    /*************************************************/
    /*************************************************
    *TIM1的互补PWM,使用完全重映射,CH1-PE9,CH1N-PE8*< /div>    GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);//使用 AFIO_MARP的位7:6=11,完全重映像TIM1_CH1到PE9,TIM1_CH1N到PE8
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOE,&GPIO_InitStructure);
    *************************************************/
    /**问题是当使用不完全重映射的互补PWM出现问题****< /div>    GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,ENABLE);//使用 AFIO_MARP的位7:6=01,部分重映像TIM1_CH1到PA8,TIM1_CH1N到PA7
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);    
    ************************************************/
    //TIM1
    TIM_TimeBaseStructure.TIM_Period=arr;
    TIM_TimeBaseStructure.TIM_Prescaler=psc;
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_ClockDivision=0;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
    
    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;//翻转,当 TIMx_CCR1=TIMx_CNT时,翻转OC1REF的电平
    TIM_OCInitStructure.TIM_Pulse=ccr1;
    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //CCER的CC1P,输入/捕获输出极性
    TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High;
    
    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //CER的CC1N,输入/捕获输出使能
    TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //OIS1位
    TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset; //CR2的OIS1N位,当MOE位0时,输出空闲状态位OIS1N.
    TIM_OC1Init(TIM1,&TIM_OCInitStructure);
     
    TIM_OCInitStructure.TIM_Pulse=ccr2;//316
    TIM_OC2Init(TIM1,&TIM_OCInitStructure);
    
    TIM_BDTRInitStructure.TIM_OSSRState=TIM_OSSRState_Disable; //OSSR:运行模式下“关闭状态”选择(Off-state selection for Run mode)
    TIM_BDTRInitStructure.TIM_OSSIState=TIM_OSSIState_Disable; //OSSI:空闲模式下“关闭状态”选择 (Off-state selection for Idle mode)
    TIM_BDTRInitStructure.TIM_LOCKLevel=TIM_LOCKLevel_1;//锁定设 置,级别为1,
    TIM_BDTRInitStructure.TIM_DeadTime=deadtime;//死区延时时间< /div>    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_CtrlPWMOutputs(TIM1,ENABLE);//BDTR的MOE位< /div>    
    TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);//CCMR1
    TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);//CCMR1
    TIM_ARRPreloadConfig(TIM1,ENABLE);//CR1
    TIM_Cmd(TIM1,ENABLE);//CR1
}< /div>

函数调用
void main(void)
{
    ......
    TIM1_PWM_DeadtimeInit(719,0,100,316,72)//uint16_t arr,uint16_t psc,uint16_t ccr1,uint16_t ccr2,uint16_t deadtime

      

你可能感兴趣的:(STM32F103VCT6 高级定时器的PWM输出)