Step 5 of 11
Configure Timer0 (no ISR yet)
Goal
Set up Timer0 in 16-bit mode with prescaler 1:256 and initial value for ~2 s @ 20 MHz.
Timer0 is the most common interrupt source when starting. Flags TMR0IE and TMR0IF are in INTCON — not PIE1/PIR1.
Important: Timer0 does not need PEIE. Chain is: set T0CON + TMR0H/L → then (step 5) enable TMR0IE and GIE.
Values 0x676A come from Timer 0 — 2 s example.
Follow these steps
- Replace empty
init_timer0RETURN with highlighted lines (T0CON, TMR0H, TMR0L). - Do not set TMR0ON or GIE yet — only configure the timer.
- Build with no errors.
- Optional: compare T0CON with the table in Timer 0.
Your file so far
LIST P=18F4550
#include <P18F4550.INC>
CONFIG FOSC = HS
CONFIG WDT = OFF
CONFIG LVP = OFF
CONFIG PBADEN = OFF
ORG 0x0000
GOTO inicio
ORG 0x0008
GOTO isr_alta
ORG 0x0018
GOTO isr_baja
CBLOCK 0x20
temp_w
temp_status
ENDC
init_gpio:
BCF TRISB, 0
BCF LATB, 0
RETURN
init_timer0:
MOVLW B'00000111' ; 16 bit, interno, prescaler 1:256
MOVWF T0CON
MOVLW B'01100111' ; TMR0H = 0x67
MOVWF TMR0H
MOVLW B'01101010' ; TMR0L = 0x6A (~2 s @ 20 MHz)
MOVWF TMR0L
RETURN
isr_alta:
RETFIE
isr_baja:
RETFIE
inicio:
CALL init_gpio
CALL init_timer0
bucle:
GOTO bucle
END
What you add in this step
init_timer0:
MOVLW B'00000111' ; 16 bit, interno, prescaler 1:256
MOVWF T0CON
MOVLW B'01100111' ; TMR0H = 0x67
MOVWF TMR0H
MOVLW B'01101010' ; TMR0L = 0x6A
MOVWF TMR0L
RETURN
Checklist before you continue
- `init_timer0` loads T0CON and TMR0H/L initial values.
- I understand Timer0 does not use PEIE.
- Builds with no errors.
Tip: PEIE is for Timer1/2/3, UART, ADC… — not Timer0.