Timers are essential components in microcontroller-based projects, as they allow for precise timekeeping and event scheduling without the need for manual delays, which can block other processes. The PIC16F877A microcontroller has three timers—Timer0, Timer1, and Timer2—each offering different features for a variety of applications. In this post, we will break down the working principles of each timer and provide example code to show how they can be used in real-world embedded projects.
Overview of PIC16F877A Timers
Timer0:
- 8-bit timer.
- Can be configured as a counter (counts external events) or a timer (counts internal clock cycles).
- Can generate interrupts.
Timer1:
- 16-bit timer.
- Can operate as a timer or counter.
- Can be used for external event counting or clock generation.
Timer2:
- 8-bit timer with a built-in prescaler and postscaler.
- Primarily used for generating Pulse Width Modulation (PWM) signals.
Timer Configuration Basics
Each timer has configurable prescaler values, which divide the clock to control how fast the timer counts. You can also configure the timer to generate an interrupt when it overflows (reaches its maximum value and resets to 0), allowing you to execute code at specific intervals.
Key Registers for Timers:
- TMR0, TMR1H, TMR2: Timer registers that hold the current count value.
- T0CON, T1CON, T2CON: Control registers that configure the timers.
- INTCON, PIE1: Interrupt control registers used to enable/disable timer interrupts.
Example 1: Using Timer0 to Create Delays
Let’s configure Timer0 to generate a 1-second delay.
Steps:
- Set the timer mode (internal clock).
- Choose the appropriate prescaler.
- Calculate the value to preload the timer for a 1-second overflow.
- Enable the Timer0 interrupt.
Example Code:
Explanation:
- We configure Timer0 in timer mode with a prescaler of 256. The 8-bit timer overflows every ~16 ms, so 61 overflows give approximately 1 second.
- The
TMR0
register is preloaded with60
to adjust the timing for a 1-second delay. - PORTB is used to toggle an LED connected to pin RB0.
Circuit Diagram
Example 2: Using Timer1 for External Event Counting
Timer1 can be used to count external pulses. For instance, we could connect a switch to count how many times the button is pressed.
Steps:
- Set Timer1 to counter mode.
- Configure the external pin.
- Display the count on an LED or LCD.
Example Code:
Explanation:
- Timer1 is configured in counter mode and connected to an external pin that will increment the counter for each pulse received.
- The current count is displayed on PORTB, where LEDs are connected to visualize the count.
Example 3: Generating a PWM Signal Using Timer2
Timer2 is often used to generate PWM signals, useful in controlling motor speed, brightness of LEDs, etc.
Steps:
- Set Timer2 to PWM mode.
- Configure the PWM duty cycle and frequency.
- Output the PWM signal on a designated pin.
Example Code:
Explanation:
- Timer2 is set up to generate a PWM signal on RC2.
- The PWM duty cycle is set to 50%, meaning the output signal is HIGH for half the period and LOW for the other half.
Conclusion
Timers in the PIC16F877A provide versatile tools for handling time-based tasks such as creating delays, counting external events, and generating PWM signals. By understanding how to configure and use each timer, you can enhance the functionality of your embedded projects. Whether you need accurate delays or precise event scheduling, timers are indispensable components for any microcontroller-based system.
Further Reading: