Calculating Timer Values for Delays (Mode 1) - 3.3 | Experiment 7: "Microcontroller Fundamentals: 8051 Basic I/O and Timers" | Microcontroller Lab
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Understanding Timer Mode 1

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Today, we'll discuss Timer Mode 1. In this mode, we have a 16-bit timer that counts for each machine cycle. Can anyone remind me what a machine cycle is?

Student 1
Student 1

Isn't that how long it takes for the processor to execute an instruction?

Teacher
Teacher

Exactly! For a standard 8051 with a 12 MHz crystal, each machine cycle takes 1 microsecond. So, our timer increments 1,000,000 times a second. How would this information assist us?

Student 2
Student 2

We could calculate delays using the timer!

Teacher
Teacher

Right! The formula is critical here: Initial Value = 65536 - Desired Delay in microseconds. Can anyone explain why we subtract from 65536?

Student 3
Student 3

Because the maximum count is 65536, so if we want a specific delay, we need to set the timer to overflow after that time.

Teacher
Teacher

Excellent! So, based on our formula, for a 1000 µs delay, what initial value do we get?

Student 4
Student 4

64536, or 0xFA00 in hexadecimal!

Teacher
Teacher

Correct! That's how you set the timer to achieve your desired delay. Remember this formula—it will be crucial for your programming!

Practical Example: Generating a Delay

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Let's look at an example. If we want to generate a 500 ms delay using Timer 0, we first need to compute our initial value. Can anyone apply our earlier formula?

Student 1
Student 1

Sure! Initial Value = 65536 - 500,000 microseconds. That gives us an initial value of 65036.

Teacher
Teacher

Close, but remember, 500 milliseconds is 500,000 microseconds, so it's 65036, correct. But actually, for Timer 0, we would load this value in a different manner.

Student 2
Student 2

So we load that into TH0 and TL0?

Teacher
Teacher

Exactly! You load the high byte into TH0 and the low byte into TL0. After computing from decimal to hexadecimal, how would that look?

Student 3
Student 3

It will be 0xFA00, so TH0 = 0xFA and TL0 = 0x00.

Teacher
Teacher

Great! Next, what do we need to do after loading TH0 and TL0?

Student 4
Student 4

We start the timer with TR0 and monitor the TF0 flag for overflow.

Teacher
Teacher

Exactly! Monitoring the overflow flag is crucial for keeping track of the delay. You all are picking this up very well!

Polling Method for Timer Overflows

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Now, let's explore the procedure for delay generation using the polling method, a straightforward approach. Can anyone summarize the steps we discussed?

Student 1
Student 1

First, we configure TMOD to set the timer mode.

Teacher
Teacher

Correct! And then what comes next?

Student 2
Student 2

Then we load TH0 and TL0 with the initial value.

Student 3
Student 3

After that, we need to start the timer by setting TR0 to 1.

Teacher
Teacher

Exactly! And what do we do once the timer starts?

Student 4
Student 4

We monitor the TF0 flag until it indicates an overflow!

Teacher
Teacher

Perfect! After the flag is set, we stop the timer and clear the overflow flag. Remember, this method is vital for timing tasks without using interrupts.

Recap and Discussion

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

To summarize, we have learned the methods of calculating the timer values and generating delays using Timer 0. Who can recite the fundamental formula we derived for calculating the initial value?

Student 1
Student 1

Initial Value = 65536 - Desired Delay in microseconds.

Teacher
Teacher

Excellent! And why is that calculation important?

Student 2
Student 2

It helps us set the timer so that it overflows after the specified delay.

Teacher
Teacher

Absolutely! Can you also explain the steps in using the polling method?

Student 3
Student 3

1. Configure TMOD, 2. Load TH0 and TL0, 3. Start Timer, 4. Wait for overflow, 5. Stop Timer, and 6. Clear the overflow flag.

Teacher
Teacher

Excellent recall! With this knowledge, you will be able to effectively use timers in your microcontroller programming. Remember the practical aspects and keep practicing!

Introduction & Overview

Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

This section explains how to calculate the initial timer values for generating specific delays using Timer 0 in Mode 1 of the 8051 microcontroller.

Standard

In this section, the relationship between the timer's initial value and the desired delay is established. It focuses on how to calculate the initial value based on the delay required, emphasizing the significance of the microcontroller's clock frequency in the calculations. Practical examples illustrate these concepts.

Detailed

In Mode 1 of the 8051 microcontroller, the timer operates as a 16-bit counter that increments for each machine cycle. The machine cycle for a standard 8051 with a 12 MHz crystal is 1 microsecond, resulting in the timer counting up to 1,000,000 increments per second. To generate a specific delay, you must determine a suitable initial value for the timer, which is calculated as follows:

Formula for Initial Timer Value (Mode 1, 16-bit Timer):
Initial Value = 65536 - (Desired Delay in microseconds)

This calculation is crucial, as the maximum count of the 16-bit timer is 65536. The section also provides an example where a 1 ms (1000 µs) delay is generated by loading the timer with an initial value of 64536, which corresponds to 0xFA00 in hexadecimal. Detailed procedural steps for delay generation using the polling method are offered, making this section essential for understanding timer utilization in microcontroller applications.

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Understanding Timer Increments

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

In Mode 1 (16-bit timer), the timer increments by 1 for every machine cycle. A standard 8051 with a 12 MHz crystal has a machine cycle time of 1 microsecond (µs). This means the timer increments 1,000,000 times per second.

Detailed Explanation

In Timer Mode 1 of the 8051 microcontroller, the timer counts up by 1 each time the machine cycle occurs. For a typical 8051 running at a 12 MHz clock speed, each cycle lasts 1 microsecond. To put it simply, every million microseconds (or 1 second), the timer counts up to 1,000,000. This increment happens every time the microcontroller completes an instruction cycle.

Examples & Analogies

Think of a timer as a tally counter during a game. Each time you complete a round (machine cycle), you add one tally mark. If you can complete a round every microsecond, after a whole second, you'll have a million tally marks.

Calculating the Initial Timer Value

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

To generate a specific delay, we need to load the timer with an initial value such that it overflows after the desired time. The maximum count for a 16-bit timer is 65536 (from 0 to 65535).

Formula for Initial Timer Value (Mode 1, 16-bit Timer):
Initial Value = 65536 - (Desired Delay in microseconds)

Detailed Explanation

The aim here is to set the timer so that it counts up to its maximum value (65535) and then resets, which is known as overflowing. To determine what number we need to load into the timer before it begins counting, we subtract the desired delay in microseconds from the maximum timer count. The result is the value that must be loaded into the timer for it to reach the overflow at the exactly desired delay.

Examples & Analogies

Imagine you're setting a timer on your oven. If you want it to ring after 10 minutes (600 seconds), and the maximum it can count to is 60 minutes (3600 seconds), you would set it to 3600 - 600, which equals 3000 seconds. This way, it will ring exactly when 10 minutes are up.

Applying the Formula: Example Calculation

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

For a 12 MHz crystal:
Initial Value = 65536 - Desired Delay (in µs)
Example: Generate a 1 ms (1000 µs) delay using Timer 0, Mode 1 with a 12 MHz crystal.

Initial Value = 65536 - 1000 = 64536
This 16-bit value (64536) needs to be loaded into TH0 and TL0.

Detailed Explanation

In this specific example, we want to create a delay of 1 millisecond. Using our formula, we subtract 1000 from 65536, resulting in 64536. This number is then converted into two separate bytes because the timer is 16-bit; one byte will go to TH0 (the high byte) and the other to TL0 (the low byte) to form the complete timer value.

Examples & Analogies

If you were preparing a 2-liter bottle of punch and had to fill it up with 300 ml of syrup, you would think: 2000 ml (the full bottle) minus 300 ml gives you how much water to add. Similarly, instead of doing it directly, we break down the final amount into two parts, like doing the syrup and water separately.

Converting to Hexadecimal

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

To convert 64536 to hexadecimal: 64536_10=FA00_16
So, TH0 = 0xFA and TL0 = 0x00.

Detailed Explanation

Converting decimal numbers to hexadecimal is a common task in microcontroller programming. In this case, the decimal value of 64536 is converted to hexadecimal, which results in FA00. The high byte, TH0, is set to FA, and the low byte, TL0, is set to 00. This is important as the microcontroller expects these values in hexadecimal format.

Examples & Analogies

Consider converting currency from dollars to cents. If you have 645 dollars, you would need to express that in cents, which would be 64500 cents. In the same way, if we think of hexadecimal as a special way of packing numbers for electronic devices, we need to understand how to break it down into forms they can easily read.

Procedure for Delay Generation

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

Procedure for Delay Generation using Timers (Polling Method - Mode 1):
1. Configure TMOD: Set the desired timer mode (e.g., Mode 1 for Timer 0: TMOD = 0x01).
2. Load Timer Registers: Load THx and TLx with the calculated initial value.
3. Start Timer: Set the TRx bit in TCON to 1 (e.g., TR0 = 1).
4. Wait for Overflow: Monitor the TFx flag in TCON. Wait until TFx becomes 1.
5. Stop Timer: Clear the TRx bit (e.g., TR0 = 0).
6. Clear Flag: Clear the TFx flag (e.g., TF0 = 0). This is crucial, especially if you're not using interrupts, as the flag needs to be ready for the next overflow.

Detailed Explanation

This section outlines the step-by-step method to generate a delay using the timer in Mode 1. First, we configure the timer mode in the TMOD register. Next, we load the calculated values for TH0 and TL0. Once the timer is started by setting the TR0 bit, we actively wait for the overflow flag (TF0) to indicate that the timer has exceeded its set value. Upon overflow, we stop the timer and clear the flag to prepare for future use. This sequence ensures that we get precise timing in our applications.

Examples & Analogies

Think about baking a cake where you set a timer on the oven. First, you choose the mode according to the cake type (configure TMOD), you add the ingredients (load timer registers), and then set the timer (start timer). You keep an eye on it to know exactly when it's done (waiting for overflow). Once it rings, you turn it off (stop timer) and reset it for the next cake (clear flag).

Definitions & Key Concepts

Learn essential terms and foundational ideas that form the basis of the topic.

Key Concepts

  • Timer Mode 1: The mode that allows the timer to function as a 16-bit counter.

  • Initial Value Calculation: A formula to set the timer for a desired delay.

  • Overflow Flag: A flag that indicates the timer has reached its maximum count and wrapped around.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • To generate a 1 ms delay using a 12 MHz crystal, the initial timer value is calculated as 65536 - 1000 (µs), yielding 64536, which is 0xFA00 in hexadecimal.

  • For a 500 ms delay, the initial value would be 65536 - 500000 = 65036, which shows the need to correctly scale delays.

Memory Aids

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎵 Rhymes Time

  • For a timer to be right, set the initial value tight, subtract from six-five-three-six, and delays you will fix.

📖 Fascinating Stories

  • Imagine a stopwatch in a race, it counts every tick! To set it right and not miss the place, knowing the starting number does the trick.

🧠 Other Memory Gems

  • Dare - Delay equals 65536 minus initial. (D for Delay)

🎯 Super Acronyms

TID - Timer Initial Delay

  • Remember this acronym to calculate initial timer values.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: Timer Mode 1

    Definition:

    A mode of operation for the 8051 microcontroller's timer where it functions as a 16-bit timer or counter.

  • Term: Initial Value

    Definition:

    The starting value loaded into the timer registers to achieve a specific delay.

  • Term: Overflows

    Definition:

    When a timer count exceeds its maximum value and wraps around to zero, triggering an overflow flag.

  • Term: Polling Method

    Definition:

    A technique of checking the status of a device or flag repeatedly to determine if it has completed an operation or indicates a condition, such as a timer overflow.