How to use this code for having a delay(number input)?

Status
Not open for further replies.

smiles

Advanced Member level 4
Joined
Jul 27, 2007
Messages
110
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,096
is there anyway that I input a number, for i.e 123, than we have a 123ms delay ?
Thanks !!!
Can we utilize this code for that purpose ?
Code:
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'   ; 50000x20microseconds than we have 1s delay
      nop               
      goto    TestCode   ;Go back to top of program and
                                ;run the delay again.
;Subroutine, called by the Macro Dly32 (20 microseconds 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
 

delay(number input)

well, uC delay are very Xtal dependent... so in your PIC (I think its PIC16F--- huh?) say, you are using a 4MHz Xtal, just set my reg7 as data variable and use a fixed 1ms delay...
Code:
DELAY_1ms:
	movlw	.199
	movwf	reg7
	goto	$+1
	decfsz	reg7
	goto	$ - .2
	RETURN
and make another subroutine to "call" this one the times you want/need.

say, you want to set a delay as PORTB data input...
Code:
main:
 banksel  TRISB
 movlw    0xFF
 movwf    TRISB
 banksel  PORTB
 movf      PORTB,w
 movwf   reg6
 call       DELAY_1ms
 decfsz   reg6
 goto      $-3
 ; so it was delay(portb)
 goto main
hope its useful
 

Re: delay(number input)

Code:
DELAY_1ms:
   movlw   .199
   movwf   reg7
   goto   $+1
   decfsz   reg7
   goto   $ - .2
   RETURN
Are you sure these code take 1ms to finish ?
goto $ - .2, what does it do ?
Thanks and sorry about my late response, I ask this affair quite early, till now I just get in it
 

Re: delay(number input)

Last version (4.x) of CCS compiler, now accept any size of input parameter.

So, You could use a demo version to compile delay(number), and see the assembly code at C/ASM viewer.
 

delay(number input)

well, the goto $ - .2 just jump back 2 instructions....

its equal to:
Code:
DELAY_1ms: 
   movlw   .199 
   movwf   reg7 
   goto   nextinst ; for a 2cm delay
nextinst: 
   decfsz   reg7 
   goto   nextinst
   RETURN
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…