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

📄 closebldc.asm

📁 采用模糊控制方式控制直流无刷电机的转速,可使电机的转速稳定性大幅度提高
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;**********************************************************************
;*                                                                    *
;用此软件控制高压直流无刷电机的运转(2007/6/10)
;电机转速低于2500RPM时闭环控制
;*                                                                    *
;**********************************************************************
;    Filename:	    SensBLDC.asm                                      *
;**********************************************************************
;    Files required: p16f877.inc                                      *
;**********************************************************************
;    Notes: Sensored brushless motor control                          *
;**********************************************************************

	list      p=16f877a            ; list directive to define processor
	#include <p16f877a.inc>        ; processor specific variable definitions
	

;**********************************************************************
;*
;* Define variable storage
;*
	CBLOCK 0x20

	ADC		; PWM threshold is ADC result
	adctemp	;当前ADC的值
	adctemp1
	LastSensor	; last read motor sensor data
	DriveWord	; six bit motor drive data
	sensortemp
	flag
	delaycount1
	delaycount2
	timer2ifcount
	timer1ifcount	;用于增加TIMER1的时间
	timer1ifcounttemp
	timer2ifcounttemp
	pulse			;当前转速脉冲数
	prepulse
	pulsetemp1
	pulsetemp2
	pulsetemp3
	pulse0			;规定的转速脉冲数
	e1
	e1temp				;当前转速偏差
	e0				;上一次转速偏差
	e0temp
	z1				;当前规正转速偏差
	z1temp
	z0				;上一次
	z0temp
	de1				;转速偏差的偏差
	de1temp
	de0
	de0temp
	deltw1			;查表偏移值
	deltw2			;delte查表偏移值
	fuzzyflag		;模糊计算标志
	wtemp
	ENDC

;**********************************************************************
;*
;* Define I/O
;*

#define	OffMask		B'00000000'
#define	DrivePort	PORTB
#define DrivePortTris	TRISB
#define	SensorMask	B'00000111'	;RA2,RA3,RA4分别对应SA,SB,SC
#define	SensorPort	PORTA

#define led1 portd,0	;灯1
#define led2 portd,1	;灯2
#define led3 portd,2	;灯3
#define RUNSTOPKEY PORTD,6	;运行停止键
#define directionkey portd,7 ;改变方向键
#define directionbit flag,0	;方向标志位
#define runstopbit flag,1	;运行标志,1代表运行,0代表停止
#define starttime flag,2	;软启动开始
#define errorflag flag,3	;错误标志
#define voltageerrorflag flag,4
#define led4 portc,0	;灯4
#define overcurrent portc,1	;过流检测
#define voltagefault portc,2 ;电压检测

#define overtemp porte,2	;IGBT模块温度检测

#define startadc d'0'	;电机启动时的ADC
#define startendadc d'50'	;电机启动结束时的ADC
#define deltadc d'8'	;启动时增加的ADC
#define refmin d'25'
#define adcmin d'20'
#define deltstopadc d'8'	;停止时减小的ADC

模糊控制参数
#define tmr1hstart d'15'
#define tmr1lstart d'0' 
#define timer1ifcountmax d'5'	;采样时间为500ms
#define prescale d'3'
#define edn6 d'0'	;e的模糊数等级
#define edn5 d'1'
#define edn4 d'2'
#define edn3 d'3'
#define edn2 d'4'
#define edn1 d'5'
#define ed0 d'6'
#define edp1 d'7'
#define edp2 d'8'
#define edp3 d'9'
#define edp4 d'10'
#define edp5 d'11'
#define edp6 d'12'

#define dedn6 d'1'	;de的模糊数等级
#define dedn5 d'14'
#define dedn4 d'27'
#define dedn3 d'40'
#define dedn2 d'53'
#define dedn1 d'66'	
#define ded0 d'79'
#define dedp1 d'92'
#define dedp2 d'105'
#define dedp3 d'118'
#define dedp4 d'131'
#define dedp5 d'144'
#define dedp6 d'157'

#define fodn6 d'168'	;模糊输出等级adc=40
#define fodn55 d'140'
#define fodn5 fodn55-d'2'
#define fodn45 fodn55-d'3'
#define fodn4 fodn55-d'4'
#define fodn35 fodn55-d'5'
#define fodn3 fodn55-d'6'
#define fodn25 fodn55-d'7'
#define fodn2 fodn55-d'8'
#define fodn15 fodn55-d'9'
#define fodn1 fodn55-d'10'
#define fodn05 fodn55-d'11'
#define fod0 d'0'
#define fodp6 d'40'	;模糊输出等级adc=200
#define fodp55 d'12'
#define fodp5 fodp55-d'2'
#define fodp45 fodp55-d'3'
#define fodp4 fodp55-d'4'
#define fodp35 fodp55-d'5'
#define fodp3 fodp55-d'6'
#define fodp25 fodp55-d'7'
#define fodp2 fodp55-d'8'
#define fodp15 fodp55-d'9'
#define fodp1 fodp55-d'10'
#define fodp05 fodp55-d'11'
#define fon6adc d'40'
#define fop6adc d'200'
#define speedoverflag fuzzyflag,0
#define firstflag fuzzyflag,1 

;**********************************************************************
	org	0x000		; startup vector
	nop			; required for ICD operation
	clrf    PCLATH          ; ensure page bits are cleared
  	goto    Initialize      ; go to beginning of program


	ORG     0x004           ; interrupt vector location
;	call closedeltadcsub
	retfie                  ; return from interrupt

;**********************************************************************
;*
;* Initialize I/O ports and peripherals
;*

Initialize
	clrf	DrivePort	; all drivers off

	banksel TRISA
; setup I/O
	MOVLW B'11000000'	;PORTD7,6输入,其他输出
	movwf trisd
	MOVLW B'11111110'	;PORTC0输出,其他输入
	MOVWF TRISC

	clrf	DrivePortTris	; set motor drivers as outputs
	movlw	B'00011111'	; A/D on RA1,  Motor sensors on RA<4:2>
	movwf	TRISA		; 
; setup Timer0
	movlw	B'11010010'	; Timer0: Fosc, 1:4
	movwf	OPTION_REG
; Setup ADC (bank1)
	movlw	B'00001110'	; ADC left justified, AN0 ONLY
	movwf	ADCON1

	banksel	ADCON0
; setup ADC (bank0)
	movlw	B'11000001'	; ADC clock from int RC, AN0, ADC on
	movwf	ADCON0

	bsf	ADCON0,GO	; start ADC
	movlw b'01111111'	;timer2的prescale为1:16,后分频比为1:16,on,
	movwf t2con			;用于判断电机是否堵转
	clrf portd
	clrf flag
	clrf LastSensor	; initialize last sensor reading
	call Commutate	; determine present motor positon
	clrf ADC		; start speed control threshold at zero until first ADC reading
	bsf led1	;灯1亮
	bcf led2
	bcf led3
	bcf led4
mainloop
	btfss runstopkey	;按下runstopkey?
	goto mainrunloop
	btfss directionkey	;按下DIRECTIONKEY
	call directionsub	;是的则DIRECTIONSUB
	goto mainloop
mainrunloop		;运行
	clrf flag
	bcf led2
	bcf led3
	bcf led4
	call readadcsub
	bsf led2
	call commutate
	clrf tmr2
	clrf timer2ifcount
	clrf timer1ifcount
	clrf pulse
	clrf fuzzyflag
	clrf tmr1L	;TIMER1为0
	clrf tmr1h
	clrf t1con
	bsf t1con,tmr1on	;启动TIMER1
	bsf starttime	;电机处于起动时间
	movlw STARTADC	;开始启动时ADC的值
	movwf adc
increaserun	;加速运行(软启动)
	btfss starttime	;起动结束?
	goto normalrun	;正常运转
	BTFSs PIR1,TMR1IF	;TIMER1时间到
	goto normalrun	;正常运转
	bcf pir1,tmr1if	;清除tmr1if
	movlw deltadc
	addwf adc,f
	btfsc status,c	;是否超过255?
	goto adcgeadctemp	;是的则ADC>=ADCTEMP
	movlw startendadc		;没有超过则比较startendadc
	subwf adc,w
	btfss status,c
	goto normalrun	;adc<startendadc
adcgeadctemp		;adc>=startendadc
;	movf adctemp,w
	movlw d'100'
	movwf adc
;	movlw d'130'
;	movwf pulse0
	bcf starttime

	bcf t1con,tmr1on	;关闭TIMER1的运行
;	banksel pie1
;	bsf pie1,0	;开放TMR1中断使能
;	banksel t1con
;	movlw 0c0h
;	movwf intcon
	movlw b'00110000'
	movwf t1con
	movlw tmr1lstart
	movwf TMR1L
	movlw tmr1hstart
	movwf tmr1h
	clrf e1
	clrf e0
	clrf z1
	clrf z0
	clrf de1
	clrf de0
	clrf pulse
	clrf prepulse
	clrf timer1ifcount
	clrf pulsetemp1
	clrf pulsetemp2
	bsf t1con,tmr1on
	bsf firstflag
normalrun
	btfss starttime
	call readadcsub	;运行时读取ADCTEMP
	call runsub
	movf adc,w
	movwf adctemp1
	btfss starttime
	call closedeltadcsub	;闭环控制
	btfss voltagefault	;如电压超过242V或低于170V则电机停止运转
	goto voltagefaultstoprun
	btfsc errorflag
	goto errorstoprun
	btfsc directionkey	;是否停止?
	goto increaserun
	goto stoprun

errorstoprun
	bsf led4
	goto stoprun
voltagefaultstoprun
	bsf led4
	bsf led3
	bsf voltageerrorflag
stoprun
	call norunsub	;减速停止
	bcf led2
	call delaysub
	call delaysub
	goto mainloop


;闭环时DELTADC计算子程序
closedeltadcsub
	btfss pir1,tmr1if	;TIMER1溢出?
	return	;没有溢出则返回
	bcf pir1,tmr1if
	bcf t1con,tmr1on
	movlw tmr1lstart
	movwf tmr1l
	movlw tmr1hstart
	movwf tmr1h
	bsf t1con,tmr1on
	incf timer1ifcount,f	;timer1count=38?
	movlw timer1ifcountmax
	subwf timer1ifcount,w
	btfss status,z
	return
	btfss led2
	goto setled2
	bcf led2
	goto led2start
setled2
	bsf led2
led2start
	btfss firstflag
	goto secondtime
	movf pulse,w
	movwf pulsetemp1
	bcf firstflag
	goto secondtimeend
secondtime
	bcf status,c
	rrf pulsetemp1,f
	bcf status,c
	rrf pulse,f
	movf pulsetemp1,w
	addwf pulse,f
	movf pulse,w
	movwf pulsetemp1
secondtimeend
	movf timer1ifcount,w
	movwf timer1ifcounttemp
	movlw d'255'	;如PULSE=255则不进行闭环控制
	subwf pulse,w
	btfsc status,z
	return

	movf pulse0,w				;该次速度脉冲数计算结束,需要计算DELTADC
	SUBWF PULSE,w
	movwf e1				;e1=pulse-pulse0
	btfss status,c			;e1>=0
	goto e1ltzero			;e1<0则转到E1LTZERO
	btfsc status,z
	goto e1eqzero			;e1=0
	movlw d'2'				;e1=1?
	subwf e1,w
	btfss status,c
	goto e1eqp1				;e1=1
	movlw d'4'				;e1=2?
	subwf e1,w			
	btfss status,c
	goto e1eqp2				;e1=2
	movlw d'6'				;e1=3?
	subwf e1,w
	btfss status,c
	goto e1eqp3				;e1=3
	movlw d'8'				;e1=4?
	subwf e1,w
	btfss status,c
	goto e1eqp4				;e1=4
	movlw d'10'				;e1=5?
	subwf e1,w			
	btfss status,c
	goto e1eqp5				;e1=5
	movlw edp6				;转速差大于30以上
	movwf deltw1						;e1>3(n>30rpm)
	goto e1pend
e1eqzero
	movlw ed0			;转速差为ZERO
	movwf deltw1
	goto e1pend
e1eqp1
	movlw edp1			;转速差为10RPM
	movwf deltw1
	goto e1pend
e1eqp2
	movlw edp2			;转速差为20RPM
	movwf deltw1
	goto e1pend
e1eqp3	
	movlw edp3			;转速差为30RPM
	movwf deltw1
	goto e1pend
e1eqp4
	movlw edp4
	movwf deltw1
	goto e1pend
e1eqp5
	movlw edp5
	movwf deltw1
e1pend
	goto e1end

e1ltzero
	movf pulse,w
	subwf pulse0,w
	movwf e1
	movlw d'2'				;e1=-1?
	subwf e1,w
	btfss status,c
	goto e1eqn1				;e1=-1
	movlw d'4'				;e1=-2?
	subwf e1,w			
	btfss status,c
	goto e1eqn2				;e1=-2
	movlw d'6'				;e1=-3?
	subwf e1,w
	btfss status,c
	goto e1eqn3				;e1=-3
	movlw d'8'				;e1=-4?
	subwf e1,w			
	btfss status,c
	goto e1eqn4				;e1=-4
	movlw d'10'				;e1=-5?
	subwf e1,w
	btfss status,c
	goto e1eqn5				;e1=-3
	movlw edn6				;转速差大于-30以上
	movwf deltw1						;e1>-3(n>30rpm)
	goto e1nend
e1eqn1
	movlw edn1			
	movwf deltw1
	goto e1nend
e1eqn2
	movlw edn2			
	movwf deltw1
	goto e1nend
e1eqn3	
	movlw edn3			
	movwf deltw1
	goto e1nend
e1eqn4
	movlw edn4			
	movwf deltw1
	goto e1nend
e1eqn5	
	movlw edn5			
	movwf deltw1
e1nend
e1end

	movf deltw1,w		;计算转速偏差的偏差
	movwf z1	
	movf z0,w			
	SUBWF z1,w
	movwf de1				;de1=z1-z0
	btfss status,c			;de1>=0
	goto de1ltzero			;de1<0则转到dE1LTZERO
	btfsc status,z
	goto de1eqzero			;de1=dez
	movlw d'1'				;de1=1?
	subwf de1,w
	btfsc status,z
	goto de1eqp1			;de1=1
	movlw d'2'				;de1=2?
	subwf de1,w
	btfsc status,z
	goto de1eqp2			;de1=2
	movlw d'3'				;de1=3?
	subwf de1,w
	btfsc status,z
	goto de1eqp3			;de1=3	
	movlw d'4'				;de1=4?
	subwf de1,w
	btfsc status,z
	goto de1eqp4			;de1=4
	movlw d'5'				;de1=5?
	subwf de1,w
	btfsc status,z
	goto de1eqp5			;de1=5
	movlw dedp6				;de1=dep6
	movwf deltw2			
	goto de1pend
de1eqzero
	movlw ded0			;de1=dez
	movwf deltw2
	goto de1pend
de1eqp1
	movlw dedp1			;de1=1
	movwf deltw2
	goto de1pend
de1eqp2
	movlw dedp2			;de1=2
	movwf deltw2
	goto de1pend
de1eqp3
	movlw dedp3			;de1=3
	movwf deltw2
	goto de1pend
de1eqp4
	movlw dedp4			;de1=4
	movwf deltw2
	goto de1pend
de1eqp5
	movlw dedp5			;de1=5
	movwf deltw2
de1pend
	goto de1end

de1ltzero
	movf z1,w
	subwf z0,w
	movwf de1
	movlw d'1'				;de1=-1?
	subwf de1,w
	btfsc status,z
	goto de1eqn1				;de1=-1
	movlw d'2'				;de1=-2?
	subwf de1,w
	btfsc status,z
	goto de1eqn2				;de1=-2
	movlw d'3'				;de1=-3?
	subwf de1,w
	btfsc status,z
	goto de1eqn3				;de1=-3
	movlw d'4'				;de1=-4?
	subwf de1,w
	btfsc status,z
	goto de1eqn4				;de1=-4

⌨️ 快捷键说明

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