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

📄 open_bldchalic.asm

📁 This program controls a BLDC motor in closed loop using PIC18Fxx31 devices. Hardware used is PICDE
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;-----------------------------------------------------------------
;This program controls a BLDC motor using PIC18Fxx31 devices. 
;Hardware used is PICDEM MC development board 
;Hall sensors are used for commutation. Hall sensors sre connected to Input Capture pins 1,2,3 
;-----------------------------------------------------------------
;	Author	: Padmaraja Yedamale
;			: Home Appliance Solutions Group
;			: Microchip Technology Inc
; Version 	:1.0
;******************************************************************
;For Fundamentals of brushless DC motors, read app note AN885 from Microchip Technology
;This program is part of app note AN899-BLDC motor control using PIC18Fxx31 MCUs
;-----------------------------------------------------------------
;Description: 
;Up on reset, the LED1 to 4 display the potentiometer R44(REF) level. 
;LEDs will turn on for every 25% of the potentiometer level from counter clockwise(minimum)
;to clockwise(max) position. 
;Potentiometer R44(REF)connected to ADChannel AN1 is used for varying the speed.
;Switch SW1 is used for toggling between RUN and STOP  
;Switch SW2 is used for toggling between FORWARD and REVERSE direction of rotation
;Study blinking LED1 indicates a overcurrent fault condition 
;Study blinking LED2 indicates a overvoltage fault condition(based on Jumper JP5 setting either 200V or 400V.
;Study blinking LED1 indicates a overtemparature fault condition 
;Pressing any of the switches clear the fault.

;--------------------------------------------------
	include 	"BLDC_DATA.inc"		;Include file, all compile time options are defined in this file

;Include following files into the project
;This file(open_bldcHalIC.asm)
;motor_com1.asm
;BLDC_DATA.inc
;Apropriate linker script file
;--------------------------------------------------
;Setting configuration bits
	LIST p=18f4431,f=INHX32

	__CONFIG _CONFIG1H, 0x02 ;_OSC_HS_1H &_FCMEN_OFF_1H&_IESO_OFF_1H
	__CONFIG _CONFIG2L, 0x0E ;_PWRTEN_ON_2L & _BOREN_ON_2L & _BORV_20_2L  
	__CONFIG _CONFIG2H, 0x1E ;_WDTEN_OFF_2H
	__CONFIG _CONFIG3L, 0x3C ;0x24 _PWMPIN_OFF_3L & _LPOL_HIGH_3L & _HPOL_HIGH_3L & _GPTREN_ON_3L
	__CONFIG _CONFIG3H, 0x9D ;_FLTAMX_RC1_3H & _PWM4MX_RB5_3H
	__CONFIG _CONFIG4L, 0x01 
	__CONFIG _CONFIG5L, 0x0F 
	__CONFIG _CONFIG5H, 0xC0  
	__CONFIG _CONFIG6L, 0x0F 
	__CONFIG _CONFIG6H, 0xE0 
	__CONFIG _CONFIG7L, 0x0F 
	__CONFIG _CONFIG7H, 0x40    
;--------------------------------------------------
#define		HALL_60_DEGREE			
;Bodine motor has Hall sensor signals @ 60 degrees phase shift to each other 	
;#define		HALL_120_DEGREE	
;HURST motor has Hall sensor signals @ 120 degrees phase shift to each other 	

#define	SEND_TO_HYPERTERMINAL	
;Few Key parameters sent to hyper terminal at fixed interval, for debug purpose


#define	CYCLE_COUNT_MAXH	0x4E
#define	CYCLE_COUNT_MAXL	0x20
#define	MAX_FLTA_COUNT	.20				;Over current fault
#define	MAX_FLTB_COUNT	.25				;Over voltage fault
#define	MAX_HEATSINKTEMP	.180		;;Over Temparature fault

;----------------------------------------------
;FLAGS bits
#define	HALL_FLAG		0
#define	FLAG_FAULT		1
#define	PARAM_DISPLAY 	2
#define CALC_PWM		6

;FLAGS1 bits
#define	DEBOUNCE	0
#define	KEY_RS		1
#define	KEY_FR		2
#define	KEY_PRESSED 3
#define	RUN_STOP 4
#define	FWD_REV	5

;FLT_FLAGS bits
#define	OCUR	0
#define	OVOLT	1
#define	OTEMP	2


;Keys parameters
#define KEY_PORT PORTD
#define RUN_STOP_KEY 6
#define FWD_REV_KEY 7
#define DEBOUNCE_COUNT 0x4F
;Delay parameters
#define	DELAY_COUNT1	0x1F
#define	DELAY_COUNT2	0xFF
;LED parameters
#define LED_PORT PORTD
#define RUN_STOP_LED 1
#define FWD_REV_LED 2

#define	LED1	PORTD,0
#define	LED2	PORTD,1
#define	LED3	PORTD,2
#define	LED4	PORTC,0

;******************************************************************
;Routines used for displaying few key parameters 
;on the screen using Hyper terminal(serial port), refer to the motor_com1.asm
	extern		INITIALIZE_SERIAL_PORT
	extern		WELCOME_MESSAGE
	extern		DISPLAY_PARAMETERS
;----------------------------------------------------------------
	extern	DISPLAY_SPEED_REF	
	extern	DISPLAY_SPEED_ACTH	
	extern	DISPLAY_SPEED_ACTL	
	extern	DISPLAY_CURRENT_Iu	
	extern	DISPLAY_CURRENT_Iv	
	extern	DISPLAY_CURRENT_Iw	
	extern	DISPLAY_MISC_PARAH
	extern	DISPLAY_MISC_PARAL
	extern	DISPLAY_MISC_PARA2H
	extern	DISPLAY_MISC_PARA2L
;******************************************************************
BLDC_MOTOR_CONTROL	UDATA_ACS	;Variable definition in RAM, access bank

TEMP			res	1			;Temp registers
TEMP1			res	1
SPEED_REFH			res	1		;Speed referance read from ADC
SPEED_REFL			res	1
FLAGS				res	1		;Flag bits for vaious status indications
FLAGS1				res	1
DEBOUNCE_COUNTER	res	1		;Debounce Counter for toggle switches
COUNTER			res	1			; Counter for Delay routine
COUNTER1			res	1

POSITION_TABLE_FWD	res	8		;Sequence table for forward direction 
POSITION_TABLE_REV	res	8		;reverse direction

CURRENT_UH			res	1		;Current Phase H or DC bus current value stored 
CURRENT_UL			res	1
CURRENT_VH			res	1
CURRENT_VL			res	1
HEATSINK_TEMPH		res	1		;Heatsink temparature
HEATSINK_TEMPL		res	1

PDC_TEMPH			res	1
PDC_TEMPL			res	1

CYCLE_COUNTH		res	1		;PWM cycle count for various purposes	
CYCLE_COUNTL		res	1
FAULTA_COUNT		res	1		;FLTA count in cycle by cycle mode
FAULTB_COUNT		res	1		;FLTB count in cycle by cycle mode
PWM_CYCLE_COUNT		res	1		;PWM count for monitoring Fault window
FLT_FLAGS			res	1		;Flgs indicating faults
;----------------------------------------------------------------
STARTUP	code 0x00
	goto	Start		;Reset Vector address 
	
	CODE	0x08
	goto	ISR_HIGH	;Higher priority ISR at 0x0008

PRG_LOW	CODE	0x018
	goto	ISR_LOW		;Lower priority ISR at 0x0018
	
;****************************************************************
PROG	code
Start
;****************************************************************
	clrf	PDC_TEMPH				;Clear parameters
	clrf	PDC_TEMPL		
	clrf	SPEED_REFH		
	clrf	FLAGS
	clrf	FLAGS1
	
	call	FIRST_ADC_INIT			;Initialize ADC for first test

#ifdef	SEND_TO_HYPERTERMINAL
	call	INITIALIZE_SERIAL_PORT	;If serial communication is enabled, the serial port is initialized to communicate with hyper terminal
	call	WELCOME_MESSAGE			;dispaly "BLDC motor control"
#endif

WAIT_HERE
	call	LED_ON_OFF				;LED1-4 are turnedON/OFF based on pot R44(REF)level.
	call	KEY_CHECK
	btfss	FLAGS1,KEY_PRESSED
	bra		WAIT_HERE				; A press of SW1 or SW2 will take the control to motor control firmware 

	call	INIT_PERPHERALS			;Initialize ADCs, PWMs and ports for motor control application
	
	bcf		LED1					;OFF all LEDs
	bcf		LED2
	bcf		LED3
	bcf		LED4
	
	clrf	FLAGS
	clrf	FLAGS1
	clrf	FLT_FLAGS
	
	
	bsf		INTCON,PEIE				;Peripheral interrupts enable
	bsf		INTCON,GIE				;Global interrupt enable
;End of initialization 
;-------------------------------------------------------------------------
;Main loop: Control stays in this main loop.
;PWM duty cycle calculation,Checking for key activities 
;and sending parameters on serial port is done in this loop 
;-------------------------------------------------------------------------
MAIN_LOOP
	btfss	FLAGS,CALC_PWM			;Is new Speed referance ready?  
	bra		KEEP_SAME_PWM			;No, No need to update PWM duty cycle
	call	UPDATE_PWM				;Yes, Calculate new PWM duty cycle
	bcf		FLAGS,CALC_PWM			;Clear the flag

KEEP_SAME_PWM	
	call	KEY_CHECK				;Check for key activity
	call	PROCESS_KEY_PRESSED		;Process Key activity, if any

	btfss	ADCON0,GO				;Is ADC go bit set?
	bsf		ADCON0,GO				;Set GO bit for ADC conversion start	

DO_NEXT1
	btfss	FLAGS,PARAM_DISPLAY		;Check is it time for display parameters?
	goto	MAIN_LOOP
#ifdef	SEND_TO_HYPERTERMINAL
	call	DISPLAY_PARAMETERS		;Yes, display the slelcted parameters
#endif
	bcf		FLAGS,PARAM_DISPLAY
	goto	MAIN_LOOP
;--------------------------------------------------------------
;High priority interrupt service routine
;Input Capture 1,2,3, ADC and PWM interrupts are serviced in this 
;--------------------------------------------------------------
ISR_HIGH
	btfsc	PIR3,IC1IF				;Input capture 1, HallA state change?
	bra		HALL_A_HIGH
	btfsc	PIR3,IC2QEIF			;Input capture 2, HallB state change?
	bra		HALL_B_HIGH
	btfsc	PIR3,IC3DRIF			;Input capture 3, HallC state change?
	bra		HALL_C_HIGH

	btfsc	PIR1,ADIF				;ADC convertion over?
	bra		AD_CONV_COMPLETE	

	btfsc	PIR3,PTIF				;PWM interrupt?
	bra		PWM_INTERRUPT
	
	RETFIE	FAST

;******************************************************************
AD_CONV_COMPLETE			;ADC interrupt
	movff	ADRESL,CURRENT_UL	;Sample A = Iu
	movff	ADRESH,CURRENT_UH

	movff	ADRESL,SPEED_REFL	;Sample B = speed ref
	movff	ADRESH,SPEED_REFH

	movff	ADRESL,CURRENT_VL	;Sample C=Iv;Dummy, if all 3 current sensors are installed, 
	movff	ADRESH,CURRENT_VH	;check for phase current V

	movff	ADRESL,HEATSINK_TEMPL	;Sample D = Heatsink temparature
	movff	ADRESH,HEATSINK_TEMPH

	movff	CURRENT_UL,DISPLAY_CURRENT_Iu 	;Display current on hyper terminal
	movff	CURRENT_UH,DISPLAY_CURRENT_Iv  
	movff	HEATSINK_TEMPH,DISPLAY_CURRENT_Iw ;Display Heatsink temparature
	movff	SPEED_REFH,DISPLAY_SPEED_REF 		;Display Speed referance

	bsf		FLAGS,CALC_PWM		
	bcf		PIR1,ADIF		;ADIF flag is cleared for next interrupt
	RETFIE	FAST		

;******************************************************************
HALL_A_HIGH
	call	UPDATE_SEQUENCE		;Update the commutation sequence 
	btg		PORTD,0				;Turn on LED1 accordingly
	bcf		PIR3,IC1IF	
	RETFIE	FAST

HALL_B_HIGH
	call	UPDATE_SEQUENCE		;Update the commutation sequence 
	btg		PORTD,1				;Turn on LED2 accordingly
	bcf		PIR3,IC2QEIF
	RETFIE	FAST


HALL_C_HIGH
	call	UPDATE_SEQUENCE		;Update the commutation sequence 
	btg		PORTD,2				;Turn on LED3 accordingly
	bcf		PIR3,IC3DRIF	
	RETFIE	FAST

;-----------------------------------------
;This routine updates the commutation sequence based on the Hall sensor input state
;Hall sensors are connected to IC1,IC2 and IC3 pins on PORTA<4:2>
;----------------------------------------
UPDATE_SEQUENCE
;init table again
	btfss	FLAGS1,FWD_REV			;Is the command reverse?
	bra		ITS_REVERSE				;Yes, 
	lfsr	0,POSITION_TABLE_FWD	;No forward, Initialize FSR0 to the beginning of the Forward Table
	bsf		PORTC,0					;Turn ON LED4 to indicate Forward direction
	bra		PICK_FROM_TABLE
ITS_REVERSE
	lfsr	0,POSITION_TABLE_REV	;Initialize FSR0 to the beginning of the Reverse Table
	bcf		PORTC,0					;Turn OFF LED4 to indicate Forward direction
;--
PICK_FROM_TABLE
	movf	PORTA,W					;Read Hall input state
	andlw	0x1C					;IC1/IC2/IC3
	rrncf	WREG,W
	rrncf	WREG,W					;Shift Hall states to LSBits 
	movf	PLUSW0,W				;Read the value from table
	movwf	OVDCOND					;Load OVDCOND to allow rewuired PWMs and mask other PWMs
	return

;******************************************************************
ISR_LOW

	RETFIE	FAST		

;******************************************************************
;This routine checks for the Faults. Also sets the flag for parameter display
;Over current (Fault A),Over voltage(FaultB) is initialized in cycle-by-cycle mode. 
;If these faults occur very frequently, the mode is changed to catestriphic mode and PWMs are shut down
;The occurence of these faults are checked every PWM interrupt with a limit and if it exeeds the limit
;in 256 PWM cycles, the mode is changed to catestrophic.
;Heatsink temperature is compared with a pre defined limit and PWMs shut down, if the temp crosses the limit.
;LED1 is blinked at fixed rate, if the Overcurrent fault is detected in catestrophic mode 
;LED2 is blinked at fixed rate, if the Overvoltage fault is detected in catestrophic mode 
;LED3 is blinked at fixed rate, if the Overtemparature is detected 
PWM_INTERRUPT
	incfsz	PWM_CYCLE_COUNT,F
	bra		CHECK_FOR_FAULTS
	clrf	FAULTA_COUNT
	clrf	FAULTB_COUNT
	bra		CHECK_PARAMETER_DISPLAY	
	
CHECK_FOR_FAULTS
	btfss	FLTCONFIG,FLTAS
	bra		CHECK_FLTB
	incf	FAULTA_COUNT,F
	movlw	MAX_FLTA_COUNT
	cpfsgt	FAULTA_COUNT
	bra		CHECK_FLTB
	bcf		FLTCONFIG,FLTAMOD
	bsf		FLT_FLAGS,OCUR
CHECK_FLTB
	btfss	FLTCONFIG,FLTBS
	bra		CHECK_HEATSINK_TEMP
	incf	FAULTB_COUNT,F
	movlw	MAX_FLTB_COUNT
	cpfsgt	FAULTB_COUNT
	bra		CHECK_HEATSINK_TEMP
	bcf		FLTCONFIG,FLTBMOD
	bsf		FLT_FLAGS,OVOLT
CHECK_HEATSINK_TEMP
	movlw	MAX_HEATSINKTEMP
	cpfsgt	HEATSINK_TEMPH
	bra		CHECK_PARAMETER_DISPLAY		
	call	STOP_MOTOR
	bsf		FLT_FLAGS,OTEMP
;----------------------------------
CHECK_PARAMETER_DISPLAY
	movlw	CYCLE_COUNT_MAXH
	cpfseq	CYCLE_COUNTH
	bra		NOT_YET_THERE
	movlw	CYCLE_COUNT_MAXL
	cpfsgt	CYCLE_COUNTL
	bra		NOT_YET_THERE
	bsf		FLAGS,PARAM_DISPLAY
	clrf	CYCLE_COUNTH
	clrf	CYCLE_COUNTL
	btfsc	FLT_FLAGS,OCUR
	btg		LED1
	btfsc	FLT_FLAGS,OVOLT
	btg		LED2
	btfsc	FLT_FLAGS,OTEMP
	btg		LED3
	bcf		PIR3,PTIF
	retfie	FAST
NOT_YET_THERE
	incfsz	CYCLE_COUNTL,F
	retfie	FAST
	incf	CYCLE_COUNTH,F
	bcf		PIR3,PTIF
	retfie	FAST

;******************************************************************
;This routine calcuclates the PWM duty cycle based on the speed referance input from potentiometer
;Compile time constant gives a ratio of the DC bus voltage input and motor rated voltage wrt to the speed referance

UPDATE_PWM
	movlw	0x30					;Setting a minimum speed ref of 0x30
	cpfsgt	SPEED_REFH				
	bra		RESET_DUTY_CYCLE		;If the Speed ref<0x30, reset the PWM duty cycle
	
	movlw	0xF8
	cpfslt	SPEED_REFH	
	movwf	SPEED_REFH

;PWM = [(MotorVoltage/DCbus voltage)*(PTPER*4)]*[SpeedRef/255] *16
;16 is the multiplication factor; 
	movf	SPEED_REFH,W	
	mullw	(MAIN_PWM_CONSTANT)		;Compile time calculation, in .inc file
	swapf	PRODL,W
	andlw	0x0F
	movwf	PDC_TEMPL				;Devide the result by 16
	swapf	PRODH,W
	andlw	0xF0
	iorwf	PDC_TEMPL,F
	swapf	PRODH,W
	andlw	0x0F
	movwf	PDC_TEMPH
 
	bsf		PWMCON1,UDIS		;Disable the PWM buffer update

	movf	PDC_TEMPH,W			;Load the  duty cycles to all PWM duty cycle registers 
	movwf	PDC0H
	movwf	PDC1H
	movwf	PDC2H	
	movwf	PDC3H	

	movf	PDC_TEMPL,W
	movwf	PDC0L	
	movwf	PDC1L
	movwf	PDC2L
	movwf	PDC3L

	bcf		PWMCON1,UDIS	;Enable the PWM buffer update

	RETURN



;---------------------------------------------
RESET_DUTY_CYCLE
	clrf	PDC0H			;All duty cycles reset to zero
	clrf	PDC1H
	clrf	PDC2H
	clrf	PDC3H	
	clrf	PDC0L

⌨️ 快捷键说明

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