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

📄 snsles2431.asm

📁 汇编语言开发的BLDC驱动程序。 基于PIC18F1330单片机。
💻 ASM
📖 第 1 页 / 共 3 页
字号:
;******************************************************************************
;
; Software License Agreement                                         
;                                                                    
; The software supplied herewith by Microchip Technology             
; Incorporated (the "Company") is intended and supplied to you, the  
; Company抯 customer, for use solely and exclusively on Microchip    
; products. The software is owned by the Company and/or its supplier,
; and is protected under applicable copyright laws. All rights are   
; reserved. Any use in violation of the foregoing restrictions may   
; subject the user to criminal sanctions under applicable laws, as   
; well as to civil liability for the breach of the terms and         
; conditions of this license.                                        
;                                                                     
; THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,  
; WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED  
; TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A       
; PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,  
; IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR         
; CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.       
;
;*********************************************************************************
;-----------------------------------------------------------------
;This program is used for sensorless control of BLDC motor.
;Hardwrae used is PICDEM MC LV board.
;Motor used for testing is NTDynamo Hurst motor. 
;Theory and implementation is expalined in application note AN970
;Motor connections to the board is as follows:
;Motor power connection(White square connector with 4 wires)
;Red : connect to M1 on J9 on the board 
;White : connect to M2 on J9 on the board 
;Black : connect to M3 on J9 on the board 
;Green : connect to G on J9 on the board 
;-----------------------------------------------------------------
;	Author	: Padmaraja Yedamle
;			: Home Appliance Solutions Group
;			: Microchip Technology Inc
;	Date	: June-25-2004
;
;******************************************************************
	include 	"snsrles_DATA.inc"
;--------------------------------------------------
;Configuration register settings

	LIST p=18f2431,f=INHX32

	
	__CONFIG _CONFIG1H,0x06             ;_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_LOW_3L & _HPOL_LOW_3L & _GPTREN_ON_3L
	__CONFIG _CONFIG3H, 0x9D            ;_FLTAMX_RC1_3H & _PWM4MX_RB5_3H
	__CONFIG _CONFIG4L, 0x80 
	__CONFIG _CONFIG5L, 0x0F 
	__CONFIG _CONFIG5H, 0xC0  
	__CONFIG _CONFIG6L, 0x0F 
	__CONFIG _CONFIG6H, 0xE0 
	__CONFIG _CONFIG7L, 0x0F 
	__CONFIG _CONFIG7H, 0x40    
;--------------------------------------------------
;******************************************************************
#define		HURST_MOTOR	
;#define 	PICDEM_MC_LV_SCHEMATIC
;#define		AN970_SCHEMATIC

#define	CYCLE_COUNT_MAXH	0x4E
#define	CYCLE_COUNT_MAXL	0x20
#define	MAX_FLTA_COUNT	0x20
#define	MAX_FLTB_COUNT	.20
#define	MAX_HEATSINKTEMP	.20

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


;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

;FLAGS_SRLS bits
#define	IC_ALOWED	0
#define	SWITCH_SENSORLESS	1
#define PHASE_SHIFT_READY	2
#define	OPEN_SPEED_STEP2	3

#define	PC_COM	7

;Delay parameters
#define	DELAY_COUNT1	0x01
#define	DELAY_COUNT2	0xFF

;#ifdef	PICDEM_MC_LV_SCHEMATIC
;Keys parameters
;#define KEY_PORT PORTC
;#define RUN_STOP_KEY 3
;#define FWD_REV_KEY 4
;#define DEBOUNCE_COUNT 0x8F
;LED parameters
;#define LED_PORT PORTC
;#define RUN_STOP_LED 5
;#define FWD_REV_LED 6

;#define	LED1	PORTC,5
;#define	LED2	PORTC,6
;#define	LED3	PORTC,7

;#define	LED1	TRISC,5
;#define	LED2	TRISC,4
;#define	LED3	TRISC,3
;#endif

;#ifdef	AN970_SCHEMATIC
;Keys parameters
;#define KEY_PORT PORTC 
;#define RUN_STOP_KEY 0
;#define FWD_REV_KEY 2
;#define DEBOUNCE_COUNT 0x8F
;LED parameters
;#define LED_PORT PORTC
;#define RUN_STOP_LED 0
;#define FWD_REV_LED 2

;#define	LED1	PORTC,5
;#define	LED2	PORTC,4
;#define	LED3	PORTC,3
;#endif
;#ifdef	AN970_SCHEMATIC
;Keys parameters
#define KEY_PORT PORTC
#define RUN_STOP_KEY 0
#define FWD_REV_KEY 2
#define DEBOUNCE_COUNT 0x8F
;LED parameters
#define LED_PORT PORTC
#define RUN_STOP_LED 0
#define FWD_REV_LED 2

#define	LED1	PORTC,5
#define	LED2	PORTC,4
#define	LED3	PORTC,3
;#endif

;******************************************************************
;BLDC_MOTOR_CONTROL	UDATA_ACS

HALL_SENSOR_COUNT	res	1
SPEED_REFH			res	1
SPEED_REFL			res	1
FLAGS				res	1
FLAGS1				res	1
FLT_FLAGS			res	1
FLAGS_SRLS			res	1

DEBOUNCE_COUNTER	res	1
COUNTER				res	1
COUNTER1			res	1
COUNTER_SP			res	1
COUNTER_SP1			res	1
RPM_COUNTER			res	1
VELOCITY_READH		res	1
VELOCITY_READL		res	1

SPEED_REF_RPMH		res	1
SPEED_REF_RPML		res	1
SPEED_FEEDBACKH		res	1
SPEED_FEEDBACKL		res	1
SPEED_ERRORH		res	1
SPEED_ERRORL		res	1
ERROR_PWMH			res	1
ERROR_PWML			res	1

POSITION_TABLE_FWD	res	8
POSITION_TABLE_REV	res	8

CURRENT_UH			res	1
CURRENT_UL			res	1

PDC_TEMPH			res	1
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
CYCLE_COUNTL		res	1

FAULTA_COUNT		res	1
FAULTB_COUNT		res	1
PWM_CYCLE_COUNT		res	1
OPEN_HALL			res	1
TABLE_OFFSET		res	1
COUNT_OPEN_LOOPL	res	1
COUNT_OPEN_LOOPH	res	1

DISPLAY_TEMP1		res	1
DISPLAY_TEMP2		res	1

OVDCOND_TEMP		res	1

BEMF_ZC				res	1
LOCK_ROTOR_COUNT	res	1
OVDCOND_TEMP1       res 1  ;;;;;;;;;;;;;;;;;;;;
CX                  res 1
CX1                 res 1
CX2                 res 1
CX3                 res 1
CX4                 res 1
CX5                 res 1
;Time               res 8


T                   res 1
J                   res 1

HIGH_OL_SPEEDH      res 1
HIGH_OL_SPEEDL      res 1


;----------------------------------------------------------------
	org 0x00
	goto	Start		;Reset Vector address 
	
	org	0x0008
	goto	ISR_HIGH	;Higher priority ISR at 0x0008

	org	0x0018
	goto	ISR_LOW		;Lower priority ISR at 0x0018
 
  org    0x30
    CONVERT    addwf   PCL,1

    retlw     0x85
    retlw     0xed  ;800,1

    retlw     0x9e
    retlw     0x57  ;1000,2

    retlw     0xb4
    retlw     0xe0  ;1300,3

    retlw     0xc2
    retlw     0xf6  ;1600,4

    retlw     0xcc
    retlw     0x99  ;1900,5

    retlw     0xd3
    retlw     0x9b  ;2200,6

    retlw     0xd8
    retlw     0xef  ;2500,7

    retlw     0xdd
    retlw     0x1e  ;2800,8

    retlw     0xe0
    retlw     0x7e  ;3100,9

   
    

    

    return


	
;****************************************************************
Start
;**************************************************************** 
	clrf	PDC_TEMPH	
	clrf	PDC_TEMPL		
	clrf	SPEED_REFH		
	clrf	FLAGS
	clrf	FLAGS1
;	call  LED_Display
	call	FIRST_ADC_INIT				;Initialize ADC

WAIT_HERE	
   
	call	LED_BLINK
	call	KEY_CHECK
  
	btfss	FLAGS1,KEY_PRESSED
	bra		WAIT_HERE
  
   
	call	INIT_PERPHERALS				; Initialize all required peripherals(ADC,PWM,IC etc)

	
	bcf		LED1
	bcf		LED2
	bcf		LED3

	movlw	0xA0
	movwf	COUNTER_SP1

	
	clrf	FLAGS
	clrf	FLAGS1
	clrf	FLT_FLAGS
	clrf	FLAGS_SRLS
	movlw	0xF0
	movwf	COUNT_OPEN_LOOPH
	movwf	COUNT_OPEN_LOOPL
		
	bsf		INTCON,PEIE			;Port interrupts enable
	bsf		INTCON,GIE			;Global interrupt enable

	bsf		FLAGS1,FWD_REV
	clrf	OVDCOND_TEMP

    movlw  0x85
    movwf  HIGH_OL_SPEEDH 
    movlw  0xed
    movwf  HIGH_OL_SPEEDL
   
    movlw   0x00
    movwf   CX
    movwf   CX1
    movwf   CX2
    movwf   CX3
    movwf   CX4
    movwf   CX5
;*******************************************************************
;The control loops in this routine
MAIN_LOOP

   
	btfss	FLAGS_SRLS,SWITCH_SENSORLESS	;Is the control ready to switch to sensoless?			
	bra		KEEP_SAME_PWM					;No; ramp up is in progress
	btfss	FLAGS,CALC_PWM					;
	bra		KEEP_SAME_PWM	
	call	UPDATE_PWM						;Reload PWM duty cycles according to the speed requiredA57H
	bcf		FLAGS,CALC_PWM
    goto	MAIN_LOOP

KEEP_SAME_PWM	
	call	KEY_CHECK						;Any key pressed?
	call	PROCESS_KEY_PRESSED				;Service Key routine

	btfsc	FLT_FLAGS,OCUR					;Over current condition?
	call	FAULTA_PROCESS					;Stop motor and stand by

	btfsc	FLAGS,FLAG_FAULT				;If the fault exists, toggle the LEDs
	call	TOGGLE_LEDS
	
	btfss	ADCON0,GO	
	bsf		ADCON0,GO	                    ;Set GO bit for ADC conversion start	
	goto	MAIN_LOOP


;------------------------------------------------------
;------------------------------------------------------
LED_Display
    bcf     TRISC,5
    bsf     LED1
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    bcf     LED1
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    bsf     LED1
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    bcf     LED1
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
    call    Delay_300ms
   
    return

;--------------------------------------------------------------
;Higher priority Interrupt Service routine 
;In this routine, activities on Input capture pins are monitored.
;Back EMF signals are compared with a DC voltage in the hardware. 
; Based on the BEMF states, the switchig sequence is changed.
;--------------------------------------------------------------

ISR_HIGH
	btfsc	FLAGS_SRLS,IC_ALOWED		;Check if the control switched to sensorless
	bra		BYPASS_IC_INT				;No
 	btfsc	PIR3,IC1IF					;Yes, monitor Input capure pin1
	bra		HALL_A_HIGH

BYPASS_IC_INT
	btfsc	PIR1,TMR1IF					;Timer1 overflow interrupt
	bra		TIMER1_INT

	btfsc	PIR1,ADIF					;ADC interrupt
	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	
	bsf		FLAGS,CALC_PWM
	bcf		PIR1,ADIF		            ;ADIF flag is cleared for next interrupt
	RETFIE	FAST		

;******************************************************************
HALL_A_HIGH
   
	bcf		PIR3,IC1IF
    RETFIE	FAST      ;测试

  
	clrf	LOCK_ROTOR_COUNT
	call	CHECK_SEQUENCE		        ;UPDATE_SEQUENCE		;Update the commutation sequence 
	RETFIE	FAST

;-----------------------------------------
;This routine accelerates the motor speed in two levels.
;In first level, the speed is set to 450RPM, open loop.
;In second level it is acceleated to 800RPM openloop.
;Then the BEMF signals are monitored for 256 cycles and the switching sequence is synched with BEMF signals.
;-----------------------------------------
CHECK_SEQUENCE
   
	bsf		FLAGS_SRLS,PC_COM

	btfss	FLAGS_SRLS,OPEN_SPEED_STEP2
	return
	btfsc	FLAGS_SRLS,SWITCH_SENSORLESS
	bra		SENSERLESS_ON
	incfsz	COUNT_OPEN_LOOPH,F
	return
	bsf		FLAGS_SRLS,SWITCH_SENSORLESS
    movlw	b'00111110'
	movwf	DFLTCON                                 ;Digital filters 1:4 on ic1,2,3
	return
SENSERLESS_ON
	bsf		FLAGS_SRLS,IC_ALOWED

	bcf		PIE3,IC1IE		;Cap1 interrupt
	movlw	0xEA
	cpfsgt	VELOCITY_READH,W		;Digital filters are programmed
	bra		LOWER_SPEED_LEVEL1		;at 3 levels
	movlw	b'00111001'
	movwf	DFLTCON
	bsf		LED2
	bra		CONTINUE_SPEED
LOWER_SPEED_LEVEL1
	movlw	0xE4
	cpfsgt	VELOCITY_READH,W
	bra		LOWER_SPEED_LEVEL2
	movlw	b'00111101'
	movwf	DFLTCON
	bcf		LED2
	bra		CONTINUE_SPEED
LOWER_SPEED_LEVEL2
	movlw	b'00111110'
    
	movwf	DFLTCON
	btg		LED2
CONTINUE_SPEED
	movff	VELOCITY_READH,TMR1H
	movff	VELOCITY_READL,TMR1L
	bsf	PIE1,TMR1IE
	btg		LED3
	return

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

⌨️ 快捷键说明

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