版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c/* SOFTWARE LICENSE AGREEMENT:* Microchip Technology Incorporated (Microchip) retains all ownership and* intellectual property rights in the code accompanying this message and in all* derivatives hereto. You may use t
2、his code, and any derivatives created by* any person or entity by or on your behalf, exclusively with Microchips* proprietary products. Your acceptance and/or use of this code constitutes* agreement to the terms and conditions of this notice.* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP
3、AS IS. NO* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED* TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A* PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIPS* PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN AN
4、Y APPLICATION.* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER* IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY),* STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL,* PUNITIVE, EXEMPLARY, INCIDENTAL OR CO
5、NSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF* ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN* ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT* ALLOWABLE BY LAW, MICROCHIPS TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO*
6、 THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO* HAVE THIS CODE DEVELOPED.* You agree that you are solely responsible for testing the code and* determining its suitability. Microchip has no obligation to modify, test,* certify, or support the code.* REVISION HIS
7、TORY:*/ File: sinusoidalBLDC v1.1.c/ Written By: Jorge Zambada, Microchip Technology/ The following files should be included in the MPLAB project:/ sinusoidalBLDC v1.1.c - Main source code file/ SVM.c - Space Vector Modulation file/ SVM.h/ p30f4012.gld - Linker script file / / Revision History/ July
8、/5/2005 - first version/#include p30f4012.h#include svm.h/Device Configuration_FOSC(CSW_FSCM_OFF & XT_PLL16); _FWDT(WDT_OFF);_FBORPOR(PBOR_ON & BORV_20 & PWRT_64 & MCLR_EN); / Hurst Motor Terminals | MC LV PICDEM Board Connection / |/ Ground Phase -|- G/ Phase Red -|- M1/ Phase Black -|- M2/ Phase W
9、hite -|- M3/ Hall White |- HA/ Hall Brown -|- HB/ Hall Green -|- HCtypedef signed int SFRAC16;#define CLOSED_LOOP / if defined the speed controller will be enabled#define PHASE_ADVANCE / for extended speed ranges this should be defined#define FCY 20000000 / xtal = 5Mhz; PLLx16 - 20 MIPS#define FPWM
10、20000 / 20 kHz, so that no audible noise is present.#define _10MILLISEC 10 / Used as a timeout with no hall effect sensors/ transitions and Forcing steps according to the/ actual position of the motor#define _100MILLISEC 100 / after this time has elapsed, the motor is/ consider stalled and its stopp
11、ed1F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c #define _1000MILLISEC 1000/ These Phase values represent the base Phase value of the sinewave for each/ one of the sectors (each sector is a translation of the hall effect sensors/ reading#define PHASE_ZERO 57344#
12、define PHASE_ONE (PHASE_ZERO + 65536/6) % 65536)#define PHASE_TWO (PHASE_ONE + 65536/6) % 65536)#define PHASE_THREE (PHASE_TWO + 65536/6) % 65536)#define PHASE_FOUR (PHASE_THREE + 65536/6) % 65536)#define PHASE_FIVE (PHASE_FOUR + 65536/6) % 65536)#define MAX_PH_ADV_DEG 40 / This value represents the
13、 maximum allowed phase/ advance in electrical degrees. Set a value from/ 0 to 60. This value will be used to calculate/ phase advance only if PHASE_ADVANCE is defined/ This is the calculation from the required phase advance to the actual/ value to be multiplied by the speed of the motor. So, if PHAS
14、E_ADVANCE is/ enabled, a certain amount of shit angle will be added to the generated/ sine wave, up to a maximum of the specified value on MAX_PH_ADV_DEG. This/ maximum phase shift will be present when the MeasuredSpeed variable is a/ fractional 1.0 (for CW) or -1.0 (for CCW).#define MAX_PH_ADV ( in
15、t )( float )MAX_PH_ADV_DEG / 360.0) * 65536.0)#define HALLA 1 / Connected to RB3#define HALLB 2 / Connected to RB4#define HALLC 4 / Connected to RB5#define CW 0 / Counter Clock Wise direction#define CCW 1 / Clock Wise direction#define SWITCH_S2 (!PORTCbits.RC14) / Push button S2/ Period Calculation/
16、 Period = (TMRClock * 60) / (RPM * Motor_Poles)/ For example/ Motor_Poles = 10/ RPM = 6000 (Max Speed)/ Period = (20,000,000 / 64) * 60) / (6000 * 10) = 312.5/ RPM = 60 (Min Speed)/ Period = (20,000,000 / 64) * 60) / (60 * 10) = 31250#define MINPERIOD 313 / For 6000 max rpm and 10 poles motor#define
17、 MAXPERIOD 31250 / For 60 min rpm and 10 poles motor/ Use this MACRO when using floats to initialize signed 16-bit fractional/ variables#define SFloat_To_SFrac16(Float_Value) (Float_Value 0.0) ? (SFRAC16)(32768 * (Float_Value) - 0.5) : (SFRAC16)(32767 * (Float_Value) + 0.5)void InitADC10( void ); /
18、Initialization of ADC used for Speed Commandvoid InitMCPWM( void ); / Initialization for PWM at 20kHz, Center aligned,/ Complementary mode with 1 us of deadtimevoid InitTMR1( void ); / Initialization for TIMER1 used for speed control/ and motor stalled protectionvoid InitTMR3( void ); / Initializati
19、on for TIMER3 used as a timebase/ for the two input capture channelsvoid InitUserInt( void ); / This function initializes all ports/ (inputs and outputs) for the applicationvoid InitICandCN( void ); / Initializes input captures and change notification,/ used for the hall sensor inputsvoid RunMotor(
20、void ); / This function initializes all variables/ and interrupts used for starting and running/ the motor void StopMotor( void ); / This function clears all flags, and stops anything/ related to motor control, and also disables PWMsvoid SpeedControl( void ); / This function contains all ASM and C o
21、perations/ for doing the PID Control loop for the speedvoid ForceCommutation( void ); / When motor is to slow to generate interrupts/ on halls, this function forces a commutation void ChargeBootstraps( void ); / At the begining of the motor operation, the/ bootstrap caps are charged with this functi
22、on/ Constants used for properly energizing the motor depending on the/ rotors positionint PhaseValues6 _attribute_( far ,section(.const,r)=PHASE_ZERO, PHASE_ONE, PHASE_TWO, PHASE_THREE, PHASE_FOUR, PHASE_FIVE;/ In the sinewave generation algorithm we need an offset to be added to the/ pointer when e
23、nergizing the motor in CCW. This is done to compensate an2F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c / asymetry of the sinewaveint PhaseOffset = 4100;/ Flags used for the applicationstructunsigned MotorRunning :1; / This bit is 1 if motor runningunsigned unus
24、ed :15;Flags;unsigned int Phase; / This variable is incremented by the PWM interrupt/ in order to generate a proper sinewave. Its value/ is incremented by a value of PhaseInc, which/ represents the frequency of the generated sinewavesigned int PhaseInc; / Delta increments of the Phase variable, calc
25、ulated/ in the TIMER1 interrupt (each 1 ms) and used in/ the PWM interrupt (each 50 us)signed int PhaseAdvance; / Used for extending motor speed range. This value/ is added directly to the parameters passed to the/ SVM function (the sine wave generation subroutine) unsigned int HallValue; / This var
26、iable holds the hall sensor input readings unsigned int Sector; / This variables holds present sector value, which is / the rotor position unsigned int LastSector; / This variable holds the last sector value. This / is critical to filter slow slew rate on the Hall/ effect sensors hardwareunsigned in
27、t MotorStalledCounter = 0; / This variable gets incremented each/ 1 ms, and is cleared everytime a new/ sector is detected. Used for/ ForceCommutation and MotorStalled / protection functions/ This array translates the hall state value read from the digital I/O to the/ proper sector. Hall values of 0
28、 or 7 represent illegal values and therefore/ return -1. char SectorTable = -1,4,2,3,0,5,1,-1;unsigned char Current_Direction; / Current mechanical motor direction of/ rotation Calculated in halls interrupts unsigned char Required_Direction; / Required mechanical motor direction of / rotation, will
29、have the same sign as the/ ControlOutput variable from the Speed/ Controller/ Variables containing the Period of half an electrical cycle, which is an/ interrupt each edge of one of the hall sensor input unsigned int PastCapture, ActualCapture, Period;/ Used as a temporal variable to perform a fract
30、ional divide operation in/ assemblySFRAC16 _MINPERIOD = MINPERIOD - 1;SFRAC16 MeasuredSpeed, RefSpeed; / Actual and Desired speeds for the PID/ controller, that will generate the errorSFRAC16 ControlOutput = 0; / Controller output, used as a voltage output,/ use its sign for the required direction/
31、Absolute PID gains used by the controller. Position form implementation of/ a digital PID. See SpeedControl subroutine for detailsSFRAC16 Kp = SFloat_To_SFrac16(0.1); / P GainSFRAC16 Ki = SFloat_To_SFrac16(0.01); / I GainSFRAC16 Kd = SFloat_To_SFrac16(0.000); / D Gain/ Constants used by the PID cont
32、roller, since a MAC operation is used, the/ PID structure is changed (See SpeedControl() Comments) SFRAC16 ControlDifference3 _attribute_(_space_(xmemory), _aligned_(4);SFRAC16 PIDCoefficients3 _attribute_(_space_(ymemory), _aligned_(4);/ Used as a temporal variable to perform a fractional divide op
33、eration in/ assemblySFRAC16 _MAX_PH_ADV = MAX_PH_ADV; /* Function: void _attribute_(_interrupt_) _T1Interrupt (void)PreCondition: The motor is running and is generating hall effect sensors interrupts. Also, the actual direction of the motor used in this interrupt is assumed to be previously calculat
34、ed.3F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c Input: None.Output: None.Side Effects: None.Overview: In this ISR the Period, Phase Increment and MeasuredSpeed are calculated based on the input capture of one of the halls. The speed controller is also called i
35、n this ISR to generate a new output voltage (ControlOutput). ThePhase Advance is calculated based on the maximum allowed phase advance (MAX_PH_ADV) and the actual speed of the motor. The last thing done in this ISR is the forced commutation, which happens each time the motor doesnt generate a new ha
36、ll interrupt after a programmed period of time. If the timeout for generating hall ISR is too much (i.e. 100 ms) the motor is then stopped.Note: The MeasuredSpeed Calculation is made in assembly to take advantage of the signed fractional division.void _attribute_(interrupt, no_auto_psv) _T1Interrupt
37、 (void ) IFS0bits.T1IF = 0;Period = ActualCapture - PastCapture; / This is an UNsigned substraction/ to get the Period between one/ hall effect sensor transition/ These operations limit the Period value to a range from 60 to 6000 rpmif (Period ( unsigned int )MAXPERIOD) / MAXPERIOD or 60 rpm Period
38、= MAXPERIOD;/ PhaseInc is a value added to the Phase variable to generate the sine/ voltages. 1 electrical degree corresponds to a PhaseInc value of 184,/ since the pointer to the sine table is a 16bit value, where 360 Elec/ Degrees represents 65535 in the pointer./ _builtin_divud(Long Value, Int Va
39、lue) is a function of the compiler/ to do Long over Integer divisions.PhaseInc = _builtin_divud(512000UL, Period); / Phase increment is used/ by the PWM isr (SVM)/ This subroutine in assembly calculates the MeasuredSpeed using/ fractional division. These operations in assembly perform the following/
40、 formula:/ MINPERIOD (in fractional)/ MeasuredSpeed = / Period (in fractional)/ int divr;_asm_ volatile (repeat #17ntdivf %1,%2nt: /* output */ =a(divr): /* input */ r(_MINPERIOD),e(Period);MeasuredSpeed = divr;/ MeasuredSpeed sign adjustment based on current motor direction of/ rotationif (Current_
41、Direction = CCW)MeasuredSpeed = -MeasuredSpeed;/ The following values represent the MeasuredSpeed values from the/ previous operations:/ CONDITION RPM SFRAC16 SINT HEX/ Max Speed CW - 6000 RPM - 0.996805 - 32663 - 0 x7F97/ Min Speed CW - 60 RPM - 0.009984 - 327 - 0 x0147/ Min Speed CCW - -60 RPM - -
42、0.009984 - -327 - 0 xFEB9/ Max Speed CCW - -6000 RPM - -0.996805 - -32663 - 0 x8069SpeedControl(); / Speed PID controller is called here. It will use/ MeasuredSpeed, RefSpeed, some buffers and will generate/ the new ControlOutput, which represents a new amplitude/ of the sinewave that will be genera
43、ted by the SVM/ subroutine.4F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c #ifdef PHASE_ADVANCE/ Calculate Phase Advance Based on Actual Speed and MAX_PH_ADV define/ The following assembly instruction perform the following formula/ using fractional multiplication
44、:/ PhaseAdvance = MAX_PH_ADV * MeasuredSpeed/#if !defined(_C30_VERSION_) | (_C30_VERSION_ = _1000MILLISEC)StopMotor(); / Stop motor is no hall changes have occured in/ specified timeoutreturn ;Function: void _attribute_(_interrupt_) _CNInterrupt (void) PreCondition: The inputs of the hall effect sen
45、sors should have low pass filters. A simple RC network works.Input: None.Output: None.Side Effects: None.Overview: This interrupt represent Hall A ISR. Hall A - RB3 - CN5. This is generated by the input change notification CN5.The purpose of this ISR is to Calculate the actual mechanical direction o
46、f rotation of the motor, and to adjust the Phase variable depending on the sector the rotor is in.Note 1: The sector is validated in order to avoid any spurious interrupt due to a slow slew rate on the halls inputs due to hardware filtering.Note 2: For Phase adjustment in CCW, an offset is added to
47、compensate non-symetries in the sine table used.*/void _attribute_(interrupt, no_auto_psv) _CNInterrupt (void )IFS0bits.CNIF = 0; / Clear interrupt flagHallValue = ( unsigned int )(PORTB 3) & 0 x0007); / Read halls Sector = SectorTableHallValue; / Get Sector from table/ This MUST be done for getting
48、 around the HW slow rate5F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c if (Sector != LastSector)/ Since a new sector is detected, clear variable that would stop/ the motor if stalled. MotorStalledCounter = 0;/ Motor current direction is computed based on Sector
49、if (Sector = 5) | (Sector = 2) Current_Direction = CCW;elseCurrent_Direction = CW;/ Motor commutation is actually based on the required direction, not/ the current dir. This allows driving the motor in four quadrants if (Required_Direction = CW)Phase = PhaseValuesSector; else / For CCW an offset mus
50、t be added to compensate difference in / symmetry of the sine table used for CW and CCW Phase = PhaseValues(Sector + 3) % 6 + PhaseOffset;LastSector = Sector; / Update last sector return ; /* Function: void _attribute_(_interrupt_) _IC7Interrupt (void) PreCondition: The inputs of the hall effect sen
51、sors should have low pass filters. A simple RC network works.Input: None. Output: None.Side Effects: None.Overview: This interrupt represent Hall B ISR. Hall B - RB4 - IC7. This is generated by the input Capture Channel IC7.The purpose of this ISR is to Calculate the actual Period between hall effec
52、t sensor transitions, calculate the actual mechanical direction of rotation of the motor, and also to adjust the Phase variable depending on the sector the rotor is in.Note 1: The sector is validated in order to avoid any spurious interrupt due to a slow slew rate on the halls inputs due to hardware
53、 filtering.Note 2: For Phase adjustment in CCW, an offset is added to compensate non-symetries in the sine table used.void _attribute_(interrupt, no_auto_psv) _IC7Interrupt (void )IFS1bits.IC7IF = 0; / Cleat interrupt flagHallValue = ( unsigned int )(PORTB 3) & 0 x0007); / Read halls Sector = Sector
54、TableHallValue; / Get Sector from table / This MUST be done for getting around the HW slow rate if (Sector != LastSector)/ Calculate Hall period corresponding to half an electrical cyclePastCapture = ActualCapture;ActualCapture = IC7BUF;IC7BUF;IC7BUF;IC7BUF;/ Since a new sector is detected, clear va
55、riable that would stop / the motor if stalled.MotorStalledCounter = 0;/ Motor current direction is computed based on Sector if (Sector = 3) | (Sector = 0)6F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.c Current_Direction = CCW;elseCurrent_Direction = CW;/ Motor co
56、mmutation is actually based on the required direction, not / the current dir. This allows driving the motor in four quadrants if (Required_Direction = CW)Phase = PhaseValuesSector;else/ For CCW an offset must be added to compensate difference in / symmetry of the sine table used for CW and CCW Phase
57、 = PhaseValues(Sector + 3) % 6 + PhaseOffset;LastSector = Sector; / Update last sectorreturn ;/*Function: void _attribute_(_interrupt_) _IC8Interrupt (void) PreCondition: The inputs of the hall effect sensors should have low pass filters. A simple RC network works.Input: None.Output: None.Side Effec
58、ts: None.Overview: This interrupt represent Hall C ISR. Hall C - RB5 - IC8. This is generated by the input Capture Channel IC8.The purpose of this ISR is to Calculate the actual mechanical direction of rotation of the motor, and to adjust the Phase variable depending on the sector the rotor is in. N
59、ote 1: The sector is validated in order to avoid any spurious interrupt due to a slow slew rate on the halls inputs due to hardware filtering.Note 2: For Phase adjustment in CCW, an offset is added to compensate non-symetries in the sine table used.*/void _attribute_(interrupt, no_auto_psv) _IC8Inte
60、rrupt (void )IFS1bits.IC8IF = 0; / Cleat interrupt flagHallValue = ( unsigned int )(PORTB 3) & 0 x0007); / Read hallsSector = SectorTableHallValue; / Get Sector from table/ This MUST be done for getting around the HW slow rate if (Sector != LastSector)/ Since a new sector is detected, clear variable
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度智能家庭無線網(wǎng)絡(luò)接入服務(wù)合同3篇
- 二零二五年度生態(tài)保護(hù)區(qū)臨時(shí)設(shè)施搭建勞務(wù)分包合同范例4篇
- 科技創(chuàng)新推動(dòng)安全生產(chǎn)與環(huán)境保護(hù)的進(jìn)步
- 2025版旅行社與旅游保險(xiǎn)產(chǎn)品定制服務(wù)協(xié)議3篇
- 小學(xué)語文課堂中的師生互動(dòng)與心理溝通
- 智能化學(xué)校教學(xué)樓電氣安裝及優(yōu)化策略
- 建立高效且令人滿意的在線客戶服務(wù)團(tuán)隊(duì)
- 激發(fā)小學(xué)生學(xué)習(xí)熱情的夜空之旅天文科普教育活動(dòng)實(shí)施報(bào)告
- 二零二五版美團(tuán)商家糾紛處理及投訴解決協(xié)議4篇
- 2025年度建設(shè)項(xiàng)目環(huán)境影響評價(jià)與環(huán)保驗(yàn)收咨詢合同3篇
- 2025春夏運(yùn)動(dòng)戶外行業(yè)趨勢白皮書
- 《法制宣傳之盜竊罪》課件
- 通信工程單位勞動(dòng)合同
- 高低壓配電柜產(chǎn)品營銷計(jì)劃書
- 租賃車輛退車協(xié)議
- 醫(yī)療護(hù)理技術(shù)操作規(guī)程規(guī)定
- 盤式制動(dòng)器中英文對照外文翻譯文獻(xiàn)
- 社會(huì)系統(tǒng)研究方法的重要原則
- 重癥醫(yī)學(xué)科健康宣教手冊
- 2022版《義務(wù)教育英語課程標(biāo)準(zhǔn)》解讀培訓(xùn)課件
- 五個(gè)帶頭方面談心談話范文三篇
評論
0/150
提交評論