Login

Lost your password?
Don't have an account? Sign Up

Configuring Timer module of PIC18F4550

Timers of PIC microcontroller

Timers are essential in embedded systems for generating time delays, measuring event durations, and scheduling periodic tasks. The PIC18F4550 microcontroller provides four timers (Timer0 to Timer3), each designed for specific timing and counting operations.

Timers allow your microcontroller to perform tasks like:

  • Creating accurate delays
  • Counting external pulses
  • Generating PWM signals
  • Scheduling periodic interrupts
  • Measuring time between events
TimerBitsModeSpecial Features
Timer08/16Timer/CounterConfigurable prescaler, simple delay generation
Timer116Timer/CounterCan use an external oscillator, ideal for a real-time clock
Timer28TimerCan use an external oscillator, ideal for real-time clock
Timer316Timer/CounterOften used with CCP modules for advanced timing

A timer is essentially a counter that increments with each clock tick.
When the counter reaches its maximum value, it overflows and can trigger an interrupt or a specific event.

  • Internal clock (Fosc / 4) → Timer mode
  • External clock signal → Counter mode

Each timer has control and count registers. For example, Timer0 includes:

  • T0CON – Control register
  • TMR0H:TMR0L – 16-bit counter register
  • INTCON – Interrupt control bits

Example configuration for Timer0 (16-bit mode):

T0CON = 0b00000100;  // 16-bit mode, prescaler 1:32
TMR0H = 0x0B;        // Load high byte
TMR0L = 0xDC;        // Load low byte (for 1ms delay at 48MHz)
TMR0ON = 1;          // Turn ON Timer0
while(!INTCONbits.TMR0IF); // Wait for overflow
TMR0ON = 0;          // Stop Timer
INTCONbits.TMR0IF = 0; // Clear flag
C

Timer delay depends on: Delay = [(65536−TMR preload)×Prescaler] / (Fosc/4)

Example:
For a 1 ms delay at 48 MHz with a prescaler of 32,

Preload = 65536 – [ { (48 × 106/4) × 0.001 } / 32 ] = 30364

Circuit:
Connect an LED to RB0 through a 330Ω resistor.

Code Example:

#include <xc.h>
#define _XTAL_FREQ 48000000

void Timer0_Delay_ms(unsigned int ms)
{
    for(unsigned int i=0; i<ms; i++)
    {
        T0CON = 0b00000100; // Prescaler 1:32
        TMR0H = 0x0B;
        TMR0L = 0xDC;
        TMR0ON = 1;
        while(!INTCONbits.TMR0IF);
        TMR0ON = 0;
        INTCONbits.TMR0IF = 0;
    }
}

void main(void)
{
    TRISB0 = 0;
    while(1)
    {
        LATB0 = 1;
        Timer0_Delay_ms(500);
        LATB0 = 0;
        Timer0_Delay_ms(500);
    }
}
C

  • LED blinking or delay generation
  • Time measurement or event counting
  • PWM signal generation (Timer2 + CCP)
  • Real-time clock applications (Timer1 + external crystal)
  • Task scheduling in real-time systems

To explore timer interrupts, PWM generation, and time-based control projects:
🎓 Join our Full Practical Video Course on PIC18F4550 Microcontroller — including timers, ADC, UART, I2C, SPI, and USB modules with hands-on examples.

1. How many timers are available in PIC18F4550?

There are four timers: Timer0, Timer1, Timer2, and Timer3.

2. What’s the difference between Timer and Counter mode?

Timer mode counts internal clock pulses, while Counter mode counts external events on the timer input pin.

3. Which timer is best for PWM?

Timer2 is typically used for PWM generation in CCP modules.

4. Can timers generate interrupts automatically?

Yes, timers can trigger interrupts on overflow or match events.

5. What is the use of prescaler?

A prescaler divides the clock frequency to slow down the timer count rate for longer delay generation.

6. Can I use multiple timers at once?

Yes! Each timer operates independently and can be configured simultaneously.

7. Which software tools do I need?

Use MPLAB X IDE and XC8 compiler for firmware development.

Home » Recent posts » Learn › Configuring USB Module of PIC18F4550

https://bitziga.com

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*