很详细的dspic逆变svpwm程序_第1页
很详细的dspic逆变svpwm程序_第2页
很详细的dspic逆变svpwm程序_第3页
很详细的dspic逆变svpwm程序_第4页
很详细的dspic逆变svpwm程序_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

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

文档简介

1、F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC vl.I.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

2、 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.

3、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 APPLICATI

4、ON.*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 LOS

5、S, 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 N

6、OT 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: sinuso

7、idalBLDC 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 vers

8、ion/ -#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/ -1-/ Ground Phase -|- G/ Phase Red-|- M1/ Phase Black-|- M2/ Phase White-T- M3/ Hal

9、l 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,

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

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

12、(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

13、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/ ena

14、bled, 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 (nt )( float )MAX_PH_

15、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

16、 * 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 /

17、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 AD

18、C used for Speed Commandvoid InitMCPWMvoid ); / 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

19、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

20、 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 operations/ for doing th

21、e 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 p

22、roperly 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 CC

23、W. This is done to compensate an2F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC vl.I.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

24、 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 int

25、errupt (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 senso

26、r 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; / T

27、his 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 value

28、s 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/ Contr

29、olOutput 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/ assemb

30、lySFRAC16 _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 co

31、ntroller. 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

32、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_P

33、H_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_01090

34、8CE003_Sinusoidal_BLDCsourceSinusoidalBLDC vl.I.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 in this ISR to generate a new output vo

35、ltage (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 hall interrupt after a programmed period

36、 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 (void )IFS0bits.T1IF = 0;Period = A

37、ctualCapture - 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 = MAXPERIOD;/ PhaseInc is a value add

38、ed 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/ t

39、o 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)/

40、 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 = -Measur

41、edSpeed;/ 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 - -0.009984 - -327 - 0 xFEB9/ Max Speed CC

42、W - -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 generated by the SVM/ subroutine.4F:dspicCE00

43、3_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC vl.I.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 * Measured

44、Speed/#if !defined(_C30_VERSION_) | (_C30_VERSION_ = _1000MILLISEC)StopMotor(); / Stop motor is no hall changes have occured in/ specified timeout return ;/*Function: void _attribute_(_interrupt_) _CNInterrupt (void) PreCondition: The inputs of the hall effect sensors should have low pass filters. A

45、 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 of rotation of the motor, and to adjus

46、t 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 compensate non-symetries in the sine

47、table used.*void _attribute_(interrupt, no_auto_psv) _CNInterrupt (void )IFS0bits.CNIF = 0; / Clear 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 rate5F:dspicCE003_

48、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 Sectorif (Sector = 5) | (Sector = 2)Current_Di

49、rection = 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/ symm

50、etry 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 netw

51、ork 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 Periodbetween hall effect sensor transitions, calculate the actualmechanical d

52、irection of rotation of the motor, and also toadjust the Phase variable depending on the sector the rotoris 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 offse

53、t is added to compensate non-symetries in the sine table used.*void _attribute_(interrupt, no_auto_psv) _IC7lnterrupt (void )IFS1bits.lC7IF = 0; / Cleat interrupt flagHallValue = ( unsigned int )(PORTB 3) & 0 x0007); / Read hallsSector = SectorTableHallValue; / Get Sector from table/ This MUST be do

54、ne for getting around the HW slow rateif (Sector != LastSector)/ Calculate Hall period corresponding to half an electrical cyclePastCapture = ActualCapture;ActualCapture = IC7BUF;IC7BUF;IC7BUF;IC7BUF;/ Since a new sector is detected, clear variable that would stop/ the motor if stalled.MotorStalledC

55、ounter = 0;/ Motor current direction is computed based on Sectorif (Sector = 3) | (Sector = 0)6F:dspicCE003_Sinusoidal_BLDC_010908CE003_Sinusoidal_BLDCsourceSinusoidalBLDC v1.1.cCurrent_Direction = CCW;elseCurrent_Direction = CW;/ Motor commutation is actually based on the required direction, not/ t

56、he 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

57、; / Update last sectorreturn ;/* *Function: void _attribute_(_interrupt_) _IC8lnterrupt (void) PreCondition: The inputs of the hall effect sensors should have low pass filters. A simple RC network works.Input: None.Output: None.Side Effects: None.Overview: This interrupt represent Hall C ISR. Hall C

58、 - 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.Note 1: The sector is validated in order to avoid any spurious in

59、terrupt 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) _IC8Interrupt (void )IFS1bits.IC8IF = 0; / Cleat interrupt flagHallValue

60、 = ( unsigned int )(PORTB 3) & 0 x0007); / Read hallsSector = SectorTableHallValue; / Get Sector from table/ This MUST be done for getting around the HW slow rateif (Sector != LastSector)/ Since a new sector is detected, clear variable that would stop/ the motor if stalled.MotorStalledCounter = 0;/

温馨提示

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

评论

0/150

提交评论