MIC Unit 4 Paper Solution
MIC Unit 4 Paper Solution
Q.1) Draw and explain the Timer 1, 16 bit operation in details compare the Timer 0,1, & 2. [9] (Nov-Dec 2022)
Timer 1 Operation (16-bit) in PIC18F4550:
Timer 1 is a 16-bit timer, meaning it counts from 0x0000 to 0xFFFF (65,535) before rolling over. Its higher
resolution makes it useful for longer delays, more accurate timing, and event capture.
Key Features:
16-bit Resolution: Timer 1 counts from 0 to 65,535.
Multiple Clock Sources:
o Internal Clock (Fosc/4).
o External clock source (can be selected via T1OSCEN bit in T1CON register).
o Secondary external oscillator option for real-time clock purposes.
Prescaler: Provides scaling options of 1:1, 1:2, 1:4, and 1:8, enabling flexibility in timing.
Interrupts: Timer 1 can generate an interrupt when it overflows from 0xFFFF to 0x0000.
Capture and Compare: Works with Capture/Compare/PWM (CCP) modules for precise event timing,
input capture, or PWM signal generation.
Power-saving Features: Can operate in Sleep mode using an external oscillator for real-time clock
functions.
Key Registers of Timer 1:
1. TMR1H and TMR1L:
o These are the high and low byte of the 16-bit Timer 1 register. They store the current count of
the timer.
2. T1CON (Timer 1 Control Register):
o Controls the operation of Timer 1, including clock source selection, prescaler settings, enabling
or disabling the timer, and oscillator configurations.
Key bits in T1CON:
o TMR1ON: Enables or disables Timer 1.
o T1CKPS1 and T1CKPS0: Set the prescaler (1:1, 1:2, 1:4, 1:8).
o T1OSCEN: Enables the external oscillator.
o TMR1CS: Selects the clock source (internal or external).
3. TMR1IF: The interrupt flag for Timer 1. It is set when Timer 1 overflows from 0xFFFF to 0x0000.
4. TMR1IE: The interrupt enable bit. If enabled, an interrupt is generated when the TMR1IF flag is set.
o When an external clock is selected, Timer 1 operates as a counter, incrementing on each rising
edge of the external signal. This mode is ideal for counting external events like pulses.
3. Capture Mode:
o Timer 1 can work with the Capture mode of the CCP modules, where the timer's value is
latched into the CCP register when an event (such as a rising or falling edge) occurs.
4. Compare Mode:
o In Compare mode, Timer 1 is compared with a set value, and when they match, an action can
be triggered (such as setting a pin high/low or generating an interrupt).
Power-Saving and Real-Time Clock:
By using an external clock (typically a 32.768 kHz crystal oscillator), Timer 1 can function as a real-
time clock (RTC), even in Sleep mode, which is useful for low-power applications.
Q.2) Write a program for 1 KHz and 10% duty cycle PWM generation, use Fosc= 10MHz [8] (Nov-Dec 2022)
To generate a 1 kHz PWM signal with a 10% duty cycle on the PIC18F49500 (or similar microcontroller like
PIC18F4550) with a 10 MHz clock frequency (Fosc = 10 MHz), you will need to configure one of the CCP
(Capture/Compare/PWM) modules and set up Timer 2 to control the frequency.
Key Calculations for PWM:
1. PWM Period: The PWM period is determined by the PR2 register, the prescaler of Timer 2, and the
system clock frequency. The formula for calculating the PWM period is:
Where:
o PR2 is the value in the Timer 2 period register.
o Tosc is the oscillator period (Tosc=1/Fosc)
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
o TMR2 Prescaler is the prescaler value for Timer 2 (options are 1, 4, or 16).
Rearranging for PR2:
Thus,
Duty Cycle: The duty cycle is set by the CCPR1L and CCP1CON registers. The formula for the duty cycle is:
Program
#include <xc.h>
// Configuration bits
#pragma config FOSC = HS // High-Speed Oscillator
#pragma config WDT = OFF // Watchdog Timer Disabled
#pragma config LVP = OFF // Low Voltage Programming Disabled
void pwm_init(void) {
// Step 1: Set the CCP1 module in PWM mode
CCP1CON = 0b00001100; // CCP1 in PWM mode
void main(void) {
pwm_init(); // Initialize PWM
while (1) {
// Infinite loop (PWM signal will be generated continuously)
}
}
Q.3) Write program to generate delay of 1 ms using timer 0, 16 bit and no prescaler. [9] (Nov-Dec 2022)
To generate a 1 ms delay using Timer 0 in 16-bit mode with no prescaler on the PIC18F4550, we need to
configure Timer 0 and calculate the initial count value that Timer 0 should load to generate the desired delay.
Steps to Implement the Delay:
1. Clock Frequency:
o The PIC18F4550 uses a clock frequency Fosc = 10 MHz, and the internal timer clock (Tosc) will
be Fosc/4 = 2.5 MHz. This means the timer will increment every 400 ns.
2. Timer 0 in 16-bit Mode:
o In 16-bit mode, Timer 0 counts from 0x0000 to 0xFFFF (65,535), and then it overflows and sets
the TMR0IF (Timer 0 Interrupt Flag).
To achieve a 1 ms delay:
o We need the timer to generate an interrupt or overflow after 1 ms.
o The number of timer ticks needed for 1 ms delay:
The timer can count up to 65,535, so we need to preload the timer with an initial value such that it overflows
after counting 2500 ticks.
Initial Value=65,535−2500=63,035=0xF663
So, to generate a 1 ms delay, we will load 0xF663 into TMR0H and TMR0L.
Code to Generate 1 ms Delay Using Timer 0:
The C program for the PIC18F4550 to generate a 1 ms delay using Timer 0 in 16-bit mode with no
prescaler:
#include <xc.h>
// Configuration bits
#pragma config FOSC = HS // High-Speed Oscillator
#pragma config WDT = OFF // Watchdog Timer Disabled
#pragma config LVP = OFF // Low Voltage Programming Disabled
while (1) {
// Call the delay function to generate a 1 ms delay
delay_1ms();
Q.4) State specification of ADC and explain with block schematic functions of inbuilt ADC of PIC 18F4550 [8]
(Nov-Dec 2022) (Nov-Dec 2023)
Specification Analog to Digital Converter
When we interface sensors to the microcontroller, the output of the sensor many of the times is analog
in nature. But microcontroller processes digital signals.
Hence we use ADC in between sensors and microcontrollers. It converts an analog signal into digital
and gives it to the microcontroller.
There are a large number of applications of ADC like in a biometric application, Environment
monitoring, Gas leakage detection, etc.
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
1 = Right Justified. However, the lower 8-bits are kept in ADRESL, and the remaining MSB side two bits kept in
ADRESH. And remaining 6 bits (bit 2-7) of ADRESH filled with 0’s.
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
0 = Left Justified. However, the upper 8-bits are kept in ADRESH, and the remaining two lower bits are placed in
ADRESL at bit 7-6 location. And the remaining 6 lower bits of ADRESL filled with 0’s.
Q.5) Explain the timer 2 with block schematic, compare Timer 0,1 and 2. [9] (Nov-Dec 2023)
Timer 2 in the PIC18F4550 is an 8-bit timer, meaning it counts from 0 to 255. It is primarily used for PWM
generation and periodic interrupt generation.
Key Components:
1. TMR2: An 8-bit register that acts as the counter for Timer 2. The timer counts from 0 to 255 (since it's
8-bit) and rolls over back to 0.
2. Prescaler: Divides the input clock (Fosc/4) to slow down the counting rate. The prescaler has
selectable options of 1:1, 1:4, and 1:16. This allows for flexibility in choosing how fast or slow the
timer should count.
3. PR2 (Period Register): This 8-bit register defines the period of the timer. When TMR2 reaches the
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
value set in PR2, it resets to 0, and an interrupt flag is set (if interrupts are enabled). The timer will
cycle back from 0 and repeat the process.
4. Postscaler: After the timer reaches the PR2 value and triggers a match, the Postscaler further divides
the output frequency of the timer before generating an interrupt. It can divide by values from 1:1 to
1:16.
5. Interrupt Flag: When the timer matches the value in PR2, the TMR2IF (Timer 2 Interrupt Flag) is
set. If enabled, this can trigger an interrupt.
Functionality of Timer 2:
Periodic Interrupt Generation: Timer 2 is ideal for generating periodic interrupts because the timer
counter value can be compared against the PR2 register.
Pulse Width Modulation (PWM): Timer 2 is frequently used in conjunction with the CCP
(Capture/Compare/PWM) modules for generating PWM signals. The period of the PWM signal is
determined by the PR2 register, and the duty cycle is controlled by the CCPRx registers.
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
Q.6) Describe the block schematic of compare mode of operation with applications in PIC18F4550 [8] (Nov-Dec
2023)
PIC18F4550 has an inbuilt CCP module which has Capture, Compare, and PWM mode for various
applications.
CCP in Compare mode is used to generate a waveform of various duty cycles like PWM and also used to
trigger an event when the pre-determined time expires.
Also, it is used to generate specific time delay.
PIC18F4550 has 2 in-built CCP modules i.e. CCP1 and CCP2.
Output (e.g. Waveform generation) of CCP2 and CCP1 in compare mode is generated on two pins i.e.
RC1 and RC2 respectively.
CCPR1 Register
CCP module has 16-bit register CCPR1 [CCPR1H (8-bit): CCPR1L (8-bit)] given below in which count is loaded
to compare.
CCPR1 Register
The compare mode of the CCP module is selected using bits in the CCP1CON Register.
Only Timer1 and Timer3 can be used for Compare mode in PIC18F4550.
In compare mode, the 16-bit CCPR1 register value is constantly compared against either the TMR1 or the TMR3
register pair value shown in the figure given below.
When a match occurs between the CCPR1 Register value and Timer value, the CCP1IF interrupt flag is
generated and one of the following actions may occur on the associated RC2 pin:
1. Toggle output on the RC2 pin.
2. RC2 pin drives to a High level.
3. RC2 pin drives to a low level.
4. Generate software interrupt
5. Generate a special trigger event.
Now, what output logic will generate when a compare match occurs depend on the CCP1CON register.
CCP1CON Register
DC1B1:DC1B0:
Used for PWM mode.
CCP1M3:CCP1M0: CCP1 module mode select bit
These bits decide which action takes on compare match.
0010 = Toggle output on match
1000 = Initialize CCP1 pin low, on compare match this pin is set to high.
1001 = Initialize CCP1 pin high, on compare match this pin is set to low.
1010 = On compare match generates software interrupt.
1011 = On compare match trigger special event, reset the timer, start ADC conversion.
So Configure compare mode as per application using CCP1CON Register.
Other combinations are used for Capture and PWM mode.
Interrupt on Match
Also, remember that after every compare match between CCPR1 and Timer1/Timer3 CCP1IF interrupt flag is
set.
This flag is located at PIR1<bit2> Register.
Also, note that the T3CON register is used to select Timer1 or Timer3 for compare mode.
T3CON Register
Application 1
Here let’s generate a square wave of 1 kHz having a 50% duty cycle.
For 50% Duty Cycle
Assume Oscillator frequency = 8MHz
Period of waveform = 1ms (1 KHz given)
So Instruction Cycle = 1/ (FOSC/4) = 1/ (8MHz/4) = 0.5 us
That means Timer will increment its count continuously after each 0.5us delay.
Here I used the Timer3 register.
Now calculate CCPR1 count.
For 50% Duty Cycle – ON time=OFF time=0.5ms
0.5ms/0.5us = 1000 i.e. 0x03E8
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
CCPR1 = 0x03E8
#include "osc.h"
#include <p18f4550.h>
void main()
{
OSCCON=0x72; /* Configure internal clock at 8MHz */
TRISCbits.TRISC2=0;/* Configure RC2 pin as output */
CCP1CON=0x02; /* Module is configured for compare mode and is set up so that upon a compare match
of CCPR1 and TMR3, RC2 is driven low*/
PIR1bits.CCP1IF=0; /* Clear interrupt flag*/
TMR3=0; /* Clear Timer3 Register*/
T3CON=0xC0; /* Timer3 used for compare mode with TMR3 register in 16-bit format*/
CCPR1=1000; /* Load a count for generating 1khz*/
T3CONbits.TMR3ON=1; /* Turn On Timer3*/
while (1)
{
/* Wait for CCP Interrupt flag to set, it is set on compare match between CCPR1 and TMR3*/
while(PIR1bits.CCP1IF==0);
PIR1bits.CCP1IF=0;/* Clear interrupt flag */
TMR3=0;
}
}
Output
Q.8) Write a program for 2.5 KHz and 75 % duty cycle PWM generation with N = 4, use Fosc = 10MHz. [8] (Nov-
Dec 2023) (May-Jun 2023)
To generate a PWM signal with a frequency of 2.5 kHz and a duty cycle of 75% using the PIC18F4550
microcontroller, we'll use one of the CCP (Capture/Compare/PWM) modules in PWM mode. The system clock
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
Where:
Fosc = 10 MHz (system clock frequency)
N = Prescaler value (N = 4)
PR2 = 8-bit period register
Duty Cycle Formula
The duty cycle is set using the CCPRxL register and DCxB1
bits:
Step-by-Step Calculation
1. Calculate PR2 for 2.5 kHz PWM frequency:
void PWM1_Init() {
// Set CCP1 module to PWM mode
CCP1CON = 0b00001100; // CCP1 in PWM mode, DCxB1:DCxB0 = 00
// Set the duty cycle (CCPR1L = 187 for 75% duty cycle, DC1B1:DC1B0 = 10)
CCPR1L = 187; // CCPR1L holds the 8 most significant bits
CCP1CONbits.DC1B1 = 1; // DC1B1 bit (bit 1 of the least significant bits)
CCP1CONbits.DC1B0 = 0; // DC1B0 bit (bit 0 of the least significant bits)
// Timer2 configuration
T2CON = 0b00000101; // Timer2 ON, Prescaler = 4
}
void main() {
// Set the direction of the CCP1 pin as output (PWM output on RC2)
TRISCbits.TRISC2 = 0; // Make RC2/CCP1 pin an output
// Initialize PWM
PWM1_Init();
while(1) {
// Main loop (the PWM signal is generated in hardware)
}
}
Explanation:
1. Configuration of CCP1 Module in PWM Mode:
o The CCP1 module is configured in PWM mode by setting the CCP1CON register to 0b00001100.
2. PR2 Register:
o We calculated the value of PR2 = 249 to generate a PWM frequency of 2.5 kHz.
3. CCPR1L Register and Duty Cycle Bits (DC1B1):
o The duty cycle is set to 75% by assigning CCPR1L = 187 (most significant bits) and DC1B1
= 10 (least significant bits).
4. Timer 2 Configuration:
o T2CON is set to 0b00000101 to turn on Timer 2 and use a prescaler of 4.
5. PWM Output Pin:
o The TRISC2 register is used to configure the RC2/CCP1 pin as an output pin for the PWM
signal.
Q.9) Draw and explain the Timer 0, 8 bit operation in details compare the Timer 0,1, and 2. [9] (May-Jun 2023)
In the PIC18F4550, Timer0 is a highly flexible and configurable timer/counter module. It can operate in both
8-bit and 16-bit modes. It also has multiple clock sources, including the internal instruction clock (FOSC/4)
and an external clock source via the T0CKI pin.
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
Operation Example:
Assume an internal clock of 20 MHz:
FOSC/4 = 5 MHz (each instruction cycle is 200 ns).
The prescaler is set to 1:8, so each increment of TMR0 occurs every 1.6 μs (200 ns × 8).
In 8-bit mode, TMR0 counts from 0 to 255. Therefore, the timer overflows after 256 increments.
The time to overflow is: Overflow Time=256×1.6 μs=409.6 μs\text{Overflow Time} = 256 \times
1.6 \, \mu s = 409.6 \, \mu sOverflow Time=256×1.6μs=409.6μs So, Timer 0 will overflow
approximately every 409.6 μs.
If an interrupt is enabled, the microcontroller can be programmed to handle the overflow by executing an
interrupt service routine.
Applications:
Time Delay Generation: By calculating overflow times, precise delays can be generated.
Event Counting (Counter Mode): Timer 0 can count external events, useful in frequency counting and
similar applications.
PWM and Signal Generation: Timer0 can be combined with other peripherals for generating square
waves or pulse-width modulation (PWM).
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
Q.10) Write program to generate delay of 10 ms using timer 0, 16 bit and no prescaler. [9] (May-Jun 2023)
To generate a delay of 10 ms using Timer 0 in 16-bit mode and no prescaler in a PIC18F4550, we can
calculate the timer overflow value and set up the timer accordingly.
Steps:
1. Clock Calculation:
o Assume the clock frequency of the PIC18F4550 is 20 MHz.
o The internal clock used for the timer is FOSC/4, which means:
5. Since Timer 0 is 16-bit, it can count from 0x0000 to 0xFFFF (65536). We need the timer to count 50,000
ticks, so we preload the timer with:
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
6. TMR0 Registers:
o TMR0H (Timer 0 High Byte): 0x3C
o TMR0L (Timer 0 Low Byte): 0xB0
CODE
#include <xc.h>
// Configuration bits (adjust as necessary)
#pragma config FOSC = HS // High-Speed Oscillator
#pragma config WDT = OFF // Watchdog Timer disabled
void Timer0_Delay10ms() {
// Set Timer0 in 16-bit mode, no prescaler
T0CON = 0x08; // T08BIT = 0 (16-bit mode), T0CS = 0 (Internal Clock), PSA = 1 (No prescaler)
// Turn on Timer0
T0CONbits.TMR0ON = 1;
void main(void) {
// Example usage of the Timer0_Delay10ms function
while (1) {
// Call delay function
Timer0_Delay10ms();
Q.11) Explain capture mode of PlC microcontroller, discuss the role of PIR register in capture mode. [9] (May-
Jun 23/24)
Capture Mode in PIC Microcontroller
Capture Mode is a feature of the Enhanced Capture/Compare/PWM (ECCP) module in PIC microcontrollers,
such as the PIC18F4550. This mode allows the microcontroller to capture the value of a timer (typically
Timer1) when an external event occurs. The event can be a rising or falling edge or both on a specific pin
(typically CCP1 or CCP2 pin).
Capture mode is typically used to measure the time between two events (e.g., to measure pulse width,
frequency of a signal, etc.).
How Capture Mode Works:
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
o Timer1 is usually used as the time base for the capture module. Configure Timer1 with a
suitable prescaler and start it.
3. Enable Capture Interrupts (Optional):
o Enable capture interrupts by setting the CCP1IE or CCP2IE bits in the PIE1 register and enable
global and peripheral interrupts.
4. Read Captured Value:
o When a capture event occurs, read the captured value from the CCPR1H or CCPR2H register.
Applications of Capture Mode:
1. Frequency Measurement: By capturing the time between successive rising or falling edges of a signal,
the frequency of the signal can be calculated.
2. Pulse Width Measurement: The width of a pulse can be measured by capturing both the rising and
falling edges of the signal.
3. Event Timing: Useful for measuring the time between events or for generating timestamps of specific
external events.
Capture mode is an essential feature in applications where precise timing or event tracking is needed. The PIR1
register, specifically the CCP1IF and CCP2IF bits, plays a vital role in indicating when a capture event has
occurred, allowing the microcontroller to respond accordingly.
Q.12) Explain the operation of T0CON and T1CON register of PIC 18F4550. [9] (May-Jun 2024)
The T0CON and T1CON registers in the PIC18F4550 microcontroller are used to control and configure the
operation of Timer 0 and Timer 1, respectively. These timers are versatile modules that can be used for timing
operations, event counting, or generating delays.
1. T0CON Register (Timer 0 Control Register)
The T0CON register is an 8-bit register that controls the operation of Timer 0, which can be configured as an
8-bit or 16-bit timer/counter. It controls various parameters like prescaler, clock source, and timer mode.
T0CON Register Layout (Bits):
Q.13) Write an embedded c program to generate square wave of 1KHz with the duty cycle of 25% and prescale
1:16. [10] (May-Jun 2024)
Program:-
#include <p18F4550.h>
// Configuration bits for PIC18F4550
#pragma config FOSC = HS // High-Speed Oscillator (External Oscillator)
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config LVP = OFF // Low-Voltage Programming Disable
void setupPWM() {
// Set PR2 to 124 for 1kHz frequency
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4
PR2 = 124;
void main(void) {
setupPWM(); // Initialize the PWM
while(1) {
// Infinite loop, PWM runs in hardware
}
}
Q.14) Draw and explain the interrupt structure in priority mode of PIC microcontroller. [8] (May-Jun 2024)
Interrupt Structure in Priority Mode of PIC Microcontroller
The PIC18 series microcontrollers, such as PIC18F4550, feature an interrupt system that allows peripherals or
internal conditions to interrupt the main program flow. The interrupt structure in priority mode is an
advanced system that categorizes interrupts into high-priority and low-priority levels, allowing more flexible
and responsive interrupt handling.
Below is an explanation and a diagram of the interrupt structure in priority mode for a typical PIC18
microcontroller:
Components of the Interrupt Structure:
1. INTCON Register:
o Controls global interrupts and individual external interrupts.
o Key bits:
GIEH (Global Interrupt Enable High): Enables high-priority interrupts.
GIEL (Global Interrupt Enable Low): Enables low-priority interrupts.
PEIE (Peripheral Interrupt Enable): Enables peripheral interrupts.
2. RCON Register:
o Contains the IPEN (Interrupt Priority Enable) bit, which enables the use of interrupt priority
levels.
o When IPEN = 1, the PIC microcontroller uses both high-priority and low-priority interrupts.
3. INTCON2 Register:
o Contains the INTEDG bits to configure edge-triggering for external interrupts.
4. INTCON3 Register:
o Controls external interrupts (like INT1 and INT2) and assigns priority using the INT1IP and
INT2IP bits.
5. Peripheral Interrupts:
o Many peripherals have dedicated interrupt enable and flag bits located in peripheral registers
Department of Electronics and Telecommunication Engineering
Previous SPPU Insem Question Paper Solution
Unit 4