無刷電機(jī)驅(qū)動代碼_第1頁
無刷電機(jī)驅(qū)動代碼_第2頁
無刷電機(jī)驅(qū)動代碼_第3頁
無刷電機(jī)驅(qū)動代碼_第4頁
無刷電機(jī)驅(qū)動代碼_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

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 = GPIO_P

2、in_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 & GPIO_Pin_15#define SV_HOR GPIO

3、A-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;/* 函數(shù): void IO_Init(void)* 描述: IO* 參數(shù):* 返回:* 其它:* /void IO_Init(void)GPIO_InitTypeDef GPIO_InitStruct;EXTI_InitTypeDef EXTI_Ini

4、tStructure;/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_Speed=GPIO_Speed_2MHz;GPIO_Init(GPIOB, &GPIO_InitStruct);GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11|GPI

5、O_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_PortSourceGPIOA, GPIO_PinSource11);GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource12);GPIO_EXTILineConfig(GPIO_Por

6、tSourceGPIOA, 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);/* 函數(shù): void PW

7、M_Init(void)描述:配置 PWM 定時器 TIM1參數(shù):返回:無*/void PWM_Init(void)GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;TIM_OCInitTypeDef TIM_OCInitStructure;/TIM_BDTRInitTypeDef TIM_BDTRInitStructure;TIM_Cmd(TIM1 , DISABLE);TIM_CtrlPWMOutputs(TIM1, DISABLE);/ 禁止 OC 輸出/IO 口設(shè)置GPI

8、O_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_Init(GPIOA, &GPIO_InitStructure);GPIO_SetBits(GPIOB, GPI0_Pin_13 |

9、 GPI0_Pin_14 | GPI0_Pin_15);/厝通 10 口GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPI0_InitStructure.GPI0_Speed = GPI0_Speed_10MHz;GPI0_InitStructure.GPI0_Mode = GPI0_Mode_0ut_PP;GPI0_Init(GPI0B, &GPI0_InitStructure);/定時器設(shè)置TIM_TimeBaselnitStruct.TIM_Period = PWM_PERI0D_T;/5

10、極電機(jī),3000RPM,每個 Step 有 10 個脈沖,載波 15KHZTIM_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_C

11、learITPendingBit(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_Di

12、sable;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_OC

13、InitStructure);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, TIM_OCPreload_Enable);TIM_ARRPreloadConfig(TIM1, ENABLE);/ 使能 TIMx 在 ARR上的預(yù)裝載寄存器

14、 TIM_Cmd(TIM1 , ENABLE);TIM_Cmd(TIM1 , ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); /*函數(shù): void Motor_Init(void)描述:參數(shù):返回:*/void Motor_Init(void)IO_Init(); PWM_Init(); /*函數(shù): void Flash_Led(u8 n) 描述:參數(shù):*返回:*/void Flash_Led(u8 n) u8 i=0; for(i=0;iCCR1=pulse;break;case 2 :TIM1-CCR2=pulse;break;case 3 :TIM1

15、-CCR3=pulse;break;default :break;/*函數(shù): PWM_T_Output描述:設(shè)置相應(yīng)的 PWM 梯形波輸出參數(shù): 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_

16、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);TIM_SetCompare3(TIM1,0);TI

17、M_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 * mRate / 100); break;defaul

18、t:TIM_SetCompare1(TIM1,0);TIM_SetCompare2(TIM1,0);TIM_SetCompare3(TIM1,0);TIM_SetAutoreload(TIM1, PWM_PERIOD_T);/*函數(shù): PWM_T_Calculation 描述:梯形波計算 參數(shù): HallValue 霍爾值, mRate 調(diào)制百分比, direction 方向 返回:無*/void PWM_T_Calculation(u8 hallValue , u8 mRate , u8 direction)if(direction = 1)switch(hallValue)case 5:P

19、WM_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;elseswitch(hallValue)cas

20、e 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);break;/*函數(shù): PWM_T_Int 描述:

21、定時器中斷程序,根據(jù)霍爾位置設(shè)置輸出梯形波 參數(shù): direction= 順時針 /逆時針 ,mRate 調(diào)制率 返回:無*/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 = ha

22、llValueTemp;PWM_T_Calculation(hallValue , mRate , direction); /* 函數(shù): PWM_Stop描述: PWM 控制信號使能輸出 ,禁止 TIM1 中斷,使能 TIM1 中斷參數(shù):pwmflag=DISABLE,PWM信號使能輸出,pwmflag=ENABLE禁止 PWM 輸出 返回:無*/void PWM_Stop(u8 pwmflag)NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;if(pwmflag = ENABLE)/ 禁止 PWM

23、 輸出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(&NVIC_InitStructure);GPIO_Ini

24、tStructure.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 = D

25、ISABLE)/ 使能 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

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論