Basic Timer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 0

MCO556

Basic Timer
Slide 1

MCO556
Basic Timer

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-1-

MCO556
Basic Timer
Slide 2

Timer Block Diagram

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-2-

MCO556
Basic Timer
Slide 3

Basic Timer Overflow

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-3-

MCO556
Basic Timer
Slide 4

Operation
Microcontroller has 2 timers: TPM1 and TPM2
Separate configuration and status register for
each timer
All configuration contained in 1 register TPMxSC
To determine when the counter overflows,
interrogate register TPMxSC bit 7

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-4-

MCO556
Basic Timer
Slide 5

TPMxSC
TPMxSC
Bit7

Bit0

Read:
Write:

TOF

TOIE

CPWMS

CLKSB

CLKSA

PS2

PS1

PS0

Reset:

CLKSB:CLKSA - Clock Selection


PS2:PS1:PS0 prescale select
Prescale factor = 2n
TOF

timer overflow flag


1 = Timer has overflowed
Writing 1 has no effect
To clear TOF, read TPMxSC with
TOF = 1 then write TOF = 0

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-5-

MCO556
Basic Timer
Slide 6

TPMxCNT

TPMxCNT 16 bit up counter


can be read as unsigned integer
can be read as two unsigned char:
TPMxCNTH - high byte
TPMxCNTL - low byte
Any write to TPMxCNTH, TPMxCNTL or TPMxCS will reset
all 16 bits of the counter

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-6-

MCO556
Basic Timer
Slide 7

Register Addresses
TPM1 base  Address is $0030
TPM2 base  Address is $0060
TPMxCS offset is $0
TPM1CS  $0030
TPM2CS  $0060
TPMxCNT, TPMxCNTH offset is $1
TPMxCNTL offset is $2
TPM1CNT  $0031
TPM2CNT  $0061
TPM1CNTH  $0031
TPM2CNTH  $0061
TPM1CNTL  $0032
TPM2CNTL  $0062

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-7-

MCO556
Basic Timer
Slide 8

Code Fragments
// Configure register TPM1SC - C Statements
TPM1SC = 0x09; // use bus clock as source
// prescale clock by 2 - counter fed 20 MHz clock/2
// Code to detect when timer overflows
if(TPM1SC_TOF)
// check for overflow - test TOF flag
{
// clear TOF flag
TPM1SC_TOF = 0; // read register
// and write 0 to TOF
/* Code placed here executes when the timer overflows */
}

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________

Seneca College School of Information and Communications Technology 133

-8-

You might also like