How to Use Timer-1 Interrupt in AT89C1051
The following code demonstrate, how to generate clock of 10Khz using timer-1 interrupt. Every 50 micro-second timer-1 interrupt is generated and the clock is generated on pin P3.0. The code is written in Keil uVision2 IDE and simulation is done with Proteus 8.0. At the end of code, you can find complete project files for download.
Code Using Keil uVision2
#include
#include
// proto-types
void ConfigT1Interrupt(void);
void Delayms(unsigned int x_time);
// pin labling
sbit PULSE = P3^0;
sbit LED = P1^0;
// timer-1 interrupt isr
void timer1_isr(void) interrupt 3
{
// 10khz clock
PULSE = ~PULSE;
// reload timer-1 registers
TL1 = 0xCE;
TH1 = 0xFF;
}
#include
// proto-types
void ConfigT1Interrupt(void);
void Delayms(unsigned int x_time);
// pin labling
sbit PULSE = P3^0;
sbit LED = P1^0;
// timer-1 interrupt isr
void timer1_isr(void) interrupt 3
{
// 10khz clock
PULSE = ~PULSE;
// reload timer-1 registers
TL1 = 0xCE;
TH1 = 0xFF;
}
void main(void)
{
// make pins as output
PULSE = 0;
LED = 0;
// config timer-1 interrupt
ConfigT1Interrupt();
while(1)
{
LED = ~LED;
Delayms(500);
}
}
void ConfigT1Interrupt(void)
{
// master clock is 12MHz
// timer clock is 1/12th => 1MHz
// in 16-bit mode timer overflow
// after 65536 usec.
// for overflow every 50 usec
// 65,536-50 = 65,486 (0xFFCE)
TL1 = 0xCE;
TH1 = 0xFF;
// Configure timer-1 interrupt
ET1 = 1;
// Enable Global Interrupt Flag
EA = 1;
// set timer-1 mode-1 (16-bit)
TMOD = 0x10;
// start timer-1
TR1 = 1;
}
void Delayms(unsigned int x_time)
{
unsigned int x,y;
for(x=0;x<x_time;x++)
for(y=0;y<122;y++);
}
{
// make pins as output
PULSE = 0;
LED = 0;
// config timer-1 interrupt
ConfigT1Interrupt();
while(1)
{
LED = ~LED;
Delayms(500);
}
}
void ConfigT1Interrupt(void)
{
// master clock is 12MHz
// timer clock is 1/12th => 1MHz
// in 16-bit mode timer overflow
// after 65536 usec.
// for overflow every 50 usec
// 65,536-50 = 65,486 (0xFFCE)
TL1 = 0xCE;
TH1 = 0xFF;
// Configure timer-1 interrupt
ET1 = 1;
// Enable Global Interrupt Flag
EA = 1;
// set timer-1 mode-1 (16-bit)
TMOD = 0x10;
// start timer-1
TR1 = 1;
}
void Delayms(unsigned int x_time)
{
unsigned int x,y;
for(x=0;x<x_time;x++)
for(y=0;y<122;y++);
}
Download Files
For download Keil project and Proteus 8.0 simulation files, click here.
Comments
Post a Comment