8051 timer Interrupt program to copy data from P0 to P1 ,while simultaneously generating square wave of time period 200 uS at P2^0 .

When it comes to interrupt programming , we have to consider some important registers. For this program the registers used are IE(interrupt enable),TCON(timer control),TMOD(timer mode).
    In timer interrupt programming calculating the value to be loaded to the timer is very important.

Here XTAL frequency i have considered is 11.0592 MHz.If you are using different frequencies,the just divide the XTAL/12.Then inverse the obtained answer.This will be the time required to complete one machine cycle.Hence for my 11.059 MHz the time will be 1.085 uS .

Now the time period of the square wave required is 200 uS .Therefore now we have to calculate the no of cycles required to generate 200 uS of delay so that the output switches between 0 and 1 to generate a square wave. 

N=200 uS/ 1.085 uS = 184.33179 = 184

Using timer 0 in 8 bit auto reload mode  we can generate the given delay. Value to be loaded to the TH0,TH1 can be calculated as follows

                      255-184=71=0x47
We have to load this value to TH0


CODE:

#include<REGx51.h>
sbit sqwave=P2^0;


void timer0() interrupt 1
{
    sqwave=~sqwave;  //generating sq wave

}


void main()
{
  TMOD=0x02;  //Timer0 in 8 bit autoreload mode
IE=0x82; //enabling intrupt
TH0=0x47; //loading the calculated value
     TR0=1;     //starting timer

while(1)
{
 P1=P0;  //copying data from p0 to P1
 
}

}

Enjoy coding..

                                                                                                                                    ---J_P

No comments:

Powered by Blogger.