我需要计算来自串口的字节数,并在超过300时执行某些操作,但内存地址只能从0到255计数,而我无法计算如何计算超过255
抱歉,如果是一个愚蠢的问题,但我没有asm开发经验... PD:我知道我可以用C编写图片,但我正在编辑以前为其他人工作的软件PIC16F77
COUNT
INCF COUNTRX,1
MOVLW D'255' ;need these value over 300
MOVWF VALUE
MOVF COUNTRX,W
SUBWF VALUE,W
BTFSS STATUS,0
GOTO ITSVALUE
GOTO NOTITSVALUE
答案 0 :(得分:2)
您需要使用额外的寄存器才能计数到255以上。以下代码应该有效:
counter =(COUNTERX2 * 255 + COUNTERX)
COUNT
BTFSC COUNTRX2,0 ; helper variable to hold more significant byte of counter
GOTO OVER255 ; if COUNTERX2 is not zero, it means counter > 255
INCF COUNTRX,1 ; if counter is less than 256, increment it
; COUNTERX is zero at this point only
; if it was earlier 255 and was just incremented
BTFSC STATUS,Z
INCF COUNTRX2,1 ; if COUNTERX is zero, increment COUNTERX2
GOTO NOTITSVALUE
OVER255
INCF COUNTRX,1 ; again increment COUTERX to continue counting
MOVLW D'44' ; = 300 - 256
MOVWF VALUE
MOVF COUNTRX,W
; 44 - COUNTERX, effectively 300 - (COUNTERX2*255 + COUNTERX)
SUBWF VALUE,W
BTFSC STATUS,Z
GOTO ITSVALUE
GOTO NOTITSVALUE
我使用MPLABX Simulator进行了测试,它可以工作。这可能不是最佳的,因为我是汇编程序设计的初学者。
答案 1 :(得分:2)
有多种方法可以做,但是你需要额外的变量来存储大于255的结果:
1)使用额外的第9位,因此最多可以计数511(2 ^ 9 - 1)。
;Data memory definition
SomeVariable SET 1
#define CountRxBit9 SomeVariable, n ;define CountRxBit9 bit vhere n is in range 0..7
;...
;Clear variable
CLRF COUNTRX
BCF CountRxBit9
;...
;increment COUNTRX
INCF COUNTRX,1
BTFSC STATUS, 2 ;Test Zero flag after increment
BSF CountRxBit9 ;Set ninth bit if ZERO is one
2)使用额外的字节,因此最多可以计算65535(2 ^ 16 - 1)。
;Data memory definition
HighCountRxBit SET 1
;...
;Clear variable
CLRF COUNTRX
CLRF HighCountRx
;...
;increment COUNTRX
INCF COUNTRX,1
BTFSC STATUS, 2 ;Test Zero flag after increment
INCF HighCountRx, 1;Increment high byte of counter if ZERO is one