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.

a delay - an explanation needed

Status
Not open for further replies.

newbie111

Member level 2
Member level 2
Joined
Nov 29, 2009
Messages
51
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
h.k
Activity points
1,609
delay

Code:
 Delay255	movlw	0xff	;delay 255mS
		goto	d0
Delay250	movlw	d'250'	;delay 250mS
		goto	d0
Delay100	movlw	d'100'	;delay 100mS
		goto	d0
Delay50		movlw	d'50'	;delay 50mS
		goto	d0
Delay20		movlw	d'20'	;delay 20mS
		goto	d0
Delay10		movlw	d'10'	;delay 10mS
		goto	d0
Delay1		movlw	d'1'	;delay 1mS
		goto	d0
Delay5		movlw	0x05	;delay 5ms
d0		movwf	count1
d1		movlw	0xE7
		movwf	counta
		movlw	0x04
		movwf	countb
Delay_0		decfsz	counta, f
		goto	$+2
		decfsz	countb, f
		goto	Delay_0
		decfsz	count1	,f
		goto	d1
		retlw	0x00



My two questions are

What does " $ +2 " mean"?

and take delay 250 ms for an example

how these codes make the delay 250ms delay?

Please explain it to me

Thanks a lot
 

Re: delay

Hi,

The $+2 means goto plus 2 Bytes of program memory, in the case of a 16F chip that is normally 2 instructions - while this may seem a handy shortcut to using a goto and label, if you move your code to other pics like the 18F then the same thing will only move it forward 1 instruction.

The code you posted is a little incomplete, what processor and what osc frequency are you using at it is the frequency/4 = clock cycle, that determines the delay period .

Also you are using 'goto' to get the delays routine and then 'retlw' - use 'call'

Try running your code with breakpoints and Simulators Stop watch at 4meg gives the results shown in the pic.
 

    newbie111

    Points: 2
    Helpful Answer Positive Rating
Re: delay

Thank you so much. It helped!
 

Re: delay

Hi,

Have you been able to see how it creates the delay ?

The osc frequency/4 gives the clock frequency or clock cycle that runs the micro.
If you look at the datasheet Instruction Set Summary, the 2nd page that shows a list of all of them, there is a column labelled ' cycles' ie the number of 'clock cylces' to execute an instruction.
If you are using a 4meg osc, then thats 4meg/4= a 1 meg clock cycle or .000,001 of a second = 1micro second per instruction ( well most instructions )

Your delay routine is just two loops of instructions use to create the required delay period.

As you can see the Sim Stop watch gives the delay time acheived and the number of intructions executed ( ie the delay loop ) to acheive this.

Have you been able to run the Sim ? it might seem a bit complicated to start with but its a handy tool to let you see what is going on at the single instruction rate - you can also view how all the system and user registers are being changed as your code runs though.
 

    newbie111

    Points: 2
    Helpful Answer Positive Rating
Re: delay

Hi,

I am able to run it, but I seldom use those fuctions.

it will take me some time to get used to it.
If I have further qs, I will surely ask away.

Thank you so much for being so helpful.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top