📄 foc_1.asm.bak
字号:
;===========================================================================
; Induction Motor FOC Solution
;===========================================================================
;定标:
;电流定标=实际值*2^10
;电压定标=实际值*2^5
;===========================================================================
;包含头文件
.include f240.h
.include NewMacro.h
.include sinetable.h
.include NewISR.h
;===========================================================================
; 常量定义
;---------------------------------------------------------------------------
;F240初始化所用常量
.data
PWMPRD .set 1000 ;PWM Period=2*1000 ->Tsample=2*1000*50ns=100us (50ns resolution)
DEADTIME .set 0dFFH ;0DFFh=DEAD TIME 5US
;---------------------------------------------------------------------------
ia_OFFSET_K .set 9343-78 ;U相电流零漂对应的转换结果(已包含2.5伏偏移)
ib_OFFSET_K .set 9343-65 ;W相电流零漂对应的转换结果(已包含2.5伏偏移)
;---------------------------------------------------------------------------
;---------------------------------------------------------------------------
;保护的阈值
VDCMAX_K .set 700*32 ;直流母线电压保护的门限=700*2^5
;---------------------------------------------------------------------------
;码盘速度检测所用常量
QEP_num .set 100 ;多少个PWM周期读一次码盘计数器
;----------------------------
;current scale parameter
Kcurrent .word 1383h
*Kcurrent=4096/(512*Ib/Imax)=4096/(512*1.414*In/Imax)
*where,In is the nominal phase current.
*Imax is the max measurable current,which is not necessarily equal to the max phase current
*The true value must be changed to the 8.8f value.
;----------------------------
*************************************************************************************
*the current model constant
*input:isd,isq,n
*output:theta_cm
*************************************************************************************
*-------------------------------------------------------
Kr .set 0eh
*Kr=T/Tr
*where, T is the sampling time
*Tr is the rotor time contant,Tr=Lr/Rr
*Kr should be 4.12f
*-------------------------------------------------------
Kt .set 1b0h
*Kt=1/(Tr*wBase)
*Tr:the rotor time constant,Tr=Lr/Rr
*wBase:the electrical nominal rotor flux speed,wBase=2*pi*fn=2*3.1415926*50=314.15926
*Kt should be 4.12f
*-------------------------------------------------------
K .set 148h
*K converts te (0:2*pi) range into the (0:65536) range
*K=65536/200,where 200 is the sampling time.
*-------------------------------------------------------
;坐标变换常数
SQRT3by2 .set 0ddbh ;SQRT(3)/2 4.12 format
SQRT3inv .set 093dh ;1/sqrt(3) 4.12 format
SR8BIT .word 100h;*used to shift bits 8 right
***********************************************
*PI current regulators parameters
***********************************************
Ki .word 0100h
Kpi .word 0100h
Kcor .word 0100h
***********************************************
*PI speed regulators parameters
***********************************************
Kin .word 35h
Kpin .word 482bh
Kcorn .word 0bh
***********************************************
*Field weakening polynomial coefficients in 8.8f
*isdref=f(nref)
*these values is the MATLAB output polynomial
*MATLAB polyfit parameters,X:nref(pu) 1.1 to 4.0
* Y:idsref(pu)
*condition:keep the phase voltage at 0.9pu
***********************************************
p3 .word -5
p2 .word 56
p1 .word -209
p0 .word 300
************************************************************
*Vsqref and Vdsr limitations
************************************************************
Vmin .set 0ec00h ;4.12f for -1.25 pu
Vmax .set 1400h ;4.12f for 1.25 pu
*************************************************
*isqref limitations
*************************************************
Isqrefmin .set -3277 ;4.12f for -0.8 pu
Isqrefmax .set 3277 ;4.12f for 0.8 pu
*************************************************
*speed calculation constants
*Kspeed=1000h/np=4096/np=4096/(1500*4000*SPEEDSTEP*T/60),in 8.8f
*SPEEDSTEP is the soft counter,and equal to 30
*It is used to convert encoder pulses to a speed value
*speed sampling period = current sampling period*SPEEDSTEP
*************************************************
Kspeed .set 0da7h
SPEEDSTEP .set 30
SHOWINDEX .set 2000
SAVEINDEX .set 10
;-----------;其它常量
RPT_NUM .equ 022bH ;用于延时子程序
MSEC .equ 022cH ;用于延时子程序
S01_K .set 1000 ;S01计数的最大值
;===========================================================================
;变量定义 DP=4,(200H-27FH)128个单元
; DP=5,(280H-2FFH)128个单元
;中断环境保护堆栈全用B2的32个单元,DP=0,(60H-7FH)
;---------------------------------------------------------------------------
*DP=4
TEMP .equ 0200H ;temporary variable (to use in ISR only)
TEMP1 .equ 0201h ;temporary variable
WSGR_tmp .equ 0202H ;仅用于WSGR的设置
RunFlag .equ 0203H ;运行标志,1为运行,0为停机
FailFlag .equ 0204H ;故障标志
;1-过流故障标志
;2-直流母线过压标志
;3-IPM的FO标志
RunEnable .equ 0205H ;允许允许的标志
ia .equ 0207H ;U相电流的A/D转换结果
ib .equ 0208H ;V相电流的A/D转换结果
ic .equ 0209H ;W相电流的A/D转换结果
vcap .equ 020AH ;三电平电容电压A/D转换结果
;f .equ 020BH ;频率,18对应50Hz
t1 .equ 020ch ;SVPWM T1 (see SV PWM references for details)
t2 .equ 020dh ;SVPWM T2 (see SV PWM references for details)
Vref1 .equ 020eh ;扇区计算变量
Vref2 .equ 020fh ;扇区计算变量
Vref3 .equ 0210h ;扇区计算变量
vdc .equ 0211h ;直流母线电压减去2个管压降
taon .equ 0212h ;PWM commutation instant phase 1
tbon .equ 0213h ;PWM commutation instant phase 2
tcon .equ 0214h ;PWM commutation instant phase 3
;fobj .equ 0215H ;目标频率
;TDEC .equ 0216H
;TINC .equ 0217H
Vsalfa_ref .equ 0218h ;alfa-axis 参考电压
Vsbeta_ref .equ 0219h ;beta-axis 参考电压
Vsd_ref .equ 021ah ;d-axis 参考电压
Vsq_ref .equ 021bh ;q-axis 参考电压
X .equ 021ch ;SVPWM 变量
Y .equ 021dh ;SVPWM 变量
Z .equ 021eh ;SVPWM 变量
sector .equ 021fh ;SVPWM 扇区
Angle .equ 0220h ;改进型电压模型中转子磁通位置
SinAngle .equ 0221h ;改进型电压模型中正弦转子磁通位置,4。12
CosAngle .equ 0222h ;改进型电压模型中正弦转子磁通位置,4。12
TEMP2 .equ 0225h ;临时变量
VDC .equ 0226h ;DC voltage pu value
VDCinvT .equ 0227h ;VDCinv*(T/2) (used in SVPWM)
MULTEMP .equ 0229H ;乘积中放16位常量的变量
S01 .equ 022dH ;用于软件计时的变量
IncDecCounter .equ 022eH ;加减速处理计数器
MFT .equ 022fH ;端子排输入值,高8位D8-D15对应于MFT.1-MFT.8
;端子接通COM时,相应位为0。
VIN .equ 0230H ;模拟输入
IIN .equ 0231H ;模拟输入
temp3 .equ 0239H ;用于CLARK变换中的临时变量
isalfa .equ 023aH
isbeta .equ 023bH
kia .equ 0248h
kib .equ 0249h
kvdc .equ 024ah
;直极变换所用的变量
Amplitude .equ 023cH
ValX .equ 023dH
ValY .equ 023eH
partnum .equ 024dh
absx .equ 024eh
absy .equ 024fh
SinTheta .equ 0250h
CosTheta .equ 0251h
arctgxby .equ 0252h
arctgybx .equ 0253h
temprate .equ 0254h
fluxtheta .equ 0255h
theta .equ 0256h
;码盘程序所用的变量
QEPCNT .equ 0258h
spdQEP .equ 0259h
lia .equ 025ah
lib .equ 025bh
lvdc .equ 025ch
**************************************************
*FOC control variables
**************************************************
tmp .equ 025dh ;temporrary variable (to use in ISR only)
tmp1 .equ 025eH ;temporrary variable
n_ref8_8 .equ 025fH ;8.8f reference speed for Field Weakening behavior
option .equ 0260H ;virtual menu option number
daout .equ 0261H ;address of the variable to send to the DAS
daouttmp .equ 0262H ;value to send to the DACs
vSal_ref .equ 0263h ;alfa-axis reference voltage
vSbe_ref .equ 0264h ;beta-axis reference voltage
iSdref .equ 0265h ;d-axis reference current
iSqref .equ 0266h ;q-axis reference current
iSd .equ 0267h ;d-axis current
iSq .equ 0268h ;q-axis current
vSdref .equ 0269h ;d-axis reference voltage
vSqref .equ 026ah ;q-axis reference voltage
epiq .equ 026bh ;q-axis current regulator error
epid .equ 026ch ;d-axis current regulator error
xiq .equ 026dh ;q-axis current regulator integal component
xid .equ 026eh ;d-axis current regulator integal component
n .equ 026fh ;speed
n_ref .equ 0270h ;speed reference
epin .equ 0271h ;speed error(used in speed regulator)
xin .equ 0272h ;speed regulator integral component
Teta_cm1 .equ 0273h ;rotor flux position with curent model
;used only in the communication programe
sinTeta_cm .equ 0274h ;sine rotor flux position with current model,4.12f
cosTeta_cm .equ 0275h ;cosine rotor flux position with current model,4.12f
i_mr .equ 0276h ;magnetizing current(used only in the current model),4.12f
fs .equ 0277h ;rotor flux speed 4.12f
Teta_cm .equ 0279h ;real rotor flux position,output of the current model
serialtmp .equ 027ah ;serial communication temporary variable
da1 .equ 027bh ;DAC siaplaying table offset pfor DAC1
da2 .equ 027ch ;DAC siaplaying table offset pfor DAC2
da3 .equ 027dh ;DAC siaplaying table offset pfor DAC3
da4 .equ 027eh ;DAC siaplaying table offset pfor DAC4
tetaincr .equ 027fh ;variable used in current model
*DP=5
Index .equ 0280h ;pointer used to access sine look-up table
upi .equ 0281h ;PI regulator(currrent an speed)
elpi .equ 0282h ;PI regulator(current and speed)limitation error
encincr .equ 0283h ;encoder pulses increcement
;between two consective sampling period
speedtmp .equ 0284h ;used to accumulate encoder pulses increments
;(to calculate the speed each speed sampling period)
speedstep .equ 0285h ;sampling periods down counter
;to used define speed sampling period
running .equ 0286h ;initializing flag
adr1 .equ 0287h ;show data adress
adr2 .equ 0288h
adr3 .equ 0289h
adr4 .equ 0290h
showindex .equ 0291h ;show circle
num .equ 0292h ;tx counter
saveindex .equ 0293h ;savebuf cunter
.bss BUFFERSTART,1200 ;plot values buffer
*****************************************************
*end variables and constants initializations
*****************************************************
;===========================================================================
.SECT ".VECTORS"
RSVECT B START
INT1 B XINT1FO ;IPM的FO保护
INT2 B GISR2 ;GISR2包括TIMER1和PDPINT
INT3 B PHANTOM ;INTERRUPT LEVEL 3
INT4 B PHANTOM ;INTERRUPT LEVEL 4
INT5 B PHANTOM ;INTERRUPT LEVEL 5
INT6 B PHANTOM ;INTERRUPT LEVEL 6
RESERVED B PHANTOM ;RESERVED
SW_INT8 B PHANTOM ;USER S/W INTERRUPT
SW_INT9 B PHANTOM ;USER S/W INTERRUPT
SW_INT10 B PHANTOM ;USER S/W INTERRUPT
SW_INT11 B PHANTOM ;USER S/W INTERRUPT
SW_INT12 B PHANTOM ;USER S/W INTERRUPT
SW_INT13 B PHANTOM ;USER S/W INTERRUPT
SW_INT14 B PHANTOM ;USER S/W INTERRUPT
SW_INT15 B PHANTOM ;USER S/W INTERRUPT
SW_INT16 B PHANTOM ;USER S/W INTERRUPT
TRAP B PHANTOM ;TRAP VECTOR
NMINT B PHANTOM ;NON-MASKABLE INTERRUPT
EMU_TRAP B PHANTOM ;EMULATOR TRAP
SW_INT20 B PHANTOM ;USER S/W INTERRUPT
SW_INT21 B PHANTOM ;USER S/W INTERRUPT
SW_INT22 B PHANTOM ;USER S/W INTERRUPT
SW_INT23 B PHANTOM ;USER S/W INTERRUPT
;===========================================================================
.text
;======================================================================
;主程序:F240初始化模块开始
;--;初始化ST0,ST1,IMR,IFR
;--------------------------------------------------------------------
START: ;_c_int0
******************************
* Board general settings
******************************
clrc xf
LDP #00H ;0000H-007FH
SETC INTM ;DISABLE INTERRUPTS
splk #0000H,IMR ;MASK ALL CORE INTERRUPTS
LACC IFR ;READ INTERRUPT FLAGS
SACL IFR ;CLEAR ALL INTERRUPT FLAGS
CLRC SXM ;SUPPRESSES SIGN EXTENSION
CLRC OVM ;RESET OVERFLOW MODE
CLRC CNF ;CONFIG BLOCK B0 TO DATA MEM(address FE00H-FFFFH)
SPM 0 ;SUPPRESSES SHIFT ACTION WHILE MULTIPLY
;---------------------------------------------------------------------
; SET UP CLOCK AND PLL
;---------------------------------------------------------------------
INICLOCKPLL: LDP #0E0H ;7000H-707FH
splk #00BBh,CKCR1 ;CLKIN(OSC)=10MHz,CPUCLK=20MHz
splk #00C3h,CKCR0 ;CLKMD=PLL Enable,SYSCLK=CPUCLK/2
splk #4000H,SYSCR ;0100 0000 0000 0000
;CLKOUT PIN =Digital I/O mode
LDP #00E0h ;DP for addresses 7000h-707Fh
lacc SYSSR
and #69FFh ;and 0110100111111111, clear 15,12,10,9
sacl SYSSR ;可靠的初始化,清非法故障标志
;---------------------------------------------------------------------
; DISABLE WATCHDOG AND SET WAIT STATE GENERATOR
;---------------------------------------------------------------------
LDP #00E0h ;DP for addresses 7000h-707Fh
INIRTICR splk #0007H, RTICR ;DISABLE RTI
INIWDCR
LDP #00E0h
splk #006FH,WDCR
splk #05555H,WDKEY ;WDKEY,clear WDCOUNTER
splk #0AAAAH,WDKEY
splk #006Fh, WDCR ;01 101 111b
;Disable WD if VCCP=5V
;Overflow TIME=1s
;________________________________________________________
; 初始化等待状态发生器
LDP #4 ;200H-027FH
splk #4H,WSGR_tmp ;地址信号不可见状态
;SET WAIT STATE GENERATOR FOR:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -