robban
Senior Member
Offline
Posts: 265
Thank You
-Given: 34
-Receive: 38
Warrior
|
|
« on: July 17, 2007, 10:26:55 22:26 » |
|
Hi there!
Here comes a shorter delay inline routine: ;Routine for calculating a precise delay in MPASMWIN(or command-line MPASM). ;Use it as inline assembly unless You ;coding solely in ASM ;It's blistering fast. Don't forget to change the value for at least cblock,d1,d2 and d3 according ;to Yr. specifics. Also remember that the PIC operates at one quarter of the crystal speed(Fosc). The resulting HEX- ;code is 154 byte(beat that!)
;Some BASIC or C/C++ code
;=======Delay.ASM======================================================================= ;This code block is here for the sake of clarity, because You can't compile the rest without it
processor 16F876A ;or whatever #include C:\MPASMAssembler\p16f876a.inc ;(use Yr. own path) radix hex ;Redundant really, since hex is default, __config _WDT_OFF & _CP_OFF & _LVP_OFF & _HS_OSC ;You can also use 'b' for binary,'d' for decimal or an ASCII character enclosed in ;single quotes. Just put it anywhere in the code. ;(Fuses are found at the bottom of the .INC files) __idlocs 0x4455 ;Choose Yr. own ID. The PIC18 series ;can also specify address and their content
; Delay = 0.5 seconds(choose Yr. own) ; Clock frequency = 20 MHz, 5 MIPS(an example)
; Actual delay = 0.5 seconds = 2500000 cycles ; Error = 0 % (Well, if You use a precise clock crystal at 32.768 kHz, then You must change _HS_OSC to _XT_OSC) ;======================================================================================= _asm cblock 0x3 ;loop 3 times(cblock is a way to create run-time variables in assembler) d1 d2 d3 endc ;must end a cblock statement
movlw 0x16 ;charge variable d1 with 16hex movwf d1 movlw 0x74 ;charge var d2 with 74hex movwf d2 movlw 0x06 ;charge d3 with 6h movwf d3 ;total of 2499999 cycles Delay_0 decfsz d1, f ;You can read about this technique in the MPASM manual in MPLAB IDE goto $+2 decfsz d2, f goto $+2 decfsz d3, f goto Delay_0
nop ;1 cycle _endasm
;More BASIC or C/C++ code
END ;Or end, You can always declare case-sensitive in MPASMWIN
;/Cheers, Robban
|