# include "stepmotor.h"
// 主定时器TIM2,从定时器TIM3 ,ITR1.
void STEP_MOTOR_PWM_Configuration(u16 arr,u16 pre) //主定时器设置
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_TIM2); //
// PWM PA3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //复用推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化 PB14
TIM_TimeBaseStructure.TIM_Period= arr-1;
TIM_TimeBaseStructure.TIM_Prescaler= pre-1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //TIM2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = arr/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_SelectMasterSlaveMode( TIM2, TIM_MasterSlaveMode_Enable); // 定时器主从模式使能
TIM_SelectOutputTrigger( TIM2, TIM_TRGOSource_Update);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2,ENABLE);
void TIM3_Config(u32 PulseNum_B )//从定时器设置
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = PulseNum_B;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure);
TIM_SelectInputTrigger( TIM3, TIM_TS_ITR1); // TIM2-主,TIM3-从
TIM_SelectSlaveMode( TIM3, TIM_SlaveMode_Gated);
TIM_ITConfig( TIM3, TIM_IT_Update, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &NVIC_InitStructure);
}
void PWM_Output_B( u32 PulseNum_B,u8 DIR) // TIM2-主,TIM3-从
{
if(DIR == 0)
GPIO_SetBits(GPIOC, GPIO_Pin_4);// C4--DIR
else
GPIO_ResetBits(GPIOC, GPIO_Pin_4);// C4--DIR
TIM3_Config(PulseNum_B);
TIM_Cmd( TIM3, ENABLE);
TIM_ClearITPendingBit( TIM3, TIM_IT_Update);
TIM_ITConfig( TIM3, TIM_IT_Update, ENABLE);
STEP_MOTOR_PWM_Configuration( 1000,84); //F429:通用定时器是 84MHz, 故84MHz / 84 = 1MHz
TIM_Cmd( TIM2, ENABLE);
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus( TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit( TIM3, TIM_IT_Update); // 清除中断标志位
TIM_Cmd( TIM2, DISABLE); // 关闭定时器2
TIM_Cmd( TIM3, DISABLE); // 关闭定时器3
TIM_ITConfig( TIM3, TIM_IT_Update, DISABLE);
}
}
}
|