1595 views|3 replies

20

Posts

0

Resources
The OP
 

[N32L43X Review] 7.PWM Test [Copy link]

 This post was last edited by 805721366 on 2022-7-31 09:18

This article introduces the TIM3 timer output PWM test

Hardware Hookup

TIM3CH1 —— PA6

TIM3CH2 —— PA7

Software Code

The TIM3 output PWM frequency is set to 10kHz, CH1 duty cycle is 50%, and CH2 duty cycle is 25%.

/**
 * @brief 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(&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, &GPIO_InitStructure);

    GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM3;
    GPIO_InitPeripheral(GPIOB, &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, &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, &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, &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, &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, &TIM_OCInitStructure);
    TIM_ConfigOc4Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);

    TIM_ConfigArPreload(TIM3, ENABLE);

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

}

Running Tests

Test code

N32L43x_PWM.rar (480.45 KB, downloads: 1)

This post is from Domestic Chip Exchange

Latest reply

The shapes of the two square waves are very good. Will it be the same if the frequency is increased?  Details Published on 2022-8-14 22:06
 
 

6587

Posts

0

Resources
2
 

The square wave effect is very good, indicating that the PWM test code is correct.

This post is from Domestic Chip Exchange
 
 
 

6841

Posts

11

Resources
3
 
The shapes of the two square waves are very good. Will it be the same if the frequency is increased?
This post is from Domestic Chip Exchange

Comments

Tested, the frequency is faster and the output waveform is also very good  Details Published on 2022-8-14 23:18
 
 
 

20

Posts

0

Resources
4
 
lugl4313820 posted on 2022-8-14 22:06 The two square waves have very good shapes. If the frequency is increased, will it be the same?

After testing, the frequency is increased and the output waveform is also very good

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list