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.

delay generation using for loop

Status
Not open for further replies.

shruv

Member level 2
Joined
Jun 13, 2013
Messages
47
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
270
Code:
void delay(unsigned int i)
{
	unsigned int j,k;
	for(j=0;j<i;j++)
		for(k=0;k<1275;k++);
}

Delay calculation for 8051 interfacing with lcd.
This code if from Mazidi text book.I haven't understood the delay calculation.How did they arrive at the value 1275?
 

1275 may correspond some delay value say x which is normally depend on LCD datasheet. As every LCD required some delay between 2 command so this delay may be for the same. u may change value to see it's effect.
 

hello

it's depends also with the FOSC ( Quartz, clock value..number of cycle ..)
see the asm code produced by this C function
to calculate the amount of cycles machine
and find out the corresponding delay .
 

1275 may correspond some delay value say x which is normally depend on LCD datasheet. As every LCD required some delay between 2 command so this delay may be for the same. u may change value to see it's effect.
This code is for generation of milli sec(any value) delay.The question is how did they arrive at the value 1275. yes we usually give 250 as count(i value in the code above) and this code will generate 250msec delay.
 

It depend upon ur instruction time (Crystal). If suppose u use PIC controller then for 8 Mhz ur instruction time = 500 ns &

(500 ns * 1275) + For loop delay = 1 ms.
 
  • Like
Reactions: shruv

    shruv

    Points: 2
    Helpful Answer Positive Rating
This code is for generation of milli sec(any value) delay.The question is how did they arrive at the value 1275. yes we usually give 250 as count(i value in the code above) and this code will generate 250msec delay.

crystal oscillator frequency = 11.0592 Mhz
clock cycles per machine instruction = 12
clock peroid of one machine cycle = 11.0592M/12

time period of one machine cycle = 1/clock peroid = 12 / 11.0592 M ~= 1.08506 micro

1275 in loop * 1.08506 = 1.3 milli ~= 1milisec

if you want perfect time period to be loaded you have to calculate TH and TL of timer delay.
 
if u want exact 1ms delay u can go with 12Mhz crystal and use timer in 8-bit auto-reload mode. u will get exact 1ms without error by controller.
 

hello,

Be carrefull with delay loop in C langage,
not realy the same approach as in ASM langage


crystal oscillator frequency = 11.0592 Mhz
clock cycles per machine instruction = 12
clock peroid of one machine cycle = 11.0592M/12

time period of one machine cycle = 1/clock peroid = 12 / 11.0592 M ~= 1.08506 micro

1275 in loop * 1.08506 = 1.3 milli ~= 1milisec

i don't know C langage for 8051
but if you have a look on how is convert this same C delay routinefrom C18 (and PIC18F) to asm langage
you will get a big surprise ...

Code:
             void delay_8051(unsigned int i)
  08EE    CFD9     MOVFF 0xfd9, 0xfe6
  08F0    FFE6     NOP
  08F2    CFE1     MOVFF 0xfe1, 0xfd9
  08F4    FFD9     NOP
  08F6    0E04     MOVLW 0x4
  08F8    26E1     ADDWF 0xfe1, F, ACCESS
756:               {
757:               	unsigned int j,k;
758:               	for(j=0;j<i;j++)
  08FA    6ADE     CLRF 0xfde, ACCESS
  08FC    6ADD     CLRF 0xfdd, ACCESS
  08FE    CFDE     MOVFF 0xfde, 0
  0900    F000     NOP
  0902    CFDD     MOVFF 0xfdd, 0x1
  0904    F001     NOP
  0906    0EFD     MOVLW 0xfd
  0908    CFDB     MOVFF 0xfdb, 0x2
  090A    F002     NOP
  090C    0EFE     MOVLW 0xfe
  090E    CFDB     MOVFF 0xfdb, 0x3
  0910    F003     NOP
  0912    5002     MOVF 0x2, W, ACCESS
  0914    5C00     SUBWF 0, W, ACCESS
  0916    5003     MOVF 0x3, W, ACCESS
  0918    5801     SUBWFB 0x1, W, ACCESS
  091A    E21A     BC 0x950
  0946    2ADF     INCF 0xfdf, F, ACCESS
  0948    0E01     MOVLW 0x1
  094A    E301     BNC 0x94e
  094C    2ADB     INCF 0xfdb, F, ACCESS
  094E    D7D7     BRA 0x8fe
759:               		for(k=0;k<1275;k++);
  091C    0E02     MOVLW 0x2
  091E    6ADB     CLRF 0xfdb, ACCESS
  0920    0E03     MOVLW 0x3
  0922    6ADB     CLRF 0xfdb, ACCESS
  0924    0E02     MOVLW 0x2
  0926    CFDB     MOVFF 0xfdb, 0
  0928    F000     NOP
  092A    0E03     MOVLW 0x3
  092C    CFDB     MOVFF 0xfdb, 0x1
  092E    F001     NOP
  0930    0EFB     MOVLW 0xfb
  0932    5C00     SUBWF 0, W, ACCESS
  0934    0E04     MOVLW 0x4
  0936    5801     SUBWFB 0x1, W, ACCESS
  0938    E206     BC 0x946
  093A    0E02     MOVLW 0x2
  093C    2ADB     INCF 0xfdb, F, ACCESS
  093E    0E03     MOVLW 0x3
  0940    E301     BNC 0x944
  0942    2ADB     INCF 0xfdb, F, ACCESS
  0944    D7EF     BRA 0x924
760:               } 
  0950    0E04     MOVLW 0x4
  0952    5CE1     SUBWF 0xfe1, W, ACCESS
  0954    E202     BC 0x95a
  0956    6AE1     CLRF 0xfe1, ACCESS
  0958    52E5     MOVF 0xfe5, F, ACCESS
  095A    6EE1     MOVWF 0xfe1, ACCESS
  095C    52E5     MOVF 0xfe5, F, ACCESS
  095E    CFE7     MOVFF 0xfe7, 0xfd9
  0960    FFD9     NOP
  0962    0012     RETURN 0

and if you count the amount of machine cycles , you are fare away of simple prediction ...

can you post your asm result of compiling concerning this delay subroutine ?
This is the only way to know what is the reel delay..

i am agree with other suggestion : as to use of a dedicated Timer to build a precise delay.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top