void PWM_Init(void)
{
timer_parameter_struct timer_initpara;
timer_oc_parameter_struct timer_ocintpara ;
rcu_periph_clock_enable(RCU_TIMER2); //TIM2时钟使能
rcu_periph_clock_enable(RCU_TIMER1); //TIM1时钟使能
timer_initpara.period = 65535; //自动重装载寄存器周期的值
timer_initpara.prescaler = 0; //先不装入分频值
timer_initpara.clockdivision = 0;
timer_initpara.alignedmode = TIMER_COUNTER_UP;
timer_init(TIMER2,&timer_initpara); // TIM2
timer_init(TIMER1,&timer_initpara); // TIM1
timer_prescaler_config(TIMER2,PrescalerValue,TIMER_PSC_RELOAD_NOW); //预分频值即时装入TIM2
timer_prescaler_config(TIMER1,PrescalerValue,TIMER_PSC_RELOAD_NOW); //预分频值即时装入TIM1
//定时器2的CC1配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR1寄存器预装
//定时器2的CC2配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR1寄存器预装
//定时器2的CC3配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR2寄存器预装
//定时器2的CC4配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER2,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR1寄存器预装
//配置TIM2中断
nvic_irq_enable(TIMER2_IRQn,0,1);
timer_enable(TIMER2);//开启定时器2
//定时器1的CC1配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER1,TIMER_CH_0,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_0,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER1,TIMER_CH_0,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER1,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR1寄存器预装
//定时器1的CC2配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR1寄存器预装
//定时器1的CC3配置输出比较模式
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; //输出极性
timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,First_Motor_Time_Step); //比较寄存器值
timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_TIMING); //输出比较触发模式
timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);//禁止TIM_CCR1寄存器预装
//配置TIM1中断
nvic_irq_enable(TIMER1_IRQn,0,2);
timer_enable(TIMER1);//开启定时器1
}
void TIMER2_IRQHandler(void) //TIM2中断
{
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH0) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
First_Motor_Step=!First_Motor_Step;
if(First_Motor_Step==1)First_Motor_Step_Count++;
TIMER_CH0CV(TIMER2)+=First_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH0);
}
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH1) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
Second_Motor_Step=!Second_Motor_Step;
if(Second_Motor_Step==1)Second_Motor_Step_Count++;
TIMER_CH1CV(TIMER2)+=Second_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH1);
}
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH2) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
Third_Motor_Step=!Third_Motor_Step;
if(Third_Motor_Step==1)Third_Motor_Step_Count++;
TIMER_CH2CV(TIMER2)+=Third_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH2);
}
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH3) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
Seventh_Motor_Step=!Seventh_Motor_Step;
if(Seventh_Motor_Step==1)Seventh_Motor_Step_Count++;
TIMER_CH3CV(TIMER2)+=Seventh_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH3);
}
}
void TIMER1_IRQHandler(void) //TIM1中断
{
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH0) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
Fourth_Motor_Step=!Fourth_Motor_Step;
if(Fourth_Motor_Step==1)Fourth_Motor_Step_Count++;
TIMER_CH0CV(TIMER1)+=Fourth_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER1,TIMER_INT_FLAG_CH0);
}
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH1) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
Fifth_Motor_Step=!Fifth_Motor_Step;
if(Fifth_Motor_Step==1)Fifth_Motor_Step_Count++;
TIMER_CH1CV(TIMER1)+=Fifth_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER1,TIMER_INT_FLAG_CH1);
}
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH2) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
Sixth_Motor_Step=!Sixth_Motor_Step;
if(Sixth_Motor_Step==1)Sixth_Motor_Step_Count++;
TIMER_CH2CV(TIMER1)+=Sixth_Motor_Time_Step;
timer_interrupt_flag_clear(TIMER1,TIMER_INT_FLAG_CH2);
}
} |