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 = GPIO_Pin_14#define V_Up_Off GPIOB-BRR = GP
2、IO_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-BRR = GPIO_Pin_10#define SU_HOR GPIOA-IDR /u8 Motor_EN=0;/u8 Hor_Value
3、=7;/u16 TIM2_Conter=0;u16 Hall_Conter=0;MotorStruct Motor=CLOCK,40,STOP;/* 函数:void IO_Init(void)* 描述: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
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_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;G
5、PIO_Init(GPIOA, /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_Line15; EXTI_InitStructure.EXTI_Mode
6、= EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init( /*函数:void PWM_Init(void)描述:配置 PWM 定时器 TIM1参数:返回:无*/void PWM_Init(void)GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;TIM_OCIn
7、itTypeDef TIM_OCInitStructure;/TIM_BDTRInitTypeDef 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.
8、GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, 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
9、.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOB, /定时器设置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_CounterMode_Up;
10、TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1 , /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_Ou
11、tputState_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_OCIni
12、tStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; TIM_OC1Init(TIM1, 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 在
13、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();/*函数:void Flash_Led(u8 n)描述:参数:返回:*/void Flash_Led(u8 n)u8 i=0;for(i=0;iCCR1=pulse;break;case 2 :TIM1-CCR2=pulse;break;case 3 :TI
14、M1-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:GPIO_SetBits(GPIOB, GPIO_Pin_13
15、| 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);TIM_SetCompare3(TIM1,0);TIM_SetC
16、ompare1(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 * mRate / 100);break;default:TIM_Set
17、Compare1(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)switch(hallValue) case 5:PWM_T_Output(1 ,
18、 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;elseswitch(hallValue)case 5:PWM_T_Outpu
19、t(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);break;/*函数:PWM_T_Int描述:定时器中断程序,根据霍尔位置设置输
20、出梯形波参数: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, GPIO_Pin_15) 2);hallValue = hallValueTemp;PWM_T_Calc
21、ulation(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_InitStructure;if(pwmflag = ENABLE)/禁止 PWM 输出TIM_Cmd(TIM1 , DISABLE)
22、;/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(GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 |
23、 GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_Init(GPIOA, GPIO_SetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10); /PWM 口/PWM_T_Output(4 , 4 , 0);if(pwmflag = DISABLE)/使能 PWM 输出NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM16_I
24、RQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;GPIO_InitStructure
25、.GPIO_Mode = GPIO_Mode_AF_OD;GPIO_Init(GPIOA, / TIM_Cmd(TIM2 , ENABLE);/*函数:void Motor_Start(void)描述:启动电机参数:返回:*/void Motor_Start(void) PWM_T_Int(Motor.Dir,Motor.Speed);/*函数:void Motor_Stop(void)描述:停止参数:返回:*/void Motor_Stop(void)/U_Up_On;V_Up_On;W_Up_On;/U_Dn_On;V_Dn_On;W_Dn_On;PWM_T_Output(4,4,0);/
26、*函数:void EXTI15_10_IRQHandler(u8 dir,u8 hall)描述:霍尔传感器中断参数:返回:*/void EXTI15_10_IRQHandler(void)Hall_Conter+;if (EXTI_GetITStatus(EXTI_Line11) != RESET)|(EXTI_GetITStatus(EXTI_Line12) != RESET)|(EXTI_GetITStatus(EXTI_Line15) != RESET)if(Motor.State!=STOP)PWM_T_Int(Motor.Dir,Motor.Speed); EXTI_ClearITPendingBit(EXTI_Line11); /清除标志EXTI_ClearITPendingBit(EXTI_Line12); /清除标志EXTI_ClearITPendingBit(EXTI_Line15); /清除标志/*函数:void Test_Motor(u8 dir,u8 hall)描述:电机控制参数:返回:
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。