2

我需要计算来自串行端口的字节,并在它们超过 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
4

2 回答 2

2

有几种方法可以做到这一点,但您需要额外的变量来存储大于 255 的结果:

1)使用额外的第九位,因此您最多可以数到 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
于 2013-09-19T10:03:10.220 回答
2

您需要使用额外的寄存器才能计数超过 255。以下代码应该可以工作:

计数器 = (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 模拟器对其进行了测试,它可以工作。它不可能是最佳的,因为我是汇编编程的初学者。

于 2013-09-19T09:05:49.750 回答