Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 15, 2024, 03:26:10 15:26


Login with username, password and session length


Pages: [1]
Print
Author Topic: Inline delay routine in ASM  (Read 4418 times)
0 Members and 1 Guest are viewing this topic.
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« 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!
« Last Edit: June 20, 2007, 04:51:32 16:51 by robban » Logged

Code Warrior
Midnight
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 6
-Receive: 2


« Reply #1 on: June 20, 2007, 06:06:40 06:06 »

Thanks for that robban.

More programing tips are welcome instead of so many "I want this and I want that software"

Midnight
Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #2 on: June 20, 2007, 07:53:04 19:53 »

Well, Midnight, did You try it? Normally I try to avoid anything but Microchip when it comes to deep-down code. Never mind if You use BASIC, C/C++, ADA or whatever, You can always rely on these ever lasting code snippets. Obviously, You can improve them with snippets on Yr. own, but of course, You have to understand how they work....
A hint..
Microchip AN notes, if You have the interest of improving Yr. skills...
Logged

Code Warrior
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC