React to events without polling, and stay asleep until one arrives. Reaccionar a eventos sin sondear, y dormir hasta que llegue uno.
Santi Scagliusi, PhD
bis.b #BIT1, &P1IE ; S1 puede interrumpir bis.w #GIE|LPM4, SR ; a dormir P1ISR: inc.w R10 ; una pulsación más ; LED1 = bit 0, LED2 = bit 1 de R10 bic.b #BIT1, &P1IFG reti .intvec PORT1_VECTOR, P1ISR
After 3 presses: R10? LEDs?Tras 3 pulsaciones: ¿R10? ¿LEDs?
And between presses, watch the PC.Y entre pulsaciones, mira el PC.
The same button press, handled two different ways.La misma pulsación de botón, atendida de dos formas distintas.
The CPU loops, asking the flag over and over if the event happened.La CPU itera, preguntando al flag una y otra vez si ocurrió el evento.
Burns cycles and energy while nothing happens.Quema ciclos y energía mientras no pasa nada.
The hardware watches the flag. The event calls the CPU, not the other way around.El hardware vigila el flag. El evento llama a la CPU, no al revés.
Sleeps at near-zero current, wakes only when needed.Duerme con corriente casi nula, despierta solo cuando hace falta.
How does the CPU learn which device asked, and which routine to run?¿Cómo sabe la CPU qué dispositivo pidió, y qué rutina ejecutar?
Inside the chip the lines are free, so the MSP430 gives every source its own vector.Dentro del chip las líneas son gratis, así que el MSP430 da a cada fuente su propio vector.
All three must hold at the same time, or the request just waits.Las tres deben cumplirse a la vez, o la petición espera.
Miss any one input and the request just waits. The vector must already point at a real ISR.Si falta una entrada, la petición espera. El vector ya debe apuntar a una ISR real.
bis.b #BIT1, &P1IES ; flanco de bajada bic.b #BIT1, &P1IFG bis.b #BIT1, &P1IE ; fuente habilitada bis.w #CPUOFF, SR ; duerme... sin GIE P1ISR: xor.b #BIT0, &P1OUT bic.b #BIT1, &P1IFG reti
Press S1. Why does LED1 stay off?Pulsas S1. ¿Por qué no se enciende LED1?
P1IFG says yes. Who says no?P1IFG dice que sí. ¿Quién dice que no?
From the event to the first ISR instruction: six fixed steps, about 6 cycles.Del evento a la primera instrucción de la ISR: seis pasos fijos, unos 6 ciclos.
128 bytes at 0xFF80–0xFFFE. Higher address means higher priority.128 bytes en 0xFF80–0xFFFE. Dirección más alta significa mayor prioridad.
You never read these addresses by hand. .intvec NAME, ISR installs the pointer for you.Nunca lees estas direcciones a mano. .intvec NAME, ISR instala el puntero por ti.
When several requests are pending at once, the address decides who goes first.Cuando varias peticiones están pendientes a la vez, la dirección decide quién va primero.
An ISR is not interrupted unless it sets GIE again, at the cost of more stack.Una ISR no se interrumpe salvo que vuelva a poner GIE, a costa de más pila.
The same SR holds the condition flags, the global enable, and the sleep bits.El mismo SR guarda los flags de condición, el permiso global y los bits de reposo.
GIE (bit 3) is the global switch. EINT sets it, DINT clears it.GIE (bit 3) es el interruptor global. EINT lo activa, DINT lo limpia.
Bits 4–7 select the low-power mode. Setting them is how you go to sleep.Bits 4–7 seleccionan el modo de bajo consumo. Activarlos es cómo te duermes.
Hardware calls it, you do not. A few constraints follow from that.La llama el hardware, no tú. De ahí salen unas pocas restricciones.
End with RETITermina con RETI
Save what you touchGuarda lo que toques
Clear the flagLimpia el flag
Stay shortSé breve
Configure the source, install the vector, then let the ISR count: LEDs show R10 in binary.Configura la fuente, instala el vector, y deja que la ISR cuente: los LEDs muestran R10 en binario.
main: bic.w #LOCKLPM5, &PM5CTL0 ; unlock pins bic.b #BIT1, &P1DIR ; P1.1 input bis.b #BIT1, &P1REN ; resistor on bis.b #BIT1, &P1OUT ; pull-up bis.b #BIT1, &P1IES ; falling edge bic.b #BIT1, &P1IFG ; clear flag bis.b #BIT1, &P1IE ; enable eint ; GIE = 1
P1ISR: inc.w R10 ; count press xor.b #BIT0, &P1OUT ; LED1 = bit 0 bit.b #BIT0, &P1OUT ; LED1 just off? jnz fin xor.b #BIT7, &P9OUT ; carry → LED2 fin: bic.b #BIT1, &P1IFG ; clear P1.1 flag reti .intvec PORT1_VECTOR, P1ISR
P1 is a shared vector, so the flag is cleared by hand.P1 es un vector compartido, así que el flag se limpia a mano.
All eight P1 pins share vector 0xFFDA. Which pin fired?Los ocho pines de P1 comparten el vector 0xFFDA. ¿Qué pin disparó?
bit.b #BIT0, &P1IFG jz next bic.b #BIT0, &P1IFG ; ... one test per bit ...
Long, and you set the priority order yourself.Largo, y tú fijas el orden de prioridad.
add.w &P1IV, PC reti ; 0 = none jmp P1_0 ; 2 = P1.0 jmp P1_1 ; 4 = P1.1
Value = (pin + 1) × 2. P1IV reports the highest-priority pending pin and auto-clears it.Valor = (pin + 1) × 2. P1IV informa del pin pendiente de mayor prioridad y lo autoborra.
Each mode shuts down more clocks. What stays on decides what can still wake you.Cada modo apaga más relojes. Lo que queda activo decide qué puede despertarte.
LPM3 is the common choice: the lowest that still keeps a time base. (LPM3.5/4.5 go deeper.)LPM3 es la elección habitual: lo más bajo que aún mantiene una base de tiempo. (LPM3.5/4.5 bajan más.)
Pick a mode and watch which clocks survive.Elige un modo y observa qué relojes sobreviven.
The default state is asleep. Each interrupt is a short burst of work, then back to sleep.El estado por defecto es dormir. Cada interrupción es una breve ráfaga de trabajo, y a dormir.
bis.w #GIE+LPM3, sr ; sleep
bic.w #LPM3, 0(sp) ; in stacked SR
dormir: bis.w #GIE|LPM0, SR ; a dormir... nop ; ...aquí despierta inc.w R11 ; main trabaja xor.b #BIT0, &P1OUT jmp dormir P1ISR: bic.b #BIT1, &P1IFG bic.w #LPM0, 0(SP) ; SR apilado reti
After 3 presses, R11 = ?Tras 3 pulsaciones, ¿R11 = ?
And without the BIC on 0(SP)?¿Y sin el BIC sobre 0(SP)?
Timer_A0 counts up to CCR0, fires its own vector, and toggles LED1 while the CPU sleeps in LPM3.Timer_A0 cuenta hasta CCR0, dispara su vector y conmuta LED1 mientras la CPU duerme en LPM3.
main: mov.w #20, &TA0CCR0 ; placa: 16383 mov.w #CCIE, &TA0CCTL0 mov.w #TASSEL_1|MC_1|TACLR, &TA0CTL bis.w #GIE|LPM3, SR ; sleep, ACLK on TA0ISR: xor.b #BIT0, &P1OUT ; LED1 inc.w R10 reti ; CCIFG auto-clears
M7 · Demo: Parpadeo con timer en LPM3
Watch the Timer_A panel: TA0R ramps 0 to CCR0, CCIFG sets, the CCR0 ISR runs each period.Observa el panel Timer_A: TA0R sube de 0 a CCR0, CCIFG se activa, la ISR de CCR0 corre cada periodo.
▶ Try it in the simulatorPruébalo en el simuladorInterrupts and sleep, in three ideas.Interrupciones y reposo, en tres ideas.
A flag, its enable, and the global enable. All three, or the request waits.Un flag, su habilitación y el permiso global. Las tres, o la petición espera.
Entry pushes PC and SR, clears GIE, loads the vector. RETI undoes it.La entrada apila PC y SR, limpia GIE, carga el vector. RETI lo deshace.
Sleep in main, work in the ISR, clear LPM in the stacked SR to stay awake.Duerme en main, trabaja en la ISR, limpia LPM en el SR apilado para seguir activo.