Professional Documents
Culture Documents
Basic Timer
Basic Timer
Basic Timer
Basic Timer
Slide 1
MCO556
Basic Timer
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-1-
MCO556
Basic Timer
Slide 2
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-2-
MCO556
Basic Timer
Slide 3
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-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
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-4-
MCO556
Basic Timer
Slide 5
TPMxSC
TPMxSC
Bit7
Bit0
Read:
Write:
TOF
TOIE
CPWMS
CLKSB
CLKSA
PS2
PS1
PS0
Reset:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-5-
MCO556
Basic Timer
Slide 6
TPMxCNT
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-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
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-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 */
}
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
____________________________________________________________________
-8-