发布时间:2023-02-20 13:30
选用定时器的主从模式,TIM2为主,TIM4为从。TIM2 输出PWM,TIM4记录脉冲数;选用PA3做为方向控制接线引脚。
关键代码如下:
//定时器2主模式,定时器4从模式
//PWM输出
/*设置PA3引脚,使用PA3作为方向控制信号的控制脚*/
void DIR_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO设置,创建结构体
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //控制电机运动方向
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
}
/*打开定时器2,并把TIM2设定为主定时器,*/
void TIM2_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO设置,创建结构体
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器设置结构体
TIM_OCInitTypeDef TIM_OCInitStructure; //pwm波对应设置结构体
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //TIM_CH1和T
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_TIM2);
//固定预分频值为5,通过改变重装载值的大小改变转速,当前转速为每分钟3000转
//3000x3200=9600000(每分钟需要的脉冲数),9600000/60=160000(单位时间脉冲速),
//84000000/160000=525=5x105;
TIM_TimeBaseStructure.TIM_Period = Speed-1;
TIM_TimeBaseStructure.TIM_Prescaler =(5-1); //设置用来作为TIMx时钟//频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分割:TDTS= Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //通道
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
/***定时器4从模式***/
void TIM4_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//TIMx初始化结构体
NVIC_InitTypeDef NVIC_InitStructure; //NVIC结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 3200-1; //设置自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//tim1必须加这句不然没反应
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //
TIM_SelectInputTrigger(TIM4, TIM_TS_ITR1); //对应上面的定时器主从匹配表
TIM4->SMCR|=0x07; //设置从模式寄存器
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
IM_Cmd(TIM4,ENABLE);
}
//每完成一个设定周期(3200)进入中断,在在中断里进行相应的操作
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) // TIM_IT_CC1
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); // 清除中断标志位
TIM_SetCounter(TIM4,0);
Now_Turns++; //旋转圈数加一
LCD_ShowxNum(130,200,Now_Turns,4,24,0); //显示在LCD屏上
if(Now_Turns%2==1)
{
// beep(); //测试用
}
if(Now_Turns%2==0)
{
// Shine_LED(); //测试用
}
if(Now_Turns>=Target_Turns)
{
Run_Flag=1;
TIM_CtrlPWMOutputs(TIM2, DISABLE); //主输出失能
TIM_Cmd(TIM2, DISABLE); // 关闭定时器
TIM_Cmd(TIM4, DISABLE); // 关闭定时器
TIM_ITConfig(TIM4, TIM_IT_Update, DISABLE); //关闭
}
}
}
细节后续优化.............