AT89C51 Timer/Counter 0: Mode 0


One of the most basic yet versatile modes is Mode 0. When Timer 0 is set to mode 0, it operates as a 13-bit timer. The timer counts from 0 to 8191 and then overflows. Once the overflow flag is set, the timer resets and starts counting again from zero.

In mode 0, Timer 0 can be used as a delay generator or as a counter to count external events.

To start the timer, you need to set the TR0 bit in the TCON register.

To stop the timer, you need to clear the TR0 bit in the TCON register.

8051 / AT89C51 Timer 0 - Counter 0

AT89C51 TMOD Register

The TMOD register is an 8-bit register used for selecting the operation mode of Timer 0 and Timer 1.

Bit Number 7 6 5 4 3 2 1 0
Symbol GATE1 C/T1 M11 M10 GATE0 C/T0 M01 M00

In this table as you can see first 4 bits (from 0 to 3) are used to configure the Timer 0.

Bit Bit Name Description
7 GATE1 Timer 1 Gate Control
6 C/T1 Timer 1 Mode Control
5-4 M11, M10 Timer 1 Mode Select
3 GATE0 Timer 0 Gate Control
2 C/T0 Timer 0 Mode Control
1-0 M01, M00 Timer 0 Mode Select

GATE0 (Bit 3): This bit controls Timer 0. When GATE0 is set to 1, Timer 0 is controlled by the external INT0 interrupt input. Similar to GATE1, Timer 0 will count pulses only when the INT0 input is active. When GATE0 is cleared (0), Timer 0 functions normally without being influenced by external events.

C/T0 (Bit 2): This bit controls Timer 0's mode of operation. When C/T0 is set to 1, Timer 0 operates as a counter. In this mode, Timer 0 will count external pulses applied to the T0 pin. When C/T0 is cleared (0), Timer 0 operates as a timer, counting based on its own clock source and mode configuration.

M01 (Bit 1) and M00 (Bit 0): These two bits are used to select the operating mode of Timer 0. Depending on the combination of M01 and M00, you can choose from four different modes of operation:

00: Mode 0 - 13-bit Timer/Counter: Timer 0 operates as a 13-bit timer or counter. It counts from 0 to 8191 (2^13 - 1) before overflowing. This mode provides a relatively large counting range.

So we need to clear M01 and M00 bits to configuration Timer Mode 0.


   TMOD &= ~0x03;  //Mode 0 selected

M01 M00 MODE
0 0 MODE 0
0 1 MODE 1
1 0 MODE 2
1 1 MODE 3

AT89C51 Timer/Counter 0 in Timer Mode

Certainly, the "C/T0" bit in the Timer/Counter Mode (TMOD) register of the 8051 microcontroller family stands for "Counter/Timer 0" mode control. This bit determines whether Timer 0 operates in timer mode or counter mode.

C/T0 = 0

Timer 0 functions as a timer. It increments based on its clock source (system clock or external clock) and its selected mode.


TMOD &= ~(0x03); // Mode 0 
TMOD &= ~(0x02); // In Timer Mode ( C\T0 = 0)

Creating Delays with the Polling Method

Before delving into creating delays using the timer mode, let's understand how to configure the microcontroller for timer mode. In this example, we'll assume the microcontroller is running at a clock frequency of 12 MHz.

In timer mode, the internal machine cycles are counted, and consequently, the timer register increments with each machine cycle. When the microcontroller's clock frequency is 12 MHz, the timer register will increment in correspondence with each microsecond, rather than each millisecond.

In this mode, the microcontroller disregards the external timer input pin and relies solely on its internal timing mechanism.

The general formula to calculate the time delay is:

Timer_Delay = 2^n - Timer_Count / Timer_Clock_Frequency

Timer_Count = 2^n - (Timer_Delay * Timer_Clock_Frequency)

n is the number of bits of the timer. For example, in 13-bit mode, n = 13.

Timer_Count is the value the timer counts from (usually 0) to the overflow value.

Timer_Clock_Frequency is the clock frequency of the microcontroller.

For example, let's assume we want a 100us delay:

Timer Counts = 2^13 - (100 * 10^-6 * 12 * 10^6) (converted 100us to seconds and 12MHz to Hz)

Timer Counts = 8192 - 1200

Timer Counts = 6992

Timer Counts = 0x1B50

TL0 = 0x50

TH0 = 0x1B

Or, you can use this calculator 8051 Timer Calculator

Limitations and Considerations for Delay Generation

As we explore creating delays using Timer/Counter 0 in timer mode, it's important to understand the limitations and considerations associated with this approach.

13-Bit Counter Limitation

Timer/Counter 0 operates with a 13-bit counter size, allowing it to count up to a maximum value of 8191 (2^13 - 1) before it overflows and resets.

This limitation impacts the maximum duration of delays that can be accurately generated using the polling method.

For instance, if attempting to achieve a delay as long as 1 second, the required count of timer cycles significantly exceeds the counter's capacity. As a result, the microcontroller cannot reliably produce delays of such magnitude using Timer/Counter 0 in this mode.

Efficiency and Alternatives

While Timer/Counter 0 and the polling method are efficient for generating shorter delays, it's important to consider alternative approaches for longer delays.

Employing external components or exploring other timer modes with larger counters becomes essential for accurate and consistent delay generation.

Understanding these limitations empowers us to make informed decisions when selecting the appropriate method for achieving specific delay requirements in our projects.

Basic Code Example

#include <reg51.h>

void Timer100usDelay( void ) {

    TF0 = 0;// Clear The Timer Flag
    TR0 = 0;// Stop The Timer

    TMOD &= ~0x03; //Selected Timer/Counter 0 Mode 0
    TMOD &= ~0x02; //Selected Timer/Counter 0 in Timer Mode
	  
    TL0 = 0x50; //Load Timer Counts Low Value
    TH0 = 0x1B; //Load Timer Counts High Value
	
    TR0 = 1; // Start The Timer
    while(TF0 == 0); //Wait For Timer To Complete	  
}

int main() {
	
	while(1) {
	
		Timer100usDelay();	
	}
	
	return 0;
}
Loading...

Search