MCPWM

Overview

以 AC635N 为例,输出可配比的 PWM,可用于电机控制,也可以做普通 PWM。提供 MCPWM 的应用示例、工程配置、API 介绍和常见问题。

应用示例

  • MCPWM 的具体源代码详见 sdk/bsp/AC635N/src/mcpwm.c

  • mcpwm.c 中参考示例如下:

    /**********mcpwm参考示例**********/
    void mcpwm_test(void)
    {
    #define PWM_CH3_ENABLE          1
    #define PWM_CH4_ENABLE          1
    #define PWM_CH5_ENABLE          1
    #define PWM_CH0_ENABLE          1
    #define PWM_CH1_ENABLE          1
    #define PWM_CH2_ENABLE          1
    
        struct pwm_platform_data pwm_p_data;
    
    #if PWM_CH3_ENABLE
        memset((u8 *)&pwm_p_data, 0, sizeof(struct pwm_platform_data));
        pwm_p_data.pwm_ch_num = pwm_ch3;                        //通道0
        pwm_p_data.frequency = 1000;                                //1KHz
        pwm_p_data.duty = 5000;                                 //占空比50%
        pwm_p_data.h_pin = -1;                                              //不用则填 -1
        pwm_p_data.l_pin = IO_PORTB_10;                         //硬件引脚
        mcpwm_init(&pwm_p_data);
    #endif
    
    #if PWM_CH4_ENABLE
        memset((u8 *)&pwm_p_data, 0, sizeof(struct pwm_platform_data));
        pwm_p_data.pwm_ch_num = pwm_ch4;                        //通道0
        pwm_p_data.frequency = 10000;                               //10KHz
        pwm_p_data.duty = 5000;                                 //占空比50%
        pwm_p_data.h_pin = IO_PORTA_09;                         //硬件引脚
        pwm_p_data.l_pin = IO_PORTA_10;                         //硬件引脚
        pwm_p_data.complementary_en = 1;                        //两个引脚的波形, 1: 互补, 0: 同步;
        mcpwm_init(&pwm_p_data);
    #endif
    
    #if PWM_CH5_ENABLE
        memset((u8 *)&pwm_p_data, 0, sizeof(struct pwm_platform_data));
        pwm_p_data.pwm_ch_num = pwm_ch5;                        //通道0
        pwm_p_data.frequency = 2000;                                //2KHz
        pwm_p_data.duty = 5000;                                 //占空比50%
        pwm_p_data.h_pin = IO_PORTC_04;                         //硬件引脚
        pwm_p_data.l_pin = -1;                                              //不用则填 -1
    
        mcpwm_init(&pwm_p_data);
    #endif
    
    #if PWM_CH0_ENABLE
        memset((u8 *)&pwm_p_data, 0, sizeof(struct pwm_platform_data));
        pwm_p_data.pwm_ch_num = pwm_ch0;                        //通道0
        pwm_p_data.frequency = 1000;                                //1KHz
        pwm_p_data.duty = 5000;                                 //占空比50%
        pwm_p_data.h_pin = IO_PORTA_03;                         //非硬件引脚,即任意IO
        pwm_p_data.h_pin_output_ch_num = 1;                                         //当h_pin为非硬件IO时,分频outputchannel号: 0/1/2
        pwm_p_data.l_pin = -1;                                              //不用则填 -1
        mcpwm_init(&pwm_p_data);
    #endif
    
    #if PWM_CH1_ENABLE
        memset((u8 *)&pwm_p_data, 0, sizeof(struct pwm_platform_data));
        pwm_p_data.pwm_ch_num = pwm_ch1;                        //通道0
        pwm_p_data.frequency = 10000;                               //10KHz
        pwm_p_data.duty = 5000;                                 //占空比50%
        pwm_p_data.h_pin = IO_PORTB_00;                         //硬件引脚
        pwm_p_data.l_pin = IO_PORTB_03;                         //非硬件引脚,即任意IO
        pwm_p_data.l_pin_output_ch_num = 2;                                         //当l_pin为非硬件IO时,分频outputchannel号: 0/1/2
        pwm_p_data.complementary_en = 1;                        //两个引脚的波形, 1: 互补, 0: 同步;
        mcpwm_init(&pwm_p_data);
    #endif
    
    #if PWM_CH2_ENABLE
        memset((u8 *)&pwm_p_data, 0, sizeof(struct pwm_platform_data));
        pwm_p_data.pwm_ch_num = pwm_ch2;                        //通道0
        pwm_p_data.frequency = 2000;                                //2KHz
        pwm_p_data.duty = 5000;                                 //占空比50%
        pwm_p_data.h_pin = IO_PORTB_04;                         //硬件引脚
        pwm_p_data.l_pin = -1;                                              //不用则填 -1
    
        mcpwm_init(&pwm_p_data);
    #endif
    
    #if 0
        extern void wdt_clear();
        while (1) {
            wdt_clear();
        }
    #endif
    }
    

工程配置

  • sdk/apps/main.c 中函数 user_main() 添加如下工程代码:

    int user_main()
    {
        extern void mcpwm_test(void);
        mcpwm_test();
    }
    
  • 编译下载后,用示波器连接测试IO,观察是否符合如下期望输出:

    PB10

    1KHz50%

    PA9&PA10

    10KHz%50互补

    PC4

    2KHz50%

    PA3

    1KHz50%

    PB0&PB3

    10KHz%50互补

    PB4

    2KHz50%

API参考

MCPWM 常用相关 API 介绍,具体软件代码见 sdk/bsp/AC635N/src/mcpwm.c

Functions

void mcpwm_set_frequency(pwm_ch_num_type ch, u32 frequency)

更改MCPWM的频率

Parameters:
  • ch – 通道号

  • frequency – 频率

void mcpwm_set_duty(pwm_ch_num_type pwm_ch, u16 duty)

设置一个通道的占空比

Parameters:
  • pwm_ch – 通道号:pwm_ch0,pwm_ch1,pwm_ch2

  • duty – 占空比:0 ~ 10000 对应 0% ~ 100%

void mctimer_ch_open_or_close(pwm_ch_num_type pwm_ch, u8 enable)

打开或者关闭一个时基

Parameters:
  • pwm_ch – 通道号:pwm_ch0,pwm_ch1,pwm_ch2

  • enable – 1:打开 0:关闭

void mcpwm_ch_open_or_close(pwm_ch_num_type pwm_ch, u8 enable)

打开或者关闭一个通道

Parameters:
  • pwm_ch – 通道号:pwm_ch0,pwm_ch1,pwm_ch2

  • enable – 1:打开 0:关闭

void mcpwm_open(pwm_ch_num_type pwm_ch)

关闭MCPWM模块

Parameters:

pwm_ch – 通道号:pwm_ch0,pwm_ch1,pwm_ch2

void mcpwm_close(pwm_ch_num_type pwm_ch)

关闭MCPWM模块

Parameters:

pwm_ch – 通道号:pwm_ch0,pwm_ch1,pwm_ch2

void mcpwm_init(struct pwm_platform_data *arg)

pwm初始化函数

Parameters:

arg – 初始化的参数结构体地址 : struct pwm_platform_data *

void mcpwm_test(void)

pwm参考示例函数