Summary of How to use Timers in PIC18F4550 Microcontroller
Timers in PIC18F4550 handle time-related tasks like precise delays, PWM, and peripheral triggering. This article explains delay calculation (example: 1 s with 12 MHz crystal), details Timer0–Timer3 features, and describes Timer0 registers (T0CON, INTCON, TMR0H/TMR0L). It provides programming steps and example code to generate a 1 s delay using Timer0 to sequentially blink eight LEDs, plus project components and a demonstration video.Parts used in the Timer0 1 second delay project:
- PIC18F4550 microcontroller
- LEDs (8)
- 12 MHz crystal oscillator
- Resistors for LEDs
- Power supply (suitable for PIC18F4550)
- Connecting wires and breadboard or PCB
|
Bit 7
|
Bit 6
|
Bit 5
|
Bit 4
|
Bit 3
|
Bit 2
|
Bit 1
|
Bit 0
|
|
TMR0ON
|
T08BIT
|
T0CS
|
T0SE
|
PSA
|
T0PS2
|
T0PS1
|
T0PS0
|
Fig. 2: Bit Configuration of PIC18F4550 Timer0
|
T0PS2:T0PS0
|
Prescale Value
|
|
000
|
1:256
|
|
001
|
1:128
|
|
010
|
1:64
|
|
011
|
1:32
|
|
100
|
1:16
|
|
101
|
1:8
|
|
110
|
1:4
|
|
111
|
1:2
|
|
Bit 7
|
Bit 6
|
Bit 5
|
Bit 4
|
Bit 3
|
Bit 2
|
Bit 1
|
Bit 0
|
|
GIE/GIEH
|
PEIE/GIEL
|
TMR0IE
|
INT0IE
|
RBIE
|
TMR0IF
|
INT0IF
|
RBIF
|
Fig. 4: Bit Configuration of Interrupt Control Register in PIC18F4550
Project Source Code
###
// Program to use Timer0 of PIC18F4550 Microcontroller
void T0_init();
void main()
{
TRISB=0; // COnfigure PortB as output Port.
LATB=0x01;
T0CON=0x07; // Prescaler= 1:256, 16-bit mode, Internal Clock
while(1)
{
T0_init(); // Initialize Timer0
LATB=(LATB<<1)|(LATB>>7); // Circular right shift at PortB
}
}
void T0_init()
{
TMR0H=0xD2; // Values calculated for 1 second delay with 12MHz crystal
TMR0L=0x39;
T0CON.TMR0ON=1; // Timer0 On
while(INTCON.TMR0IF==0); // Wait until TMR0IF gets flagged
T0CON.TMR0ON=0; // Timer0 Off
INTCON.TMR0IF=0; // Clear Timer0 interrupt flag
}
###
Project Components
Project Video
Source: How to use Timers in PIC18F4550 Microcontroller
Quick Solutions to Questions related to Timer0 1 second delay project:
- What is the timer internal frequency used for delay calculation?
The timer uses the internal instruction cycle frequency Fosc/4. - How is a 1 second delay calculated with a 12 MHz crystal?
Fosc/4 = 3 MHz; with prescaler 1:256 FTimer = 3000000/256 = 11718.75 Hz, TTimer = 85 µs, counts needed = 11718.75, load value = FFFF - 2DC6 = D239. - Can Timer0 operate in both 8-bit and 16-bit modes?
Yes, Timer0 can work as an 8-bit or 16-bit timer/counter. - How is the prescaler assigned or bypassed for Timer0?
Set PSA = 0 to assign the prescaler to Timer0; set PSA = 1 to bypass the prescaler. - How do you select the Timer0 clock source?
Use the T0CS bit: 1 selects external T0CKI pin, 0 selects internal instruction cycle clock. - What steps are required in software to generate 1 second using Timer0?
Select prescaler and mode in T0CON, load TMR0H/TMR0L, set TMR0ON, wait for TMR0IF, clear TMR0ON and TMR0IF, then reload to repeat. - Which register indicates Timer0 overflow?
TMR0IF in the INTCON register is the Timer0 overflow flag. - What value is loaded into TMR0H and TMR0L for 1 second delay with 12 MHz and 1:256 prescaler?
The article loads TMR0H = 0xD2 and TMR0L = 0x39. - Does Timer0 provide interrupt on overflow?
Yes, Timer0 can generate an interrupt on overflow when TMR0IE is enabled.
Related projects
PICDEM FS USB CDC Evaluation Board using PIC18F4550 with Proteus Simulation
Introduction The PICDEM FS USB CDC Evaluation Board is a practical microcontroller project built around the PIC18F4550, designed to demonstrate USB communication...
Peripheral Tutorials · PIC18F4550PICDEM FS USB Mass Storage Device using PIC18F4550 with Proteus Simulation
Introduction This microcontroller project demonstrates how to simulate a USB Mass Storage Device (MSD) using the PIC18F4550 in a Proteus simulation environment....
Development Boards & Kits · PIC18F2550Pinguino Project: A PIC Microcontroller Based Arduino
This is a complete tutorial to make a PIC Microcontroller based Arduino. This is a complete tutorial to make PIC microcontroller based...
Clocks & TimersAdaptive Alarm Clock: Build a Smart, User-Centric wake-Up system
Project Overview The goal of this ambitious undergraduate project was to develop a novel alarm clock capable of analyzing a person's unique...
Clocks & TimersDesigning Your Ideal Alarm Clock: An Introduction to PIC18
Within this laboratory session, you'll employ the C language to create an alarm clock, delving into the implementation of "C" programs on...
Clocks & TimersBuilding your Dream Alarm Clock Using the PIC18
Objective: In this laboratory session, your objective is to create an alarm clock using the C programming language. You will gain proficiency...