Skip to content

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.

  1. Delete BSF LATB, 0 from inicio (keep BCF LATB, 0 and CALL init_gpio).
  2. Replace empty loop with the 4 instructions from the highlighted block + GOTO bucle.
  3. Build, reload HEX in Proteus.
  4. Test: press button → LED on; release → off. Repeat 3 times to confirm.
						        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
					
						bucle:
        BTFSC   PORTB, 1      ; RB1 alto = pulsador suelto
        BCF     LATB, 0
        BTFSS   PORTB, 1      ; RB1 bajo = pulsador presionado
        BSF     LATB, 0
        GOTO    bucle
					
  • Loop uses PORTB to read and LATB to write.
  • I tested button and LED at least 3 times — it works.