本帖最后由 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,波形就是飘的,在示波器上波形就是不停移动的,不是在同一个点触发的;这个是什么问题,有啥寄存器没清?
|