rainchen0399 发表于 2023-7-6 18:08

定时器重复计数模式产生PWM,定时2ms触发,PWM波形飘是

本帖最后由 rainchen0399 于 2023-7-6 18:08 编辑

定时器15重复计数模式产生PWM,定时器14定时0.2ms,10次中断触发一次定时器15重复计数,但是出来的PWM波形飘,不在同一个点触发的;第一次玩AT32,各位指点一下啊;

定时器15初始化:
void Timer15_In_Init(uint32_t Freq,uint8_t Duty_Cnt,uint16_t Pluse_Num)
{
uint16_t timer_arr;//自动装载值;
uint16_t timer_psc;//预分频值;
tmr_output_config_type tmr_output_struct;
crm_clocks_freq_type crm_clocks_freq_struct;
/* get system clock */
crm_clocks_freq_get(&crm_clocks_freq_struct);
//tmr_reset(TMR15);
/* enable tmr1/gpioa/gpiob clock */
crm_periph_clock_enable(CRM_TMR15_PERIPH_CLOCK, TRUE);
/* tmr15 configuration generate pwm signal*/
/* compute the value to be set in arr regiter to generate signal frequency at Freq hz */
timer_arr = (crm_clocks_freq_struct.sclk_freq / Freq ) - 1;

/* compute c1dt value to generate a duty cycle at Duty_Cnt for channel 1 and 1c */
timer_psc = (uint16_t)(((uint32_t) (100-Duty_Cnt )* (timer_arr - 1)) / 100);
/* tmr base configuration */
      tmr_repetition_counter_set(TMR15, Pluse_Num-1);
tmr_base_init(TMR15, timer_arr, 0);
tmr_cnt_dir_set(TMR15, TMR_COUNT_UP);
tmr_clock_source_div_set(TMR15, TMR_CLOCK_DIV1);

/* channel1 configuration in output mode */
tmr_output_default_para_init(&tmr_output_struct);
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
tmr_output_struct.oc_output_state = TRUE;
tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
tmr_output_struct.oc_idle_state = TRUE;
tmr_output_channel_config(TMR15, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
tmr_channel_value_set(TMR15, TMR_SELECT_CHANNEL_1, timer_psc);

/* one cycle mode selection */
tmr_one_cycle_mode_enable(TMR15, TRUE);

      /* ADVTMR output enable */
tmr_output_enable(TMR15, TRUE);

tmr_interrupt_enable(TMR15, TMR_OVF_INT, TRUE);

/* tmr15 overflow interrupt nvic init */
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(TMR15_GLOBAL_IRQn, 0, 0);
}



定时器15溢出中断:只用来产生一个全局标志位;
void TMR15_GLOBAL_IRQHandler(void)
{
if(tmr_flag_get(TMR15, TMR_OVF_FLAG) != RESET)
{
    tmr_flag_clear(TMR15, TMR_OVF_FLAG);
    Start_Flag = 1;
    ISR_2ms_Cnt = 0;
//    tmr_counter_enable(TMR15, FALSE);
    LED_RED_OFF();
}
}


定时器14溢出中断,0.2ms定时,10次中断(2ms)触发一次定时器15产生PWM:
void TMR14_GLOBAL_IRQHandler(void)
{
if(tmr_flag_get(TMR14, TMR_OVF_FLAG) != RESET)
{
    tmr_flag_clear(TMR14, TMR_OVF_FLAG);
    if(Start_Flag == 1)
    {
      if(ISR_2ms_Cnt >= 10)
      {
      Start_Flag = 0;
      ISR_2ms_Cnt = 0;
      tmr_counter_enable(TMR15, TRUE);
      LED_RED_ON();
      }
      else
      {
      ISR_2ms_Cnt++;
      }
    }
}
}


产生的PWM波形,频率,占空比,脉冲个数都对,但是加了每隔2ms触发PWM,波形就是飘的,在示波器上波形就是不停移动的,不是在同一个点触发的;这个是什么问题,有啥寄存器没清?

rainchen0399 发表于 2023-7-6 18:19

中断里面有LED_RED_ON()和LED_RED_OFF();,这个指示灯的触发波形是稳定的;

xch 发表于 2023-7-7 12:47

用DMA

tpgf 发表于 2023-8-3 16:16

请问pwm波形发飘是什么意思能详细说一下吗

qcliu 发表于 2023-8-3 16:47

重新设置一下示波器 使用触发模式

drer 发表于 2023-8-3 17:14

pwm的占空比和频率有变化吗

coshi 发表于 2023-8-3 17:32

这个飘不飘的跟用不用dma完全没有关系

muyichuan2012 发表于 2023-8-3 19:12

两个定时器之间最好使用定时器同步功能,无需中断介入。

kxsi 发表于 2023-8-4 10:17

是否可以观察一下 每次漂移都是相差2毫秒呢

wiba 发表于 2023-8-4 10:49

可以设定触发的基准 这样就不会产生飘逸了
页: [1]
查看完整版本: 定时器重复计数模式产生PWM,定时2ms触发,PWM波形飘是