Tuesday, March 25, 2014

How To Read The Timer Value Of 89c51 When Program Running

How To Read The Timer Value Of 89c51 When Program Running

Ok this very difficult to find the Timer values of Microcontroller when a program is running but no problem im here ......
first of all i use Timer 1 in mode 2 for serial data transmission ( Means Read the timer value to serial port )
Now i use the Timer 0 in mode 1 and Start the initial value from 00H .In my given code the Port 1 is reserved for starting and stopping the Timer 0 .As in given code 




#include <reg51.h>
#include <stdio.h>
extern void MSDelay(unsigned int);
extern void SerialTx(signed int);

void main (void)
 {
      int x;
      SerialTx(-3);
    TMOD=0xA1;    //use Timer 1 in mode 2 and Timer 0 in mode 1 both are start and stop from externally
      TH0=0x00;
    TL0=0x00;
     
while(1)

{
    if( P1==0)
  {
          TR0=1;
        TI=1;
      
          x=TH0;
            x=x<<8;
        x=x+TL0;
      
          printf("The Timer 0 is ON.   And The Value Is : ");
          printf("%x, \n",x);
          MSDelay(60);
    }
      
    else
    {
      
        TI=1;
          printf("The Timer 0 is OFF. \n");
          MSDelay(60);
    }

}

}



As you see the blue colored code is my main code but in the main code there are two external Functions 
extern void MSDelay(unsigned int);
extern void SerialTx(signed int);

the First Function is Delay Function And This is given below



#include <reg51.h>
#include <stdio.h>
void MsDelay( unsigned int);
void MsDelay( unsigned int itime)
{
unsigned int i , j ;
for (i=0;i<itime;i++)
{
for (j=0;j<1275;j++);
}
}


And another Function is Serial  Data Transmit Function which is also given Below

#include <reg51.h>
#include <stdio.h>
void SerialTx(signed int );
void SerialTx(signed int a)
{
TMOD=0xA0;
TH1=a; //9600 baud rate  FD = -3
SCON=0x50;
TR1=1;   
}

Now  Proteuos Work 

When The Project is stopped
 





When Project is On But Button is Off no data received from Timer 0







When Button is On and data received from Timer 0 ........






Thanks for watching you know my english is week but i think you understand my code ....but if any problem so Inform me   





1 comment:

  1. Hello Muhammad,
    I need your little help.
    I'm currently working on 8951, there I am getting problem to read timer value.
    I am here explaining algorithm
    can you please help me for this,i will be grateful to you.
    ALGORITHM:
    1.Using external interrupt
    2.If 1st interrupt goes high from external interrupt then start timer
    3.If 2nd interrupt goes high from external interrupt then stop timer
    4.Read timer value on any pin.

    here is code i tried.

    #include

    sbit V=P3^4;
    sbit C=P3^5;
    sbit Led1=P1^1;
    sbit Led2=P1^2;
    void timer1(void);
    void main()
    {
    P3=0x00;
    P2=0x00;
    P1=0x00;
    EA=1;
    EX1=1;
    IE=1;
    timer1();
    }
    void tiemr1 ();
    void timer1 (void) interrupt 1
    {
    TMOD=0x01;
    TH0=0x00;
    TL0=0x00;
    if(V==1)
    {
    TR0=1;
    while(TF0==0);
    }
    else if(C==1)
    {
    TR0=0;
    TF0=0;
    }
    else
    {;}
    Led1=TL0;
    Led2=TH0;
    }

    ReplyDelete