Professional Documents
Culture Documents
Outline
Timer Peripherals
Periodic Interrupt Timer (PIT)
Timer/PWM Module (TPM)
Timer Peripherals
Connected to I/O pins, has input capture and output compare support
Can generate PWM signals
Can generate interrupts
Can operate as timer or counter in all power modes (including low-leakage modes)
Can wake up system with an interrupt
Can trigger hardware
Real-Time Clock
SYSTICK
Reload Value
or
Presettable
Binary Counter
Clock
Reload
2 or RS
PWM
Interrupt
Current Count
Counter mode: count pulses which indicate events (e.g. odometer pulses)
Timer mode: clock source is periodic, so counter value is proportional to elapsed
time
Generate interrupt
Reload counter with special value and continue counting
Toggle hardware output signal
Stop!
Write
1000
to TSV
TVL
PIT interrupt
counts
generated,
down to
counter
0
reloads with
1000, starts
counting
32-bit counter
Load start value (32-bit) from LDVAL
Counter counts down with each
clock pulse
Enabling
timer loads
counter with
1000, starts
counting
Generates interrupt
Reloads timer with start value
Clock
PIT interrupt
generated,
counter
reloads with
700, starts
counting
Start Value
Reload
Presettable
Binary Counter
Interrupt
PIT Configuration
First: Clock gating in SIM
SIM->SCGC6 |= SIM_SCGC6_PIT_MASK
Second: Module Control Register
(PIT->MCR)
0: module enabled
CMSIS Interface:
General PIT settings accessed as struct: PIT->MCR, etc.
Channels are accessed as an array of structs: PIT->CHANNEL[n].LDVAL, etc
PIT_LDVALn: Load value (PIT->CHANNEL[n].LDVAL)
PIT_CVALn: Current value (PIT->CHANNEL[n].CVAL)
PIT_TCTRLn: Timer control (PIT->CHANNEL[n].TCTRL)
CHN: Chain
10
No chaining of timers
PIT->CHANNEL[0].TCTRL &= ~PIT_TCTRL_CHN_MASK;
11
12
Configure NVIC
13
14
Interrupt Handler
One interrupt for entire PIT
CMSIS ISR name: PIT_IRQHandler
(can be found in startup_MKL46Z4.s )
ISR activities
15
16
17
18
Interrupt on overflow
Six channels
3 basic modes
Capture Mode: capture timers value when input signal changes
Output Compare: Change output signal when timer reaches certain value
PWM: Generate pulse-width-modulated signal. Width of pulse is proportional to specified
value.
but also Software mode
19
20
Timer Configuration
Clock source
Prescaler
21
2-0 PS
Prescaler Factor
000
001
010
011
100
16
101
32
110
64
111
128
22
23
24
TMPx_SC: TPM0->SC=
TMPx_CnSC: TMP0->CONTROLS[2].CnSC=
ARM University Program
Copyright ARM Ltd 2013
25
26
27
28
CPWMS = 0,
MSnB:MSnA = 00
TPM_CHn I/O pin
operates as edgesensitive input
ELSnB:ELSnA
select rising (01) or
falling edge (10) or
both (11)
TPM_CHn
CnV
initialized value
29
30
Operation: Repeat
31
If MSnB = 0
Toggle (01)
Clear (00)
Set (11)
If MSnB = 1
32
Pulse-Width Modulation
Uses of PWM
Digital power amplifiers are more efficient and less expensive than analog
power amplifiers
Applications: motor speed control, light dimmer, switch-mode power
conversion
Load (motor, light, etc.) responds slowly, averages PWM signal
Digital communication is less sensitive to noise than analog methods
PWM provides a digital encoding of an analog value
Much less vulnerable to noise
33
20 ms period
1 to 2 ms pulse width
34
35
36
Counter value
match
match
match
match
match
Channel out
37
38
Counter value
match
match
match
match
match
Channel out
39
40
Set IO multiplexer
//Set multiplexer to connect TPM0 Ch 2 to PTA5
PORTA->PCR[5] |= PORT_PCR_MUX(3);
//Set multiplexer to connect TPM0 Ch 4 to PTC8
PORTC->PCR[8] |= PORT_PCR_MUX(3);
41
42
43
44
TPM Triggers
External Pin
PITx
TPMx
RTC
LPTMR
45
46
47
LPTMR Overview
Features
16 bit counter
Can count time or external pulses
Can generate interrupt when counter matches compare value
Interrupt wakes MCU from any low power mode
Registers
48
Inputs available depend on chip configuration, see KL25 SRM Chapter 3: Chip
Configuration
49
0: Time counter
1: Pulse counter
50
Prescale Register
Pulse counter mode: Is glitch filter which recognizes input signal change after
2PRESCALE rising clock cycles
0: use prescaler
1: bypass prescaler
Inputs available depend on chip configuration, see KL25 SRM Chapter 3: Chip Configuration
51