---
+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Junior Member level 1
    Points: 879, Level: 6

    Join Date
    Jun 2008
    Posts
    17
    Helped
    0 / 0
    Points
    879
    Level
    6

    Write Delay Function using Assembly Language in PIC16F84

    If 4Mhz Oscillator is used with PIC16F84 then Cycle of Pic would be 1,000,000 or 1 Mhz. If presacler of 256 is used then it comes 3906.25 cycle per second. How could i acomodate it in my programming code as max no for one byte is 255.

    I also used Delay Calculator to generate code but its also not working

    Regards

    •   Alt15th December 2010, 07:55

      advertising

        
       

  2. #2
    Advanced Member level 3
    Points: 6,340, Level: 18

    Join Date
    Jun 2007
    Posts
    819
    Helped
    209 / 209
    Points
    6,340
    Level
    18

    Re: Write Delay Function using Assembly Language in PIC16F84

    you can use oscillator like 20MHz? then it is easier to manipulate it



  3. #3
    Full Member level 5
    Points: 2,176, Level: 10
    cubanflyer's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    255
    Helped
    76 / 76
    Points
    2,176
    Level
    10

    Re: Write Delay Function using Assembly Language in PIC16F84

    What size delay are you looking for?

    And how accurate must it be?

    ---------- Post added at 11:15 ---------- Previous post was at 10:45 ----------

    Code:
    Delay500u
    	nop
            goto	$+1
    	Movlw	D'164'	
    	Movwf	Delay
    delay500u_loop
    	Decfsz	Delay,f
    	goto 	delay500u_loop
    	return
    This generates 500us delay using a 4Mhz, the delay starts with the call command and finishes with the return command, so the line calling the delay is included in the delay. This routine is not dependant on the PreScaler settings, because it does not use TMR0.

    You do not say if the delay is to be used via an interrupt or via a call to stop everything while it is executing.
    Last edited by cubanflyer; 15th December 2010 at 08:55.



    •   Alt15th December 2010, 09:15

      advertising

        
       

  4. #4
    Junior Member level 1
    Points: 879, Level: 6

    Join Date
    Jun 2008
    Posts
    17
    Helped
    0 / 0
    Points
    879
    Level
    6

    Re: Write Delay Function using Assembly Language in PIC16F84

    hi,

    i am looking for 5 second delay. Can you explain how $+1 works.

    Thanks for your support and time.

    regards



  5. #5
    Full Member level 5
    Points: 2,176, Level: 10
    cubanflyer's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    255
    Helped
    76 / 76
    Points
    2,176
    Level
    10

    Re: Write Delay Function using Assembly Language in PIC16F84

    basically goto +$, jumps to the next instruction but uses 2 clock cycles and only 1 memory location.

    How critical is the 5 sec delay?
    Last edited by cubanflyer; 15th December 2010 at 18:34.



    •   Alt15th December 2010, 18:17

      advertising

        
       

  6. #6
    Advanced Member level 5
    Points: 24,578, Level: 38

    Join Date
    May 2009
    Location
    Prime Meridian
    Posts
    2,980
    Helped
    845 / 845
    Points
    24,578
    Level
    38

    Re: Write Delay Function using Assembly Language in PIC16F84

    Quote Originally Posted by cadvis View Post
    hi,

    i am looking for 5 second delay. Can you explain how $+1 works.

    Thanks for your support and time.

    regards


    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.


    You did not actually answer the easlier question of if you are trying to do a delay working on the Timers in conjuntion with the Interrupts, which alllows your program to do other things while the delay is timing out, or to use the simpler dedicated program loop delay.

    Would suggest for a beginner and a 5 second delay you use the latter loop delay which is just an extended version of the 500us delay.

    You said in your first post you used a delay calculator but it did not work ? - if you post the code you used perhaps we can see where you have gone wrong.

    Code:
    ; 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



  7. #7
    Full Member level 5
    Points: 2,176, Level: 10
    cubanflyer's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    255
    Helped
    76 / 76
    Points
    2,176
    Level
    10

    Re: Write Delay Function using Assembly Language in PIC16F84

    Quote Originally Posted by wp100 View Post
    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.

    Hi the only reason it is there is to reduce the amount of memory used I pulled it from some my code where space was tight and I needed to keep down, As WP100 says that and the nop just ensure that the complete routine from Including the call and return take exactly 500 cycles ie 500us. I could have made it 3 x nop commands



  8. #8
    Advanced Member level 5
    Points: 39,962, Level: 48
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Cornell University, Ithaca, New York, USA (from Dhaka, Bangladesh)
    Posts
    4,730
    Helped
    1690 / 1690
    Points
    39,962
    Level
    48
    Blog Entries
    19

    Re: Write Delay Function using Assembly Language in PIC16F84

    Hi,
    This code is generated from Piclist - Microchip PIC Delay Code Generator - http://www.piclist.com/techref/picli...gen/delay.htm:
    Code:
    ; 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
    The subroutine should be called as below:
    Code:
    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
    goto $+n means go n instructions forward from current instruction
    goto $-n means go n instructions backward from current instruction

    eg
    Code:
    here goto $+2
    next goto $-1
    now goto goto $+1
    then goto $-2
    Here, the program first goes from "here" 2 instructions further to "now", then 1 instruction to "then", then 2 instructions back to "next", then 1 instruction back to "here", then repeating the loop.

    Hope I could make things clear.
    Hope this helps.
    Tahmid.

    ---------- Post added at 14:48 ---------- Previous post was at 14:46 ----------

    $ indicates current instruction, ie instruction currently being processed by the microcontroller


    1 members found this post helpful.

  9. #9
    Junior Member level 1
    Points: 879, Level: 6

    Join Date
    Jun 2008
    Posts
    17
    Helped
    0 / 0
    Points
    879
    Level
    6

    Re: Write Delay Function using Assembly Language in PIC16F84

    I am using it to simulate led on and for 5 sec respectively. Using Real Pic Simulator to simulate it

    Here is the code. I finding difficulties while simulating. Does any one check ot on simulator and send me that file to verify it.

    regards


    ;---------------------------------------------------------------------------------------------------
    ;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



  10. #10
    Advanced Member level 5
    Points: 24,578, Level: 38

    Join Date
    May 2009
    Location
    Prime Meridian
    Posts
    2,980
    Helped
    845 / 845
    Points
    24,578
    Level
    38

    Re: Write Delay Function using Assembly Language in PIC16F84

    Hi,

    Well you are very nearly there, couple of things causing it not to run.

    Always specify the processor type using List and Include
    The "Delay" is one complete subroutine, you do not need to call Delay_0
    After you have called the second delay you have not put a Goto to send the code back to the 'start' of your program loop.

    I have just used MPlabs SIMulator -it checks the delay timing and shows the Ports condition -see pic.

    While you code will now work, the way you are coding will make things difficult - will send some code of the same thing showing how much easier it is to use register Labels / Names




    Code:
    	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



  11. #11
    Advanced Member level 5
    Points: 24,578, Level: 38

    Join Date
    May 2009
    Location
    Prime Meridian
    Posts
    2,980
    Helped
    845 / 845
    Points
    24,578
    Level
    38

    Re: Write Delay Function using Assembly Language in PIC16F84

    Hi Again,

    Everyone has their own way of coding , using the system register labels/ names is much easier than using the hex addresses like 03 and 05 which will only lead to confusion.

    Code:
    	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



+ Post New Thread
Please login