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

下载本文档

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

文档简介

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

评论

0/150

提交评论