How to Use Counter-0 in AT89C1051 (16-bit Mode)
The following code demonstrate, how to configure timer/counter 0 as a 16-bit counter. Each time port 3.4 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 <REG51.H>
#include <stdio.h>
// proto-types
void ConfigTimer0AsCounter(void);
void ConfigUART(void);
void Delayms(unsigned int x_time);
// port labling
sfr COUNT_PORT = 0x90;
sbit COUNT_0_IN = P3^4;
#include <stdio.h>
// proto-types
void ConfigTimer0AsCounter(void);
void ConfigUART(void);
void Delayms(unsigned int x_time);
// port labling
sfr COUNT_PORT = 0x90;
sbit COUNT_0_IN = P3^4;
void main(void)
{
// make port as output
COUNT_PORT = 0x00;
// make pin as input
COUNT_0_IN = 1;
// config counter
ConfigTimer0AsCounter();
// config uart
ConfigUART();
printf(“Counter-0 in 16-bit mode\n”);
printf (“Press UP Count-0 button (T0 input)\n”);
while(1)
{
if(COUNT_0_IN == 0)
{
COUNT_PORT = TL0;
printf (“Counter-0 = %2.2bX%2.2bXh\n”, TH0, TL0);
Delayms(300);
}
}
}
void ConfigTimer0AsCounter(void)
{
// set timer0 for 16-bit counter mode.
// set C/T bit for counter
TMOD = (TMOD & 0xF0) | 0x05;
// empty counter registers
TH0 = 0x00;
TL0 = 0x00;
// start counter
TR0 = 1;
}
void ConfigUART(void)
{
// set serial port
SCON = 0x50;
// timer-1 in auto-reload mode
TMOD |= 0x20;
// set baud-rate 9600
TH1 = -3;
// start timer-1
TR1 = 1;
// set transmit interrupt if using printf command
TI = 1;
}
{
// make port as output
COUNT_PORT = 0x00;
// make pin as input
COUNT_0_IN = 1;
// config counter
ConfigTimer0AsCounter();
// config uart
ConfigUART();
printf(“Counter-0 in 16-bit mode\n”);
printf (“Press UP Count-0 button (T0 input)\n”);
while(1)
{
if(COUNT_0_IN == 0)
{
COUNT_PORT = TL0;
printf (“Counter-0 = %2.2bX%2.2bXh\n”, TH0, TL0);
Delayms(300);
}
}
}
void ConfigTimer0AsCounter(void)
{
// set timer0 for 16-bit counter mode.
// set C/T bit for counter
TMOD = (TMOD & 0xF0) | 0x05;
// empty counter registers
TH0 = 0x00;
TL0 = 0x00;
// start counter
TR0 = 1;
}
void ConfigUART(void)
{
// set serial port
SCON = 0x50;
// timer-1 in auto-reload mode
TMOD |= 0x20;
// set baud-rate 9600
TH1 = -3;
// start timer-1
TR1 = 1;
// set transmit interrupt if using printf command
TI = 1;
}
void Delayms(unsigned int x_time)
{
unsigned int x,y;
for(x=0;x<x_time;x++)
for(y=0;y<122;y++);
}
{
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