PIC18 Timer Programming - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

PIC18 Timer Programming

Description:

Readable and writable 8-bit registers (TMR1H and TMR1L) ... Readable and writable (both registers) Software programmable prescaler (1:1, 1:4 and 1:16) ... – PowerPoint PPT presentation

Number of Views:1008
Avg rating:3.0/5.0
Slides: 41
Provided by: madi74
Category:

less

Transcript and Presenter's Notes

Title: PIC18 Timer Programming


1
PIC18 Timer Programming
  • Explain the assembly language programming for
    the timer and counter module

2
Objectives
  • List the timers of the PIC18 and their associated
    register
  • Describe the various modes of the PIC18 timers
  • Program the PIC18 timers in Assembly Language to
    generate delays
  • Program the PIC18 counters in Assembly as event
    counters

3
Introduction
  • The PIC18 has from two (2) to five (5) timers
    depending on the family member. They are referred
    to as Timer 0, 1, 2, 3 and 4.
  • What Timers can do? 1) Generate a time delay 2)
    As a Counter to count events happening outside
    the microcontroller.
  • PIC18F4580 has 4 Timers.

Timer0
Timer1
Timer3
Timer2
4
Introduction
  • Every timer needs a clock pulse to tick. The
    clock source can be internal or external.
  • Internal clock The 1/4th of the frequency of the
    crystal oscillator on the OSC1 and OSC2 pins
    (Fosc/4) is fed into the timer. Therefore, it is
    used for time delay generation. This is called a
    timer.
  • External clock Fed pulses thru one of the
    PIC18s pins This is called a counter.

5
Introduction
  • Many of the PIC18 timers are 16 bits wide.
  • Each 16-bit timer is accessed as two separate
    register, low byte (TMRxL) and high byte (TMRxH)
  • Each timer also has the TCON (Timer Control)
    register for setting modes of operation.

6
Timer0 Module
  • The Timer0 module incorporates the following
    features
  • Software selectable operation as a timer or
    counter in both 8-bit or 16-bit modes
  • Readable and writable registers
  • Dedicated 8-bit, software programmable prescaler
  • Selectable clock source (internal or external)
  • Edge select for external clock
  • Interrupt-on-overflow

7
Timer0 Registers and Programming
  • Timer0 can be used as an 8-bit or 16-bit timer
  • The 16-bit register of Timer0 is accessed as low
    byte (TMR0L) and high byte (TMR0H)

Timer0 High and Low Registers
8
Timer0 Control Register (T0CON)
9
Example 1
  • Find the value for T0CON if we want to program
    Timer0 in 16-bit mode, no prescaller. Use PIC18s
    Fosc/4 crystal oscillator for the clock source,
    increment on positive-edge.

Solution
With 64 prescaller
T0CON 0000 1000
T0CON 0000 0101
10
Timer0 Clock Source (T0CS)
  • T0CS or bit 5 is used to decide whether the clock
    source is internal (Fosc/4) or external
  • T0CS 0 The Fosc/4 is used as clock source
  • T0CS 1 The clock source is external and comes
    from the RA4/T0CKI (pin 6)

Used as an event counter
11
External Source
Internal Source
12
Example 2
  • Find the timers clock frequency and its period
    for various PIC18 based systems, with the
    following crystal frequency. Assume that no
    prescaller is used.
  • a) 10 MHz
  • b) 16 MHz
  • c) 4 MHz

XTAL Osc / 4
2.5 MHz _at_ 0.4 uS
4 MHz _at_ 0.25 uS
1 MHz _at_ 1 uS
13
INTCON (Interrupt Control)
  • TMR0IF Flag Bit

Tomer0 Overflow Flag
14
16-bit Timer Programming
  • It allows values of 0000H to FFFFH to be loaded
    into the registers TMR0H and TMR0L
  • After loaded, the timer must be started (BSF
    T0CON, TMR0ON)
  • It start to count up until it reaches its limit
    of FFFFH. When it rolls over from FFFFH to 0000H,
    it sets HIGH a flag bit (TMR0IF)
  • Repeat the process 1)Reload the TMR0H and TMR0L
    2)TMR0IF flag must be reset to 0

15
Step to Program Timer0 in 16-bit Mode
  • Load the value into the T0CON register
  • Load register TMR0H followed by register TMR0L
  • Start the timer
  • Keep monitoring the timer flag (TMR0IF)
  • Stop the timer
  • Clear the TMR0IF flag for the next round
  • Go back to the step 2)

16
Example 3
17
Example 4
  • Calculate the amount of time delay generated by
    the timer. Assume that XTAL 10MHz
  • Solution
  • T 4/10MHz 0.4us (Each tick consume 0.4us)
  • How many tick? (FFFF-FFF2) 1 14 Decimal (
    ticks)
  • Time delay 14 x 0.4us 5.6us for half the
    pulse

Rolls Over from FFFF to 0
18
Formula for Delay Calculation
Example
For TMR0H B8 and TMR0L 3E, calculate the
delay generated Assume XTAL 10MHz
Solution
(FFFF B83E 1) 47C2H 18370 x 0.4uS
7.348mS or
65536 47166 18370 x 0.4uS 7.348mS
19
Example 5
  • Write a program to toggle all the bits of PORTB
    continuously
  • with 1ms delay. Use Timer0, 16-bit mode, no
    prescaler options
  • to generate the delay. (Assume XTAL20MHz)
  • Solution
  • TCY 4/20MHz 0.2us (Each tick consume 0.2us)
  • How many ticks in 1ms delay?
  • 1ms/0.2us 5000 ticks 1388H ticks!
  • To find register value for TMR0HTMR0L
  • FFFF - register value 1 1388H ticks
  • register value EC78H
  • _at_
  • Simply take the negative value of the tick counts
  • -1388H EC78H

TMR0H ECH TMR0L 78H
For source code, please modify Example 3
20
Prescaller and Generating a Large Time Delay
  • The time delay depends on two factors, a) The
    crystal frequency b) The timers 16-bit register
  • We can use the prescaler option in the T0CON
    register to increase the delay by reducing the
    period
  • Prescaler option from 2 to 256

XTAL Osc
4
64
TMRx
21
Example 6
  • Find the timers clock frequency and its period
    with the following crystal frequency. Assume that
    a prescaler of 164 is used.
  • a) 10 MHz b) 16 MHz
  • Solution
  • 10MHz/4 2.5MHz ? 2.5MHz/64 39062.5Hz ? T
    1/39062.5Hz 25.6uS
  • 16MHZ/4 4MHz ? 4MHZ/64 62500Hz ?T 1/62500Hz
    16uS

22
Exercise 1
  • Write a program to toggle only the PORTB.4 bit
    continuously every 50ms. Use Timer0, 16-bit mode
    and the 14 prescaler to create the delay.
    (Assume XTAL 20MHz)

Solution TCY 4/20MHz 0.2us (Each tick
consume 0.2us) How many ticks in 50ms delay?
50ms/0.2us 250000 ticks 3D090H ticks! (out
of range!!) With 14 prescaller 250000/4 62500
ticks F424H ticks! Therefore, register counts
-F424H 0BDCH
For source code, please modify Example 3
23
8-bit Mode Programming of Timer0
  • Set the T0CON value register indicating 8-bit
    mode
  • Load the TMR0L register only!
  • Start the timer
  • Keep monitoring the timer flag (TMR0IF)
  • Stop the timer
  • Clear the timer flag
  • Reload TMR0L

Please refer Example 9-16
24
Timer1 Module
  • The Timer1 timer/counter module incorporates
    these features
  • Software selectable operation as a 16-bit
    timer or counter
  • Readable and writable 8-bit registers (TMR1H
    and TMR1L)
  • Selectable clock source (internal or external)
    with device clock or Timer1 oscillator internal
    options
  • Interrupt-on-overflow
  • Module Reset on CCP Special Event Trigger
  • Device clock status flag (T1RUN)

25
Timer1 Programming
  • 16-bit wide
  • Consists of a low-byte (TMR1L) and a high-byte
    (TMR1H) register
  • Can be used as 16-bit timer only!

26
Timer1
  • Important Registers
  • i) T1CON (Timer1 Control Register)
  • To start stop Timer1 and other configurations
  • ii) TMR1HTMR1L (for counting purposes)
  • Act as counting buffer
  • iii) PIR1 (Peripheral Interrupt Request Register
    1)

27
Timer1 Control Register
28
Example 7
  • Write a program to generate a square wave of 50Hz
    frequency on pin PORTB.5. Use Timer1 and the
    maximum prescaler allowed
  • Solution
  • T 1/50Hz 20mS (Square wave)
  • ½ wave 20mS/2 10mS
  • 10mS/0.4uS/8 3125 or C325H
  • Timer1 Register F3CBH

29
Timer0 Timer1 as Counter
  • Can used as Counters
  • Counter0 (Timer0 counter)
  • Count pulses on T0CKI (RA4) pin
  • Counter1 (Timer1 counter)
  • Count pulses on T13CKI (RC0) pin

30
Example - Counter
  • Please refer Example 9-22, 9-23, 9-24, 9-25,9-26
    9-27 in the textbook

31
Timer2 Module
  • The Timer2 module timer incorporates the
    following features
  • 8-Bit Timer and Period registers (TMR2 and PR2,
    respectively)
  • Readable and writable (both registers)
  • Software programmable prescaler (11, 14 and
    116)
  • Software programmable postscaler (11 through
    116)
  • Interrupt on TMR2-to-PR2 match
  • Optional use as the shift clock for the MSSP
    module

32
Timer2 (contd)
  • Important Registers
  • i) T2CON (Timer2 Control Register)
  • To start stop Timer2 and other configurations
  • ii) PR2 (to set the counting value)
  • If TMR2 PR2 TMR2IF flag is set
  • iii) PIR1 (Peripheral Interrupt Request Register
    1)

33
Timer2 (contd)
34
Example
  • Please refer Example 9-38 and 9-39

35
Timer3 Module
  • The Timer3 module timer/counter incorporates
    these features
  • Software selectable operation as a 16-bit timer
    or counter
  • Readable and writable 8-bit registers (TMR3H
    and TMR3L)
  • Selectable clock source (internal or external)
    with device clock or Timer1 oscillator internal
    options
  • Interrupt-on-overflow
  • Module Reset on CCP Special Event Trigger

36
Timer3 (contd)
  • Important Registers
  • i) T3CON (Timer3 Control Register)
  • To start stop Timer3 and other configurations
  • ii) TMR3HTMR3L (for counting purposes)
  • Act as counting buffer
  • iii) PIR2 (Peripheral Interrupt Request Register
    2)

37
Timer3 (contd)
38
Example
  • Please refer Example 9-42, 9-43 and 9-44

39
Summary
  • The PIC18 can have up to four or more
    timers/counters. Depending on the family member
  • Timers Generate Time Delays (using Crystal)
  • Counters Event counter (using Pulse outside)
  • Timers are accessed as two 8-bit registers, TMRLx
    and TMRHx
  • Can be used either 8-bit or 16-bit
  • Each timer has its own Timer Control register

40
End of Chapter 2
  • Never leave that till to-morrow which you can do
    to-day Quote by Benjamin Franklin
Write a Comment
User Comments (0)
About PowerShow.com