805721366 发表于 2022-7-31 09:18

[N32L43X评测] 7.PWM测试

本帖最后由 805721366 于 2022-7-31 09:18 编辑

<p><span style="font-family:宋体;">本篇介绍TIM3定时器输出PWM测试</span></p>

<p><span style="color:#f39c12;"><strong><span style="font-family:宋体;">硬件连接</span></strong></span></p>

<p><span style="font-family:宋体;">TIM3CH1&nbsp; &mdash;&mdash;&nbsp; PA6</span></p>

<p><span style="font-family:宋体;">TIM3CH2&nbsp; &mdash;&mdash;&nbsp; PA7</span></p>

<p class="imagemiddle" style="text-align: center;"></p>

<p><span style="color:#f39c12;"><strong><span style="font-family:宋体;">软件代码</span></strong></span></p>

<p><span style="font-family:宋体;">TIM3输出PWM频率设置为10kHz,CH1占空比50%,CH2占空比25%</span></p>

<pre>
<code>/**
* <a href="home.php?mod=space&amp;uid=159083" target="_blank">@brief</a> PWM初始化
*/
void hal_pwmInitial(void)
{
    GPIO_InitType GPIO_InitStructure;
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    OCInitType TIM_OCInitStructure;

    /* TIM3 clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
    /* GPIOA and GPIOB clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);

    GPIO_InitStruct(&amp;GPIO_InitStructure);
    /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
    GPIO_InitStructure.Pin      = GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM3;
    GPIO_InitPeripheral(GPIOA, &amp;GPIO_InitStructure);

    GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM3;
    GPIO_InitPeripheral(GPIOB, &amp;GPIO_InitStructure);
    /* -----------------------------------------------------------------------
    TIM3 Frequency = 54MHz/Prescaler*Period = 54M/54/100 = 10kHz(0.1ms)
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 25%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 75%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 10%
    ----------------------------------------------------------------------- */
    /* Time base configuration */
    TIM_TimeBaseStructure.Period    = 100-1; //1us*100 = 100us = 0.1ms 10kHz
    TIM_TimeBaseStructure.Prescaler = PrescalerValue1; //54000000/54 = 1MHz 1us
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_InitTimeBase(TIM3, &amp;TIM_TimeBaseStructure);
    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse       = CCR1_Value;
    TIM_OCInitStructure.OcPolarity= TIM_OC_POLARITY_HIGH;
    TIM_InitOc1(TIM3, &amp;TIM_OCInitStructure);
    TIM_ConfigOc1Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
    /* PWM1 Mode configuration: Channel2 */
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse       = CCR2_Value;
    TIM_InitOc2(TIM3, &amp;TIM_OCInitStructure);
    TIM_ConfigOc2Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
    /* PWM1 Mode configuration: Channel3 */
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse       = CCR3_Value;
    TIM_InitOc3(TIM3, &amp;TIM_OCInitStructure);
    TIM_ConfigOc3Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
    /* PWM1 Mode configuration: Channel4 */
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse       = CCR4_Value;
    TIM_InitOc4(TIM3, &amp;TIM_OCInitStructure);
    TIM_ConfigOc4Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);

    TIM_ConfigArPreload(TIM3, ENABLE);

    /* TIM3 enable counter */
    TIM_Enable(TIM3, ENABLE);

}</code></pre>

<p><span style="color:#f39c12;"><span style="font-family:宋体;"><strong>运行测试</strong></span></span></p>

<p></p>

<p></p>

<p><span style="font-family:宋体;"><span style="color:#f39c12;"><strong>测试代码</strong></span></span></p>

<p></p>

<p>&nbsp;</p>

Jacktang 发表于 2022-7-31 23:24

<p>方波效果很好,说明PWM测试代码是正确的吧</p>

lugl4313820 发表于 2022-8-14 22:06

两个方波形状非常好,如果频率加高,会不会也一样?

805721366 发表于 2022-8-14 23:18

lugl4313820 发表于 2022-8-14 22:06
两个方波形状非常好,如果频率加高,会不会也一样?

<p>测试过,频率加快,输出波形也很好</p>
页: [1]
查看完整版本: [N32L43X评测] 7.PWM测试