0% found this document useful (0 votes)
29 views27 pages

Pic 2

The document provides an overview of PIC Microcontrollers, specifically the 16C61/71 models, highlighting their architecture, key features, and timer functionalities. It details the operation of Timer0, Timer1, and Timer2, including their modes, applications, and configuration registers. Additionally, it explains the significance of timers in embedded systems for precise timing, event counting, and real-time applications.

Uploaded by

tstrak4567
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views27 pages

Pic 2

The document provides an overview of PIC Microcontrollers, specifically the 16C61/71 models, highlighting their architecture, key features, and timer functionalities. It details the operation of Timer0, Timer1, and Timer2, including their modes, applications, and configuration registers. Additionally, it explains the significance of timers in embedded systems for precise timing, event counting, and real-time applications.

Uploaded by

tstrak4567
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PIC Microcontroller 16C61/71 timers

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)

• The Watchdog Timer is not a typical timer used for generating


delays or counting events but is a safety feature designed to
reset the microcontroller if it becomes unresponsive.

• The WDT is an internal timer that must be periodically reset by


the software. If the software fails to reset it within a certain
time, the WDT overflows and resets the microcontroller.
• This feature is important in embedded systems to ensure the
microcontroller can recover from unexpected failures or
crashes.
• The WDT has a separate prescaler, which can be adjusted
using the OPTION_REG register. The prescaler can be used to
set the timeout period for the WDT.
Timer0 in PIC16C61/71:
• 8-bit Timer/Counter: Timer0 is an 8-bit register, meaning it can count
from 0 to 255. After reaching 255, it overflows back to 0 and can generate
an interrupt.
• Versatility: It can function as a Timer (using the internal clock) or a
Counter (counting external pulses).
• TMR0 Register: The actual register where the count is stored. When
TMR0 increments past 255, it wraps back to 0, and an interrupt flag (T0IF)
is set.

Key Features of Timer0:


• Prescaler: Timer0 includes a prescaler, a feature that divides the
incoming clock by a set factor (2, 4, 8, up to 256). This allows the timer to
increment at a slower rate, increasing the time between overflows.
• Interrupts: When the timer overflows, it can generate an interrupt if the
interrupt is enabled, allowing the microcontroller to pause its current tasks
and respond to the overflow event.
Timer0 as a Timer:
 How Timer Mode Works?
• In this mode, Timer0 uses the internal clock source derived from the
system clock. The system clock is divided by 4 (Fosc/4), so with a 4 MHz
clock, Timer0 will increment every 1 microsecond.
• Example: If you want to create a delay of 1 millisecond, you need Timer0
to overflow after counting 1000 increments (1 µs * 1000 = 1 ms).
 Timer Overflow:
• Once the 8-bit timer reaches 255, it overflows back to 0. This overflow
can trigger an interrupt, notifying the microcontroller to take action (e.g.,
incrementing a variable to track time).
 Delay Generation:
• By configuring the timer with the correct prescaler and initial value, you
can create precise delays that are needed in many embedded
applications, such as blinking an LED at regular intervals or generating
pulse-width modulation (PWM) signals for motor control.
Timer0 as a Counter:
• How Counter Mode Works?
• In Counter mode, Timer0 counts external pulses applied to the T0CKI pin
instead of using the internal clock.
• Edge Detection: The counter can be configured to increment on either
the rising or falling edge of the external pulse. This is set using the T0SE
(Timer0 Source Edge) bit in the OPTION_REG.

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:

• A prescaler is used to divide the clock source by a specific


factor before feeding it to the timer.

• For Timer0, the prescaler can divide by 2, 4, 8, 16, 32, 64,


128, or 256.

• Timer1 has its own prescaler, which can divide by 1, 2, 4, or 8.

• Example: If using an 8 MHz clock with a prescaler of 256, the


timer increments every 32 μs (since 1/8,000,000 × 256 = 32
μs).
Timer0 Interrupts

Interrupts and Timer0:


• T0IF (Timer0 Interrupt Flag) bit in the INTCON register is set when
Timer0 overflows from 255 to 0.
• If the T0IE (Timer0 Interrupt Enable) bit is set, the Timer0 overflow can
generate an interrupt, pausing the main program and jumping to the
interrupt service routine (ISR).
• ISR: In the ISR, you can reset Timer0, update time variables, or perform
any time-sensitive task.

Clearing the Interrupt:


• After handling the interrupt, the T0IF bit must be cleared manually in
the code. Otherwise, the interrupt will keep triggering since the flag will
remain set.
• Timer0 Configuration Registers:

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.

• Event Counting: In Counter mode, Timer0 can be used to


count the number of external pulses, which is useful in
applications like counting the revolutions of a motor, tracking
sensor inputs, or interfacing with incremental encoders.

• Real-Time Clock (RTC): Timer0 can be configured to


function as a basic real-time clock (RTC), measuring the
passage of seconds, minutes, or even hours by counting
increments.
Key Features of Timer1
1. 16-bit Timer:
• Timer1 consists of two 8-bit registers: TMR1H (high byte) and TMR1L (low
byte), which combine to form a 16-bit counter.
• This allows Timer1 to count from 0 to 65535, giving it a larger range than
Timer0 (which is only 8-bit and can count up to 255).
• After reaching 65535, Timer1 overflows and returns to 0, potentially
triggering an interrupt.
2. Internal or External Clock Source:
• Timer1 can be configured to use either:
 The internal clock source (Fosc/4).
 An external clock source applied to the T1CKI pin. This is useful in
applications that require counting external events.
• This feature makes Timer1 flexible as it can act as both a timer and a
counter.
3. Synchronous or Asynchronous Operation:
• Timer1 can operate in either synchronous or asynchronous mode,
depending on whether it uses the internal or external clock.
• Synchronous mode: Timer1 is synchronized with the system
clock (internal clock).
• Asynchronous mode: Used when Timer1 is driven by an external
clock. This mode allows Timer1 to operate even when the
microcontroller is in sleep mode.

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.

6. Gate Control (Optional Feature):


• Some versions of Timer1 have a gate control feature, allowing
Timer1 to count only when an external gate signal is active.
• This is useful in applications like event measurement or pulse-
width measurement, where the timer should count only during
specific events or conditions.
7. Sleep Mode Operation:
• When using an external clock in asynchronous mode, Timer1 can
continue counting even when the microcontroller is in sleep mode.
This is particularly useful for power-sensitive applications that require
timing or counting while the CPU is inactive.
8. Timer Mode:
• In Timer mode, Timer1 counts based on the internal clock source
(Fosc/4). Each clock cycle increments the timer.
• This mode is typically used for generating delays, measuring intervals,
or scheduling periodic tasks (like blinking an LED or generating a
precise time delay).
9. Counter Mode:
• In Counter mode, Timer1 counts external pulses applied to the T1CKI
pin.
• This mode is useful for event counting applications, such as counting
pulses from a sensor or measuring the frequency of an external signal.
Registers Associated with Timer1
TMR1H and TMR1L:
• TMR1H and TMR1L are the high and low byte registers for Timer1. They store the current value
of the 16-bit timer.
• We can write to these registers to initialize Timer1 with a specific starting value or read from
them to check the current timer count.
T1CON (Timer1 Control Register):
• The T1CON register controls the operation of Timer1. Key bits include:
▪ TMR1ON (Timer1 On/Off): Turns Timer1 on or off.
▪ TMR1CS (Timer1 Clock Source): Selects the clock source for Timer1 (internal or
external).
▪ T1CKPS1 (Timer1 Prescaler Select): These bits select the prescaler value (1:1, 1:2,
1:4, or 1:8).
▪ T1OSCEN (Timer1 Oscillator Enable): Enables or disables an external oscillator for
Timer1.
▪ T1SYNC (Timer1 Synchronization): Selects whether Timer1 is synchronized with the
internal clock when using an external clock source.
PIR1 (Peripheral Interrupt Register 1):
• TMR1IF (Timer1 Interrupt Flag): This bit is set when Timer1 overflows. It must be cleared
in the Interrupt Service Routine (ISR) after handling the interrupt.
• TMR1IE (Timer1 Interrupt Enable): Enables or disables the Timer1 interrupt.
Applications of Timer1:
• Precise Time Delays: Timer1 can be used to generate accurate delays, as
its 16-bit range allows it to count for longer intervals compared to Timer0.
This is useful in applications like generating periodic signals, blinking LEDs,
or controlling timed events.
• Real-Time Clock (RTC): Timer1 can be used as a basic real-time clock by
configuring it to count at precise intervals and generating an interrupt at
regular intervals (e.g., every second).
• Event Counting: In Counter mode, Timer1 is used for counting external
events like the number of pulses from a sensor, such as a rotary encoder or a
tachometer.
• Frequency Measurement: Timer1 can be used to measure the frequency
of an external signal by counting the number of pulses in a specific time
window.
• Watchdog and Power Management: In asynchronous mode, Timer1 can
continue running even in sleep mode, making it useful for low-power
applications where timing must continue even when the main processor is
not active.
Key Features of Timer2:
8-bit Timer:
• Timer2 is an 8-bit timer, meaning it can count from 0 to 255.
• After reaching 255, Timer2 overflows, resets to 0, and can trigger an interrupt.
Internal Clock Source:
• Unlike Timer0, which can use an external clock, Timer2 uses only the internal clock
source (Fosc/4).
• Timer2 increments every clock cycle divided by 4, based on the system clock frequency.
Prescaler:
• Timer2 includes a prescaler to divide the input clock before it reaches the timer.
• Prescaler options are fixed: 1:1, 1:4, and 1:16.
• A prescaler slows down the timer, allowing for longer intervals between timer
increments.
Postscaler:
• In addition to the prescaler, Timer2 also has a postscaler, which divides the overflow
frequency of the timer. o The postscaler can be configured from 1:1 to 1:16.
• Postscaler allows you to extend the range of the timer by increasing the time between
overflow interrupts.
PWM Generation:
• Timer2 is commonly used with the PWM module in the
PIC16C61/71 microcontroller to generate PWM signals.
• The duty cycle of the PWM is controlled by Timer2, making it
useful for controlling motor speed, dimming LEDs, or other
applications where adjustable signal control is required.

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.

T2CON (Timer2 Control Register):


• The T2CON register controls the operation of Timer2. Key bits include:
▪ TMR2ON: Turns Timer2 on or off.
▪ T2CKPS1 : These bits select the prescaler value (1:1,
1:4, 1:16).
▪ T2OUTPS3 : These bits configure the postscaler (1:1 to
1:16).
Applications of Timer2
PWM Generation:
• Motor Control: Timer2 is used for controlling motor speeds by
varying the PWM duty cycle.
• LED Dimming: Adjusting the duty cycle allows for dimming LEDs.
• Power Control: Timer2 controls switching signals in power
electronics.

 Periodic Interrupt Generation: Timer2 can be used to trigger


interrupts at precise intervals, useful in realtime operating systems
(RTOS) or time-sensitive tasks like scheduling.

Frequency Generation: By adjusting the prescaler, postscaler, and


overflow settings, Timer2 can be used to generate precise frequency
signals for various communication protocols or timing-based applications.
Summary Table
References:
• https://www.microchip.com
• https://www.microchip.com/en-us/support/resources-d
ocs
• https://en.wikipedia.org/wiki/PIC_microcontrollers
Thank you

You might also like