0% found this document useful (0 votes)
18 views

Lab 9 - Introduction of Timers & their configuration to generate time-based interrupts

This document outlines an experiment focused on timers and their configuration for generating time-based interrupts in microcontrollers, specifically using the PIC16F877A. It details the principles of timer operation, applications, and tasks to implement time-based interrupts with specific time delays using C programming in MPLAB X IDE. Additionally, it provides instructions for reporting the findings and includes guidelines for simulation and circuit diagram documentation.

Uploaded by

qubabaig63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab 9 - Introduction of Timers & their configuration to generate time-based interrupts

This document outlines an experiment focused on timers and their configuration for generating time-based interrupts in microcontrollers, specifically using the PIC16F877A. It details the principles of timer operation, applications, and tasks to implement time-based interrupts with specific time delays using C programming in MPLAB X IDE. Additionally, it provides instructions for reporting the findings and includes guidelines for simulation and circuit diagram documentation.

Uploaded by

qubabaig63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Digital Systems & Microprocessors Lab

Session: 2022 (AVE-8) – Spring 2024

Experiment # 9
Introduction of Timers & their configuration to
generate time-based interrupts
Objective
• To get familiar with Timers and their applications
• To generate time-based interrupts of specific time intervals (Simulation Based)

What are Timers in microcontrollers?


Timer is a counter which counts.
UP Counting: Counts from Pre-programmed value to Max Value
Down Counting: Counts from Pre-programmed value to 0
From Example, in case of 8-bit Timer, the counter ranges form 0-255. Pre-Programmed value can be
anywhere between 0-255.

Timers in microcontrollers are hardware peripherals designed to measure time intervals, generate timing
events, and control the timing of various operations within the microcontroller. They are incredibly versatile
and find applications in a wide range of tasks, from generating precise time delays to controlling the
execution time of specific operations.

Applications:
1. Time Measurement: Timers can measure elapsed time accurately by counting clock cycles or
external events. This feature is useful for tasks such as measuring the duration of an event or
generating time stamps.
2. Timing Events: Timers can be configured to generate interrupts or trigger events at specific
intervals. This capability is essential for tasks that require periodic execution, such as sampling
sensors or updating display screens.
3. Pulse Width Modulation (PWM) (Fixed Frequency & Variable Duty Cycle): Many timers in
microcontrollers support PWM functionality. PWM is used to control the power supplied to devices
like motors, LEDs, and heaters by varying the duty cycle of a square wave signal.
4. Frequency Generation (Variable Frequency & Fixed Duty Cycle): Timers can generate precise
clock signals with adjustable frequencies. This feature is handy in applications like communication
systems, where accurate timing is crucial for data transmission.
5. Input Capture (Time Measurement): Some timers have input capture functionality, allowing them
to record the timestamp when an external event occurs. This feature is useful for tasks such as
measuring the frequency of an incoming signal or capturing the time of external events.

Department of Avionics Engineering Page 1|5


Institute of Space Technology, Islamabad
Digital Systems & Microprocessors Lab
Session: 2022 (AVE-8) – Spring 2024
6. Output Compare (PWM / Freq. Gen.): Timers can compare their count value with a pre-defined
threshold and trigger an action when the two values match. This feature is commonly used in
generating precise timing for controlling peripherals or generating waveforms.
7. Real-Time Clock (RTC): In some microcontrollers, timers are used to implement real-time clock
functionality, providing accurate timekeeping even in low-power modes. Timers are essential for
precise timing control and synchronization in embedded systems.
Overall, timers play a crucial role in the operation of microcontrollers, enabling precise timing control
and synchronization in various applications. Their versatility makes them one of the fundamental
components of embedded systems design.

Note: For Further details, consult the reference manual of PIC16F877A.

Registers for Timer0 in PIC16F877A:

Department of Avionics Engineering Page 2|5


Institute of Space Technology, Islamabad
Digital Systems & Microprocessors Lab
Session: 2022 (AVE-8) – Spring 2024

Department of Avionics Engineering Page 3|5


Institute of Space Technology, Islamabad
Digital Systems & Microprocessors Lab
Session: 2022 (AVE-8) – Spring 2024
Timer Working Principle:
• For UP Count Mode, Timer counts from Pre-Programmed value to Max Value and at
the max value timer overflow interrupt / timer ISR triggered & this process continues.
• Since each count means a machine cycle and in PIC a machine cycle (fetch, decode,
execute and store) requires 4 clock cycles so the formula for time delay / time between
two consecutive timer overflow events can be expressed as:

Time delay = (Max Value – Pre-Programmed Value) * 4 * One Clock Cycle Time
While
One Clock Cycle Time = 1 / Clock Frequency
So,
Time delay = (Max Value – Pre-Programmed Value) * 4 / Clock Frequency

• To enhance the maximum limit of time delay (i.e, to enhance the number of bits of
Timer Register), a Pre-Scalar register option is available in almost all the MCUs. So,
formula can be rewritten as:

Time delay = (Max Value – Pre-Programmed Value) *Pre-Scalar * 4 / Clk Frequency

Task 1: Implement time-based interrupt for 1 second time delay


• Write and understand the following C code in MPLAB X IDE:
Don’t forget to add the configuration preprocessor directives i.e., #pragma … in the start of your
program.

Department of Avionics Engineering Page 4|5


Institute of Space Technology, Islamabad
Digital Systems & Microprocessors Lab
Session: 2022 (AVE-8) – Spring 2024

• Connect an LED to RB1 in Proteus and Load the .hex file of above program and observe the output.
Explain it in your report.
Note: 20 MHz Clock Frequency is used in the above simulation.

Task 2:
• Implement time-based interrupt for 300 m seconds time delay
• Use 4 MHz clock frequency in your simulation
• Show your detailed calculation and c programming in report

Task 3:
• Compose a concise report for submission in the forthcoming lab session, presenting it in hard copy
format.
• Include images of simulations, circuit diagram, along with your comments for all tasks within the
report.
• On the title page of your report, include only the following details: Experiment number and name,
Your name, Registration number, Section, and a thumbnail (optional).
• Adhere strictly to a professional format for the entire report.
• Ensure that the report is printed on both sides of the paper.

----------------------------------------------

Department of Avionics Engineering Page 5|5


Institute of Space Technology, Islamabad

You might also like