Hassing.org

PIC10 Microntroller Button/LED

2021/09/26 :: HACKING

Loop through LEDs using a push button using a PIC10F202 microcontroller.


#include "p10f202.inc"

    __CONFIG _WDT_OFF & _CP_OFF & _MCLRE_OFF
pressed     EQU     11
dir     EQU     12
wait_timer  EQU     13
    ORG 0x0000

INIT
    ; Setup IO
    MOVLW ~((1 << T0CS) | (1 << PSA)) ; Enable GPIO2 and internal timer
    OPTION
    MOVLW ~((1 << GP0) | (1 << GP1) | (1 << GP2))
    TRIS GPIO

    ; Clear button pressed and animation direction
    CLRF pressed
    CLRF dir

    ; Setup active LED
    CLRF GPIO
    BSF GPIO, GP0

LOOP
    ; Check if button pressed
    BTFSC GPIO, GP3
    GOTO PRESSED
    BCF pressed, GP3
    GOTO NOT_PRESSED
PRESSED
    BTFSS pressed, GP3
    CALL CHANGE_LED
    BSF pressed, GP3
NOT_PRESSED

    GOTO LOOP

; Loop through leds in loop pattern: 1, 2, 3, 2, 1, 2, ...
CHANGE_LED
    BTFSS GPIO, GP0
    GOTO CHECK_GP1
    ; GP0 active
    BCF GPIO, GP0
    BSF GPIO, GP1
    GOTO END_CHANGE_LED
CHECK_GP1
    BTFSS GPIO, GP1
    GOTO IS_GP2
    ; GP1 active
    BCF GPIO, GP1
    BTFSC dir, GP0
    GOTO $ + 4
    BSF GPIO, GP2
    BSF dir, GP0
    GOTO END_CHANGE_LED
    BSF GPIO, GP0
    BCF dir, GP0
    GOTO END_CHANGE_LED
IS_GP2
    ; GP2 active
    BCF GPIO, GP2
    BSF GPIO, GP1
END_CHANGE_LED
    ; Debounce button
    CALL WAIT
    RETLW 0

WAIT
    CLRF TMR0
    CLRF wait_timer
WAIT_LOOP
    BTFSS TMR0, 2
    GOTO WAIT_LOOP
    CLRF TMR0
    DECFSZ wait_timer, F
    GOTO WAIT_LOOP
    RETLW 0

END```