Microcontroller (Unit 4)
Model Answers for PIC18F4550
Engineering Study Notes
December 9, 2025
Contents
1 Theory: Timers 2
1.1 Timer 0 Operation (8-bit) and Comparison . . . . . . . . . . . . . . . . . . . . . 2
1.2 Timer 1 Operation (16-bit) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Timer 2 Block Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 T0CON and T1CON Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Theory: CCP Module 5
2.1 CCP Capture Mode and PIR Register . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 CCP Compare Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Theory: ADC and Interrupts 7
3.1 ADC Block Schematic and Registers . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Interrupt Structure (Priority Mode) . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Programming Questions 10
4.1 PWM Generation Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.2 Question 9: 2.5 KHz, 75% Duty . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.3 Question 10: 2.5 KHz, 50% Duty . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.4 Question 11: 1 KHz, 10% Duty . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.5 Question 12: Square Wave 1KHz, 25% Duty . . . . . . . . . . . . . . . . . . . . 11
5 Delay Programming 13
5.1 Question 13: 10ms Delay using Timer 1 (No Prescaler) . . . . . . . . . . . . . . 13
5.2 Question 14: 10ms Delay using Timer 1 (1:16 Prescaler) . . . . . . . . . . . . . 13
5.3 Question 15: 10ms Delay using Timer 0 (16-bit, No Prescaler) . . . . . . . . . . 13
5.4 Question 16: 1ms Delay using Timer 0 (16-bit, No Prescaler) . . . . . . . . . . . 14
1
1 Theory: Timers
1.1 Timer 0 Operation (8-bit) and Comparison
Question: Draw and explain the Timer 0, 8-bit operation in detail and compare Timer 0, 1,
and 2.
Timer 0 in 8-bit Mode
Timer 0 is a flexible timer/counter that can operate as an 8-bit or 16-bit timer. In 8-bit mode,
it functions as follows:
• Register: It uses the TMR0L register to store the count value. The TMR0H register is not
used for counting in this mode.
• Count Range: It counts from 00h to F F h (0 to 255).
• Rollover/Overflow: When the counter increments from F F h to 00h, the Timer 0
Interrupt Flag (TMR0IF) is set.
• Control: The mode is selected by setting bit 6 (T08BIT) of the T0CON register to 1.
Figure 1: Timer 0 8-bit Mode Block Diagram
Comparison of Timer 0, 1, and 2
Feature Timer 0 Timer 1 Timer 2
Resolution 8-bit or 16-bit 16-bit 8-bit
Prescaler 8-bit (programmable) 2-bit (1:1 to 1:8) 2-bit (1:1 to 1:16)
Postscaler None None 4-bit (1:1 to 1:16)
Clock Source Internal / External Internal / External Internal Only (Fosc /4)
PWM Support No No Yes (used for PWM base)
Table 1: Comparison of Timers
2
1.2 Timer 1 Operation (16-bit)
Question: Draw and explain the Timer 1, 16-bit operation in detail and compare Timer 0, 1,
and 2.
Timer 1 Operation
Timer 1 is a 16-bit timer/counter.
• Registers: It uses two 8-bit registers: TMR1H (High byte) and TMR1L (Low byte) to form
a 16-bit count (0000h to F F F F h).
• Operation: It increments on every instruction cycle (Fosc /4) or external clock edge.
• Read/Write: To prevent reading invalid data during a rollover (e.g., reading Low byte
just as High byte increments), PIC18 uses an internal buffer for the High byte. RD16 bit
controls 16-bit read/write operations.
• Oscillator: Timer 1 has an internal oscillator circuit allowing it to run from a 32kHz
crystal (used for Real Time Clock applications).
Figure 2: Timer 1 Block Diagram
(Refer to Table 1 above for comparison)
1.3 Timer 2 Block Schematic
Question: Explain Timer 2 with block schematic and compare Timer 0, 1, and 2.
Timer 2 Operation
Timer 2 is an 8-bit timer strictly used for internal timing, commonly for PWM generation and
baud rate generation.
• Period Register (PR2): Unlike T0 and T1, Timer 2 compares its count (TMR2) against
a value in the PR2 register.
3
• Reset: When TMR2 matches PR2, the timer resets to 00h automatically. This creates a
programmable period.
• Postscaler: The output of the comparator feeds a 4-bit postscaler which sets the inter-
rupt flag TMR2IF.
Figure 3: Timer 2 Block Diagram
1.4 T0CON and T1CON Registers
Question: Explain the operation of TOCON and TICON registers of PIC18F4550.
T0CON (Timer 0 Control Register)
• Bit 7 (TMR0ON): 1 = Enable Timer, 0 = Stop Timer.
• Bit 6 (T08BIT): 1 = 8-bit mode, 0 = 16-bit mode.
• Bit 5 (T0CS): Clock Source. 1 = External pin (T0CKI), 0 = Internal (Fosc /4).
• Bit 4 (T0SE): Edge Select. 1 = Falling edge, 0 = Rising edge (external clock).
• Bit 3 (PSA): Prescaler Assignment. 1 = Bypass prescaler, 0 = Assign prescaler.
• Bit 2-0 (T0PS2:T0PS0): Prescale value select (1:2 to 1:256).
T1CON (Timer 1 Control Register)
• Bit 7 (RD16): 16-bit Read/Write Mode Enable.
• Bit 6 (T1RUN): Timer 1 System Clock Status (used if T1 is system clock).
• Bit 5-4 (T1CKPS1:0): Prescale (1:1, 1:2, 1:4, 1:8).
• Bit 3 (T1OSCEN): 1 = Oscillator Enabled (allows 32kHz crystal), 0 = Disabled.
• Bit 2 (T1SYNC): Synchronization control (for external clock).
• Bit 1 (TMR1CS): Clock Source. 1 = External, 0 = Internal (Fosc /4).
• Bit 0 (TMR1ON): 1 = Enable, 0 = Stop.
4
2 Theory: CCP Module
2.1 CCP Capture Mode and PIR Register
Question: Explain in detail the Capture mode of operation in the CCP module and discuss
the role of the PIR register.
Capture Mode Operation
Capture mode is used to measure the duration of an event.
• When a specific event occurs on the CCPx pin (Rising edge, Falling edge, 4th rising
edge, or 16th rising edge), the current 16-bit value of Timer 1 (or Timer 3) is copied
(”captured”) into the CCPRxH:CCPRxL register pair.
• An interrupt flag is set immediately.
• Application: Measuring frequency, pulse width, or arrival time of a signal.
Role of PIR Register (Peripheral Interrupt Request)
The PIR registers hold the interrupt flags.
• CCP1IF (in PIR1): This flag is set when a capture event occurs. The software must
poll this bit or enable interrupts to read the captured value. It must be cleared in software.
2.2 CCP Compare Mode
Question: Describe the block schematic of Compare mode with applications in PIC18F4550.
Compare Mode Operation
Compare mode triggers an output event when a specific time is reached.
• The user loads a 16-bit value into the CCPRx register.
• The hardware constantly compares CCPRx with Timer 1.
• Match: When they match, the CCPx pin can be driven High, Low, or Toggled (controlled
by CCPxCON register), and the Interrupt Flag is set.
Applications
1. Generating square waves with precise frequency.
2. Triggering an ADC conversion at a specific time.
3. Creating delays without stopping the CPU.
5
Figure 4: CCP Compare Mode Block Diagram
6
3 Theory: ADC and Interrupts
3.1 ADC Block Schematic and Registers
Question: State features of ADC, draw and explain the block schematic of ADC in detail with
the function of Control registers in PIC18F4550.
Features of PIC18F4550 ADC
• 10-bit resolution (results in values 0 to 1023).
• 13 Multiplexed Input Channels (AN0 to AN12).
• Selectable voltage references (Vref+ and Vref-).
• Conversion time selectable via acquisition time settings.
Control Registers
1. ADCON0: Controls module operation.
• Channel Select (CHS3:CHS0).
• GO/DONE bit (Starts conversion / Indicates status).
• ADON (Enable ADC module).
2. ADCON1: Configures pins.
• PCFG3:PCFG0 (Determines which pins are Analog vs Digital).
• VCFG (Voltage Reference config).
3. ADCON2: Timing config.
• ADFM (Result format: Right/Left justified).
• ACQT (Acquisition time).
• ADCS (Conversion clock select).
7
Figure 5: ADC Block Schematic
3.2 Interrupt Structure (Priority Mode)
Question: Draw and explain the interrupt structure in priority mode of PIC microcontroller.
Priority Interrupts
PIC18 architecture introduces Interrupt Priorities, enabled by setting the IPEN bit in the RCON
register.
1. High Priority:
• Vector Address: 0008h.
• Uses ”Fast Return” (Shadow registers) for context saving.
• Flagged by GIEH (Global Interrupt Enable High).
2. Low Priority:
• Vector Address: 0018h.
• Flagged by GIEL (Global Interrupt Enable Low).
Every interrupt source (e.g., TMR0, ADC) has a corresponding Priority bit (e.g., TMR0IP) to
assign it to the High or Low group.
8
Figure 6: PIC18 Interrupt Structure (Priority Mode)
9
4 Programming Questions
4.1 PWM Generation Calculations
General Formula for PWM:
P W M P eriod = (P R2 + 1) × 4 × Tosc × T M R2 P rescale
P W M Duty Cycle = (CCP RxL : CCP xCON ⟨5 : 4⟩) × Tosc × T M R2 P rescale
Given Parameters: Fosc = 10M Hz ⇒ Tosc = 0.1µs.
4.2 Question 9: 2.5 KHz, 75% Duty
Objective: 2.5 KHz PWM, 75% Duty, Prescale (N ) = 4, Fosc = 10M Hz.
Calculations: 1. Period: T = 1/2.5kHz = 400µs.
400µs = (P R2 + 1) × 4 × 0.1µs × 4
400 = (P R2 + 1) × 1.6 ⇒ P R2 + 1 = 250 ⇒ PR2 = 249
2. Duty Value: 75% of 400µs = 300µs.
300µs = (DutyV al) × 0.1µs × 4
DutyV al = 300/0.4 = 750
750 in binary is 10111011102 .
• CCPR1L (Upper 8 bits) = 101110112 = 0xBB
• CCP1CON¡5:4¿ (Lower 2 bits) = 102
1 void main () {
2 TRISCbits . TRISC2 = 0; // CCP1 pin as output
3 PR2 = 249; // Set PWM Period
4 CCPR1L = 0 xBB ; // Duty Cycle MSB
5 CCP1CON = 0 x2C ; // 0010 1100 ( DC LSBs =10 , Mode = PWM )
6 T2CON = 0 x01 ; // Prescaler = 4 (01) , Timer off
7 TMR2 = 0; // Clear Timer
8 T2CONbits . TMR2ON = 1; // Start Timer 2
9 while (1) ;
10 }
Listing 1: 2.5kHz 75% Duty PWM
4.3 Question 10: 2.5 KHz, 50% Duty
Calculations: 1. Period: Same as Q9. PR2 = 249. 2. Duty Value: 50% of 400µs = 200µs.
200µs = (DutyV al) × 0.4 ⇒ DutyV al = 500
500 in binary is 01111101002 .
• CCPR1L = 011111012 = 0x7D
• CCP1CON¡5:4¿ = 002
10
1 void main () {
2 TRISCbits . TRISC2 = 0;
3 PR2 = 249;
4 CCPR1L = 0 x7D ; // 50% Duty
5 CCP1CON = 0 x0C ; // LSBs =00 , PWM mode
6 T2CON = 0 x01 ; // Prescale 4
7 TMR2 = 0;
8 T2CONbits . TMR2ON = 1;
9 while (1) ;
10 }
Listing 2: 2.5kHz 50% Duty PWM
4.4 Question 11: 1 KHz, 10% Duty
Note on Prescaler: For 1kHz (T = 1000µs) at 10MHz:
1000 = (P R2 + 1) × 4 × 0.1 × N
If N=4, P R2 + 1 = 625, which exceeds 8-bit limit (255). We must use Prescaler 16.
Calculations: 1. Period: 1000µs. N = 16.
1000 = (P R2 + 1) × 0.4 × 16 = (P R2 + 1) × 6.4
P R2 + 1 = 156.25 ≈ 156 ⇒ PR2 = 155
2. Duty: 10% of 1000µs = 100µs.
100 = V al × 0.1 × 16 ⇒ V al = 100/1.6 = 62.5 ≈ 62
62 = 00001111102 .
• CCPR1L = 000011112 = 0x0F
• CCP1CON¡5:4¿ = 102
1 void main () {
2 TRISCbits . TRISC2 = 0;
3 PR2 = 155;
4 CCPR1L = 0 x0F ;
5 CCP1CON = 0 x2C ; // LSBs =10 , PWM mode
6 T2CON = 0 x02 ; // Prescale 16 (1 X bit pattern usually )
7 TMR2 = 0;
8 T2CONbits . TMR2ON = 1;
9 while (1) ;
10 }
Listing 3: 1kHz 10% Duty PWM
4.5 Question 12: Square Wave 1KHz, 25% Duty
Note: ”Square wave” usually implies 50% duty, but the question specifies 25%. We treat this
as a PWM generation problem. Calculations: (Prescale 1:16 required for 1kHz, similar to
Q11). 1. PR2 = 155. 2. Duty: 25% of 1000µs = 250µs.
250 = V al × 1.6 ⇒ V al = 156.25 ≈ 156
156 = 00100111002 .
11
• CCPR1L = 00100111 = 0x27
• CCP1CON¡5:4¿ = 00
1 void main () {
2 TRISCbits . TRISC2 = 0;
3 PR2 = 155;
4 CCPR1L = 0 x27 ;
5 CCP1CON = 0 x0C ; // LSBs =00
6 T2CON = 0 x02 ; // Prescale 16
7 T2CONbits . TMR2ON = 1;
8 while (1) ;
9 }
Listing 4: 1kHz 25% Duty Square Wave
12
5 Delay Programming
Assume Fosc = 10M Hz ⇒ Tinst = 0.4µs. Formula: Delay = Tinst × P rescale × (65536 − Load)
5.1 Question 13: 10ms Delay using Timer 1 (No Prescaler)
10, 000µs = 0.4µs × 1 × (65536 − Load)
25, 000 = 65536 − Load ⇒ Load = 40536
Hex: 40536 = 0x9E58.
1 void T1_Delay () {
2 T1CON = 0 x80 ; // 16 - bit , Prescale 1:1 , Int Clock
3 TMR1H = 0 x9E ; // Load High Byte
4 TMR1L = 0 x58 ; // Load Low Byte
5 T1CONbits . TMR1ON = 1; // Start
6 while ( PIR1bits . TMR1IF == 0) ; // Wait for overflow
7 T1CONbits . TMR1ON = 0; // Stop
8 PIR1bits . TMR1IF = 0; // Clear Flag
9 }
Listing 5: 10ms Delay Timer 1 No Prescale
5.2 Question 14: 10ms Delay using Timer 1 (1:16 Prescaler)
Correction Note: Timer 1 max prescaler is 1:8. Timer 0 supports 1:16. Assuming question
meant max Timer 1 prescaler (1:8), or question is theoretical. If 1:8:
10, 000 = 0.4 × 8 × (65536 − Load) ⇒ 3125 = 65536 − Load
Load = 62411 = 0xF3CB
1 void T1_ Delay_ Presc ale () {
2 T1CON = 0 xB0 ; // Prescale 1:8 ( Bits 5 -4 = 11)
3 TMR1H = 0 xF3 ;
4 TMR1L = 0 xCB ;
5 T1CONbits . TMR1ON = 1;
6 while ( PIR1bits . TMR1IF == 0) ;
7 T1CONbits . TMR1ON = 0;
8 PIR1bits . TMR1IF = 0;
9 }
Listing 6: 10ms Delay Timer 1 (Prescale 1:8)
5.3 Question 15: 10ms Delay using Timer 0 (16-bit, No Prescaler)
Calculation identical to Q13. Load = 0x9E58.
1 void T0_Delay () {
2 T0CON = 0 x08 ; // Timer Off , 16 - bit , Internal Clk , No Prescale ( PSA
=1)
3 TMR0H = 0 x9E ;
4 TMR0L = 0 x58 ;
5 T0CONbits . TMR0ON = 1;
6 while ( INTCONbits . TMR0IF == 0) ;
7 T0CONbits . TMR0ON = 0;
8 INTCONbits . TMR0IF = 0;
9 }
Listing 7: 10ms Delay Timer 0
13
5.4 Question 16: 1ms Delay using Timer 0 (16-bit, No Prescaler)
1, 000µs = 0.4µs × 1 × (65536 − Load)
2, 500 = 65536 − Load ⇒ Load = 63036 = 0xF63C
1 void T0_Delay_1ms () {
2 T0CON = 0 x08 ; // 16 - bit , No Prescale
3 TMR0H = 0 xF6 ;
4 TMR0L = 0 x3C ;
5 T0CONbits . TMR0ON = 1;
6 while ( INTCONbits . TMR0IF == 0) ;
7 T0CONbits . TMR0ON = 0;
8 INTCONbits . TMR0IF = 0;
9 }
Listing 8: 1ms Delay Timer 0
14