三相直流无刷电机驱动程序文件Word文件下载.docx
《三相直流无刷电机驱动程序文件Word文件下载.docx》由会员分享,可在线阅读,更多相关《三相直流无刷电机驱动程序文件Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
BSRR=GPIO_Pin_8
#defineU_Dn_OffGPIOA->
BRR=GPIO_Pin_8
#defineV_Up_OnGPIOB->
BSRR=GPIO_Pin_14
#defineV_Up_OffGPIOB->
BRR=GPIO_Pin_14
#defineV_Dn_OnGPIOA->
BSRR=GPIO_Pin_9
#defineV_Dn_OffGPIOA->
BRR=GPIO_Pin_9
#defineW_Up_OnGPIOB->
BSRR=GPIO_Pin_15
#defineW_Up_OffGPIOB->
BRR=GPIO_Pin_15
#defineW_Dn_OnGPIOA->
BSRR=GPIO_Pin_10
#defineW_Dn_OffGPIOA->
BRR=GPIO_Pin_10
#defineSU_HORGPIOA->
IDR&
GPIO_Pin_15
#defineSV_HORGPIOA->
GPIO_Pin_12
#defineSW_HORGPIOA->
GPIO_Pin_11
//u8Motor_Dir=0;
//u8Motor_EN=0;
//u8Hor_Value=7;
//u16TIM2_Conter=0;
u16Hall_Conter=0;
MotorStructMotor={CLOCK,40,STOP};
/*******************************************************************************
*函数:
voidIO_Init(void)
*描述:
IO
*参数:
*返回:
*其它:
*******************************************************************************/
{
GPIO_InitTypeDefGPIO_InitStruct;
EXTI_InitTypeDefEXTI_InitStructure;
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
/*使能SWD禁用JTAG*/
/**********************LEDLight***********/
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&
GPIO_InitStruct);
/***********************霍尔传感器中断**********/
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&
//Harl
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource11);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource12);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource15);
EXTI_InitStructure.EXTI_Line=EXTI_Line11|EXTI_Line12|EXTI_Line15;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&
EXTI_InitStructure);
}
/***************************************************************************
函数:
voidPWM_Init(void)
描述:
配置PWM定时器TIM1
参数:
返回:
无
***************************************************************************/
GPIO_InitTypeDefGPIO_InitStructure;
TIM_TimeBaseInitTypeDefTIM_TimeBaseInitStruct;
TIM_OCInitTypeDefTIM_OCInitStructure;
//TIM_BDTRInitTypeDefTIM_BDTRInitStructure;
TIM_Cmd(TIM1,DISABLE);
TIM_CtrlPWMOutputs(TIM1,DISABLE);
//禁止OC输出
//IO口设置
GPIO_SetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
//PWM口
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
//普通IO口
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
//定时器设置
TIM_TimeBaseInitStruct.TIM_Period=PWM_PERIOD_T;
//5极电机,3000RPM,每个Step有10个脉冲,载波15KHZ
TIM_TimeBaseInitStruct.TIM_Prescaler=2;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1,&
TIM_TimeBaseInitStruct);
//TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
//TIM_ARRPreloadConfig(TIM1,ENABLE);
//TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
//配置PWM输出
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse=1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;
TIM_OC1Init(TIM1,&
TIM_OCInitStructure);
TIM_OC2Init(TIM1,&
TIM_OC3Init(TIM1,&
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
//使能TIMx在ARR上的预装载寄存器TIM_Cmd(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
/*****************************************************************************************
voidMotor_Init(void)
*****************************************************************************************/
IO_Init();
PWM_Init();
voidFlash_Led(u8n)
u8i=0;
for(i=0;
i<
n;
i++)
{
Led_On;
DelayMs(100*n);
Led_Off;
}
voidSetPWMduty(u8PWMChanel,u16pulse)
设置pwm波占空比
switch(PWMChanel)
case1:
TIM1->
CCR1=pulse;
break;
case2:
CCR2=pulse;
case3:
CCR3=pulse;
default:
PWM_T_Output
设置相应的PWM梯形波输出
pName上桥臂名称,nName下桥臂名称
********************************************************************************/
voidPWM_T_Output(u8pName,u8nName,u8mRate)
switch(pName)
case1:
GPIO_SetBits(GPIOB,GPIO_Pin_15|GPIO_Pin_14);
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
case2:
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_15);
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
case3:
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
default:
switch(nName)
TIM_SetCompare2(TIM1,0);
TIM_SetCompare3(TIM1,0);
TIM_SetCompare1(TIM1,(u16)(PWM_PERIOD_T*mRate/100));
TIM_SetCompare1(TIM1,0);
TIM_SetCompare2(TIM1,(u16)(PWM_PERIOD_T*mRate/100));
TIM_SetCompare3(TIM1,(u16)(PWM_PERIOD_T*mRate/100));
TIM_SetAutoreload(TIM1,PWM_PERIOD_T);
PWM_T_Calculation
梯形波计算
HallValue霍尔值,mRate调制百分比,direction方向
voidPWM_T_Calculation(u8hallValue,u8mRate,u8direction)
if(direction==1)
switch(hallValue)
{
case5:
PWM_T_Output(1,3,mRate);
PWM_T_Output(1,2,mRate);
PWM_T_Output(3,2,mRate);
PWM_T_Output(3,1,mRate);
case6:
PWM_T_Output(2,1,mRate);
case4:
PWM_T_Output(2,3,mRate);
PWM_T_Output(4,4,0);
else
PWM_T_Int
定时器中断程序,根据霍尔位置设置输出梯形波
direction=顺时针/逆时针,mRate调制率
voidPWM_T_Int(u8direction,u8mRate)
u8hallValueTemp;
u8hallValue;
hallValueTemp=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_11)+(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)<
<
1)+(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)<
2);
hallValue=hallValueTemp;
PWM_T_Calculation(hallValue,mRate,direction);
/************************************************************************
PWM_Stop
PWM控制信号使能输出,禁止TIM1中断,使能TIM1中断
pwmflag=DISABLE,PWM信号使能输出,pwmflag=ENABLE,禁止PWM输出
*************************************************************************/
voidPWM_Stop(u8pwmflag)
NVIC_InitTypeDefNVIC_InitStructure;
if(pwmflag==ENABLE)//禁止PWM输出
//TIM_Cmd(TIM2,DISABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_TIM16_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=DISABLE;