Timer Interfacing With 8051 and Square Wave Generation With 8051

TIMER INTERFACING WITH 8051:

Steps to interface the timer in mode 1:
1.Configure the TMOD register.
2.Load the count value in timer register(TH/TL).
3.Start the timer by setting TR bit of respective timer in TCON register.
4.Wait for the overflow flag to set.
5.Stop the timer.
6.Clear the overflow flag.
7.Repeat the process from step 2.

Steps to interface the timer in mode 2:
Mode 2 is 8 bit auto reload mode.Hence in this mode there is no need to load the value again in timer register(TH).
1.Configure the TMOD register.
2.Load the count value in timer register(TH).
3.Start the timer by setting TR bit of respective timer in TCON register
4.Wait for the overflow flag to set
5.Stop the timer
6.Clear the overflow flag
7.Repeat the process from step 3.

How to calculate the value to be loaded in timer register(TH/TL)?

1. We need to calculate the count value.

Count=Required time/Processor time

Processor time:

In micro-controller 8051,crystal frequency is internally divided by 12.Crystal oscillator frequency may vary from 4mhz to 30mhz.

Processor Time=1/(crystal oscillator frequency/12);

Supposed we attached crystal oscillator with the frequency of 11.0592mhz.Then the processor time is equal to 1/(11.0592*10^6/12)=1.08*10^-6

Required time:

First we need to know how much delay we want to generate?Supposed we want to generate delay of 5 msec.Then required time is equal to 5 msec.

2.Value to be loaded in timer register=Timer max-(count in decimal)

3.Convert the value in hex.

4.Load the upper count in TH register and lower count in TL register

Supposed we have already convert the count value in hex.Then value to be loaded in timer register=[Timer max-(count in hex)]+1.Then skip the third step.

For mode1,timer max is 65536(in decimal) and ffff(in hex).

For mode2,timer max is 256(in decimal) and ff(in hex).

For example:
Supposed our count value is 232

First method(without converting count value in hex):
  • Value to be loaded in timer register:65536-232=65304(decimal)
  • After converting value in hex:65304(decimal)=FF18(hex)
  • TH=0xFF and TL=0x18
Second method(After converting count value in hex):
  • Count=232(decimal)=00E8(hex)
  • Value to be loaded in timer register:[FFFFh-00E8h]+1=FF18h
  • TH=0xFF and TL=0x18

Note:
I have considered mode 1 for calculation.For the other mode timer max value get change as mentioned above.

SQUARE WAVE GENERATION:

At P2.0 pin of 8051 I have generated the square wave of 2khz.I have used timer 0 in mode 1.

SOFTWARE: KEIL MICRO VISION 4

SIMULATOR:  PROTEUS 8.0

CIRCUIT DIAGRAM:


PROGRAM:

#include<reg51.h>                                                                                

#include<string.h>

sbit pulse= P2^0; 

void delay(void);

void main()

{

while(1)

{

pulse=~P2^0; // 
alternatively send logic1 and logic 0 

delay();   // delay of 0.25msec

}

}

void delay (void) // delay generation

{

TMOD=0x01; //timer0, mode 1

TL0=0x18; // load the lower count

TH0=0xff; // load the higher count

TR0=1; // start the timer0

while(TF0==0); // wait for overflow

TR0=0; // stop the timer0

TF0=0; // clear overflow flag of timer0

}

PROGRAM DESCRIPTION:

Here,I have send logic 1 and logic 0 alternatively on P2.0 pin of 8051.After sending each data(logic 1 or logic 0),I have provided delay of 0.25 msec.

Calculation of timer value to be loaded in TH0 and TL0 register:

I have taken the micro-controller with crystal frequency of 11.0592mhz frequency.So the calculation is

  • (11.0592 MHZ ) / 12 = 921600 HZ
  • 1 / (921600) = 1.08*10^-6
  • Here we want square wave of 2khz.So time will be 1/(2khz)=0.5msec
  • As we want to generate the square wave,this delay is equivalent to Ton+Toff=0.5msec.As we know that in square wave Ton=Toff
  • Hence to get the required delay we will divide this 0.5msec by 2 so that we will get value of Ton and Toff.
  • Ton=Toff=(0.5*10^-3) / 2 = 0.25msec
  • Count= required time / processor time
  • Here the required time is 0.25msec
  • Count = (0.25*10^-3) / (1.08*10^-6)
  • Count = 231.481=232(decimal)
  • Value to be loaded in timer register: 65536 - 232 = 65304 = ff18(hex)
  • So in TH0 = 0xff and TL0 = 0x18.

VIDEO:








Note:

1.Please refer the below link to know about TMOD,TCON and timer registers of 8051.

https://embeddedprojects222.blogspot.com/2020/08/timer-registers-of-8051.html




Comments

Popular posts from this blog

Digital Thermometer using 8051 and ADC 0808 Interfacing With 8051

how to display numbers on lcd?

Introduction of IODIR,PINSEL,IOSET and IOCLR Registers