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.

[SOLVED] doubt in delay function subroutine

Status
Not open for further replies.

shaswat

Advanced Member level 4
Joined
Jul 2, 2013
Messages
115
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Activity points
963
I have a avery basic doubt in this program just because as a newbie in programing I don't understand the delay in this code. I read the books but didn't get how the delay code is works.
The clocl frequency is set at 11 MHz and delay is 25ms. Its hard to understand me that how he select the 250ms delay. Why 1275???



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <reg51.h>
 
void MSDelay(unsigned int);
 
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}

 
Last edited by a moderator:

Hi the delay is created by the line

Code C - [expand]
1
for (j=0;j<1275;j++);


The important thing is you have to note the ';' in the end of for loop
that means

Code C - [expand]
1
2
for (j=0;j<1275;j++)
continue;


this loops until j is 1275 so the j is incrementing by the instructions and that creates the delay.....

- - - Updated - - -

and the above for loop is to increment the amount of delay for the required time........
 

I notice that point that you mention but the code provide 250 ms delay./ I want to know how this routine provide a delay of 250ms. Dividing 11MHz by 12 gives frequency and inverse of the results gives us period which is 1usec. But how he provide a delay of 250ms I don't understand
 

For calculating this you can just observe the dis assembly in any compiler and based on the instruction you can find out that the delay will be approximately 250ms.
 

hi

its the simple thing

seconds = 1/frequency ;

so for 1 tick = 1/11=.09 microseconds(approx)


in this loop 1275 used, so the total time to take finish the loop 1275 * .09 = 114.75 micro seconds

actual delay is 114.75 * 250 = 28687.5 microseconds

its equals to 25(approx)ms
 
I want to know how he calculate 250ms????

- - - Updated - - -

Thnxx selva for the answer. But in book first it divide the 11MHz by 12 to get the period. I did the same thats why I was in trouble.
 

For this you first look at the disassembly of the for condition and you have to count the number of instructions per loop and find the total instructions...
For any controller there is a machine cycles which the hardware execution takes place according to the connected crystal refer it in ur controller and find the delay elapsed.....

the easiest method is passing a big value in delay function and finding the delay using approximation....

- - - Updated - - -

the 12 is hardware machine cycle....
 
hi shaswat,

probably , the prescaler value might be used for selecting the frequency of your micro-controller.actually the formula for

1 tick = 1/(frequency/prescaler);


you have asked for 250ms delay.you have the delay loop for 25ms. just run 10 times. it gives 250ms delay, or adjust the 1275 valuw
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top