Skip to main content

Different ways to generate delays in 8051

The delay length in 8051 microcontroller depends on three factors:
  1. The crystal frequency
  2. the number of clock per machine
  3. the C compiler.
The original 8051 used 1/12 of the crystal oscillator frequency as one machine cycle. In other words, each machine cycle is equal to 12 clocks period of the crystal frequency connected to X1-X2 pins of 8051. To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. In other words, AT89C51, DS5000, and DS89C4x0 the duration of the time to execute an instruction varies, but they all use 1/12th of the crystal's oscillator frequency for the clock source.

8051 has two different ways to generate time delay using C programming, regardless of 8051 version.

The first method is simply using Loop program function in which Delay() function is made or by providing for(); delay loop in Embedded C programming. You can define your own value of delay and how long you want to display. For example- for(i=0;i<"any decimal value";i++); this is the delay for loop used in embedded C.
Code to generate 250 ms delay on Port P1 of 8051:
#include "REG52.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++) // this is For(); loop delay used to define delay value in Embedded C
{
for (j=0;j<1275;j++);
}
}

The second method is using Timer registers TH, TL and TMOD that are accessible in embedded C by defining header file reg52.h Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various timer operation modes in 8051 C programming. There are four operating modes of timer 0 and 1. To generate Time delay using timer registers:
  1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or is selected
  2. Load registers TL and TH with initial count value
  3. Start the timer
  4. Keep monitoring the timer flag (TF) until it rolls over from FFFFH to 0000.
  5. After the timer reaches its limit and rolls over, in order to repeat the process - TH and TL must be reloaded with the original value, and TR is turned off by setting value to 0 and TF must be reloaded to 0.
Code generating delay using timer register:
#include <REG52.h>
void T0Delay(void);
void main(void){

while (1) 
{
P1=0x55;
T0Delay();

P1=0xAA;
T0Delay();
}
}
void T0Delay(){

TMOD=0x01;     // timer 0, mode 1
TL0=0x00;      // load TL0

TH0=0x35;      // load TH0
TR0=1;         // turn on Timer0

while (TF0==0);           // wait for TF0 to roll over

TR0=0;      // turn off timer
TF0=0;      // clear TF0
}


Comments

  1. You did not mention the third method.

    ReplyDelete
  2. Nice Blog, thanks for sharing with us.

    Are you looking to buy touch monitors, touch screen, Touch Display and more? then order now.

    Buy PCAP Touch Monitor Online
    Buy High Brightness Touch Monitor Online

    ReplyDelete
  3. Industrial touchscreens have revolutionized manufacturing processes by providing a user-friendly interface for controlling complex machinery and systems. Their durability and resistance to harsh conditions make them a valuable tool in industrial settings.
    industrial touchscreen

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Buzzer interface with 8051 microcontroller

Buzzer  is a electronic device that converts the electronic signal into buzzing noise, that is applied to it. It can be used as electronic bell or as quiz buzzer in many applications around us. Here, i world like to discuss the interfacing of a small buzzer with 8051 microcontroller and how different projects can be constructed. Buzzer Interfacing: This project shows the interface with AT89S52 microcontroller to a buzzer. When a push button is pressed, the buzzer will get ON and OFF ( number of times set in the code ) and then stops. Circuit Diagram: - The port P1 of the microcontroller is connected to buzzer. This type of connection is possible, if the current requirements of the buzzer is not more than 20mA. The output is in current source mode so that buzzer will turn ON when the output of the port is logic LOW. Switch is connected to port P3 which remains at logic HIGH by pull up resistor.  Code: #include "REG52.h" #define buz P1 sbit SW=P3^0; long int i; voi

How to Switch OFF Citizen Calculator

You might be confused on seeing no OFF button on your calculator. Actually, these are general calculator available at low cost in the market. They process simple calculations like division, subtraction, Addition, Multiplication. But, how will you OFF that calculator having no option on them. There is always a trick which OFF them. It depends on the company calculator and their series to which they belong. I can tell you about Citizen Calculator CT-500 (as you can see in the image), how can you switch OFF it and can save battery too. If you leave calculator idle for 6-7 minutes, it will goes OFF automatically. My calculator goes OFF in 6.50 minutes from the point of leaving it idle. But, this is not the power saving trick for these types of calculator having not much backup battery. To save the power of your normal calculator, here is the trick to switch OFF it. Press ' /(divide sign)', 'x(multiply sign)', '%(percentage sign)', 'autoreplay option(