robban
Senior Member
Offline
Posts: 265
Thank You
-Given: 34
-Receive: 38
Warrior
|
|
« on: June 18, 2007, 06:20:14 18:20 » |
|
;Hi! ;I know that some of You have headaches 'bout timing delays. Here is a inline assembly routine that Microchip provides regarding ;delays. If You really want to know what's goin' on in a - say - 16F628(which is regarded as tiny with no power at at all, rethink, it can actually can be programmed to handle serial comm. without having an UART module!)
;PIC MPASM-comman-line(or MPASMWIN) assembler is tricky, but You MUST read the Application Notes for the PIC You are about to ;use, ;otherwise You are the compiler's slave. You don't want to be a slave, but a master, right? There's no shortcut to knowledge, never ;mind Yr. IQ... ;Assembler program by Microchip for delays varying from 20 uS to abt.23.8 hours ;Each loop takes 20 clocks, or 20 us per loop,at 4MHz or 1MIPS clock. Remember that internal PIC-instructions are Fosc/4. ;The code looks huge, but is much faster and more accurate that anything You can produce yourself, or... prove me wrong!! ;Since the code is not relocatable, You don't need any linker, unless You have other as. code to be linked in, that's why I commented some of the code. ;Turn off in config bits WDT for long simulations. Use it like inline asm. e.g.:
#include p16F877A.inc ;(found in MPASM suite) ;udata 0x20 (decomment if You want to generate .obj. file, from a high-level lang. or other code) ;some BASIC or C/C++ code(maybe even ADA ot JAVA) _asm ;Here's the inline assembler routine 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, only needed for .obj. file pagesel TestCode goto TestCode ;CODE ;Code starts here, only needed for .obj.file TestCode Dly32 D'50000' ;Max 4 billion+ (runs Dly32 Macro, ;1 sec in this case). nop ;ZERO STOPWATCH, good idea to put a breakpoint here in ;debug mode if Yr. not sure how it works 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(working register) 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. _endasm ; this is where the asm. routine leaves ;more BASIC or other high-level lang. END ;/Robban, give me a hint if You hate this!
|