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
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;
}