仿照正点原子写的步进电机驱动,使用的是STM32F407ZGT6,
将原子的例程里的定时器8改成使用定时器1,无波形输出
程序如下:
/********** Çy¶ˉÆ÷ ¶Ë¿ú¶¨òå **************
//DRIVER_DIR PG0 (DCMI_D6)
//DRIVER_ERR PG1 (DCMI_D7)
//STEP_PULSE PC7 (TIM8_CH2 DCMI_D1)
******************************************/
#define Servo2_DIR PGout(0) // Dy×a·½Ïò
#define Servo2_ERR PGin(1) // ê1Äü½Å μíμçƽóDD§
/********** Çy¶ˉÆ÷ ¶Ë¿ú¶¨òå **************
//Servo2_DIR PG0
//Servo2_ERR PG1
//STEP_PULSE PA8 (TIM1_CH1)
******************************************/
static u8 rcr_remainder; //Öظ′¼Æêyóàêy2¿·Ö
static u8 is_rcr_finish=1; //Öظ′¼ÆêyÆ÷êÇ·ñéèÖÃíê3é
static long rcr_integer; //Öظ′¼ÆêyÕûêy2¿·Ö
static long target_pos=0; //óD·ûoÅ·½Ïò
static long current_pos=0; //óD·ûoÅ·½Ïò
static DIR_Type Servo2_dir=CW; //Ë3ê±Õë
static int RCR_VAL=255; //ÿ¼Æêy£¨RCR_VAL+1£©′Σ¬ÖD¶Ïò»′Σ¬Õa¸öÖ죨0~255£©éèÖÃ′óò»D©¿éòÔ½μμíÖD¶ÏÆμÂê
//ÆμÂ궨òå: 20Hz~100KHz
//Çy¶ˉÆ÷Äú2¿oˉêy
void TIM1_OPM_RCR_Init(u16 arr,u16 psc);//TIM1_CH1 μ¥Âö3åêä3ö+Öظ′¼Æêy1|Äü3õê¼»ˉ
void TIM1_Startup(u32 frequency); //Æô¶ˉ¶¨ê±Æ÷1
/************** Çy¶ˉÆ÷¿ØÖÆDÅoÅÏß3õê¼»ˉ ****************/
void Servo2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);//ê1ÄüGPIOEê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //Servo2_DIR
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //ÆÕí¨êä3öÄ£ê½
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOG, &GPIO_InitStructure);//3õê¼»ˉGPIOG
GPIO_SetBits(GPIOG,GPIO_Pin_0);//PG0êä3ö¸ß Ë3ê±Õë·½Ïò Servo2_DIR
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//ÆÕí¨êäèëÄ£ê½
GPIO_Init(GPIOG, &GPIO_InitStructure);//3õê¼»ˉGPIOG11
TIM1_OPM_RCR_Init(999,168-1); //1MHz¼ÆêyÆμÂê μ¥Âö3å+Öظ′¼ÆêyÄ£ê½
}
/***********************************************
//TIM1_CH1(PA8) μ¥Âö3åêä3ö+Öظ′¼Æêy1|Äü3õê¼»ˉ
//TIM1 ê±ÖóÆμÂê 84*2=168MHz
//arr:×Ô¶ˉÖØ×°Öμ
//psc:ê±ÖóÔ¤·ÖÆμêy
************************************************/
void TIM1_OPM_RCR_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM1ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1ÄüPORTAê±Öó
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1); //GPIOA8¸′óÃÎa¶¨ê±Æ÷1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOA8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //ÏÂà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA8
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular); /********* éèÖÃÖ»óD¼Æêyòç3ö×÷Îa¸üDÂÖD¶Ï ********/
TIM_SelectOnePulseMode(TIM1,TIM_OPMode_Single);/******* μ¥Âö3åÄ£ê½ **********/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3ö2ê1Äü
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; /****** ±è½Ïêä3ö2Nê§Äü *******/
TIM_OCInitStructure.TIM_Pulse = arr>>1; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1ԤװÔØê1Äü
TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷
TIM_ITConfig(TIM1, TIM_IT_Update ,ENABLE); //TIM1 ê1Äü»òÕßê§ÄüÖ¸¶¨μÄTIMÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; //TIM1ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÏèÕ¼óÅÏ輶1¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //′óóÅÏ輶2¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM1
}
/******* TIM1¸üDÂÖD¶Ï·tÎñ3ìDò *********/
void TIM1_UP_TIM10_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1,TIM_FLAG_Update)!=RESET)//¸üDÂÖD¶Ï
{
TIM_ClearITPendingBit(TIM1,TIM_FLAG_Update);//Çå3y¸üDÂÖD¶Ï±ê־λ
if(is_rcr_finish==0)//Öظ′¼ÆêyÆ÷Î′éèÖÃíê3é
{
if(rcr_integer!=0) //Õûêy2¿·ÖÂö3å»1Î′·¢Ëííê3é
{
TIM1->RCR=RCR_VAL; //éèÖÃÖظ′¼ÆêyÖμ
rcr_integer--; //¼õéùRCR_VAL+1¸öÂö3å
}else if(rcr_remainder!=0) //óàêy2¿·ÖÂö3å 2»Î»0
{
TIM1->RCR=rcr_remainder-1; //éèÖÃóàêy2¿·Ö
rcr_remainder=0; //Çåáã
is_rcr_finish=1; //Öظ′¼ÆêyÆ÷éèÖÃíê3é
}else goto out1; //rcr_remainder=0£¬Ö±½óíË3ö
TIM_GenerateEvent(TIM1,TIM_EventSource_Update); //2úéúò»¸ö¸üDÂê¼t ÖØDÂ3õê¼»ˉ¼ÆêyÆ÷
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE Ö÷êä3öê1Äü
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM1
if(Servo2_dir==CW) //èç1û·½ÏòÎaË3ê±Õë
current_pos+=(TIM1->RCR+1);//¼óéÏÖظ′¼ÆêyÖμ
else //·ñÔò·½ÏòÎaÄæê±Õë
current_pos-=(TIM1->RCR+1);//¼õè¥Öظ′¼ÆêyÖμ
}else
{
out1: is_rcr_finish=1;//Öظ′¼ÆêyÆ÷éèÖÃíê3é
TIM_CtrlPWMOutputs(TIM1,DISABLE); //MOE Ö÷êä3ö1رÕ
TIM_Cmd(TIM1, DISABLE); //1رÕTIM1
//printf("μ±Ç°Î»ÖÃ=%ld
",current_pos);//′òó¡êä3ö
}
}
}
/***************** Æô¶ˉTIM1 *****************/
void TIM1_Startup(u32 frequency) //Æô¶ˉ¶¨ê±Æ÷8
{
u16 temp_arr=1000000/frequency-1;
TIM_SetAutoreload(TIM1,temp_arr);//é趨×Ô¶ˉÖØ×°Öμ
TIM_SetCompare1(TIM1,temp_arr>>1); //Æ¥ÅäÖμ2μèóúÖØ×°Öμò»°ë£¬êÇòÔÕ¼¿Õ±èÎa50%
TIM_SetCounter(TIM1,0);//¼ÆêyÆ÷Çåáã
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM1
}
/*********************************************************************************/
/* óû§2ãoˉêy */
/*********************************************************************************/
/********************************************
//Ïà¶Ô¶¨Î»oˉêy
//num 0¡«2147483647
//frequency: 20Hz~100KHz
//dir: CW(Ë3ê±Õë·½Ïò) CCW(Äæê±Õë·½Ïò)
*********************************************/
void Servo2_LocateRle(long num,u32 frequency,DIR_Type dir) //Ïà¶Ô¶¨Î»oˉêy
{
if(num<=0) //êyÖμD¡μèóú0 ÔòÖ±½ó·μ»Ø
{
//printf("
The num should be greater than zero!!
");
return;
}
if(TIM1->CR1&0x01)//éÏò»′ÎÂö3å»1Î′·¢Ëííê3é Ö±½ó·μ»Ø
{
//printf("
The last time pulses is not send finished,wait please!
");
return;
}
if((frequency<20)||(frequency>100000))//Âö3åÆμÂê2»Ôú·¶Î§Äú Ö±½ó·μ»Ø
{
// printf("
The frequency is out of range! please reset it!!(range:20Hz~100KHz)
");
return;
}
Servo2_dir=dir;//μÃμ½·½Ïò
Servo2_DIR=Servo2_dir;//éèÖ÷½Ïò
if(Servo2_dir==CW)//Ë3ê±Õë
target_pos=current_pos+num;//Ä¿±êλÖÃ
else if(Servo2_dir==CCW)//Äæê±Õë
target_pos=current_pos-num;//Ä¿±êλÖÃ
rcr_integer=num/(RCR_VAL+1);//Öظ′¼ÆêyÕûêy2¿·Ö
rcr_remainder=num%(RCR_VAL+1);//Öظ′¼Æêyóàêy2¿·Ö
is_rcr_finish=0;//Öظ′¼ÆêyÆ÷Î′éèÖÃíê3é
TIM1_Startup(frequency);//¿aÆôTIM1
}
/********************************************
//¾ø¶Ô¶¨Î»oˉêy
//num -2147483648¡«2147483647
//frequency: 20Hz~100KHz
*********************************************/
void Servo2_LocateAbs(long num,u32 frequency)//¾ø¶Ô¶¨Î»oˉêy
{
if(TIM1->CR1&0x01)//éÏò»′ÎÂö3å»1Î′·¢Ëííê3é Ö±½ó·μ»Ø
{
//printf("
The last time pulses is not send finished,wait please!
");
return;
}
if((frequency<20)||(frequency>100000))//Âö3åÆμÂê2»Ôú·¶Î§Äú Ö±½ó·μ»Ø
{
// printf("
The frequency is out of range! please reset it!!(range:20Hz~100KHz)
");
return;
}
target_pos=num;//éèÖÃÄ¿±êλÖÃ
if(target_pos!=current_pos)//Ä¿±êoíμ±Ç°Î»ÖÃ2»í¬
{
if(target_pos>current_pos)
Servo2_dir=CW;//Ë3ê±Õë
else
Servo2_dir=CCW;//Äæê±Õë
Servo2_DIR=Servo2_dir;//éèÖ÷½Ïò
rcr_integer=abs(target_pos-current_pos)/(RCR_VAL+1);//Öظ′¼ÆêyÕûêy2¿·Ö
rcr_remainder=abs(target_pos-current_pos)%(RCR_VAL+1);//Öظ′¼Æêyóàêy2¿·Ö
is_rcr_finish=0;//Öظ′¼ÆêyÆ÷Î′éèÖÃíê3é
TIM1_Startup(frequency);//¿aÆôTIM1
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>