How to Use Timer-1 in AT89C1051 (13-bit Mode)

The following code demonstrate, how to generate clock of 100Hz using timer-1 in 13-bit mode. Every 5000 micro-second timer-1 overflow flag goes high and  port P3.0 is inverted. 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 ConfigTimer1(void);
void MonitorTimer1OverflowFlag(void);
void Delayms(unsigned int x_time);
// pin labling
sbit PULSE = P3^0;
sbit LED = P1^0;
void main(void)
{
// make pins as output
PULSE = 0;
LED = 0;
// config timer-1
ConfigTimer1();
while(1)
{
PULSE = ~PULSE;
MonitorTimer1OverflowFlag();
}
}
void ConfigTimer1(void)
{
// master clock is 12MHz
// timer clock is 1/12th => 1MHz
// in 13-bit mode timer overflow
// after 8,191 usec.
// for overflow every 5,000 usec
// 8,191-5,000 = 3,191 (0x0C77)
TL1 = 0x77;
TH1 = 0x0C;
// set timer-1 mode-0 (13-bit)
TMOD = 0x00;
// start timer-1
TR1 = 1;
}
void MonitorTimer1OverflowFlag(void)
{
// monitor timer-1 overflow flag
while(TF1 == 0);
// reset overflow flag
TF1 = 0;
// reload value
TL1 = 0x77;
TH1 = 0x0C;
}
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

Popular posts from this blog

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program Interrupts in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)