Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

can you explain how this pic pgm is happening?

Status
Not open for further replies.

ashwini jayaraman

Member level 2
Joined
Jan 17, 2013
Messages
49
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,601
Code:
This is the program:
Delay = 1 seconds
; Clock frequency = 11.0592 MHz

; Actual delay = 1 seconds = 2764800 cycles
; Error = 0 %

cblock
d1
d2
d3
endc

Delay
;2764795 cycles
movlw 0xDA
movwf d1
movlw 0x07
movwf d2
movlw 0x07
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0

;1 cycle
nop

;4 cycles (including call)
return
I need the answer that how this particular program consumes 2764795 cycles???Couldn't understamnd how this is happening????
 

Hi,

Its called a software delay.

Basically its three count down loops nested together.

First understand what a cycle is - thats the oscillator frequency divided by 4, if its a typical Pic chip. 11,0592,000 hz /4 = 2,764,800 hz per Machine Cycle

Most single instructions take one Machine cycle to complete, a few take 2 or 3.

So by executing a tight countdown loop that takes 2764800 machine cycles then that is equal to 1 second
 
I don't think it is working!

The theory is that you have three loops, the innermost loop (Delay_0) repeats 0xDA times before counting down the next loop. That loop resets the first loop for 0xDA counts again and itself repeats 0x07 times. The third loop repeats both the other loops 0x07 times. So the resulting delay would be (d1 * d2 * d3 * Cycles per instruction) plus a few extra cycles as each loop counter is checked for zero.

In your program the "goto $+2" makes it skip most of the instructions so unless you are using a PIC18 it wouldn't work anyway. Except in certain special circumstances, try to avoid using $+ because it makes the code incompatible across processors, instead use lablels and let the assembler make the choice of jump instructions for you. Like this:
Code:
    cblock
d1
d2
d3
    endc

Delay  ;2764795 cycles
    movlw 0xDA
    movwf d1
    movlw 0x07
    movwf d2
    movlw 0x07
    movwf d3
Delay_0
    decfsz d1, f
    goto Delay_0
    decfsz d2, f
    goto Delay_0
    decfsz d3, f
    goto Delay_0

    nop  ;1 cycle

    return  ;4 cycles (including call)

The delay calculation still doesn't work but you can see how the loops are nested now. You probably have to reload the registers in the loops to get the delays you want.

What PIC are you using and is it possible to use a hardware timer instead? The problem with this kind of delay is the processor can't do anything else while running it, a timer will just alert you when one second has elapsed.

Brian.
 
In my example works fine in 1 sec delay:

Code:
	#INCLUDE <P12F629.INC>
	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL  -302
;**********************************************************************
		CBLOCK 0X20
			D1
			D2
			D3
		ENDC
;**********************************************************************
		ORG     0
		GOTO    CONFIGURA
;**********************************************************************
		ORG     0X004
		RETFIE
;**********************************************************************
CONFIGURA:
		CALL    0X3FF
		BSF     STATUS,RP0
		MOVWF   OSCCAL
		BCF     STATUS,RP0
		
		CLRF 	GPIO
		MOVLW 	07H
		MOVWF 	CMCON
		BSF 	STATUS,RP0
		MOVLW	B'00001000'
		MOVWF	TRISIO
		BCF     STATUS,RP0
		GOTO	MAIN
;**********************************************************************
MAIN:
		MOVLW	0XFF
		MOVWF	GPIO
		CALL	SEC1
		CLRF 	GPIO 
		CALL	SEC1
		GOTO	MAIN
;**********************************************************************
SEC1:
		MOVLW	0X07
		MOVWF	D1
		MOVLW	0X2F
		MOVWF	D2
		MOVLW	0X03
		MOVWF	D3
LOOP1S:
		DECFSZ	D1, F
		GOTO	$+2
		DECFSZ	D2, F
		GOTO	$+2
		DECFSZ	D3, F
		GOTO	LOOP1S
		GOTO	$+1
		GOTO	$+1
		GOTO	$+1
		RETURN
;**********************************************************************
		ORG	0X2100
		DE	'D','E','L','A','Y',' ', '1','S','E','C'
;**********************************************************************
		END
;**********************************************************************

To understand the code, use the MPLAB SIM (Menu: Debbuger / Select Tool/ 4 MPLAB SIM)...
 
I use pic 16f887...for my program, it is enough..that's why am implementing delay routine rather than timer...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top