Module 9Módulo 9

TimersTemporizadores

A counter in hardware that keeps time so the CPU does not have to. Un contador en hardware que mide el tiempo para que la CPU no tenga que hacerlo.

Santi Scagliusi, PhD

Why a hardware timerPor qué un temporizador en hardware

Same wait. One way burns the CPU through it, the other sleeps.La misma espera. Una forma quema la CPU durante ella, la otra duerme.

delay loopbucle de retardo CPU spins at full speedCPU girando a tope 100 µA/MHz hardware timertimer en hardware CPU asleep · LPM3CPU dormida · LPM3 timer counts on its own clockel timer cuenta con su propio reloj wakedespierta 0.9 µA

The timer decides whether the chip burns milliamps or microamps while it waits. El timer decide si el chip quema miliamperios o microamperios mientras espera.

The timers on the FR6989Los temporizadores del FR6989

Four Timer_A instances plus one Timer_B. All 16-bit up-counters.Cuatro instancias de Timer_A más un Timer_B. Todos contadores ascendentes de 16 bits.

MSP430FR6989 peripheral space 0x0100–0x0FFFespacio de periféricos 0x0100–0x0FFF WE USEUSAMOS TA0 3 channels3 canales 0x0340 TA1 3 ch · 0x03803 can · 0x0380 TA2 2 ch · 0x04002 can · 0x0400 TA3 5 ch · 0x04405 can · 0x0440 TB0 Timer_B · 7 ch double-buffered · 0x03C0Timer_B · 7 can doble buffer · 0x03C0 five timer modules · all 16-bit up-counterscinco módulos de timer · todos contadores ascendentes de 16 bits

All five share the same programming model. Learn TA0 and the rest follow. Los cinco comparten el mismo modelo de programación. Aprende TA0 y el resto se deduce.

Inside Timer_ADentro del Timer_A

A clock feeds a divider, which feeds the 16-bit counter TAxR. The mode decides how it counts.Un reloj alimenta un divisor, que alimenta el contador de 16 bits TAxR. El modo decide cómo cuenta.

TASSEL
TACLK
ACLK
SMCLK
INCLK
clock sourcefuente de reloj
ID · IDEX
/1 /2 /4 /8
divider, down to /64 combineddivisor, hasta /64 combinado
TAxR
16-bit
counter
contador
16 bits
the heart of the timerel corazón del temporizador
MC
StopParado
UpArriba
ContinuousContinuo
Up/DownArriba/Abajo
count modemodo de cuenta

Every one of these choices is a field of a single register: TAxCTL. Cada una de estas elecciones es un campo de un solo registro: TAxCTL.

The control register, in one writeEl registro de control, en una escritura

Pick a source, a divider and a mode, OR them together, store once.Elige fuente, divisor y modo, los unes con OR, y escribes una vez.

mov.w #TASSEL__ACLK|ID__8|MC__UP|TACLR, &TA0CTL
0
0
0
0
0
0
0
1
1
1
0
1
0
1
0
0
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
TASSEL = ACLK ID = /8 MC = Up TACLR = 0x01D4

Four ways to countCuatro formas de contar

Field MC selects the shape of TAxR over time.El campo MC selecciona la forma de TAxR en el tiempo.

StopParado MC=0

Halted. The counter holds.Detenido. El contador no avanza.

UpArriba MC=1
CCR0

0 to CCR0, then 0. Sets the period.0 a CCR0, luego 0. Fija el periodo.

ContinuousContinuo MC=2
FFFF

0 to 0xFFFF, then 0. Free running.0 a 0xFFFF, luego 0. Cuenta libre.

Up/DownArriba/Abajo MC=3
CCR0

Up to CCR0, then back down. Symmetric.Sube a CCR0, luego baja. Simétrico.

Up mode is the workhorse: CCR0 is the period, the rest is timing inside it. El modo Up es el caballo de batalla: CCR0 es el periodo, lo demás es temporizar dentro de él.

Compare mode and the CCR registersModo comparación y los registros CCR

Each channel watches the counter for one value and acts when it matches.Cada canal vigila el contador buscando un valor y actúa cuando coincide.

TAxR t TAxCCRn matchcoincide CCIFG = 1 → drive output · request IRQ→ mueve salida · pide IRQ
CCR0 is specialCCR0 es especial

Sets the period and owns its own interrupt vector.Fija el periodo y tiene su propio vector de interrupción.

CCR1, CCR2, ...CCR1, CCR2, ...

Extra compare points inside the same period: a PWM edge or a second event.Puntos de comparación extra dentro del periodo: un flanco PWM o un segundo evento.

Two interrupt pathsDos caminos de interrupción

CCR0 gets a private vector. Everything else shares one and is decoded by TAxIV.CCR0 tiene un vector privado. Todo lo demás comparte uno y se decodifica con TAxIV.

CCR0 CCIFG (TA0CCTL0) 0xFFE8 TIMER0_A0 flag cleared automaticallybandera borrada sola CCR1·CCR2 CCIFG TAIFG TA0IV 0xFFE6 TIMER0_A1 clear the flag yourselfborra la bandera a mano

CCIFG: a compare matched. TAIFG: the counter rolled over. CCIFG: coincidió una comparación. TAIFG: el contador se desbordó.

Worked example: a 100 Hz square waveEjemplo: una onda cuadrada de 100 Hz

Toggle an output at a visible rate from ACLK. The only real work is picking CCR0.Conmutar una salida a un ritmo visible desde ACLK. El único trabajo real es elegir CCR0.

ACLK / 8 = 4096 Hz CCR0 = 19 TA0R output toggles each periodla salida conmuta cada periodo 20 ticks20 cuentas
CCR0 = 4096/200 − 1 = 19 → 4096 / 20 / 2 = 102.4 Hz

Integer division drops the remainder: that is the source of the small frequency error.La división entera descarta el resto: ese es el origen del pequeño error de frecuencia.

The configurationLa configuración
FTA0  .equ 4096
Firq  .equ 200
CCR0  .equ FTA0/Firq-1

 mov.w #CCR0, &TA0CCR0
 mov.w #OUTMOD__TOGGLE|CCIE, &TA0CCTL0
 mov.w #TASSEL__ACLK|ID__8|MC__UP|TACLR, &TA0CTL
 eint

The hardware toggles the pin and raises the IRQ. The ISR can be empty.El hardware conmuta el pin y lanza la IRQ. La ISR puede estar vacía.

Capture mode, the mirror imageModo captura, la imagen espejo

Compare drives an event at a known time. Capture records the time of an external event.Comparación provoca un evento en un instante conocido. Captura registra el instante de un evento externo.

Compare (CAP = 0) · time in → event outComparación (CAP = 0) · tiempo entra → evento sale
CCRn (you write)CCRn (lo escribes) eventevento TAxR
Capture (CAP = 1) · event in → time outCaptura (CAP = 1) · evento entra → tiempo sale
TAxR edgeflanco → CCRn

CM picks the edge, CCIS picks the input. The captured number is just TAxR frozen. CM elige el flanco, CCIS elige la entrada. El número capturado es simplemente TAxR congelado.

PWM with output mode 7PWM con modo de salida 7

Up mode plus reset/set output: CCR0 is the period, CCR1 is the duty cycle. No CPU involved.Modo Up más salida reset/set: CCR0 es el periodo, CCR1 es el ciclo de trabajo. Sin CPU.

CCR0 CCR1 HIGHALTO
set at CCR0set en CCR0
reset at CCR1reset en CCR1
duty = CCR1 / CCR0duty = CCR1 / CCR0

CCR1 sets the pulse width. The brightness of an LED, fixed by one register. CCR1 fija la anchura del pulso. El brillo de un LED, fijado por un registro.

Run it: square wave + CCR0 interruptPruébalo: onda cuadrada + interrupción de CCR0

Up mode, ACLK, toggle output, periodic IRQ on CCR0. The ISR returns immediately.Modo Up, ACLK, salida en conmutación, IRQ periódica en CCR0. La ISR vuelve enseguida.

CCR0    .equ 19

main    call  #ConfTA0
loop    nop
        jmp   loop

ConfTA0 bis.b #BIT5, &P1SELC
       bis.b #BIT5, &P1DIR
       mov.w #CCR0, &TA0CCR0
       mov.w #OUTMOD__TOGGLE|CCIE, &TA0CCTL0
       mov.w #TASSEL__ACLK|ID__8|MC__UP|TACLR, &TA0CTL
       eint
       ret

TA00ISR reti
       .intvec RESET_VECTOR, main
       .intvec TIMER0_A0_VECTOR, TA00ISR
What to watchQué observar

In the Timer_A panel, TA0R ramps 0 to CCR0, CCIFG sets, the CCR0 ISR runs, then it repeats.En el panel Timer_A, TA0R sube de 0 a CCR0, se activa CCIFG, corre la ISR de CCR0 y se repite.

Try it in the simulatorProbalo en el simulador · M9 · Timer: square wave + IRQ

Run it: continuous mode, two channelsPruébalo: modo continuo, dos canales

CCR1 and CCR2 share the second vector. The ISR reads the flag, clears it and re-arms.CCR1 y CCR2 comparten el segundo vector. La ISR lee la bandera, la borra y la re-arma.

P1      .equ 30
P2      .equ 50
main    mov.w #P1, &TA0CCR1
        mov.w #P2, &TA0CCR2
        mov.w #CCIE, &TA0CCTL1
        mov.w #CCIE, &TA0CCTL2
        mov.w #TASSEL__ACLK|MC__CONTINUOUS|TACLR, &TA0CTL
        eint
loop    jmp   loop
TA01ISR bit.w #CCIFG, &TA0CCTL1
       jz    chk2
       bic.w #CCIFG, &TA0CCTL1
       add.w #P1, &TA0CCR1
chk2    bit.w #CCIFG, &TA0CCTL2
       jz    fin
       bic.w #CCIFG, &TA0CCTL2
       add.w #P2, &TA0CCR2
fin     reti

       .intvec TIMER0_A1_VECTOR, TA01ISR
What to watchQué observar

In the Timer_A panel, TA0R runs free. CCR1 fires every 30 ticks, CCR2 every 50, both through the same ISR.En el panel Timer_A, TA0R corre libre. CCR1 dispara cada 30 ticks, CCR2 cada 50, ambos por la misma ISR.

The ISR re-arms each channel by adding its period, so the matches keep repeating.La ISR re-arma cada canal sumando su periodo, así las coincidencias se repiten.

Try it in the simulatorProbalo en el simulador · M9 · Timer: continuous (CCR1/CCR2)

Run it: PWM with no interruptsPruébalo: PWM sin interrupciones

8-bit PWM on P1.0 / TA0.1. The timer drives the output by itself, the CPU is free.PWM de 8 bits en P1.0 / TA0.1. El timer mueve la salida solo, la CPU queda libre.

pwm     bis.b #BIT0, &P1SEL0  ; P1.0 = TA0.1
       bis.b #BIT0, &P1DIR
       mov.w #OUTMOD_7, &TA0CCTL1 ; reset/set
       mov.w #64, &TA0CCR1   ; ~25% duty
       mov.w #255, &TA0CCR0  ; period
       mov.w #TASSEL__ACLK|ID__1|MC__UP|TACLR, &TA0CTL
done    jmp   done

       .intvec RESET_VECTOR, pwm
What to watchQué observar

Click LaunchPad, Run. TA0R counts to 255; the P1.0 output goes high at CCR0 and low at CCR1, no ISR at all.Pulsa LaunchPad, Run. TA0R cuenta hasta 255; la salida P1.0 sube en CCR0 y baja en CCR1, sin ninguna ISR.

Try it in the simulatorProbalo en el simulador · M9 · PWM (no interrupts)

Three ideas to take with youTres ideas para llevarte

A timer is a counter the CPU configures once and then forgets.Un temporizador es un contador que la CPU configura una vez y luego olvida.

TAxR

One counter, four modes.Un contador, cuatro modos.

Source, divider and mode are all fields of TAxCTL, written in one instruction.Fuente, divisor y modo son campos de TAxCTL, escritos en una instrucción.

CCRn

Compare or capture.Comparar o capturar.

Compare turns time into events and PWM. Capture turns events into timestamps.Comparar convierte el tiempo en eventos y PWM. Capturar convierte eventos en marcas de tiempo.

0xFFE8

CCR0 has its own vector.CCR0 tiene su propio vector.

Everything else shares the second vector, decoded by TAxIV. CCIFG vs TAIFG.Todo lo demás comparte el segundo vector, decodificado por TAxIV. CCIFG vs TAIFG.