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

下载本文档

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

文档简介

1、#include "stm32f10x.h"#include "driver_motor.h"#define PWM_PERIOD_T 400#define U_Up_On GPIOB->BSRR = GPIO_Pin_13#define U_Up_Off GPIOB->BRR = GPIO_Pin_13#define U_Dn_On GPIOA->BSRR = GPIO_Pin_8#define U_Dn_Off GPIOA->BRR = GPIO_Pin_8#define V_Up_On GPIOB->BSRR = GP

2、IO_Pin_14#define V_Up_Off GPIOB->BRR = GPIO_Pin_14#define V_Dn_On GPIOA->BSRR = GPIO_Pin_9#define V_Dn_Off GPIOA->BRR = GPIO_Pin_9#define W_Up_On GPIOB->BSRR = GPIO_Pin_15#define W_Up_Off GPIOB->BRR = GPIO_Pin_15#define W_Dn_On GPIOA->BSRR = GPIO_Pin_10#define W_Dn_Off GPIOA->BR

3、R = GPIO_Pin_10#define SU_HOR GPIOA->IDR & GPIO_Pin_15#define SV_HOR GPIOA->IDR & GPIO_Pin_12#define SW_HOR GPIOA->IDR & GPIO_Pin_11/u8 Motor_Dir=0;/u8 Motor_EN=0;/u8 Hor_Value=7;/u16 TIM2_Conter=0;u16 Hall_Conter=0;MotorStruct Motor=CLOCK,40,STOP; /* * 函数: void IO_Init(void)* 描

4、述: IO* 参数:* 返回:* 其它:*/void IO_Init(void)GPIO_InitTypeDef GPIO_InitStruct;EXTI_InitTypeDef EXTI_InitStructure;/GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);/* 使能SWD 禁用 JTAG*/*LED Light*/GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStruct.GPIO_Spee

5、d=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.GPIO_Speed=GPIO_Speed_2MHz;GPIO_Init(GPIOA, &GPIO_InitStruct);/HarlGPIO_EXTILineConfig(GPIO_PortSource

6、GPIOA, 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 = E

7、XTI_Trigger_Rising_Falling;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_Init(&EXTI_InitStructure);/*函数: void PWM_Init(void)描述:配置PWM 定时器 TIM1参数:返回:无*/void PWM_Init(void)GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;TIM_OCInitTypeDef TIM_OCInitStructure;/TIM_B

8、DTRInitTypeDef TIM_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; GP

9、IO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &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_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GP

10、IO_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

11、 = 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_OCMod

12、e_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.

13、TIM_OCIdleState = TIM_OCIdleState_Set;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(

14、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);/*函数: void Motor_Init(void)描述:参数:返回:*/void Motor_Init(void)IO_Init();PWM_Init();/*函数

15、: void Flash_Led(u8 n)描述:参数:.返回:*/void Flash_Led(u8 n)u8 i=0;for(i=0;i<n;i+)Led_On;DelayMs(100*n);Led_Off;DelayMs(100*n);/*函数: void SetPWMduty(u8 PWMChanel,u16 pulse)描述:设置pwm 波占空比参数:返回:* */void SetPWMduty(u8 PWMChanel,u16 pulse)switch(PWMChanel)case 1 :TIM1->CCR1=pulse;break;case 2 :TIM1->C

16、CR2=pulse;break;case 3 :TIM1->CCR3=pulse;break;default :break;/*函数: PWM_T_Output描述:设置相应的PWM 梯形波输出参数: pName 上桥臂名称,nName 下桥臂名称.返回:无*/void PWM_T_Output(u8 pName , u8 nName , u8 mRate)switch(pName)case 1:GPIO_SetBits(GPIOB, GPIO_Pin_15 | GPIO_Pin_14 );GPIO_ResetBits(GPIOB , GPIO_Pin_13);break;case 2:

17、GPIO_SetBits(GPIOB, GPIO_Pin_13 | GPIO_Pin_15 );GPIO_ResetBits(GPIOB , GPIO_Pin_14);break;case 3: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)case 1:TIM_SetCompare2(TIM1,0);

18、TIM_SetCompare3(TIM1,0);TIM_SetCompare1(TIM1,(u16)(PWM_PERIOD_T * mRate / 100); break;case 2:TIM_SetCompare3(TIM1,0);TIM_SetCompare1(TIM1,0);TIM_SetCompare2(TIM1,(u16)(PWM_PERIOD_T * mRate / 100); break;case 3:TIM_SetCompare2(TIM1,0);TIM_SetCompare1(TIM1,0);TIM_SetCompare3(TIM1,(u16)(PWM_PERIOD_T *

19、mRate / 100); break;default:TIM_SetCompare1(TIM1,0);TIM_SetCompare2(TIM1,0);TIM_SetCompare3(TIM1,0);.TIM_SetAutoreload(TIM1, PWM_PERIOD_T);/*函数: PWM_T_Calculation描述:梯形波计算参数: HallValue 霍尔值, mRate 调制百分比, direction 方向返回:无*/void PWM_T_Calculation(u8 hallValue , u8 mRate , u8 direction)if(direction = 1)s

20、witch(hallValue)case 5:PWM_T_Output(1 , 3 , mRate);break;case 1:PWM_T_Output(1 , 2 , mRate);break;case 3:PWM_T_Output(3 , 2 , mRate);break;case 2:PWM_T_Output(3 , 1 , mRate);break;case 6:PWM_T_Output(2 , 1 , mRate);break;case 4:PWM_T_Output(2 , 3 , mRate);break;default:PWM_T_Output(4 , 4 , 0);break;

21、elseswitch(hallValue)case 5:.PWM_T_Output(3 , 1 , mRate);break;case 1:PWM_T_Output(2 , 1 , mRate);break;case 3:PWM_T_Output(2 , 3 , mRate);break;case 2:PWM_T_Output(1 , 3 , mRate);break;case 6:PWM_T_Output(1 , 2 , mRate);break;case 4:PWM_T_Output(3 , 2 , mRate);break;default:PWM_T_Output(4 , 4 , 0);

22、break;/*函数: PWM_T_Int描述:定时器中断程序,根据霍尔位置设置输出梯形波参数: direction= 顺时针 / 逆时针 ,mRate 调制率返回:无*/void PWM_T_Int(u8 direction , u8 mRate)u8 hallValueTemp;u8 hallValue;hallValueTemp = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) + (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) << 1) + (GPIO_ReadInputDataBit(GPIOA

23、, GPIO_Pin_15) << 2);hallValue = hallValueTemp;PWM_T_Calculation(hallValue , mRate , direction);/*函数: PWM_Stop描述: PWM 控制信号使能输出,禁止 TIM1 中断,使能TIM1 中断.参数: pwmflag=DISABLE,PWM信号使能输出, pwmflag=ENABLE,禁止 PWM 输出返回:无*/void PWM_Stop(u8 pwmflag)NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_In

24、itStructure;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;

25、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_Out_OD;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_SetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10);

26、 /PWM 口/PWM_T_Output(4 , 4 , 0);if(pwmflag = 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, &a

温馨提示

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

评论

0/150

提交评论