Creating Delay Function for 8051 microcontrollers Using Embedded C

Introduction:

Delay is a user-defined function used for creating a delay in operation on a micro-controller. Timers present in 8051 is used for the purpose. Instead of creating this function in the program for which we are using, create a separate file for the same and include it in the Source Group(click here to know, how to add program file in the source group). Then for using the function after header file. Now let us create a delay function for 8051 micro-controller. There are 2 methods for it.

  1. Using Loop: which is easier but not accurate
  2. Using Register Programming: Little typical

Pre-Requisites:

  • Basic Knowledge of 8051 microcontroller
  • Knowledge of Embedded C
  • Knowledge of using Kiel Software

Procedure:

  1. Creating Delay program using a loop

1.1. Mathematical Calculation for calculating 1 ms Delay

  • The crystal frequency of the oscillator by default set in Kiel software is 12 MHz.
    (Note: Frequency could be changed acc to requirement by this)
  • Timer frequency is 1/12 the frequency of the crystal oscillator. Timer freqency=12*10^6/ 12= 1Mhz
  • The time period of 1 machine cycle=1/ 10^6 Hz =1usec
  • Delay Time is given by the used= N(no of machine cycles)* Time for 1 machine cycle
  • N(No of machine cycles)=Delay time given by the user/ Time of 1 machine cycle => 10^(-3) sec/10^(-6)sec = 1000 cycles
  • So, for executing a msec the value of “i” should be set such that for executing a time delay, cycles should be approximately 1000.
  • For 12Mhz crystal frequency i.e. for 1000 machine cycles, the for loop should execute for 122 times.
  • To check how “i=122” came, have a look at this

1.2. Program

void delay_ms(unsigned int d)  ///unsigned int have range 0-65536
{ 
int i;
for(;d>0;d--)   //delay value is given by user in ms 
                 ///example delay(11) function called by user then it means "put 11milli
for(i=0;i<122;i++)   ///122 times loop executes for 1ms delay
}
  1. Creating Delay function using Timer Registers

2.1. Introduction to Special Function Registers for Timer(T0, T1, TMOD, TCON)

8051 have 2 16-bit timers: Timer 1 and Timer 0. Their operations are controlled by 2 8-bit SFRs(Special Function Registers) TMOD and TCON. Both are bit controlled registers. As shown in the figure below, Timer 1 and Timer 0 both are divided into 8-8 bit registers TH( store higher bits) and TL( store lower 8 b

8051 microcontroller timers registers
Source: microcontrollerslab.com
8051 timer0 control register
Source: Microcontrollerslabs.com

bits. Now let’s look at the timer controlling SFRs i.e. TMOD and TCON.

let’s discuss TMOD first. As shown in the figure lower 4-bits are for timer 0 and upper 4 bits for Timer 1. Here,

  • GATE: The bit is used to choose whether we want internal or external control for the timer.
    Here, GATE =1 means, we can start and stop the timer from an external source.
    GATE =0 means, we don’t need an external source to start and stop the timer.
  • C/T: Event Counter/ Time delay Generator
    If, C/T= 0 means used as TIMER
    C/T=1 means used as COUNTER
M1M0ModeDescription
00013 bit (TH 8 bit, TL 5 bit)
01116 bit (TH 8 bit, TL 8 bit)
1028 bit (TH 8 bit, TL 0 bit)
113Split Timer
Mode Selection in TMOD

For further detailed clarification on modes visit https://www.youtube.com/watch?v=B3SlTAkUFoo.

Now, the second timer control SFR i.e. TCON

In TCON, TF1, TR1, IE1, IT1 are for Timer 1 and TF0, TR0, IE0, IT0 are for Timer 0. In this blog we will discuss only TF and TR as both are used in delay function.

Here, TF(Timer Overflow) means, if we use TF=0, That indicates Timer 0 or Timer 1 registers are filled and timer need to be stopped for this we use TR register.

Here, TR=0 indicates STOP the timer

TR=1 indicates START the timer

2.2. The mathematical calculation for calculating the delay

Clock source:

The timer needs a clock source. If C/T = 0, the crystal frequency attached to the 8051 is the source of the clock for the timer. The value of crystal frequency attached to the microcontroller decides the speed at which the timer ticks. Now suppose that crystal frequency is 11.059MHz.

Timer’s clock frequency:

The frequency for the timer is always 1/12th of the frequency of the crystal attached to the 8051.

TF = 1/12  x 11.059MHz = 921583 Hz

Timer’s clock period:

The time delay of one machine cycle is given below. We use this to generate the delay.

TP = 1/ 921583= 1.085 µ sec

For delay of 10ms:

  1. Firstly divide the desired time delay value (10 ms) by the timer clock period.

N=  1 / 1.085us

N= 10ms / 1.0859usec

N=9216

  1. Subtract the value of N from the maximum number of counts possible for 16 bit timer i.e. 2^16 = 65536.

M=65536-N

M=65536-9216

M= 56320

  1. Convert this value to hexadecimal and write in TH and TL registers.

MH=DC00H

TH=DCH

TL=00H

2.3. Program Code

///Let make a program for 10 msec delay in led glow
#include <reg51.h>
sbit led=P2^0;  ///set 0 pin of P2 for led
void delay(void);

int main()
{
led=0; ///set up led
while(1)
{
led=1;
delay(); ///led on
led=0;
delay();  //led off
}

}
void delay(void)
{
TMOD=0x01;   ///timer 0 mode 1
TH0=0xDC;
TL0=0x00;
TR0=1/// START TIMER
while(TF==0)
TR0=0 ///stop timer
TF0=0; ///Clear flag

}

References:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.