6626 PWM输出6通道示例

wen sir · 53次点击 · 2周前

本例中,PWM模块输入时钟频率为 1000000Hz, PWM周期为256。

因此,PWM的频率为 1000000/256= 3906Hz=3.906K

占空比分别为:

[0xFC8040]

0xFC/256=98.4%  

0x80/256 = 50%

0x40/256 = 25%

0x3060b0

0x30/256 = 18.75% 

0x60/256 = 37.5%

0xb0/256 = 68.75%


void PWM_6ch_test(void)
{
    uint32_t color[2] = {0xFC8040, 0x3060b0}; //RGB
    pin_config_t pin_cfg[] =
    {
        {5,  {PINMUX_PAD5_TIMER0_IO2_CFG}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL},     //Timer0
        {6,  {PINMUX_PAD6_TIMER0_IO3_CFG }, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL},    //Timer0
        //
        {12, {PINMUX_PAD12_TIMER1_OUT1_CFG}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL},   //Timer1
        //
        {21, {PINMUX_PAD21_TIMER2_OUT2_CFG}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL},   //Timer2
        {22, {PINMUX_PAD22_TIMER2_OUT3_CFG}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL},   //Timer2
        {11, {PINMUX_PAD11_TIMER2_OUT4_CFG}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL},   //Timer2
    };
    //
    drv_pin_init(pin_cfg, sizeof(pin_cfg) / sizeof(pin_cfg[0]));
    tim_pwm_complementary_output_config_t pwm_config =
    {
        .cnt_freq = 1000000,            // PWM 模块输入时钟 1Mhz
        .chan = {
            {1, {TIM_PWM_POL_ACTIVE_HIGH, 0}},
            {1, {TIM_PWM_POL_ACTIVE_HIGH, 0}},
            {1, {TIM_PWM_POL_ACTIVE_HIGH, 0}},
            {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}
        },
        .dma_cfg = {
            0, TIM_CHAN_ALL, NULL
        },
    };
    pwm_config.period_cnt                   = 256; //频率=cnt_freq/period_cnt=1000000/256=3906
    //定时器0
    //通道2
    pwm_config.chan[TIM_CHAN_2].cfg.complementary_output_enable = false;//互补输出禁用
    pwm_config.chan[TIM_CHAN_2].en          = 1;
    pwm_config.chan[TIM_CHAN_2].cfg.oc_val  = (color[0] >> 16) & 0xff; //示例,高8位
    //通道3
    pwm_config.chan[TIM_CHAN_3].cfg.complementary_output_enable = false;
    pwm_config.chan[TIM_CHAN_3].en          = 1;
    pwm_config.chan[TIM_CHAN_3].cfg.oc_val  = (color[0] >> 8) & 0xff;
    //
    pwm_config.chan[TIM_CHAN_1].en          = 0;//不使用
    pwm_config.chan[TIM_CHAN_4].en          = 0;//不使用
    drv_tim_init(OM_TIM0);
    drv_tim_pwm_complementary_output_start(OM_TIM0, &pwm_config);
    //定时器1
    //通道1
    pwm_config.chan[TIM_CHAN_1].cfg.complementary_output_enable = false;
    pwm_config.chan[TIM_CHAN_1].en          = 1;
    pwm_config.chan[TIM_CHAN_1].cfg.oc_val  = (color[0] >> 0) & 0xff;
    //
    pwm_config.chan[TIM_CHAN_2].en          = 0;//不使用
    pwm_config.chan[TIM_CHAN_3].en          = 0;//不使用
    pwm_config.chan[TIM_CHAN_4].en          = 0;//不使用
    drv_tim_init(OM_TIM1);
    drv_tim_pwm_complementary_output_start(OM_TIM1, &pwm_config);
    //定时器2
    //通道2
    pwm_config.chan[TIM_CHAN_2].cfg.complementary_output_enable = false;
    pwm_config.chan[TIM_CHAN_2].en          = 1;
    pwm_config.chan[TIM_CHAN_2].cfg.oc_val  = (color[1] >> 16) & 0xff;
    //通道3
    pwm_config.chan[TIM_CHAN_3].cfg.complementary_output_enable = false;
    pwm_config.chan[TIM_CHAN_3].en          = 1;
    pwm_config.chan[TIM_CHAN_3].cfg.oc_val  = (color[1] >> 8) & 0xff;
    //通道4
    pwm_config.chan[TIM_CHAN_4].cfg.complementary_output_enable = false;
    pwm_config.chan[TIM_CHAN_4].en          = 1;
    pwm_config.chan[TIM_CHAN_4].cfg.oc_val  = (color[1] >> 0) & 0xff;
    //
    pwm_config.chan[TIM_CHAN_1].en          = 0;//不使用
    drv_tim_init(OM_TIM2);
    drv_tim_pwm_complementary_output_start(OM_TIM2, &pwm_config);
    while (1);
}


int main(void)
{
    drv_pmu_pin_io13_reset_mode_enable(1);
    #if CPU_CLK_Select_64MHz
    drv_pmu_xtal32m_x2_startup();
    #endif
    …………
    //
    drv_wdt_init(0);
    board_init();
    extern void PWM_6ch_test(void);

    PWM_6ch_test();  //测试

    …………



image.png

被收藏 0  ∙  0 赞  
加入收藏
点赞
0 回复  
善言善语 (您需要 登录 后才能回复 没有账号 ?)

请先登录网站