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

📄 clsd_bldchalhal.asm

📁 This program controls a BLDC motor in closed loop using PIC18Fxx31 devices. Hardware used is PICDE
💻 ASM
📖 第 1 页 / 共 3 页
字号:
;-----------------------------------------------------------------
;This program controls a BLDC motor in closed loop 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 
;Timer5 is used to measure speed, between each Hall transition. 
;This actual speed is compared with the set referance speed and the error is amplified using PID algorithm
; and the PWM duty cycle is corrected to get a closed loop operation.  
;-----------------------------------------------------------------
;	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(Clsd_bldcHalHal.asm)
;motor_com1.asm
;p18_math.asm
;pid_main.asm
;BLDC_DATA.inc
;Apropriate linker script file
;--------------------------------------------------
;Setting configuration bits
	LIST p=18f4431,f=INHX32

	include		"P18F4431.inc"
	
	__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, 0x24 ;_PWMPIN_OFF_3L & _LPOL_LOW_3L & _HPOL_LOW_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
#define	MAX_FLTB_COUNT	.40
#define	MAX_HEATSINKTEMP	.240

;----------------------------------------------

;FLAGS bits
#define	HALL_FLAG		0
#define	FLAG_FAULT		1
#define	PARAM_DISPLAY 	2
#define	POSITION_BIT 	3
#define	VELOCITY_READY 	4
#define	NEGATIVE_ERROR 	5
#define CALC_PWM		6
#define	VALID_HALL		7

;#define	FWD_REV		1
;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
#define	FLT		7

;Keys parameters
#define KEY_PORT PORTD
#define RUN_STOP_KEY 6
#define FWD_REV_KEY 7
#define DEBOUNCE_COUNT 0x2F
;Delay parameters
#define	DELAY_COUNT1	0x2F
#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

;PID routines are defined in PID_main.asm
	extern		PID_INIT
	extern		PID_MAIN
	extern		PID_INT
	
;---------------------------------------------------
;Variables defined in PID_main routine

	extern	error0
	extern	error1
	extern	pid_out0
	extern	pid_out1
	extern	pid_out2
	extern	pid_stat1
	extern	kp
	extern	ki
;----------------------------------------------------------------
;Variables used for displaying the parameters on Hyper terminal
	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

	extern	DISPLAY_SFH
	extern	DISPLAY_SFL
	extern	DISPLAY_SRH
	extern	DISPLAY_SRL
	extern	DISPLAY_EIH
	extern	DISPLAY_EIL
	extern	DISPLAY_EOH
	extern	DISPLAY_EOL
	extern	DISPLAY_EOLL

;******************************************************************
BLDC_MOTOR_CONTROL	UDATA_ACS	;Variable definition in RAM, access bank

TEMP			res	1		;Temp registers
TEMP1			res	1
HALL_SENSOR_COUNT	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
COUNTER_SP			res	1
COUNTER_SP1			res	1
RPM_COUNTER			res	1
VELOCITY_READH		res	1		;Velocity registers copy of Timer5 avaraged value
VELOCITY_READL		res	1

SPEED_REF_RPMH		res	1		;Speed ref in RPM	
SPEED_REF_RPML		res	1
SPEED_FEEDBACKH		res	1		;Speed feedback in RPM	
SPEED_FEEDBACKL		res	1
SPEED_ERRORH		res	1		;Speed error in RPM	
SPEED_ERRORL		res	1
ERROR_PWMH			res	1		;PWM error
ERROR_PWML			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		;Temparary registers
PDC_TEMPL			res	1

ARG1H				res	1
ARG1L				res	1
ARG2H				res	1
ARG2L				res	1
RESH				res	1
RESL				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
	
	call	PID_INIT				;Initialize PID parameters

	clrf	FLAGS
	clrf	FLAGS1
	clrf	FLT_FLAGS
	
			
	bsf		INTCON,PEIE	;Port 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 and PID 
;-------------------------------------------------------------------------

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	
	btfsc	FLT_FLAGS,FLT
	call	CHECK_PWM_TICK
	
	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	

	btfss	FLAGS,VELOCITY_READY	;Is velociry reading ready yet?
	goto	DO_NEXT1
	call	CALCULATE_SPEED			;Yes, calcuclate actual speed
	call	CALCULATE_SPEED_ERROR	;and speed error
	bcf		FLAGS,VELOCITY_READY


DO_NEXT1
	movlw	.96			;10 x 16, Kp, Ki & Kd are 8-bit vlaues that cannot exceed 255
	movwf	kp,1		;Enter the PID gains scaled by a factor of 16, max = 255
	movlw	.144  		 ;80 ;10 x 16
	movwf	ki,1

	call	PID_MAIN	;Call PID after setting new PI gains

	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, Timer1 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,TMR1IF					;has T1 overflowed?
	goto	PID_INT	
	
	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		LED1				;Turn on LED1 accordingly
	bcf		PIR3,IC1IF
	btfss	FLAGS,VALID_HALL
	bra		CHECK_HALL_COUNT
	movf	CAP1BUFL,W
	addwf	VELOCITY_READL,F	;Capture Timer5 count and avarage the count 
	movf	CAP1BUFH,W
	addwfc	VELOCITY_READH,F
	bcf		STATUS,C
	rrcf	VELOCITY_READH,F
	rrcf	VELOCITY_READL,F
	bra		CHECK_HALL_COUNT

HALL_B_HIGH
	call	UPDATE_SEQUENCE
	btg		LED2
	bcf		PIR3,IC2QEIF
	btfss	FLAGS,VALID_HALL
	bra		CHECK_HALL_COUNT
	movf	CAP2BUFL,W
	addwf	VELOCITY_READL,F
	movf	CAP2BUFH,W
	addwfc	VELOCITY_READH,F
	bcf		STATUS,C
	rrcf	VELOCITY_READH,F
	rrcf	VELOCITY_READL,F
	bra		CHECK_HALL_COUNT


HALL_C_HIGH
	call	UPDATE_SEQUENCE
	btg		LED3
	bcf		PIR3,IC3DRIF	
	btfss	FLAGS,VALID_HALL
	bra		CHECK_HALL_COUNT
	movf	CAP3BUFL,W
	addwf	VELOCITY_READL,F
	movf	CAP3BUFH,W
	addwfc	VELOCITY_READH,F
	bcf		STATUS,C
	rrcf	VELOCITY_READH,F
	rrcf	VELOCITY_READL,F
	bra		CHECK_HALL_COUNT

CHECK_HALL_COUNT
	bsf		FLAGS,VALID_HALL			;Avarage Timer5 reading every say 12 Hall transitions
	incf	HALL_SENSOR_COUNT,F			;
	btfss	HALL_SENSOR_COUNT,4
	RETFIE	FAST
	clrf	HALL_SENSOR_COUNT
	bsf		FLAGS,VELOCITY_READY
	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

⌨️ 快捷键说明

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