无刷电机驱动代码_第1页
无刷电机驱动代码_第2页
无刷电机驱动代码_第3页
无刷电机驱动代码_第4页
无刷电机驱动代码_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#includestm32f10 x.h#includedriver_motor.h#definePWM_PERIOD_T400defineU_Up_OnGPIOB-BSRR=GPIO_Pin_13defineU_Up_OffGPIOB-BRR=GPIO_Pin_13defineU_Dn_OnGPIOA-BSRR=GPIO_Pin_8defineU_Dn_OffGPIOA-BRR=GPIO_Pin_8defineV_Up_OnGPIOB-BSRR=GPIO_Pin_14defineV_Up_OffGPIOB-BRR=GPIO_Pin_14defineV_Dn_OnGPIOA-BSRR=GPIO

2、_Pin_9defineV_Dn_OffGPIOA-BRR=GPIO_Pin_9defineW_Up_OnGPIOB-BSRR=GPIO_Pin_15defineW_Up_OffGPIOB-BRR=GPIO_Pin_15defineW_Dn_OnGPIOA-BSRR=GPIO_Pin_10defineW_Dn_OffGPIOA-BRR=GPIO_Pin_10#defineSU_HORGPIOA-IDR&GPIO_Pin_15#defineSV_HORGPIOA-IDR&GPIO_Pin_12#defineSW_HORGPIOA-IDR&GPIO_Pin_11/u8Motor_Dir=0;/u8

3、Motor_EN=0;/u8Hor_Value=7;/u16TIM2_Conter=0;u16Hall_Conter=0;MotorStructMotor=CLOCK,40,STOP;*函数:voidIO_Init(void)*描述:IO*参数:返回:其它:/voidIO_Init(void)GPIO_InitTypeDefGPIO_InitStruct;EXTI_InitTypeDefEXTI_InitStructure;/GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);/*使能SWD禁用JTAG*/*LEDLight/*GPIO

4、_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_InitStruct.G

5、PIO_Speed=GPIO_Speed_2MHz;GPIO_Init(GPIOA,&GPIO_InitStruct);/HarlGPIO_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_

6、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参数:返回:无voidPWM_Init(void)GPIO_InitTypeDefGPIO_InitStructure;TIM_TimeBaseInitTy

7、peDefTIM_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

8、_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|610_15);/普通IO口GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;GPIO_In

9、itStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_Init(GPIOB,&GPIO_InitStructure);/定时器设置TIM_TimeBaseInitStruct.TIM_Period=PWM_PERIOD_T;/5极电机,3000RPM,每个Step有10个脉冲,载波15KHzTIM_TimeBaseInitStruct.TIM_Prescaler=2;TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_C

10、ounterMode_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_OCInitStructur

11、e.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

12、;TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Set;TIM_OC1Init(TIM1,&TIM_OCInitStructure);TIM_OC2Init(TIM1,&TIM_OCInitStructure);TIM_OC3Init(TIM1,&TIM_OCInitStructure);TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);TIM_OC3PreloadConfig(TIM1,TI

13、M_OCPreload_Enable);TIM_ARRPreloadConfig(TIM1,ENABLE);使能TIMx在ARR上的预装载寄存器TIM_Cmd(TIM1,ENABLE);TIM_Cmd(TIM1,ENABLE);TIM_CtrlPWMOutputs(TIM1,ENABLE);函数:voidMotor_Init(void)描述:参数:返回:voidMotor_Init(void)IO_Init();PWM_Init();函数:voidFlash_Led(u8n)描述:参数:返回:voidFlash_Led(u8n)u8i=0;for(i=0;iCCR1=pulse;break;:

14、TIM1-CCR2=pulse;break;:TIM1-CCR3=pulse;break;default:break;*函数:PWM_T_Output描述:设置相应的PWM梯形波输出参数:pName上桥臂名称,nName下桥臂名称返回:无*/voidPWM_T_Output(u8pName,u8nName,u8mRate)switch(pName)GPIO_SetBits(GPIOB,GPIO_Pin_15|GPIO_Pin_14);GPIO_ResetBits(GPIOB,GPIO_Pin_13);break;GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_1

15、5);GPIO_ResetBits(GPIOB,GPIO_Pin_14);break;GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14);GPIO_ResetBits(GPIOB,GPIO_Pin_15);break;default:GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);switch(nName)TIM_SetCompare2(TIM1,0);TIM_SetCompare3(TIM1,0);TIM_SetCompare1(TIM1,(u16)(PWM_PERIOD_T*mRate/10

16、0);break;TIM_SetCompare3(TIM1,0);TIM_SetCompare1(TIM1,0);TIM_SetCompare2(TIM1,(u16)(PWM_PERIOD_T*mRate/100);break;TIM_SetCompare2(TIM1,0);TIM_SetCompare1(TIM1,0);TIM_SetCompare3(TIM1,(u16)(PWM_PERIOD_T*mRate/100);break;default:TIM_SetCompare1(TIM1,0);TIM_SetCompare2(TIM1,0);TIM_SetCompare3(TIM1,0);T

17、IM_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);break;PWM_T_Output(1,2,mRate);break;case3:PWM_T_Output(3,2,mRate);break;PWM_T_

18、Output(3,1,mRate);break;case6:PWM_T_Output(2,1,mRate);break;PWM_T_Output(2,3,mRate);break;default:PWM_T_Output(4,4,0);break;elseswitch(hallValue)PWM_T_Output(3,1,mRate);break;PWM_T_Output(2,1,mRate);break;case3:PWM_T_Output(2,3,mRate);break;PWM_T_Output(1,3,mRate);break;case6:PWM_T_Output(1,2,mRate)

19、;break;case4:PWM_T_Output(3,2,mRate);break;default:PWM_T_Output(4,4,0);break;*函数:PWM_T_Int描述:定时器中断程序,根据霍尔位置设置输出梯形波参数:direction:顺时针/逆时针,mRate调制率返回:无*/voidPWM_T_Int(u8direction,u8mRate)u8hallValueTemp;u8hallValue;hallValueTemp=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_11)+(GPIO_ReadInputDataBit(GPIOA,GPIO_

20、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;GPIO_InitTypeDefGPIO_

21、InitStructure;if(pwmflag=ENABLE)/禁止PWM输出TIM_Cmd(TIM1,DISABLE);/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;NVIC_Init(&N

22、VIC_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_SetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);/PWM口/PWM_T_Output(4,4,0);if(pwmflag

23、=DISABLE)/使能PWM输出NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_TIM16_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_OD;GPIO_Init(GPIOA,&GPIO_InitStructure);/TIM_Cmd(TIM2,ENABLE);函数:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论