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.

How is the delay time calculated?

Status
Not open for further replies.

expert

Member level 2
Joined
Nov 28, 2007
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,626
using 4MHZ crystal

movlw D'13'
movwf CounterB
movlw D'251'
movwf CounterA
loop decfsz CounterA,1
goto loop
decfsz CounterB,1
goto loop
return

the answer should be 10ms delay.but is there someone can explain to me how is the delay time being calculate?

thxsss
 

Re: Delay time!!!!!!!

First, determine the length of time per machine cycle. That's the internal instruction clock. For PIC, a machine cycle is equal to your Oscillator speed divided by 4. So, if you have a 4MHz oscillator, your internal PIC speed is 4MHz/4 = 1MHz. That's the frequency. The period, or length of time, of each machine cycle then is 1/1MHz = 1 microsecond.

Each ASM instruction is 1 machine cycle. A GOTO, SKIP, and RETURN are 2 machine cycles each. For illustration, the following:

Code:
movlw         D'251';         // 1 cycle
movwf         CounterA;       // 1 cycle
loop decfsz   CounterA,1;     // 1 cycle (2 cycles for Skip when 0)
goto          loop;           // 2 cycles
In this example, 2 machine cycles for loading CounterA + 251 * 3 cycles for the Loop = 755 cycles = 755uS.

Hope that helps you do the rest of the calculation. Just follow the path of the code and start counting cycles.

In your code above, you need to put one loop inside of the other loop to get multiple 755us delays. Right now, you only have 2 separate delay loops in sequence and then it's done. It can't be 10mS like that. The following will give you 13 * 755us + some extra cycles:

Code:
    movlw     D'13'
    movwf     CounterB
outerloop:
    movlw     D'251'
    movwf     CounterA
innerloop:
    decfsz    CounterA,1
    goto      innerloop
    decfsz    CounterB,1
    goto      outerloop
    return
 

Re: Delay time!!!!!!!

thxs for the explaination!!

if i wan to use p16F877 pwm to generate a 38KHZ carrier frequency, do i need to consider the delay time for each cycle for generating a 38KHZ frequency??
 

Re: Delay time!!!!!!!

Using delays is not a good method for developing a constant frequency, because delays occupy all the processor's time when you should be doing other things. The simplest method for creating 38KHz is to use the PWM module of the PIC and set it for 50% duty cycle.

Here is a little program I wrote to write PWM values for the Swordfish Basic compiler for PIC18:
**broken link removed**
You will see from this program that a 4MHz Oscillator (Fosc), Timer2 Prescaler=1, and PR2=25 you can achieve a 38462 Hz Frequency..... very close to exactly 38KHz.

The complete code module which makes it happen is here:
http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.PWM2

The beauty of using the PIC's PWM module is that the frequency is constant, no interrupts or delays are required, and it will not affect your ability to do other code functions.
 

Re: Delay time!!!!!!!

thxs for the source
but i'm using assembly language to design an infrared to generate 38KHZ for transmiting, i plan to use pwm to generate a 38KHZ to transmit data coz my using an IR receiver 38KHZ to receive data. do u have any idea ?
 

Re: Delay time!!!!!!!

can anyone give me a long delay program for 8051 microcontroller
 

Delay time!!!!!!!

Hi,
It is quit simple you can use the Timer interrupts.

Added after 6 minutes:

Oooops,
Abrakadabra:
 

Re: Delay time!!!!!!!

The original code is OK, the loops are nested if you look carefully and it takes exactly 10000 cycles including the return. First time through CounterA = 251 but then it rolls...

Code:
    4 cycles to load the counters.
  755 cycles first time through.
 9240 cycles next 12 times (12 * 770).
   -1 cycles last time skip the last goto.
    2 cycles return.

10000 total
 

Re: Delay time!!!!!!!

Encrypted said:
The original code is OK, the loops are nested if you look carefully and it takes exactly 10000 cycles including the return. First time through CounterA = 251 but then it rolls...

Code:
    4 cycles to load the counters.
  755 cycles first time through.
 9240 cycles next 12 times (12 * 770).
   -1 cycles last time skip the last goto.
    2 cycles return.

10000 total

You're correct... I missed that it's a loop back to CounterA again and it's going to rollover for full count for the following loops.... ooopsie!
 

Re: Delay time!!!!!!!

Encrypted said:
The original code is OK, the loops are nested if you look carefully and it takes exactly 10000 cycles including the return. First time through CounterA = 251 but then it rolls...

Code:
    4 cycles to load the counters.
  755 cycles first time through.
 9240 cycles next 12 times (12 * 770).
   -1 cycles last time skip the last goto.
    2 cycles return.

10000 total

but counter B should be 13, so y multiply 12 only?
and how u guy get 770?
 

Re: Delay time!!!!!!!

It takes 4 cycles to load your counters.

The first time through your code CounterA = 251, the loop take 3 cycles except the when CounterA = 0 it takes only 2, plus 3 cycles to decrease CounterB to 12.

251 x 3 - 1 + 3 = 755

There are now 12 outer loops left. CounterA = 0 and CounterB = 12.

12 x (256 x 3 - 1 + 3) = 12 x 770 = 9240.

The last time through the outer loop takes only 2 cycles not 3.

9240 - 1 = 9239.

The final return takes 2 cycles.

4 + 755 + 9239 + 2 = 10000.
 

Re: Delay time!!!!!!!

this is a 7.5ms delay

Code:
    movlw	D'10'
	  movwf	CounterB
	  movlw	D'188'
	  movwf	CounterA
loop decfsz	CounterA,1
	  goto	loop
	  decfsz  CounterB,1
	  goto	loop
	  retlw

the calculation is
1. 188*3-1+3=566
2. 9*(193*3-1+3)=9*(581)=5229
3. the last cycle, 5229-1=5228
4. total cycle,
= 4 + 566 + 5228 + 2 = 5800

i onli get 5800. but i should get 7500 for 7.5ms delay
i follow the calculation above
y i can't get the correct answer
is it anymistake?
 

Re: Delay time!!!!!!!

It takes 4 cycles to load your counters.

The first time through your code CounterA = 188, the loop take 3 cycles except the when CounterA = 0 it takes only 2, plus 3 cycles to decrease CounterB to 9.

188 x 3 - 1 + 3 = 566

There are now 9 outer loops left. CounterA = 0 and CounterB = 9.

9 x (256 x 3 - 1 + 3) = 9 x 770 = 6930.

The last time through the outer loop takes only 2 cycles not 3.

6930 - 1 = 6929.

The final return takes 2 cycles.

4 + 566 + 6929 + 2 = 7501.
 

Re: Delay time!!!!!!!

Encrypted said:
It takes 4 cycles to load your counters.

The first time through your code CounterA = 188, the loop take 3 cycles except the when CounterA = 0 it takes only 2, plus 3 cycles to decrease CounterB to 9.

188 x 3 - 1 + 3 = 566

There are now 9 outer loops left. CounterA = 0 and CounterB = 9.

9 x (256 x 3 - 1 + 3) = 9 x 770 = 6930.

The last time through the outer loop takes only 2 cycles not 3.

6930 - 1 = 6929.

The final return takes 2 cycles.

4 + 566 + 6929 + 2 = 7501.

really appreciate and thxs for the explaination!!!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top