📄 motor_speed.asm
字号:
;caller to the op registers #
;op_0 op_1 + add_0 add_1 #
;--> op_0 op_1 #
;###############################################################
ADD16_16BIT:
PUSH ACC
CLR C
MOV R5,ADD_0
MOV A,OP_0
ADDC A,R5 ;low byte first
MOV OP_0,A
MOV R5,ADD_1
MOV A,OP_1
ADDC A,R5 ;high byte + carry
MOV OP_1,A
POP ACC
RET
;-------------------------------------------------
;multiply the 32 bit op with the 16 value
;supplied
;op_0 op_1 op_2 op_3 * mul_16_0 mul_16_1 -->
;op_0 op_1 op_2 op_3
;-------------------------------------------------
MUL32_16BIT:
clr a
mov tmp_0,a
mov tmp_1,a
mov tmp_2,a
mov tmp_3,a
;generate the lowest byte of the result
mov a,OP_0
mov b,a
mov a,MUL_16_0
mul ab
;low-order result
mov tmp_0,a
;high_order result
mov a,b
mov tmp_1,a
;now generate the nest higher order byte
mov a,OP_1
mov b,a
mov a,MUL_16_0
mul ab
mov r0,a
mov a,tmp_1
add a,r0 ;low-order result
;save
mov tmp_1,a
mov a,tmp_2
mov r0,a
mov a,b ;get high-order result
addc a,r0 ;include carry from previous operation
mov tmp_2,a ;save
jnc mul_loop1
;propagate carry into tmp_3
mov a,tmp_3
inc a
mov tmp_3,a
mul_loop1:
mov a,OP_0
mov b,a
mov a,MUL_16_1
mul ab
mov r0,a ;low-order result
;save
mov a,tmp_1 ;get high-order result
add a,r0 ;include carry from previous operation
;save
mov tmp_1,a
mov a,tmp_2
mov r0,a
mov a,b
addc a,r0
mov tmp_2,a
jnc mul_loop2
;propagate carry into tmp_3
mov a,tmp_3
inc a
mov tmp_3,a
mul_loop2:
;now start working on the 3rd byte
mov a,OP_2
mov b,a
mov a,MUL_16_0
mul ab
mov r0,a ;low-order result
;save
mov a,tmp_2 ;get high-order result
add a,r0 ;include carry from previous operation
;save
mov tmp_2,a
mov a,tmp_3
mov r0,a
mov a,b
addc a,r0
mov tmp_3,a
; now the other half
mov a,OP_1
mov b,a
mov a,MUL_16_1
mul ab
mov r0,a ;low-order result
;save
mov a,tmp_2 ;get high-order result
add a,r0 ;include carry from previous operation
;save
mov tmp_2,a
mov a,tmp_3
mov r0,a
mov a,b
addc a,r0
mov tmp_3,a
; now finish off the highest order byte
mov a,OP_3
mov b,a
mov a,MUL_16_0
mul ab
mov r0,a ;low-order result
;save
mov a,tmp_3 ;get high-order result
add a,r0 ;include carry from previous operation
;save
mov tmp_3,a
; forget about the high-order result,this
mov a,OP_2
mov b,a
mov a,MUL_16_1
mul ab
mov r0,a ;low-order result
;save
mov a,tmp_3 ;get high-order result
add a,r0 ;include carry from previous operation
;save
mov tmp_3,a
; now we are all done, move the tmp values back into op
mov OP_0,tmp_0
mov OP_1,tmp_1
mov OP_2,tmp_2
mov OP_3,tmp_3
ret
;--------------------------------------------------
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;this divides the 32 bit op register by @
;the value supplied @
;op_0 op_1 op_2 op_3 / div_16_0 div_16_1 --> @
;op_0 op_1 op_2 op_3 remainder --> r6 r7 @
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DIV32_16BIT:
MOV R7,#0
MOV R6,#0 ;zero out partial remainder
CLR A
MOV tmp_0,A
MOV tmp_1,A
MOV tmp_2,A
MOV tmp_3,A
MOV R1,DIV_16_1
MOV R0,DIV_16_0 ;load divisor
MOV R5,#32 ;loop count
;this begins the loop
DIV_LOOP1:
CALL SHIFT_D ;shift the dividend and return msb in c
MOV A,R6 ;shift carry into lsb of partial remainder
RLC A
MOV R6,A
MOV A,R7
RLC A
MOV R7,A
;now test to see if r7:r6 >= r1:r0
JC CAN_SUB1 ;carry out of r7 shift means r7:r6> r1:r0
CLR C
MOV A,R7 ;subtract r1 from r7 to see if r1 < r7
SUBB A,R1 ;a = r7 -r1 ,carry set if r7 < r1
JC CANT_SUB1
;at this point r7>r1 or r7=r1
JNZ CAN_SUB1 ;jump if r7>r1
;if r7 = r1, test for r6>=r0
CLR C
MOV A,R6
SUBB A,R0 ;a=r6-r0,carry set if r6 < r0
JC CANT_SUB1
CAN_SUB1:
;subtract the divisor from the partial remainder
CLR C
MOV A,R6
SUBB A,R0 ;a=r6-r0
MOV R6,A
MOV A,R7
SUBB A,R1 ;a= r7-r1-borrow
MOV R7,A
SETB C ;shift a 1 into the quotient
JMP QUOT1
CANT_SUB1:
;shift a 0 into the quotient
CLR C
QUOT1:
;shift the carry bit into the quotient
CALL SHIFT_Q1
;test for competion
DJNZ R5,DIV_LOOP1
;now we are all done, move the tmp values baDIR into op
MOV OP_0,tmp_0
MOV OP_1,tmp_1
MOV OP_2,tmp_2
MOV OP_3,tmp_3
RET
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;shift the dividend one bit to the left @
;and return the msb in c @
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SHIFT_D:
CLR C
MOV A,OP_0
RLC A
MOV OP_0,A
MOV A,OP_1
RLC A
MOV OP_1,A
MOV A,OP_2
RLC A
MOV OP_2,A
MOV A,OP_3
RLC A
MOV OP_3,A
RET
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;shift the quotent one bit to the left @
;and shift the c into lsb @
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SHIFT_Q1: MOV A,tmp_0
RLC A
MOV tmp_0,A
MOV A,tmp_1
RLC A
MOV tmp_1,A
MOV A,tmp_2
RLC A
MOV tmp_2,A
MOV A,tmp_3
RLC A
MOV tmp_3,A
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------------------------------
D10MS:
MOV TIME, #01H
CALL DELAY1
RET
;-----------------------------------------------------------------------------------------------
D50MS: MOV TIME, #01H
CALL DELAY
RET
;-----------------------------------------------------------------------------------------------
D100MS:
MOV TIME, #02H
CALL DELAY
RET
;-----------------------------------------------------------------------------------------------
D200MS:
MOV TIME, #04H
CALL DELAY
RET
;-----------------------------------------------------------------------------------------------
D300MS: MOV TIME, #06H
CALL DELAY
RET
;-----------------------------------------------------------------------------------------------
D500MS: MOV TIME, #0AH
CALL DELAY
RET
;-----------------------------------------------------------------------------------------------
D1S: MOV TIME, #14H
CALL DELAY
RET
;-----------------------------------------------------------------------------------------------
DELAY:
MOV R5, TIME
CLR TR0
CLR TF0
DELAY_H1: MOV TH0, #HIGH(65536-50000)
MOV TL0, #LOW(65536-50000)
SETB TR0
WAIT: JBC TF0, OUT_TIME
JMP WAIT
OUT_TIME: DJNZ R5, DELAY_H1
CLR TR0
RET
;-----------------------------------------------------------------------
DELAY1:
MOV R5, TIME
CLR TR0
CLR TF0
DELAY_H2: MOV TH0, #HIGH(65536-10000)
MOV TL0, #LOW(65536-10000)
SETB TR0
WAIT1: JBC TF0, ONT_TIME
JMP WAIT1
ONT_TIME: DJNZ R5, DELAY_H2
CLR TR0
RET
;-----------------------------------------------------------------------------------------------
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -