Está en la página 1de 23

EJEMPLO1

Descripción: Programa que solo prende el LED cuando ambos interruptores están encendidos, de
resto permanece apagado.
Código:
#include "p16f877a.inc"
; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa
START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,0
BSF TRISB,1
BCF TRISB,2
BCF STATUS,RP0 ;APUNTA AL BANCO 0
CICLO
BTFSS PORTB,0
GOTO PRENDE_LEDS
BCF PORTB,2
GOTO CICLO
PRENDE_LEDS
BTFSS PORTB,1
GOTO APAGA_LEDS
BCF PORTB,2
GOTO CICLO
APAGA_LEDS
BSF PORTB,2
GOTO CICLO
END
Imagen del funcionamiento:
EJEMPLO2
Descripción: Programa que con 4 LEDs y 2 botones se prenda cada bombillo según la combinación,
como un Flip-Flop tipo D

Código:

#include "p16f877a.inc"
; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa
START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,2
BSF TRISB,3
BCF TRISB,4
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0
CICLO
BTFSS PORTB,2
GOTO PRENDE_LED_S1
BTFSC PORTB,2
GOTO PRENDE_LED_S2
GOTO CICLO
PRENDE_LED_S1
BTFSS PORTB,3
GOTO PRENDE_LED4
BTFSC PORTB,3
GOTO PRENDE_LED2
GOTO CICLO
PRENDE_LED_S2
BTFSS PORTB,3
GOTO PRENDE_LED3
BTFSC PORTB,3
GOTO PRENDE_LED1
GOTO CICLO
PRENDE_LED4
BSF PORTB,7
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
GOTO CICLO
PRENDE_LED3
BSF PORTB,6
BCF PORTB,4
BCF PORTB,5
BCF PORTB,7
GOTO CICLO
PRENDE_LED2
BSF PORTB,5
BCF PORTB,4
BCF PORTB,6
BCF PORTB,7
GOTO CICLO
PRENDE_LED1
BSF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
GOTO CICLO
END
Imagen del funcionamiento:
EJEMPLO3
Descripción: Programa donde hay 4 LEDs y 2 botones, donde si ambos botones están oprimidos
todos los LEDs se enciendan, si solo uno está oprimido, el 1 y el 4 se prendan, y si el otro se oprime
se enciendan el 2 y el 3, y si ambos están inactivos todos permanecen apagados.

Código:

#include "p16f877a.inc"
; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa
START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,2
BSF TRISB,3
BCF TRISB,4
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0
CICLO
BTFSS PORTB,2
GOTO PRENDE_LED_S1
BTFSC PORTB,2
GOTO PRENDE_LED_S2
GOTO CICLO
PRENDE_LED_S1
BTFSS PORTB,3
GOTO PRENDE_LED4
BTFSC PORTB,3
GOTO PRENDE_LED2
GOTO CICLO
PRENDE_LED_S2
BTFSS PORTB,3
GOTO PRENDE_LED3
BTFSC PORTB,3
GOTO PRENDE_LED1
GOTO CICLO
PRENDE_LED4
BSF PORTB,7
BSF PORTB,4
BSF PORTB,5
BSF PORTB,6
GOTO CICLO
PRENDE_LED3
BCF PORTB,6
BSF PORTB,4
BCF PORTB,5
BSF PORTB,7
GOTO CICLO
PRENDE_LED2
BSF PORTB,5
BCF PORTB,4
BSF PORTB,6
BCF PORTB,7
GOTO CICLO
PRENDE_LED1
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
GOTO CICLO
END
Imagen del funcionamiento:
EJEMPLO4
Descripción: Programa con 4 LEDs y 2 botones, donde en 00 se ven los LEDs palpitando, en 01 se
encienden los LEDs 1 y 3, en 10 se encienden los LEDs 2 y 4, y en 11 todos los LEDs se encienden.

Código:

#include "p16f877a.inc"

; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa

START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,2
BSF TRISB,3
BCF TRISB,4
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0
CICLO
BTFSS PORTB,2
GOTO PRENDE_LED_S1
BTFSC PORTB,2
GOTO PRENDE_LED_S2
GOTO CICLO
PRENDE_LED_S1
BTFSS PORTB,3
GOTO PRENDE_LED4
BTFSC PORTB,3
GOTO PRENDE_LED2
GOTO CICLO
PRENDE_LED_S2
BTFSS PORTB,3
GOTO PRENDE_LED3
BTFSC PORTB,3
GOTO PRENDE_LED1
GOTO CICLO
PRENDE_LED4
BSF PORTB,7
BSF PORTB,4
BSF PORTB,5
BSF PORTB,6
GOTO CICLO
PRENDE_LED3
BSF PORTB,6
BSF PORTB,4
BCF PORTB,5
BCF PORTB,7
GOTO CICLO
PRENDE_LED2
BSF PORTB,5
BCF PORTB,4
BCF PORTB,6
BSF PORTB,7
GOTO CICLO
PRENDE_LED1
BTFSS PORTB,4
GOTO PRENDE_LED3
BTFSS PORTB,5
GOTO PRENDE_LED2
GOTO CICLO
END
Imagen de funcionamiento:
EJEMPLO5
Descripción: Un programa con 3 interruptores y 3 LEDs y al activar cada uno de los botones su
respectivo LED se enciende

Código:

#include "p16f877a.inc"

; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa

START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,2
BSF TRISB,3
BSF TRISB,4
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7

CICLO
BTFSS PORTB,2
GOTO PRENDE_LED1
BTFSS PORTB,3
GOTO PRENDE_LED2
BTFSS PORTB,4
GOTO PRENDE_LED3
GOTO CICLO
PRENDE_LED3
BCF PORTB,6
BSF PORTB,7
BCF PORTB,5
GOTO CICLO
PRENDE_LED2
BCF PORTB,5
BCF PORTB,7
BSF PORTB,6
GOTO CICLO
PRENDE_LED1
BSF PORTB,5
BCF PORTB,7
BCF PORTB,6
GOTO CICLO
END
Imagen de funcionamiento:
EJEMPLO6
Descripción: Un programa que tiene 3 interruptores y 8 LEDs donde cada combinación enciende su
respectivo LED.

Código:

#include "p16f877a.inc"
; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa
START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,0
BSF TRISB,1
BSF TRISB,2
BCF TRISB,3
BCF TRISB,4
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF TRISC,5
BCF TRISC,6
BCF TRISC,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0
CICLO
BTFSS PORTB,0
GOTO P1
BTFSC PORTB,0
GOTO P0
GOTO CICLO
P1
BTFSS PORTB,1
GOTO R11
BTFSC PORTB,1
GOTO R01
GOTO CICLO
P0
BTFSS PORTB,1
GOTO R10
BTFSC PORTB,1
GOTO R00
GOTO CICLO
R11
BTFSS PORTB,2
GOTO PRENDE_LED8
BTFSC PORTB,2
GOTO PRENDE_LED4
GOTO CICLO
R01
BTFSS PORTB,2
GOTO PRENDE_LED6
BTFSC PORTB,2
GOTO PRENDE_LED2
GOTO CICLO
R10
BTFSS PORTB,2
GOTO PRENDE_LED7
BTFSC PORTB,2
GOTO PRENDE_LED3
GOTO CICLO
R00
BTFSS PORTB,2
GOTO PRENDE_LED5
BTFSC PORTB,2
GOTO PRENDE_LED1
GOTO CICLO

PRENDE_LED8
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,5
BCF PORTC,6
BSF PORTC,7
GOTO CICLO
PRENDE_LED7
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,5
BSF PORTC,6
BCF PORTC,7
GOTO CICLO
PRENDE_LED6
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BSF PORTC,5
BCF PORTC,6
BCF PORTC,7
GOTO CICLO
PRENDE_LED5
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BSF PORTB,7
BCF PORTC,5
BCF PORTC,6
BCF PORTC,7
GOTO CICLO
PRENDE_LED4
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BSF PORTB,6
BCF PORTB,7
BCF PORTC,5
BCF PORTC,6
BCF PORTC,7
GOTO CICLO
PRENDE_LED3
BCF PORTB,3
BCF PORTB,4
BSF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,5
BCF PORTC,6
BCF PORTC,7
GOTO CICLO
PRENDE_LED2
BCF PORTB,3
BSF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,5
BCF PORTC,6
BCF PORTC,7
GOTO CICLO
PRENDE_LED1
BSF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,5
BCF PORTC,6
BCF PORTC,7
GOTO CICLO
END

Imagen de funcionamiento:
EJEMPLO7
Descripción: Un programa de 3 interruptores y 6 LEDs donde las primeras 6 combinaciones de los
interruptores encienden su respectivo LED, la séptima combinación (110) enciende 3 bits
intercalados (L1,L3,L5) y la última combinación (111) enciende los otros 3 LEDs (L2,L4,L6).

Código:

#include "p16f877a.inc"

; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa

START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,0
BSF TRISB,1
BSF TRISB,2
BCF TRISB,3
BCF TRISB,4
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF TRISC,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0

CICLO
BTFSS PORTB,0
GOTO P1
BTFSC PORTB,0
GOTO P0
GOTO CICLO

P1
BTFSS PORTB,1
GOTO R11
BTFSC PORTB,1
GOTO R01
GOTO CICLO
P0
BTFSS PORTB,1
GOTO R10
BTFSC PORTB,1
GOTO R00
GOTO CICLO

R11
BTFSS PORTB,2
GOTO PRENDE_LED8
BTFSC PORTB,2
GOTO PRENDE_LED4
GOTO CICLO
R01
BTFSS PORTB,2
GOTO PRENDE_LED6
BTFSC PORTB,2
GOTO PRENDE_LED2
GOTO CICLO
R10
BTFSS PORTB,2
GOTO PRENDE_LED7
BTFSC PORTB,2
GOTO PRENDE_LED3
GOTO CICLO
R00
BTFSS PORTB,2
GOTO PRENDE_LED5
BTFSC PORTB,2
GOTO PRENDE_LED1
GOTO CICLO

PRENDE_LED8
BCF PORTB,3
BSF PORTB,4
BCF PORTB,5
BSF PORTB,6
BCF PORTB,7
BSF PORTC,7
GOTO CICLO
PRENDE_LED7
BSF PORTB,3
BCF PORTB,4
BSF PORTB,5
BCF PORTB,6
BSF PORTB,7
BCF PORTC,7
GOTO CICLO
PRENDE_LED6
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BSF PORTC,7
GOTO CICLO
PRENDE_LED5
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BSF PORTB,7
BCF PORTC,7
GOTO CICLO
PRENDE_LED4
BCF PORTB,3
BCF PORTB,4
BCF PORTB,5
BSF PORTB,6
BCF PORTB,7
BCF PORTC,7
GOTO CICLO
PRENDE_LED3
BCF PORTB,3
BCF PORTB,4
BSF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,7
GOTO CICLO
PRENDE_LED2
BCF PORTB,3
BSF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,7
GOTO CICLO
PRENDE_LED1
BSF PORTB,3
BCF PORTB,4
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
BCF PORTC,7
GOTO CICLO
END

Imagen de funcionamiento:
EJEMPLO8
Descripción: Programa con 2 botones y un LED, donde un botón lo apaga, y el otro botón lo
prende.

Código:

#include "p16f877a.inc"

; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa

START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,0
BSF TRISB,1
BCF TRISB,2
BCF STATUS,RP0 ;APUNTA AL BANCO 0

CICLO
BTFSS PORTB,0
GOTO VERIFICAR1
BTFSS PORTB,1
GOTO VERIFICAR2
GOTO CICLO

VERIFICAR1
BCF PORTB,2
GOTO CICLO

VERIFICAR2
BSF PORTB,2
GOTO CICLO
END

Imagen de funcionamiento:

EJEMPLO9
Descripción: Programa de 2 interruptores y 3 LEDs, donde cuando los botones son 00 todo
permanece apagado, en 10 se enciende el LED 3, en 01 se enciende el LED 1, y en 11 se encienden
los 3 LEDs.
Código:

#include "p16f877a.inc"

; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa

START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,0
BSF TRISB,1
BCF TRISB,5
BCF TRISB,6
BCF TRISB,7
BCF STATUS,RP0 ;APUNTA AL BANCO 0
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7

CICLO
BTFSS PORTB,0
GOTO P1
BTFSC PORTB,0
GOTO P2
GOTO CICLO
P2
BTFSS PORTB,1
GOTO PRENDE3
BTFSC PORTB,1
GOTO APAGAR
GOTO CICLO
P1
BTFSS PORTB,1
GOTO PRENDER
BTFSC PORTB,1
GOTO PRENDE1
GOTO CICLO

PRENDER
BSF PORTB,5
BSF PORTB,6
BSF PORTB,7
GOTO CICLO

APAGAR
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7
GOTO CICLO

PRENDE1
BSF PORTB,5
BCF PORTB,6
BCF PORTB,7
GOTO CICLO

PRENDE3
BCF PORTB,5
BCF PORTB,6
BSF PORTB,7
GOTO CICLO
END

Imagen de funcionamiento:
EJEMPLO10
Descripción: Programa de 2 interruptores con 2 LEDs, donde un LED se enciende siempre que uno
de los dos esté oprimido, y el otro LED se enciende únicamente cuando ambos botones están en la
misma posición, es decir (00) y (11).

Código:

#include "p16f877a.inc"

; CONFIG
; __config 0xFF71
__CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_ON & _BOREN_ON & _LVP_OFF & _CPD_OFF &
_WRT_OFF & _CP_OFF
RES_VECT CODE 0x0000 ; vector de reset
GOTO START ; ir al inicio del programa

START
BSF STATUS,RP0 ;APUNTA AL BANCO 1
BSF TRISB,0
BSF TRISB,1
BCF TRISB,2
BCF TRISB,3
BCF STATUS,RP0 ;APUNTA AL BANCO 0
BCF PORTB,5
BCF PORTB,6
BCF PORTB,7

CICLO
BTFSS PORTB,0
GOTO P1
BTFSC PORTB,0
GOTO P2
GOTO CICLO
P2
BTFSS PORTB,1
GOTO PRENDE1
BTFSC PORTB,1
GOTO APAGAR
GOTO CICLO
P1
BTFSS PORTB,1
GOTO PRENDER
BTFSC PORTB,1
GOTO PRENDE1
GOTO CICLO

PRENDER
BSF PORTB,2
BSF PORTB,3
GOTO CICLO

APAGAR
BCF PORTB,2
BSF PORTB,3
GOTO CICLO

PRENDE1
BSF PORTB,2
BCF PORTB,3
GOTO CICLO
END

Imagen de funcionamiento:

También podría gustarte