⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modulate.s

📁 一个可以运行在dsPIC30F微处理器上的电机控制代码。该代码以及后续说明完全使读者一看就明白。
💻 S
字号:
.include "p30f6010.inc"

	.global _COR_AL_Modulation

SineTable:
.hword 0,1608,3212,4808,6393,7962,9512,11039                  
.hword 12540,14010,15446,16846,18205,19520,20787,22005      
.hword 23170,24279,25330,26319,27245,28106,28898,29621        
.hword 30273,30852,31357,31785,32138,32413,32610,32728        
.hword 32767,32728,32610,32413,32138,31785,31357,30852        
.hword 30273,29621,28898,28106,27245,26319,25330,24279        
.hword 23170,22005,20787,19520,18205,16846,15446,14010        
.hword 12540,11039,9512,7962,6393,4808,3212,1608
.hword 0,-1608,-3212,-4808,-6393,-7962,-9512,-11039
.hword -12540,-14010,-15446,-16846,-18205,-19520,-20787,-22005
.hword -23170,-24279,-25330,-26319,-27245,-28106,-28898,-29621
.hword -30273,-30852,-31357,-31785,-32138,-32413,-32610,-32728
.hword -32767,-32728,-32610,-32413,-32138,-31785,-31357,-30852
.hword -30273,-29621,-28898,-28106,-27245,-26319,-25330,-24279
.hword -23170,-22005,-20787,-19520,-18205,-16846,-15446,-14010
.hword -12540,-11039,-9512,-7962,-6393,-4808,-3212,-1608
        
.equ Offset_120, 0x5555
;------------------------------------------------------------------------------
; PWM sine wave modulation subroutine
;------------------------------------------------------------------------------
_COR_AL_Modulation:		
		push.d	W0					; Save off working registers
		push.d	W2
		push.d	W4
		push.d	W6
		push.d	W8
		push.d	W10
		
		; The next three instructions initialize the TBLPAG and pointer register
		; for access to the sinewave data in program memory using table reads.
		
		mov		#tblpage(SineTable),W0
		mov		W0,TBLPAG
		mov		#tbloffset(SineTable),W0
		
		; The next block of instructions loads various constants and variables
		; used in the sinewave modulation routine.
		
		mov		_Phase,W1			; Load the sinewave table pointer
		mov		#Offset_120,W4		; This is the value for a 120 degree offset
		mov		_Amplitude,W6		; Load the Amplitude scaling factor
		mov		PTPER,W7		; Load the PWM scaling value
		mov		_Delta_Phase,W8		; Load the Delta_Phase constant that will
									; be added to the table pointer at each
									; interrupt.
		
		; This is the pointer adjustment code.  The Delta_Phase value is added
		; to the sine pointer to move through the sine table.  Then, offsets
		; are added to this pointer to get the phase 2 and phase 2 pointers.
		
		add		W8,W1,W1			; Add the Delta_Phase value to the sine pointer
		add		W1,W4,W2			; Add 120 degree offset value for phase 2
		add		W2,W4,W3			; Add another 120 degree offset for phase 3
		
		; The sine table has 64 entries, so the pointers are right shifted
		; to get a 6-bit pointer value.
				
		lsr		W1,#9,W9			; Shift the phase 1 pointer right to get the upper 6 bits
		sl		W9,#1,W9			; Left shift by one to convert to byte address
		lsr		W2,#9,W10			; Shift the phase 2 pointer right to get the upper 6 bits
		sl		W10,#1,W10			; Left shift by one to convert to byte address
		lsr		W3,#9,W11			; Shift the phase 3 pointer right to get the upper 6 bits
		sl		W11,#1,W11			; Left shift by one to convert to byte address
		
		; Now, the pointer for each phase is added to the base table pointer
		; to get the absolute table address for the lookup value.  The lookup
		; value is then scaled for the correct amplitude and for the range
		; of valid duty cycles.  The next block of instructions calculates
		; the duty cycle for phase 1.  The phase 2 and phase 3 code is the same.
		
		add		W0,W9,W9			; Form the table address for phase 1
		tblrdl 	[W9],W5				; Read the lookup value for phase 1

		mpy		W5*W6,A				; Multiply by the amplitude scaling
		sac 	A,W5				; Store the scaled result
		mpy		W5*W7,A				; Multiply by the PWM scaling factor
		sac		A,W8				; Store the scaled result
		add		W7,W8,W8			; Add the PWM scaling factor to produce 50% offset
		mov		W8,PDC1				; Write the PWM duty cycle
		
		; The next block of code calculates the duty cycle for phase 2.
		
		add		W0,W10,W10			; Form the table address for phase 2
		tblrdl 	[W10],W5			; Read the lookup value for phase 2

		mpy		W5*W6,A				; Multiply by the amplitude scaling
		sac 	A,W5				; Store the scaled result
		mpy		W5*W7,A				; Multiply by the PWM scaling factor
		sac		A,W8				; Store the scaled result
		add		W7,W8,W8			; Add the PWM scaling factor to produce 50% offset
		mov		W8,PDC2				; Write the PWM duty cycle
		
		; The next block of code calculates the duty cycle for phase 3.
		
		add		W0,W11,W11			; Form the table address for phase 3
		tblrdl 	[W11],W5			; Read the lookup value for phase 3

		mpy		W5*W6,A				; Multiply by the amplitude scaling
		sac 	A,W5				; Store the scaled result
		mpy		W5*W7,A				; Multiply by the PWM scaling factor
		sac		A,W8				; Store the scaled result
		add		W7,W8,W8			; Add the PWM scaling factor to produce 50% offset
		mov		W8,PDC3				; Write the PWM duty cycle
		
		; Now, save off the adjusted sinewave table pointer so it can be
		; used during the next iteration of this code.
		
		mov		W1,_Phase
		
		pop.d	W10					; restore working registers
		pop.d	W8
		pop.d	W6
		pop.d	W4
		pop.d	W2
		pop.d	W0
		
		return						; return from the subroutine
		

.end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -