Pic 2
Pic 2
Somesh Maurya
M.Sc. – II
Roll No. 308
What is a PIC Microcontroller?
• PIC (Peripheral Interface Controller): A family of microcontrollers
designed by Microchip Technology. PIC microcontrollers are commonly
used in embedded systems for automation and control. They are known
for their simplicity, small instruction set, low cost, and flexible
interfacing features.
Key Features of PIC Microcontrollers:
• RISC Architecture: PIC microcontrollers are based on a Reduced
Instruction Set Computing (RISC) architecture, meaning they use fewer,
simpler instructions to operate.
• Harvard Architecture: Separates data and program memory, allowing
for faster execution of instructions.
• Compact Instruction Set: The mid-range PIC devices, like the
PIC16C61/71, use only 35 instructions, making programming simpler.
• Low Power Consumption: Designed for efficient energy use, making
them ideal for battery-powered applications.
Features and Architecture of PIC16C61/71
• Core Features: 8-bit Data Bus: All data manipulation occurs 8 bits
at a time, making it suitable for small, simple applications.
• Program Memory: Uses Flash memory to store programs that can
be reprogrammed if needed.
• General-Purpose I/O Pins: Supports multiple I/O pins to connect
to external devices like LEDs, sensors, or displays.
• EEPROM (for 16C71): Allows non-volatile data storage for
configurations or small amounts of data.
Important Registers:
• W Register: The working register used in most instructions for
operations such as arithmetic or data movement.
• Status Register: Holds important flags like carry, zero, or
overflow, used for decision-making during program execution.
• Option Register: Controls the configuration of the Timer0, pre-
scaler, and other key functions of the microcontroller.
Clock Speed:
• The PIC16C61/71 operates typically with a 4 MHz or 20 MHz oscillator.
• Instruction Cycle: The clock speed is divided by 4 to determine the instruction
cycle, meaning with a 4 MHz clock, the instruction cycle is 1 microsecond (µs).
What is a Timer?
• A timer in a microcontroller is a hardware peripheral that counts the number of
clock cycles or external events. This is essential for creating precise delays,
measuring time intervals, or triggering actions at set intervals.
• PIC microcontrollers typically have multiple timers that can be used for different
purposes.
Timer Types:
• Timer0: The primary timer available in the 16C61/71 models. It’s an 8-bit
timer/counter.
• Timer1 and Timer2: 16-bit and 8-bit timers respectively, found in other PIC
models but not in 16C61/71.
Significance:
• Timers are vital for real-time applications where precise timing and event
control are needed, such as motor control, sensor data acquisition, and
communication protocols like UART (Universal Asynchronous Receiver-
• Watchdog Timer (WDT)
Applications:
• Event Counting: It can count pulses from external events like a rotary
encoder or a tachometer. For example, you could use Timer0 to count
the number of rotations of a wheel by counting each pulse generated by
a sensor.
• Frequency Measurement: By counting the number of pulses received
in a specific time window, you can measure the frequency of an external
signal.
Prescaler and Timer0 Operation:
• What is a Prescaler?
• A prescaler is a frequency divider that reduces the rate at which Timer0
increments. By slowing down the clock, the timer can operate over
longer periods before overflowing, allowing it to count for longer delays.
• Prescaler Division:
• The prescaler divides the clock source by values ranging from 2 to 256.
For example, with a 4 MHz clock and a prescaler of 256, Timer0 would
increment every 256 µs instead of every 1 µs.
• Prescaler Value Selection: The prescaler value is selected by the PS2
bits in the OPTION_REG.
• Prescaler Sharing: One important feature in the PIC16C61/71 is that
the prescaler is shared between Timer0 and the Watchdog Timer (WDT).
If the prescaler is used by Timer0, it is not available for the WDT, and
vice versa.
• Prescaler Calculation:
TMR0 Register: Holds the current value of Timer0 and is incremented by the
clock pulses.
OPTION_REG:
• T0CS (Timer0 Clock Source Select): 0 for internal clock, 1 for external clock.
• T0SE (Timer0 Source Edge Select): Controls whether Timer0 increments on
the rising or falling edge of external pulses.
• PSA (Prescaler Assignment): Determines whether the prescaler is assigned to
Timer0 (PSA = 0) or to the Watchdog Timer (PSA = 1).
• PS2 : Controls the prescaler value, determining how much the clock is
divided by.
INTCON Register: Contains control bits for enabling the Timer0 interrupt
(T0IE) and checking the interrupt flag (T0IF).
Applications of Timer0 in Embedded Systems
• Delay Generation:Timer0 is frequently used to generate
precise time delays in embedded systems, such as controlling
the timing of an LED flash, generating a PWM signal, or
delaying system events.
4. Prescaler:
• Timer1 includes a prescaler to divide the input clock before it is
fed to the timer. The prescaler values can be set to 1:1, 1:2, 1:4,
or 1:8.
• This feature allows Timer1 to operate at different clock speeds,
giving more flexibility in timing applications.
5. Interrupt Generation:
• Timer1 can generate an interrupt when it overflows (when it
counts from 65535 to 0).
• The TMR1IF (Timer1 Interrupt Flag) bit in the PIR1 register is set
when the timer overflows.
• If the TMR1IE (Timer1 Interrupt Enable) bit is set, the overflow
can trigger an interrupt, allowing the microcontroller to handle
time-sensitive tasks immediately.
Interrupts:
• Timer2 can generate an interrupt when it overflows (from 255
to 0).
• The TMR2IF (Timer2 Interrupt Flag) bit is set when the overflow
occurs, and if TMR2IE (Timer2 Interrupt Enable) is set, it
triggers an interrupt.
Registers Associated with Timer2:
TMR2:
• This is the 8-bit register that holds the current value of the timer.
• We can read and write to this register to monitor or change the count
value.