How to Use Counter-1 in AT89C1051 (16-bit Mode)

The following code demonstrate, how to configure timer/counter 1 as a 16-bit counter. Each time port 3.5 goes low, the 16-bit counter is incremented by 1. 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 ConfigTimer1AsCounter(void);
void Delayms(unsigned int x_time);
// port labling
sfr COUNT_PORT = 0x90;
sbit COUNT_1_IN = P3^5;
void main(void)
{
// make port as output
COUNT_PORT = 0x00;
// make pin as input
COUNT_1_IN = 1;
// config counter
ConfigTimer1AsCounter();
while(1)
{
if(COUNT_1_IN == 0)
{
COUNT_PORT = TL1;
Delayms(300);
}
}
}
void ConfigTimer1AsCounter(void)
{
// set timer1 for 16-bit counter mode.
// set C/T bit for counter
TMOD = 0x50;
// empty counter registers
TH1 = 0x00;
TL1 = 0x00;
// start counter
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

Popular posts from this blog

How to Program Interrupts in PIC16F877A

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)