新會員
Joined
: 2006/7/24 19:43 最後登入時間
: 2006/10/31 22:30
Group:
註冊會員
Level : 1 HP : 0 / 18 MP : 2 / 978 EXP : 73
|
這是在Datasheet中看到的範例,看了很久,不知為什麼用這種架構寫,不是不懂如何寫程式,而是不知道為何這麼寫和每個動作的實際作為,有高人能以加以解釋一下嗎,感恩啊。
#include p16F877A.inc
udata 0x20 Dly0 res 1 ;Stores 4 bytes of data for the delay count Dly1 res 1 ;Dly0 is the least significant byte Dly2 res 1 ;while Dly3 is the most significant byte Dly3 res 1
Dly32 MACRO DLY goto $+1 ;delay 2 cycles goto $+1 ;delay total of 4 cycles
;Take the delay value argument from the macro, precalculate ;the required 4 RAM values and load the The RAM values Dly3 ;though Dly0.
BANKSEL Dly3 movlw (DLY-1) & H'FF' movwf Dly0 movlw (DLY-1) >>D'08' & H'FF' movwf Dly1 movlw (DLY-1) >>D'16' & H'FF'
;Bytes are shifted and anded by the assembler to make user ;calculations easier.
movwf Dly2 movlw (DLY-1) >>D'24' & H'FF' ;Call DoDly32 to run the delay loop. movwf Dly3 call DoDly32 ENDM ;End of Macro definition
RST CODE 0x00 ;Reset Vector pagesel TestCode goto TestCode
CODE ;Code starts here TestCode Dly32 D'50000' ;Max 4 billion+ (runs Dly32 Macro, ;1 sec in this case). nop ;ZERO STOPWATCH, put breakpoint here
goto TestCode ;Go back to top of program and ;run the delay again. ;Subroutine, called by the Macro Dly32 (20 Tcy per loop)
DoDly32 movlw H'FF' ;Start with -1 in W addwf Dly0,F ;LSB decrement btfsc STATUS,C ;was the carry flag set? clrw ;If so, 0 is put in W addwf Dly1,F ;Else, we continue. btfsc STATUS,C clrw ;0 in W addwf Dly2,F btfsc STATUS,C clrw ;0 in W addwf Dly3,F btfsc STATUS,C clrw ;0 in W iorwf Dly0,W ;Inclusive-OR all variables iorwf Dly1,W ;together to see if we have reached iorwf Dly2,W ;0 on all of them. iorwf Dly3,W btfss STATUS,Z ;Test if result of Inclusive-OR's is 0 goto DoDly32 ;It was NOT zero, so continue counting retlw 0 ;It WAS zero, so exit this subroutine.
END
發表於: 2006/10/31 22:45
|