CN / EN
CN / EN

提问

设计支持

您的项目私密技术问题如需获得一对一支持,请点击下方联系我们。

spwm动态修改占空比 GR551x

今天加班吗

2025-10-29 15:37

采用GR551x.SDK-v2.1.0\projects\peripheral\pwm\app_pwm\Keil_5例程,无法动态修改占空比,通道B代码输出为10Khz,50%占空比方波,用GR551x_SDK_V1.7.0例程可动态修改占空比,请问原因是在哪里

#define APP_PWM0_MODULE                 APP_PWM_ID_1//APP_PWM_ID_0
#define APP_PWM0_GPIO_MUX               APP_IO_MUX_5
#define APP_PWM0_CHANNEL_A              APP_IO_PIN_7//APP_IO_PIN_2
#define APP_PWM0_CHANNEL_B              APP_IO_PIN_8//APP_IO_PIN_3
#define APP_PWM0_CHANNEL_C              APP_IO_PIN_9//APP_IO_PIN_4
#define APP_PWM0_GPIO_TYPE              APP_IO_TYPE_NORMAL
app_pwm_params_t pwm_params = {
    .id = APP_PWM0_MODULE,
    .pin_cfg = {
        .channel_a = {
            .type   = APP_PWM0_GPIO_TYPE,
            .mux    = APP_PWM0_GPIO_MUX,
            .pin    = APP_PWM0_CHANNEL_A,
            .pull   = APP_IO_NOPULL,
            .enable = APP_PWM_PIN_ENABLE,
        },
        .channel_b = {
            .type   = APP_PWM0_GPIO_TYPE,
            .mux    = APP_PWM0_GPIO_MUX,
            .pin    = APP_PWM0_CHANNEL_B,
            .pull   = APP_IO_NOPULL,
            .enable = APP_PWM_PIN_ENABLE,
        },
        .channel_c = {
            .type   = APP_PWM0_GPIO_TYPE,
            .mux    = APP_PWM0_GPIO_MUX,
            .pin    = APP_PWM0_CHANNEL_C,
            .pull   = APP_IO_NOPULL,
            .enable = APP_PWM_PIN_ENABLE,
        },
    },
    .active_channel = APP_PWM_ACTIVE_CHANNEL_ALL,
    .init = {
        .mode     = PWM_MODE_FLICKER,
        .align    = PWM_ALIGNED_EDGE,
        .freq     = 10000,
        .bperiod  = 500,
        .hperiod  = 200,
        .channel_a = {
            .duty = 30,
            .drive_polarity = PWM_DRIVEPOLARITY_POSITIVE,
        },
        .channel_b = {
            .duty = 50,
            .drive_polarity = PWM_DRIVEPOLARITY_NEGATIVE,
        },
        .channel_c = {
            .duty = 80,
            .drive_polarity = PWM_DRIVEPOLARITY_POSITIVE,
        },
    },
};


void app_pwm_test(void)
{
    uint16_t ret = APP_DRV_SUCCESS;
    app_pwm_channel_init_t channel_cfg = {0};

    ret = app_pwm_init(&pwm_params);

    if (ret != APP_DRV_SUCCESS)
    {
        printf("\r\nPWM initial failed! Please check the input paraments.\r\n");
    }

    app_pwm_start(APP_PWM0_MODULE);
		
		while(1)
		{
    channel_cfg.duty = 20;
    channel_cfg.drive_polarity = PWM_DRIVEPOLARITY_POSITIVE;	
		app_pwm_config_channel(APP_PWM0_MODULE, APP_PWM_ACTIVE_CHANNEL_ALL, &channel_cfg);
		delay_ms(2000);
		channel_cfg.duty = 80;
    channel_cfg.drive_polarity = PWM_DRIVEPOLARITY_POSITIVE;	
		app_pwm_config_channel(APP_PWM0_MODULE, APP_PWM_ACTIVE_CHANNEL_ALL, &channel_cfg);
		delay_ms(2000);
		
		}
//    delay_ms(2000);

//    app_pwm_update_freq(APP_PWM0_MODULE, 1000);

//    channel_cfg.duty = 20;
//    channel_cfg.drive_polarity = PWM_DRIVEPOLARITY_POSITIVE;
//#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
//    channel_cfg.fstoplvl = PWM_STOP_LVL_LOW;
//#endif
//    app_pwm_config_channel(APP_PWM0_MODULE, APP_PWM_ACTIVE_CHANNEL_B, &channel_cfg);
//#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
//    app_pwm_inactive_channel(APP_PWM0_MODULE, APP_PWM_ACTIVE_CHANNEL_A);
//#endif

//    delay_ms(2000);

//    app_pwm_stop(APP_PWM0_MODULE);

//    delay_ms(2000);

//    channel_cfg.duty = 60;
//    channel_cfg.drive_polarity = PWM_DRIVEPOLARITY_POSITIVE;
//#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
//    channel_cfg.fstoplvl = PWM_STOP_LVL_HIGH;
//#endif
//    app_pwm_config_channel(APP_PWM0_MODULE, APP_PWM_ACTIVE_CHANNEL_ALL, &channel_cfg);

//    app_pwm_start(APP_PWM0_MODULE);

//    delay_ms(2000);

//    app_pwm_stop(APP_PWM0_MODULE);
//    app_pwm_deinit(APP_PWM0_MODULE);
}


int main(void)
{
    board_init();

    printf("\r\n");
    printf("***********************************************************\r\n");
    printf("*                  PWM_Output example.                    *\r\n");
    printf("*                                                         *\r\n");
    printf("*                     PWM_a(MSIOA*)                       *\r\n");
    printf("*                     PWM_b(MSIOA*)                       *\r\n");
    printf("*                     PWM_c(MSIOA*)                       *\r\n");
    printf("*                                                         *\r\n");
    printf("* This sample code will show PWM output.                  *\r\n");
    printf("* You can use logic analyzer to get PWM_a/b/c wave        *\r\n");
    printf("* on MSIOA*/MSIOA*/MSIOA*                                 *\r\n");
    printf("***********************************************************\r\n");

    app_pwm_test();

#if (APP_DRIVER_CHIP_TYPE == APP_DRIVER_GR5332X)
    app_pwm_coding();
    app_pwm_coding_dma();
#endif

    printf("\r\nThis example demo end.\r\n");

    while(1);
}


0收藏

0赞成

登录后可回答问题,请 注册

我们时刻倾听您的声音
联系销售

扫描关注公众号

打开微信,使用“扫一扫”即可关注