How the CPU reads a button and lights an LED, one bit at a time. Cómo la CPU lee un botón y enciende un LED, bit a bit.
Santi Scagliusi, PhD
sondeo: bit.b #BIT1, &P1IN ; S1 = P1.1 jz ocupado ; 0 = pulsado bic.b #BIT0, &P1OUT ; libre: rojo off bis.b #BIT7, &P9OUT ; verde on jmp sondeo ocupado: bis.b #BIT0, &P1OUT ; rojo on bic.b #BIT7, &P9OUT ; verde off jmp sondeo
Holding S1, what does P1IN.1 read?Con S1 pulsado, ¿qué lee P1IN.1?
0 or 1? The answer is the whole module.¿0 o 1? La respuesta es todo el módulo.
The same instruction that writes RAM writes a port. The address decides.La misma instrucción que escribe RAM escribe un puerto. La dirección decide.
Writing a 1 into address 0x0202 puts a voltage on physical pin P1.0. Escribir un 1 en la dirección 0x0202 pone tensión en el pin físico P1.0.
PxDIR steers the pin in or out. The others drive it or read it.PxDIR decide si el pin es entrada o salida. Los demás lo accionan o lo leen.
P1 to P10 are 8-bit. Pairs combine into 16-bit word ports PA to PE.P1 a P10 son de 8 bits. Las parejas forman puertos de 16 bits PA a PE.
Only P1, P2, P3, P4 have edge-interrupt logic. P5 to P10 and PJ do not. Solo P1, P2, P3, P4 tienen lógica de interrupción por flanco. P5 a P10 y PJ no.
EXP430FR6989: two LEDs, two buttons, fixed pins.EXP430FR6989: dos LEDs, dos botones, pines fijos.
LEDs are active-high. A pressed button ties the pin to ground, so it reads 0: active-low. Los LEDs son activos a nivel alto. Un botón pulsado lleva el pin a masa, así que lee 0: lógica negativa.
After reset, the FR6989 pins are locked in high impedance.Tras el reset, los pines del FR6989 están bloqueados en alta impedancia.
Skip the third line and the LED never lights. Sin la tercera línea, el LED no se enciende nunca.
Direction first, then the level. P1.0 = LED1.Primero la dirección, luego el nivel. P1.0 = LED1.
Same start byte, same mask #BIT2|BIT0. Only the named bits change.Mismo byte inicial, misma máscara #BIT2|BIT0. Solo cambian los bits nombrados.
Start 1000 0110 in all three. Bits outside the mask never change. Las tres parten de 1000 0110. Los bits fuera de la máscara no cambian.
mov.b #0xF0, &P1OUT ; 1111 0000 bis.b #BIT0, &P1OUT ; 0xF1 xor.b #BIT0, &P1OUT ; 0xF0 xor.b #BIT0, &P1OUT ; 0xF1 bic.b #BIT0, &P1OUT ; 0xF0 mov.b #BIT0, &P1OUT ; P1OUT = ?
What is P1OUT after the last MOV?¿Cuánto vale P1OUT tras el último MOV?
What happened to bits 4..7?¿Qué ha pasado con los bits 4..7?
Pull-up to Vcc, button to ground. Pressed wins and pulls the pin to 0.Pull-up a Vcc, botón a masa. Pulsado gana y lleva el pin a 0.
A button to ground needs three writes before it reads cleanly.Un botón a masa necesita tres escrituras para leerse bien.
One press, several edges in the first milliseconds.Una pulsación, varios flancos en los primeros milisegundos.
leer: mov.b &P1IN, R6 and.b #BIT1, R6 ; R6 = ahora cmp.b R5, R6 ; R5 = antes jeq leer ; sin cambio mov.b R6, R5 ; ...retardo antirrebote... tst.b R5 jnz leer ; 0→1: soltar inc.w R10 ; 1→0: contar jmp leer
Hold S1 down. Does R10 keep climbing?Mantén S1 pulsado. ¿R10 sigue subiendo?
Watch R5 and R6. And on the board, without the delay?Mira R5 y R6. ¿Y en la placa, sin el retardo?
An edge sets the flag. If the gates are open, the CPU jumps to the ISR.Un flanco pone la bandera. Si las puertas están abiertas, la CPU salta a la ISR.
Same dispatch as Module 7. Here the source is a pin edge on P1 to P4. Mismo despacho que el Módulo 7. Aquí la fuente es un flanco en un pin de P1 a P4.
One vector serves all eight pins of the port. PxIV decodes the highest priority.Un vector sirve a los ocho pines del puerto. PxIV decodifica el de mayor prioridad.
The CPU sleeps. Each press wakes it and toggles the LED.La CPU duerme. Cada pulsación la despierta y conmuta el LED.
Click "LaunchPad" in the I/O panel first: P1.0 = LED1, P1.1 = S1, P1.2 = S2.Pulsa "LaunchPad" en el panel de E/S primero: P1.0 = LED1, P1.1 = S1, P1.2 = S2.
Step it: watch P1OUT bits set, clear and flip.Paso a paso: mira los bits de P1OUT ponerse, borrarse y conmutar.
Hold S1: green (free) turns to red (busy).Mantén S1: verde (libre) pasa a rojo (ocupado).
R5 vs R6: only the press edge adds 1 to R10.R5 frente a R6: solo el flanco de pulsar suma 1 a R10.
Digital I/O is just reads and writes to memory-mapped bits.La E/S digital son solo lecturas y escrituras de bits mapeados en memoria.
Set PxDIR first. Write PxOUT, read PxIN. Reset leaves every pin an input.Pon PxDIR primero. Escribe PxOUT, lee PxIN. Tras reset todo pin es entrada.
A byte mask and the .B suffix change one pin and leave the rest of the port intact.Una máscara de byte y el sufijo .B cambian un pin y dejan el resto del puerto intacto.
Pull-up holds high, pressed reads 0. P1 to P4 can interrupt on the edge.El pull-up mantiene alto, pulsado lee 0. P1 a P4 interrumpen en el flanco.