
Configuring Timer module of PIC18F4550
Timers of PIC microcontroller
Complete Video Course on PIC18F4550 Microcontroller

- Complete Video Course on PIC18F4550 Microcontroller
- Introduction: Configuring Timers of PIC Microcontroller (PIC18F4550)
- Why Are Timers Important?
- Timer Modules in PIC18F4550
- Understanding Timer Operation
- Timer Clock Source:
- Registers Involved
- Timer Delay Calculation
- Practical Example: LED Blinking with Timer0
- Applications of Timers
- Learn More
- Complete Video Course on PIC18F4550 Microcontroller
- 💬Frequently Asked Questions (FAQ)
Introduction: Configuring Timers of PIC Microcontroller (PIC18F4550)
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.
Why Are Timers Important?
Timers allow your microcontroller to perform tasks like:
- Creating accurate delays
- Counting external pulses
- Generating PWM signals
- Scheduling periodic interrupts
- Measuring time between events
Timer Modules in PIC18F4550
| Timer | Bits | Mode | Special Features |
|---|---|---|---|
| Timer0 | 8/16 | Timer/Counter | Configurable prescaler, simple delay generation |
| Timer1 | 16 | Timer/Counter | Can use an external oscillator, ideal for a real-time clock |
| Timer2 | 8 | Timer | Can use an external oscillator, ideal for real-time clock |
| Timer3 | 16 | Timer/Counter | Often used with CCP modules for advanced timing |
Understanding Timer Operation
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.
Timer Clock Source:
- Internal clock (Fosc / 4) → Timer mode
- External clock signal → Counter mode
Registers Involved
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 flagCTimer Delay Calculation
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
Practical Example: LED Blinking with Timer0
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);
}
}CApplications of Timers
- 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
Learn More
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.
➡️ Start Learning Now at “Master PIC18F4550 Programming“
Complete Video Course on PIC18F4550 Microcontroller

Software development tools & documents:
💬Frequently Asked Questions (FAQ)
There are four timers: Timer0, Timer1, Timer2, and Timer3.
Timer mode counts internal clock pulses, while Counter mode counts external events on the timer input pin.
Timer2 is typically used for PWM generation in CCP modules.
Yes, timers can trigger interrupts on overflow or match events.
A prescaler divides the clock frequency to slow down the timer count rate for longer delay generation.
Yes! Each timer operates independently and can be configured simultaneously.
Use MPLAB X IDE and XC8 compiler for firmware development.


