In our previous 8051 LED blinking example we used loop iterations to generate the delay but, in this post, we are trying to make use of 8051 timers. With the help of 8051 timers, we will generate exactly 1 second delay. We will explain the process of timer configuration, timer registers, timer register value calculation process and finally we will see practical example of 8051 timer assembly code to generate 1 second delay using timers in 8051 assembly language code. So, Let’s get started.
Table of Contents
Introduction to 8051 Timers
The 8051 microcontroller has two built-in 16-bit timers/counters, Timer 0 and Timer 1. These timers can be used to generate precise time delays, count external events, or even generate baud rates for serial communication.
Time delay calculation in 8051
To create a specific delay, you need to perform a simple yet crucial calculation. Suppose you want to create a delay of D
microseconds. The first step is to determine how many ticks of the timer clock (each 1 µsecond, assuming 12Mhz Crystal) are required for this delay. This is straightforward:
Next, you need to adjust this value to fit within the timer’s counting range. Since the timer can count up to 65535, you subtract the desired number of ticks from 65536. This gives you the initial value to load into the timer registers (TH and TL):
This value ensures that the timer will overflow exactly after D
microseconds, thus generating the precise delay you need.
Deriving the Timer Clock Frequency
At the heart of the 8051 microcontroller is its oscillator, a component that generates a clock signal at a specific frequency, in this case, 12 MHz. This clock signal drives the entire microcontroller, including its timers. However, the timer doesn’t operate directly at this frequency. Instead, the timer clock input is derived from the oscillator output and then divided by 12. This division results in a timer clock frequency of 1 MHz, which means that the timer increments its count every microsecond (1 µs).
8051 Timer Limits
The 8051 microcontroller features a 16-bit timer, capable of counting from 0 to 65535. This range corresponds to a maximum count of 65536 ticks. Since each tick represents one microsecond, the maximum time delay the timer can measure before it overflows and resets is 65536 microseconds (or approximately 65.536 milliseconds).
An Example: Blinking an LED
Let’s get back to our today’s focus to generate delay with timers. So we want to blink an LED with a delay of 1 second (1,000,000 microseconds). Here’s how you would calculate and implement this delay:
Calculate the Timer Initial Value:
First of all let’s create the initial timer value which in our case could be calculated something like following.
Since 1,000,000 is much larger than 65536, you need to handle overflow correctly. Each full timer cycle of 65536 µs means multiple timer overflows.
Each overflow will take:
Therefore, the initial value will be:
We also generated one second delay with two 500 millisecond delays in our Frequency Counter 8051 example code. You can check that working example.
8051 assembly code for 1 second delay
Here is the complete code for 1 second delay using the 8051 timers with the help of polling method without using timer interrupts. In this example we are not using any interrupt but if you are interested in interrupt based code our other article cover that which you can check out later.
ORG 0H ; Origin, set program counter to 0
MOV TMOD, #01H ; Set Timer 0 in Mode 1 (16-bit timer mode)
MAIN_LOOP:
MOV TH0, #78H ; Load high byte of initial value (31040 in hex is 0x78C0)
MOV TL0, #C0H ; Load low byte of initial value
SETB TR0 ; Start Timer 0
WAIT_LOOP:
JNB TF0, WAIT_LOOP ; Wait for Timer 0 to overflow (TF0 = 1)
CLR TR0 ; Stop Timer 0
CLR TF0 ; Clear overflow flag
CPL P1.0 ; Toggle LED connected to P1.0
SJMP MAIN_LOOP ; Repeat the loop
END
Code language: AVR Assembler (avrasm)