Delay500u
nop
goto $+1
Movlw D'164'
Movwf Delay
delay500u_loop
Decfsz Delay,f
goto delay500u_loop
return
hi,
i am looking for 5 second delay. Can you explain how $+1 works.
Thanks for your support and time.
regards
; Delay = 5 seconds
; Clock frequency = 4 MHz
; Actual delay = 5 seconds = 5000000 cycles
; Error = 0 %
cblock
d1
d2
d3
endc
;5000000 cycles
movlw 0x2D
movwf d1
movlw 0xE7
movwf d2
movlw 0x0B
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
; Generated by http://www.golovchenko.org/cgi-bin/delay (December 7, 2005 version)
; Wed Dec 15 18:17:18 2010 GMT
; See also various delay routines at http://www.piclist.com/techref/microchip/delays.htm
Hi,
Th $+1 simply means goto the next instruction; the program counter plus 1.
It seems a bit pointless in the way its used here, the only reason is that a Nop instruction takes 1 machine cycle whereas a Goto takes 2, its often used in loops to make the timing exact.
; Delay = 5 seconds
; Clock frequency = 4 MHz
; Actual delay = 5 seconds = 5000000 cycles
; Error = 0 %
Delay
;4999993 cycles
movlw 0x2C
movwf d1
movlw 0xE7
movwf d2
movlw 0x0B
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
list P=16F84A
#include "p16f84a.inc"
__config 0x3ff1 ; or any value that you set
cblock 0x0c
d1
d2
d3
endc
org 0x00
goto start
start
;.......................
;.......................
;.......................
; Your code here
call Delay ;Calling the subroutine when you need the 5 second delay
;.......................
;Remainder of code
end
here goto $+2
next goto $-1
now goto goto $+1
then goto $-2
;---------------------------------------------------------------------------------------------------
;EQUATES
TRM0 equ 01h ;Address of TIMER0. It runs at 1/4 of
;clock speed. If clock is of 32,768 HZ
;then it is 8,192 pulses per second.
STATUS equ 03h ;Address of Status Register.
PORTA equ 05h ;Adress of PORTA.
TRISA equ 85h ;Address of TRISA.To Designate I\O
;of PORTA. Specify Which Port is
;input or output.
OPTION_R equ 81h ;Address of Option Register. IT is used to
;reduce the pulse rate by factor 2, 4,8,....
; 256. here 256 is used.
COUNT equ 08h
cblock 0x0c
d1
d2
d3
endc
;---------------------------------------------------------------------------------------------------
bsf 03h,5 ; Moving from 'Bank 0' to 'Bank 1'.
movlw b'00000' ; Move adress 00000 to 'w' register .
movwf 85h ; move value of W into TRISA. It designate all bits as
;output
bcf 03h,5 ; Move back to 'Bank 0'.
;---------------------------------------------------------------------------------------------------
start
movlw b'00001' ; means 2 in decimal, 00010 in decimal. logic 1 to 2nd bit.
movwf 05h ; move logic 1 to 2nd Bit of PORTA. It let to on LED connected to 2nd bit i.s A1.
call Delay ;Call delay function
call Delay_0
movlw 00h ; to send 0 at w.
movwf 05h ; to get off lED.
call Delay
call Delay_0 ; call delay function.
;---------------------------------------------------------------------------------------------------
;4999993 cycles
Delay movlw 0x2C
movwf d1
movlw 0xE7
movwf d2
movlw 0x0B
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
;---------------------------------------------------------------------------------------------------
end
LIST P=16f84
#INCLUDE"P16f84.INC"
;---------------------------------------------------------------------------------------------------
;EQUATES
TRM0 equ 01h ;Address of TIMER0. It runs at 1/4 of
;clock speed. If clock is of 32,768 HZ
;then it is 8,192 pulses per second.
STATUS equ 03h ;Address of Status Register.
PORTA equ 05h ;Adress of PORTA.
TRISA equ 85h ;Address of TRISA.To Designate I\O
;of PORTA. Specify Which Port is
;input or output.
OPTION_R equ 81h ;Address of Option Register. IT is used to
;reduce the pulse rate by factor 2, 4,8,....
; 256. here 256 is used.
COUNT equ 08h
cblock 0x0c
d1
d2
d3
endc
;---------------------------------------------------------------------------------------------------
bsf 03h,5 ; Moving from 'Bank 0' to 'Bank 1'.
movlw b'00000' ; Move adress 00000 to 'w' register .
movwf 85h ; move value of W into TRISA. It designate all bits as
;output
bcf 03h,5 ; Move back to 'Bank 0'.
;---------------------------------------------------------------------------------------------------
start
movlw b'00001' ; means 2 in decimal, 00010 in decimal. logic 1 to 2nd bit.
movwf 05h ; move logic 1 to 2nd Bit of PORTA. It let to on LED connected to 2nd bit i.s A1.
call Delay ;Call delay function
; call Delay_0
movlw 00h ; to send 0 at w.
movwf 05h ; to get off lED.
call Delay
; call Delay_0 ; call delay function.
GOTO start ; COMPLETE LOOP
;---------------------------------------------------------------------------------------------------
; SUBROUTINE CALLED DELAY
;4999993 cycles
Delay movlw 0x2C
movwf d1
movlw 0xE7
movwf d2
movlw 0x0B
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
;---------------------------------------------------------------------------------------------------
end
LIST P=16f84
#INCLUDE"P16f84.INC" ; all the system EQUATES are in this file
; It is located in Mpasm open it and have a look
; its complicated but it will make sense one day
cblock 0x0c ; user registers
d1
d2
d3
COUNT
endc
;---------------------------------------------------------------------------------------------------
banksel TRISA ; Moving from 'Bank 0' to 'Bank 1'.
movlw b'00000' ; Move adress 00000 to 'w' register .
movwf TRISA ; move value of W into TRISA. It designate all bit output
banksel 0 ; Move back to 'Bank 0'.
;---------------------------------------------------------------------------------------------------
start
movlw b'00000001' ; load W to turn on Porta, bit 0
movwf PORTA ; Move value in W to POORTA to turn on led on RA0
call Delay5SEC ;Call delay function
movlw b'00000000' ; Load W to turn off Portabit 0
movwf PORTA ; Move value in W to POORTA to turn off led on RA0
call Delay5SEC ; call delay function.
GOTO start ; COMPLETE LOOP
;---------------------------------------------------------------------------------------------------
; SUBROUTINE CALLED DELAY
;4999993 cycles
Delay5SEC movlw 0x2C
movwf d1
movlw 0xE7
movwf d2
movlw 0x0B
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return ; SPECIFIES THE END OF A SUBROUTINE
;---------------------------------------------------------------------------------------------------
end ; SPECIFIES THE END OF ALL YOUR CODE
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?