Step 9 of 11
PORT: button controls the LED
Goal
Move logic to the loop: read RB1 with PORTB and drive the LED with LATB.
Remove fixed BSF LATB,0 from inicio (keep BCF LATB,0 after init). In bucle, PORT reads the button and LAT updates the LED.
NA button + pull-up: RB1 = 1 released, 0 pressed. Press → LED on.
Follow these steps
- Delete
BSF LATB, 0frominicio(keepBCF LATB, 0andCALL init_gpio). - Replace empty loop with the 4 instructions from the highlighted block +
GOTO bucle. - Build, reload HEX in Proteus.
- Test: press button → LED on; release → off. Repeat 3 times to confirm.
Your file so far
LIST P=18F4550
#include <P18F4550.INC>
CONFIG FOSC = HS
CONFIG WDT = OFF
CONFIG LVP = OFF
CONFIG PBADEN = OFF
ORG 0x0000
GOTO inicio
init_gpio:
BCF TRISB, 0
BSF TRISB, 1
RETURN
inicio:
CALL init_gpio
BCF LATB, 0
bucle:
BTFSC PORTB, 1 ; RB1 alto = pulsador suelto
BCF LATB, 0
BTFSS PORTB, 1 ; RB1 bajo = pulsador presionado
BSF LATB, 0
GOTO bucle
END
What you add in this step
bucle:
BTFSC PORTB, 1 ; RB1 alto = pulsador suelto
BCF LATB, 0
BTFSS PORTB, 1 ; RB1 bajo = pulsador presionado
BSF LATB, 0
GOTO bucle
Try it
Optional: invert logic (LED on when you release the button).
Checklist before you continue
- Loop uses PORTB to read and LATB to write.
- I tested button and LED at least 3 times — it works.