完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#ifndef __MOTOR_X_H__
#define __MOTOR_X_H__ #include "stm32f10x.h" //#define OutCurrent_Off() GPIO_ResetBits(GPIOD,GPIO_Pin_2) //Cut off the out current //#define OutCurrent_On() GPIO_SetBits(GPIOD,GPIO_Pin_2) //#define OutCurrent_Off() GPIO_ResetBits(GPIOA,GPIO_Pin_0) //Cut off the out current //#define OutCurrent_On() GPIO_SetBits(GPIOA,GPIO_Pin_0) // //#define HoldCurrent_Full() GPIO_ResetBits(GPIOA,GPIO_Pin_0) //full current when stop //#define HoldCurrent_Half() GPIO_SetBits(GPIOA,GPIO_Pin_0) //half current when stop //#define FH_Half() GPIO_ResetBits(GPIOA,GPIO_Pin_1) //Half step mode //#define FH_Full() GPIO_SetBits(GPIOA,GPIO_Pin_1) //Full step mode //#define CW_PUL_SetOFF() GPIO_SetBits(GPIOA,GPIO_Pin_6) // //#define CW_PUL_SetON() GPIO_ResetBits(GPIOA,GPIO_Pin_6) // #define DIR_X_SetOFF() GPIO_SetBits(GPIOA,GPIO_Pin_1) // #define DIR_X_SetON() GPIO_ResetBits(GPIOA,GPIO_Pin_1) // //#define Buf_Out_On() GPIO_ResetBits(GPIOB,GPIO_Pin_9) //#define Buf_Out_Off() GPIO_SetBits(GPIOB,GPIO_Pin_9) //#define GetStatus_LSCCW() GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) // //#define GetStatus_LSORG() GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) // //#define GetStatus_LSCW() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) // //#define GetStatus_LSCCW() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) // //#define GetStatus_LSORG() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) // //#define GetStatus_LSCW() GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) // #define X_Stage_Forward '+' //CCW,向正方向 #define X_Stage_Backward '-' //CW,向负方向 #define X_Stage_Range_Upper 8500 #define X_Stage_Range_Lower -4000 #define RT_FALSE 0 #define RT_TRUE 1 #define ADDSPEEDNUM 1000 #define TIM2_Prescaler 0 #define TIM2_ClockDiv 0 #pragma pack(1) typedef struct _FreSpeed { uint16_t MaxPeriod ; //最大速度比较寄存器值 uint32_t MaxNum ; uint32_t IncPeriod[ADDSPEEDNUM]; uint32_t IncNum ; // 加减速脉冲数 uint8_t AvrFlag ; //匀速标志 uint8_t CurState ; //当前状态 } FreSpeed; #pragma pack() enum SpeedFlagType { IncSpeedState, // DecSpeedState, AvrSpeedState }; extern FreSpeed X_motor ; / void X_Timer2_Init(void); static void Set_Timer2_Isr_Enable(void); static void Set_Timer2_Isr_Disable(void); uint8_t CalculateXModelLine(FreSpeed *iSpeed,float len, uint32_t PluseNum, float fre_Min ,float fre_Max,float flexible ); static void X_moveTrans(float iMaxVel,uint32_t PulseNum ,float flexible ); void X_T2Ctrl(uint32_t cnt ); void X_TIM2_ITConfigure(void); static void X_NVIC_TIM2_Configure(void); #endif #include "motor_x.h" #include "math.h" #include "bsp_timer.h" FreSpeed X_motor; void X_Timer2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); /* TIM2总线时钟打开,设置TIM2CLK为72MHz */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化TIM3 TIM_TimeBaseStructure.TIM_Period = 1000; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler = TIM2_Prescaler; //设置用来作为TIMx时钟频率除数的预分频值 //不分频 TIM_TimeBaseStructure.TIM_ClockDivision = TIM2_ClockDiv; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 //初始化TIM3 Channel2 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高 TIM_OC3Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2 TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器 TIM2_ITConfigure(); } static void Set_Timer2_Isr_Enable(void) { TIM_ClearFlag(TIM2, TIM_IT_CC3); TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE); // TIM_Cmd(TIM2, ENABLE); } static void Set_Timer2_Isr_Disable(void) { //UNMOTORY_TEAR; TIM_Cmd(TIM2, DISABLE); TIM_ITConfig(TIM2, TIM_IT_CC3, DISABLE); } uint8_t CalculateXModelLine(FreSpeed *iSpeed,float len, uint32_t PluseNum, float fre_Min ,float fre_Max,float flexible ) { int i ; float TimClock = 72000000.0 / (TIM2_Prescaler+1 )/ ( TIM2_ClockDiv +1); float deno; float melo; float delt = fre_Max - fre_Min ; if(delt <=0 ) { return RT_FALSE; } if((PluseNum - 2*len)>0) //存在匀速运动 { iSpeed->AvrFlag = RT_TRUE; iSpeed->MaxPeriod = (uint16_t)(TimClock/(fre_Max )); iSpeed->MaxNum = PluseNum - 2*len; iSpeed->IncNum = len; } else { iSpeed->AvrFlag = RT_FALSE; len = PluseNum/2.0; iSpeed->AvrFlag = RT_FALSE; iSpeed->MaxPeriod = RT_FALSE; iSpeed->MaxNum = RT_FALSE; iSpeed->IncNum = len; } for(i = 0 ;i< len ; i++ ) { melo = flexible* (i - len/2)/(len/2); deno = 1.0/ ( 1 + exp( -melo)); iSpeed->IncPeriod = (uint16_t)(TimClock/( deno *delt + fre_Min )) ; } return RT_TRUE; } static void X_moveTrans(float iMaxVel,uint32_t PulseNum ,float flexible ) { float fre_min = 1000; float Addlen = ADDSPEEDNUM ; if(CalculateXModelLine(&X_motor,Addlen,PulseNum,fre_min,iMaxVel,flexible ) == RT_TRUE ) { //Set_Timer2_Isr_Enable(); } } void X_T2Ctrl(uint32_t cnt ) { X_moveTrans(200000 , cnt ,8 ); Set_Timer2_Isr_Enable(); } /** * @brief This function handles TIM2 Handler. * @param None * @retval None */ void TIM2_IRQHandler(void) { static uint16_t iCount=0; static uint8_t SpeedMode = IncSpeedState; uint32_t period; if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET) { TIM_ClearITPendingBit(TIM2,TIM_IT_Update); switch(SpeedMode) { case IncSpeedState: period = X_motor.IncPeriod[iCount++]; TIM2->ARR = period; //TIM2_ARR赋值 TIM2->CCR3 = (period)/2; if( iCount ==X_motor.IncNum -1 ) { SpeedMode = ( X_motor.AvrFlag == RT_TRUE ) ? AvrSpeedState :DecSpeedState; } break; case AvrSpeedState: TIM2->ARR = X_motor.MaxPeriod; TIM2->CCR3 = (X_motor.MaxPeriod)/2; X_motor.MaxNum --; if( X_motor.MaxNum == 0) { SpeedMode = DecSpeedState; } break; case DecSpeedState: period = X_motor.IncPeriod[iCount--]; TIM2->ARR = period; //TIM2_ARR赋值 TIM2->CCR3 = (period)/2; if(iCount == 0) { SpeedMode = IncSpeedState; Set_Timer2_Isr_Disable(); } break; default: SpeedMode = IncSpeedState; break; } } } //void TIM2_IRQHandler(void) //{ // // if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET) // { // TIM_ClearITPendingBit(TIM2,TIM_IT_Update); // // if(Stage_Dir == Stage_Forward)Stage_Coord ++; else if(Stage_Dir == Stage_Backward)Stage_Coord--; Stage_Pace_Count--; /* 如果步数走完,在中断内停止脉冲输出 */ // printf("stage_pace_count IT %d n" ,Stage_Pace_Count); if(Stage_Pace_Count<=0) { Stage_Pace_Count=0; Pulse_Disable(); } // // // // // TIM_ClearITPendingBit(TIM2,TIM_IT_Update); // } //} |
|
|
|
只有小组成员才能发言,加入小组>>
2386 浏览 0 评论
8910 浏览 4 评论
36486 浏览 19 评论
4981 浏览 0 评论
24307 浏览 34 评论
1375浏览 2评论
1627浏览 1评论
2017浏览 1评论
1441浏览 0评论
386浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 03:56 , Processed in 1.203116 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号