freeelectron 发表于 2022-7-21 09:21

【N32L43x评测】9、互补PWM、死区测试

<p><span style="font-size:24px;">1、前言</span></p>

<p><span style="font-size:18px;">N32L43X支持两个高级定时器,TIM1和TIM8,高级定时器支持输入捕获、输出比较、PWM、死区、刹车等功能,本文主要测试互补pwm和死区。</span></p>

<p>&nbsp;</p>

<p><span style="font-size:24px;">2、TIM1的pwm通道与互补通道</span></p>

<p><span style="font-size:16px;">&nbsp;本文使用PA8和PA7,其中PA8为TIM1的CH1通道,PA7位互补通道。</span></p>

<p>&nbsp;</p>

<p><span style="font-size:24px;">3、死区时间计算</span></p>

<p><strong>&nbsp;可以看出,死区时间的计算是根据上面的公式的,上面分四种情况来计算死区时间,本文中系统时钟频率为108M,那么可以计算出<span style="font-size:24px;">T</span>DTS=1/108000000=9.26ns;</strong></p>

<p><strong>情况1:DTGN=0xx,那么DTGN的取值范围为0-127,那么DT=(0~127)*9.26ns=(0ns~1176ns);</strong></p>

<p><strong>情况2:DTGN=10x,那么DTGN的取值范围为0-63,那么DT=(64+DTGN)*2*9.26ns=(64+(0~63))*2*9.26ns=(1185ns~2352ns);</strong></p>

<p><strong>情况3:DTGN=110,那么DTGN的取值范围为0-31,那么DT=(32+DTGN)*8*2.96ns=(32+(0-31))*8*9.26ns=(2370~4667);</strong></p>

<p><strong>情况4:DTGN=111,那么DTGN的取值范围为0-31,那么DT=(32+DTGN)*16*2.96ns=(32+(0-31))*16*9.26ns=(4741~9334);</strong></p>

<p><strong>从上面计算可以看出,在<span style="font-size:24px;">T</span>DTS=9.26ns的时候,最大的死区时间为9334ns=9.3us,</strong></p>

<p>&nbsp;</p>

<p><strong>本文通过设置死区时间为9.3us测试,可以反推出DTGN的值为255,即就是0xff。</strong></p>

<p>&nbsp;</p>

<p><span style="font-size:24px;">4、代码实现</span></p>

<pre>
<code class="language-cs">void PwmInit(void)
{
        //CH1:PA8   CH1N:PA7
    /* GPIOA and GPIOB clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA| RCC_APB2_PERIPH_AFIO, ENABLE);
       
        /* TIM1 clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
       
        GPIO_InitType GPIO_InitStructure;

    GPIO_InitStruct(&amp;GPIO_InitStructure);
    /* GPIOA Configuration:TIM1 Channel1 as alternate function push-pull */
    GPIO_InitStructure.Pin      = GPIO_PIN_7 ;
    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF5_TIM1;
    GPIO_InitPeripheral(GPIOA, &amp;GPIO_InitStructure);
       
        GPIO_InitStructure.Pin      =   GPIO_PIN_8;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM1;
    GPIO_InitPeripheral(GPIOA, &amp;GPIO_InitStructure);
       
    //108M
        TIM_TimeBaseInitType TIM1_TimeBaseStructure;

        TIM1_TimeBaseStructure.Period    = 99; //999
    TIM1_TimeBaseStructure.Prescaler = 107;
    TIM1_TimeBaseStructure.ClkDiv    = TIM_CLK_DIV1;
    TIM1_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM1, &amp;TIM1_TimeBaseStructure);
        TIM_Enable(TIM1, ENABLE);

        OCInitType TIM_OCInitStructure;

        /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM2;
        TIM_OCInitStructure.Pulse       = 49;//499

    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
        TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_RESET;
    TIM_OCInitStructure.OcPolarity= TIM_OC_POLARITY_HIGH;
       
        TIM_OCInitStructure.OutputNState= TIM_OUTPUT_NSTATE_ENABLE;
        TIM_OCInitStructure.OcNIdleState= TIM_OCN_IDLE_STATE_SET;
        TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_HIGH;

    TIM_InitOc1(TIM1, &amp;TIM_OCInitStructure);

    TIM_ConfigOc1Preload(TIM1, TIM_OC_PRE_LOAD_ENABLE);
        TIM_EnableCtrlPwmOutputs(TIM1, ENABLE);


        TIM_BDTRInitType TIM_BDTRInitStruct;
       
        TIM_BDTRInitStruct.OssrState=TIM_OSSR_STATE_ENABLE;
        TIM_BDTRInitStruct.OssiState=TIM_OSSI_STATE_ENABLE;
        TIM_BDTRInitStruct.LockLevel=TIM_LOCK_LEVEL_OFF;
        TIM_BDTRInitStruct.DeadTime =0xff; //9us
        TIM_BDTRInitStruct.Break=TIM_BREAK_IN_DISABLE;
        TIM_BDTRInitStruct.BreakPolarity=TIM_BREAK_POLARITY_HIGH;
        TIM_BDTRInitStruct.AutomaticOutput=TIM_AUTO_OUTPUT_ENABLE;
        TIM_ConfigBkdt(TIM1, &amp;TIM_BDTRInitStruct);
       
}</code></pre>

<p>&nbsp;</p>

<p><span style="font-size:24px;">5、示波器观察现象</span></p>

<p><br />
</p>

<p>可以看出PWM互补输出、死区时间与程序中设置的一样。</p>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【N32L43x评测】9、互补PWM、死区测试