How to Use Timer-0 in AT89C1051 (16-bit Mode)
The following code demonstrate, how to generate clock of 1.0Hz using timer-0 in 16-bit mode. Every 50,000 micro-second timer-0 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 ConfigTimer0(void);
void MonitorTimer0OverflowFlag(void);
void Delayms(unsigned int x_time);
// pin labling
sbit PULSE = P3^0;
sbit LED = P1^0;
#include
// proto-types
void ConfigTimer0(void);
void MonitorTimer0OverflowFlag(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-0
ConfigTimer0();
while(1)
{
PULSE = ~PULSE;
MonitorTimer0OverflowFlag();
}
}
void ConfigTimer0(void)
{
// master clock is 12MHz
// timer clock is 1/12th => 1MHz
// in 16-bit mode timer overflow
// after 65536 usec.
// for overflow every 50,000 usec
// 65,536-50,000 = 15,536 (0x3CB0)
TL0 = 0xB0;
TH0 = 0x3C;
// set timer-0 mode-1 (16-bit)
TMOD = 0x01;
// start timer-0
TR0 = 1;
}
void MonitorTimer0OverflowFlag(void)
{
// monitor timer-0 overflow flag
while(TF0 == 0);
// reset overflow flag
TF0 = 0;
// reload value
TL0 = 0xB0;
TH0 = 0x3C;
}
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-0
ConfigTimer0();
while(1)
{
PULSE = ~PULSE;
MonitorTimer0OverflowFlag();
}
}
void ConfigTimer0(void)
{
// master clock is 12MHz
// timer clock is 1/12th => 1MHz
// in 16-bit mode timer overflow
// after 65536 usec.
// for overflow every 50,000 usec
// 65,536-50,000 = 15,536 (0x3CB0)
TL0 = 0xB0;
TH0 = 0x3C;
// set timer-0 mode-1 (16-bit)
TMOD = 0x01;
// start timer-0
TR0 = 1;
}
void MonitorTimer0OverflowFlag(void)
{
// monitor timer-0 overflow flag
while(TF0 == 0);
// reset overflow flag
TF0 = 0;
// reload value
TL0 = 0xB0;
TH0 = 0x3C;
}
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