Searching \ for ' [PIC] Delay Routine problem (a newbie)' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/begin.htm?key=delay
Search entire site for: 'Delay Routine problem (a newbie)'.

No exact or substring matches. trying for part
'[PICLIST] [PIC] Delay Routine problem (a newbie)'
2002\05\20@025849 by

I am a new to PICs programming and am into it as a hobby.
I am looking for a delay loop. Attached with this email is the code
generated by http://www.piclist.com/techref/microchip/delays.htm as
mentioned in one of previous emails for the subject.

I do not understand some of the code. Will you be kind enough to clear the
problems I am facing.

The Problems are mentioned in order of occurrence below:

Problem no 1: Mentioned is Actual delay = 0.5 seconds = 500000 cycles
What I do not understand is that the clock speed is 4000,000 CPS.
With this speed, the cycles for 0.5 seconds delay should have been =
4000,000/2=2000,000 Cycles.

Problem no 2: What is cblock?
Problem no 3: What is \$+1?
Problem no 3: How is it mentioned 2 cycles & 4 cycles?

; Delay = 0.5 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.5 seconds = 500000 cycles
; Error = 0 %

cblock
d1
d2
d3
endc

Delay
;499994 cycles
movlw          0x03
movwf          d1
movlw          0x18
movwf          d2
movlw          0x02
movwf          d3
Delay_0
decfsz         d1, f
goto           \$+2
decfsz         d2, f
goto           \$+2
decfsz         d3, f
goto           Delay_0

;2 cycles
goto           \$+1

;4 cycles (including call)
return

UbaidUllah

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

Hi UbaidUllah,

> Problem no 1: Mentioned is Actual delay = 0.5 seconds = 500000 cycles
>    What I do not understand is that the clock speed is 4000,000 CPS.
>    With this speed, the cycles for 0.5 seconds delay should
> have been =
>    4000,000/2=2000,000 Cycles.

The PIC "instruction" cycle takes 4 clock cycles to happen. Therefore the
PIC with a 4MHz Clock, instructions are executed at a rate of 1MHz. However
be aware that some instructions (CALL, GOTO, RETURN etc) that modify the
instruction pointer take 2 instruction cycles to execute.

Therefore when counting instruction cycles, you are actually counting 4
clock cycles per instruction cycle so the delay works out at 0.5 seconds.

> Problem no 2: What is cblock?

CBlock is a way to "declare" your variables. It allows you to name and
reserve RAM locations.

> Problem no 3: What is \$+1?

This is a shorthand way to say "Current Program Location" + 1 (the next
instruction). This is sometimes used instead of declaring a head of labels.
In this case it is using a GOTO instruction to jump to the next instruction.
This is done because a GOTO actually takes 2 instruction cycles to execute
so for the cost of one ROM instruction you effectively get 2 instruction
delays so your delay routine is smaller.

> Problem no 3: How is it mentioned 2 cycles & 4 cycles?

To get the exact number of instructions executed to give you the precise
delay you requested, some extra is needed on the end of the loop. Thus the
last goto \$+1 add a further 2 instruction cycles.

You also need to take into account the CALL and RETURN instructions. Each
adds another 2 instruction cycles resulting in a total of 4 instruction
cycles to get into and out of the delay routine.

Does that help?

Cheers,
Ash.

---
Ashley Roll
Digital Nemesis Pty Ltd
http://www.digitalnemesis.com
Mobile: +61 (0)417 705 718

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

Thank you Mr. Ashley Roll.
It surely is of great help to me.
Thanks again.

UbaidUllah,

Ashley Roll
<ash@DIGITALNE        To:     PICLISTMITVMA.MIT.EDU
MESIS.COM>            cc:
Sent by: pic          Subject:     Re: [PIC] Delay Routine problem (a
microcontrolle        newbie)
r discussion
list
<PICLIST@MITVM
A.MIT.EDU>

05/20/2002
12:12 PM
to pic
microcontrolle
r discussion
list

Hi UbaidUllah,

> Problem no 1: Mentioned is Actual delay = 0.5 seconds = 500000 cycles
>    What I do not understand is that the clock speed is 4000,000 CPS.
>    With this speed, the cycles for 0.5 seconds delay should
> have been =
>    4000,000/2=2000,000 Cycles.

The PIC "instruction" cycle takes 4 clock cycles to happen. Therefore the
PIC with a 4MHz Clock, instructions are executed at a rate of 1MHz. However
be aware that some instructions (CALL, GOTO, RETURN etc) that modify the
instruction pointer take 2 instruction cycles to execute.

Therefore when counting instruction cycles, you are actually counting 4
clock cycles per instruction cycle so the delay works out at 0.5 seconds.

> Problem no 2: What is cblock?

CBlock is a way to "declare" your variables. It allows you to name and
reserve RAM locations.

> Problem no 3: What is \$+1?

This is a shorthand way to say "Current Program Location" + 1 (the next
instruction). This is sometimes used instead of declaring a head of labels.
In this case it is using a GOTO instruction to jump to the next
instruction.
This is done because a GOTO actually takes 2 instruction cycles to execute
so for the cost of one ROM instruction you effectively get 2 instruction
delays so your delay routine is smaller.

> Problem no 3: How is it mentioned 2 cycles & 4 cycles?

To get the exact number of instructions executed to give you the precise
delay you requested, some extra is needed on the end of the loop. Thus the
last goto \$+1 add a further 2 instruction cycles.

You also need to take into account the CALL and RETURN instructions. Each
adds another 2 instruction cycles resulting in a total of 4 instruction
cycles to get into and out of the delay routine.

Does that help?

Cheers,
Ash.

---
Ashley Roll
Digital Nemesis Pty Ltd
http://www.digitalnemesis.com
Mobile: +61 (0)417 705 718

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

>
> Problem no 1: Mentioned is Actual delay = 0.5 seconds = 500000 cycles
>    What I do not understand is that the clock speed is 4000,000 CPS.
>    With this speed, the cycles for 0.5 seconds delay should have been =
>    4000,000/2=2000,000 Cycles.

Instructions are executed at 1/4 of the clock rate. Thus, the 500,000 cycles
refers to 500,000 cycles of the instruction clock, which is running at
1,000,000

> Problem no 2: What is cblock?

Used to allocate variables. The first CBLOCK in your program must define a
starting address  for variables to be loaded. This will depend on the
specific PIC you are using. Subsequent CBLOCKS will, by default, continue
allocating where the previous one left off.

> Problem no 3: What is \$+1?

A simple way to refer to the next memory location

> Problem no 3: How is it mentioned 2 cycles & 4 cycles?

the '2 cycles' comment is referring to the goto \$+1 instruction, which takes
two instruction cycles.

the '4 cycles' comment is referring to the 2 cycles consumed by the return
instruction, plus the 2 cycles consumed by the call instruction that got us
to this routine.

Bob Ammerman
RAm Systems

{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...