




已閱讀5頁(yè),還剩21頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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 this 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 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 ANY 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 CONSEQUENTIAL 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* 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 HISTORY:*/-/ 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/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 White -|- 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 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 stopped1F: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#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 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 PHASE_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 (int)(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/ 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 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); / 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); / Initialization 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(void); / This function initializes all variables/ and interrupts used for starting and running/ the motorvoid 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 operations/ 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 commutationvoid ChargeBootstraps(void); / At the begining of the motor operation, the/ bootstrap caps are charged with this function/ 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 energizing 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 unused :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, calculated/ 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 variable holds the hall sensor input readingsunsigned int Sector; / This variables holds present sector value, which is/ the rotor positionunsigned int LastSector; / This variable holds the last sector value. This/ is critical to filter slow slew rate on the Hall/ effect sensors hardwareunsigned int 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 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 interruptsunsigned char Required_Direction; / Required mechanical motor direction of/ rotation, will 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 inputunsigned int PastCapture, ActualCapture, Period;/ Used as a temporal variable to perform a fractional 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/ 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 controller, 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 operation in/ assemblySFRAC16 _MAX_PH_ADV = MAX_PH_ADV;/*Function: void _attribute_(_interrupt_) _T1Interrupt (void)PreCondition: The motor is running and is generating hall effect sensorsinterrupts. Also, the actual direction of the motor usedin this interrupt is assumed to be previously calculated.3F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.cInput: None.Output: None.Side Effects: None.Overview: In this ISR the Period, Phase Increment and MeasuredSpeedare calculated based on the input capture of one of thehalls. The speed controller is also called in this ISRto generate a new output voltage (ControlOutput). ThePhase Advance is calculated based on the maximum allowedphase advance (MAX_PH_ADV) and the actual speed of themotor. The last thing done in this ISR is the forcedcommutation, which happens each time the motor doesntgenerate a new hall interrupt after a programmed periodof 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 takeadvantage of the signed fractional division.*/void _attribute_(interrupt, no_auto_psv) _T1Interrupt (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 rpmPeriod = 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 Value) 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/ 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_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 - 0x7F97/ Min Speed CW - 60 RPM - 0.009984 - 327 - 0x0147/ Min Speed CCW - -60 RPM - -0.009984 - -327 - 0xFEB9/ Max Speed CCW - -6000 RPM - -0.996805 - -32663 - 0x8069SpeedControl(); / 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 generated 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:/ 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 sensors should have low passfilters. 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 actualmechanical direction of rotation of the motor, and to adjustthe Phase variable depending on the sector the rotor is in.Note 1: The sector is validated in order to avoid any spuriousinterrupt due to a slow slew rate on the halls inputs due tohardware filtering.Note 2: For Phase adjustment in CCW, an offset is added tocompensate 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) & 0x0007); / Read hallsSector = SectorTableHallValue; / Get Sector from table/ This MUST be done for getting around the HW slow rate5F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.cif (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 Sectorif (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 quadrantsif (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 CCWPhase = PhaseValues(Sector + 3) % 6 + PhaseOffset;LastSector = Sector; / Update last sectorreturn;/*Function: void _attribute_(_interrupt_) _IC7Interrupt (void)PreCondition: The inputs of the hall effect sensors should have low passfilters. 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 Captu
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 家居空間設(shè)計(jì)中的生活動(dòng)線優(yōu)化考核試卷
- 流動(dòng)小吃轉(zhuǎn)讓合同范本
- 酒店吧臺(tái)員工合同范本
- 吊頂合資協(xié)議合同范本
- 辦公區(qū)域清潔與維護(hù)工作計(jì)劃
- 農(nóng)村污水處理合同
- 企業(yè)增資擴(kuò)股方案及協(xié)議
- 環(huán)境保護(hù)的重要性征文
- 貨物公路運(yùn)輸合同
- 私人公寓樓房產(chǎn)轉(zhuǎn)讓合同
- CSCCP宮頸細(xì)胞病理學(xué)理論考試題
- 臨床試驗(yàn)嚴(yán)重不良事件報(bào)告表(SAE)
- 統(tǒng)編版五年級(jí)語(yǔ)文下冊(cè)第二單元習(xí)作:寫讀后感課件
- 2023年9月新《醫(yī)療器械分類目錄》-自2023年8月1日起施行
- 使用磷化鋁進(jìn)行常規(guī)熏蒸作業(yè)
- 天然氣培訓(xùn)課件
- 教學(xué)課件:《新能源材料技術(shù)》朱繼平
- 人教版部編七年級(jí)下冊(cè)語(yǔ)文必背古詩(shī)文言文
- 2022年七年級(jí)初一數(shù)學(xué)希望杯競(jìng)賽模擬真題含答案27屆
- 自動(dòng)駕駛數(shù)據(jù)安全白皮書
- 工期定額-民用建筑
評(píng)論
0/150
提交評(píng)論