guÍa de ejercicios resueltos tema 3

30
Prof. Luis Zurita 1 Microcontroladores I GUÍA DE EJERCICIOS RESUELTOS TEMA 4: RUTINAS INTERMEDIAS, TABLAS Y ESTRUCTURA DE CONTADORES

Upload: luis-zurita

Post on 22-Jun-2015

5.156 views

Category:

Education


13 download

TRANSCRIPT

Page 1: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 1 Microcontroladores I

GUÍA DE EJERCICIOS RESUELTOS

TEMA 4:

RUTINAS INTERMEDIAS, TABLAS Y

ESTRUCTURA DE CONTADORES

Page 2: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 2 Microcontroladores I

1. Diseñe un contador de 4 cifras para una empresa enlatadora de

pescado. Diseño Libre.

SOLUCIÓN:

Paso 1. Enunciado y delimitación del Hardware: Ya disponemos del

enunciado, pero al ser diseño libre, como la mayoría de los problemas que nos

tocarán realizar, debemos asignarle los pines de entrada y salida de control del

microcontrolador.

Una sugerencia es la siguiente:

ENTRADA ¿Qué pin

Asignamos?

SALIDA ¿Qué pin

Asignamos?

Sensor Latas RA4 Salida Decodificar

7448

RB0 – RB3

Salida Multiplexación

Displays

RA0-RA3

¿Por qué un 7448 y no un 7447? ¿Por qué un cátodo común y no un ánodo

común?

Como es diseño libre, la elección depende del diseñador, en otros diseños

elegiremos el ánodo común, generalmente el consumo de corriente utilizando un

decodificador es casi el mismo para cualquiera de los dos modelos disponibles

de displays, más no así cuando no se utiliza un decodificador, y se controla

directamente con un puerto de un microcontrolador. Generalmente en los

sistemas digitales se prefiere trabajar como sumidero o vertidero y no como

fuente de corriente.

Page 3: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 3 Microcontroladores I

El hardware quedaría delimitado de la siguiente manera:

VDD

X1CRYSTAL

C1

22p

C2

22p

VSSR55

330R

R66

330RR14

330RR13

330RR15

330RR12

330R

R33

330R

A7

QA13

B1

QB12

C2

QC11

D6

QD10

BI/RBO4

QE9

RBI5

QF15

LT3

QG14

U2

74LS48

R41k

VSS

Q12N3904

Q2

2N3904

R31k

VSS

R251k

VSS

Q112N3904

Q22

2N3904

R311k

VSS

RA0RA1RA2RA3

RA0RA1RA2

RA3

RZ11k

VSS

VDD

SW1SW-SPDT

SW1(NC)

OSC1/CLKIN16

RB0/INT6

RB17

RB28

RB39

RB410

RB511

RB612

RB713

RA017

RA118

RA21

RA32

RA4/T0CKI3

OSC2/CLKOUT15

MCLR4

U1

PIC16F84A

RA4

RA4

Page 4: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 4 Microcontroladores I

Paso 2. Diagramas de Flujo:

INICIO

ConfigurarPuerto A y B

Declarar registros:UNI, DEC, CEN, UMIL

¿Se activóSensor?

UNI=UNI+1

¿Se desactivóSensor?

¿UNI=9?

NO

SI

SI

NO

Inicializar registros:UNI, DEC, CEN, UMIL

Puerto A y B

MOSTRAR

Delay 5 ms

UNI=0

DEC=DEC+1¿DEC=

9?

DEC=0

CEN=CEN+1¿CEN=

9?

CEN=0

UMIL=UMIL+1¿UMIL=

9?

UMIL=0

1

1

1

1

1

1

MOSTRAR

UNI → PORTB

Display UNI=ONResto Displays= OFF

Delay 5 ms

DEC → PORTB

Display DEC=ONResto Displays= OFF

Delay 5 ms

CEN → PORTB

Display CEN=ONResto Displays= OFF

Delay 5 ms

UMIL → PORTB

Display UMIL=ONResto Displays= OFF

Delay 5 ms

Todos los Displays= OFF

Return

Delay 5 ms

La sacamos del PDEL

Return

SI

NO

SI

NO

SI

NO

SI

NO

Page 5: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 5 Microcontroladores I

Nota: observe en el diagrama de flujo anterior, que las subrutinas que son

llamadas dentro del programa principal, deben ser expresadas como diagramas

de flujo adicionales para su entendimiento y posterior conversión al lenguaje

ensamblador.

Paso 3. Del Diagrama de Flujo al Lenguaje Ensamblador:

LIST P=16F84A ;Encabezado del Programa

INCLUDE P16F84A.INC

UNI EQU 21H ;Zona de declaraciones

DEC EQU 22H

CEN EQU 23H

UMIL EQU 24H

PDel0 EQU 25H

PDel1 EQU 26H

ORG 00H

GOTO INICIO

INICIO BSF STATUS,5 ;Banco 1

movlw B'00010000' ;Configuramos Puerto A y B

movwf TRISA

CLRF TRISB

BCF STATUS,5 ;Regresamos al Banco 0 a trabajar

INICIAR CLRF UNI ;Inicializamos los registros

CLRF DEC

CLRF CEN

CLRF UMIL

EXPLORA CALL DISPLAYS ;Llamamos Subrutina de Displays

BTFSC PORTA,4 ;Preguntamos si Sensor se activó

GOTO EXPLORA ;No, Seguimos esperando

AQUI CALL DEMORA ;Si, Introducimos Antirrebote

BTFSS PORTA,4

GOTO AQUI

MOVF UNI,0 ;Preguntamos si UNI=9?

Page 6: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 6 Microcontroladores I

SUBLW .9

BTFSS STATUS,2

GOTO SUBEUNI ;No, vamos a incrementar UNI

CLRF UNI ;Si, UNI=0

MOVF DEC,0 ;Preguntamos si DEC=9

SUBLW .9

BTFSS STATUS,2

GOTO SUBEDEC ;No, vamos a incrementar DEC

CLRF DEC ;Si, DEC=0

MOVF CEN,0 ; Y hacemos lo mismo con CEN

SUBLW .9 ;y Unidad de mil (UMIL)

BTFSS STATUS,2

GOTO SUBECEN

CLRF CEN

MOVF UMIL,0

SUBLW .9

BTFSS STATUS,2

GOTO SUBEUMIL

CLRF UMIL

GOTO EXPLORA

SUBEUNI INCF UNI,1 ;UNI= UNI+1

GOTO EXPLORA ;Vamos a ver si ha pasado otra lata

SUBEDEC INCF DEC,1 ;DEC= DEC+1

GOTO EXPLORA ;Vamos a ver si ha pasado otra lata

SUBECEN INCF CEN,1 ;CEN= CEN+1

GOTO EXPLORA ;Vamos a ver si ha pasado otra lata

SUBEUMIL INCF UMIL,1 ;UMIL= UMIL+1

GOTO EXPLORA ;Vamos a ver si ha pasado otra lata

DISPLAYS MOVF UNI,0 ;Subrutina para mostrar datos en

MOVWF PORTB ;Displays bajo la técnica de la

MOVLW B'11111110' ;Multiplexación

MOVWF PORTA

CALL DEMORA

movlw 0ffh

movwf PORTA

MOVF DEC,0

MOVWF PORTB

Page 7: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 7 Microcontroladores I

MOVLW B'11111101'

MOVWF PORTA

CALL DEMORA

movlw 0FFH

movwf PORTA

MOVF CEN,0

MOVWF PORTB

MOVLW B'11111011'

MOVWF PORTA

CALL DEMORA

movlw 0ffh

movwf PORTA

MOVF UMIL,0

MOVWF PORTB

MOVLW B'11110111'

MOVWF PORTA

CALL DEMORA

movlw 0ffh

movwf PORTA

CALL DEMORA

RETURN

;****************************************************

;*** Rutina de retardo de 5 ms, generada por el PICDEL***

;****************************************************

DEMORA movlw .248 ; 1 set numero de repeticion

movwf PDel0 ; 1 |

PLoop0 clrwdt ; 1 clear watchdog

decfsz PDel0, 1 ; 1 + (1) es el tiempo 0 ?

goto PLoop0 ; 2 no, loop

PDelL1 goto PDelL2 ; 2 ciclos delay

PDelL2 clrwdt ; 1 ciclo delay

return ; 2+2 Fin.

END ;Fin del programa.

Page 8: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 8 Microcontroladores I

2. Diseñe un contador de dos cifras ascendente, sin utilizar un

decodificador. Diseño libre.

SOLUCIÓN:

Paso 1. Enunciado y delimitación del Hardware: Ya disponemos del

enunciado, pero al ser diseño libre, debemos asignarle los pines de entrada y

salida de control del microcontrolador.

Una sugerencia es la siguiente:

ENTRADA ¿Qué pin

Asignamos?

SALIDA ¿Qué pin

Asignamos?

Pulsador RA4 Salidas a los segmentos

del display

RB0 – RB7

Salida Multiplexación

Displays

RA0 (Unidad)

RA1 (Decena)

El hardware quedaría delimitado de la siguiente manera:

OSC1/CLKIN16

RB0/INT6

RB17

RB28

RB39

RB410

RB511

RB612

RB713

RA017

RA118

RA21

RA32

RA4/T0CKI3

OSC2/CLKOUT15

MCLR4

U?

PIC16F84A

Q12N3904

Q?2N3904

R2

1k

R3

1k

R11k

X?CRYSTAL

C1

22p

C?

22pVSS

Page 9: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 9 Microcontroladores I

Paso 2. Diagramas de Flujo:

INICIO

ConfigurarPuerto A y B

Declarar registros:UNI, DEC

¿Se activóPulsador?

UNI=UNI+1

¿Se desactivóPulsador?

¿UNI=9?

NO

SI

SI

NO

Inicializar registros:UNI, DEC

Puerto A y B

MOSTRAR

Delay 5 ms

UNI=0

DEC=DEC+1¿DEC=

9?

DEC=0

1

1

1

1

MOSTRAR

UNI → W

W → PORTB

Display UNI= ONDisplay DEC= OFF

Delay 5 ms

Todos los Displays= OFF

Return

Delay 5 ms

La sacamos del PDEL

Return

TABLA

DEC → W

W → PORTB

Display UNI= OFFDisplay DEC= ON

Delay 5 ms

TABLA

TABLA

W + PCL → PCL

Return

Retornamos con el valoren W extraido de la tabla

NO

SI

NO

SI

Page 10: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 10 Microcontroladores I

Paso 3. Lenguaje ensamblador:

LIST P=16F84A

INCLUDE P16F84A.INC

ORG 00H

GOTO INICIO

UNI EQU 20H ;Registro con el valor de la UNIDAD

DEC EQU 21H ;Registro con el valor de la DECENA

PDel0 EQU 24H ;Registro para retardo

PDel1 EQU 25H ;Registro para retardo

PDel2 EQU 26H ;Registro para retardo

PDel3 EQU 27H ;Registro para retardo

PDelX5 EQU 2AH ;Registro para retardo

CONTA equ 2bh ;Contador para decodificación con tabla

INICIO BSF STATUS,5

CLRF TRISB ; Se configura al Puerto B como salida

; para manejar los displays

BCF TRISA,0 ; Selector de display de UNIDAD

BCF TRISA,1 ; Selector de display de DECENA

BSF TRISA,4 ; Pulsador de incremento de cuenta

BCF STATUS,5 ;Banco 0.

CLRF UNI ;Inicializamos registros y puerto B

CLRF DEC

clrf PORTB

SUBE CALL MOSTRAR

BTFSC PORTA,4 ; Se ha pulsado INCREMENTO?

GOTO SUBE ;No. Seguimos explorando

AUMENTA CALL RET5mS ; Retardo de 5 ms, elimina rebotes.

BTFSS PORTA,4 ;Esperamos a que se suelte Pulsador

GOTO AUMENTA

MOVLW 09H ;Preguntamos si UNI=9

SUBWF UNI,0

BTFSC STATUS,0

GOTO SUBEDEC ;Si. Vamos a preguntar por la Decena

INCF UNI,1

CALL MOSTRAR ;Muestra datos en Displays

GOTO SUBE

Page 11: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 11 Microcontroladores I

SUBEDEC CLRF UNI

MOVLW 09H ;Preguntamos si Decena=9

SUBWF DEC,0

BTFSC STATUS,0

GOTO RONDA ;Si, vamos a RONDA

INCF DEC,1 ;No. Incrementamos Decena

CALL MOSTRAR ;Muestra datos en Displays

GOTO SUBE ;Vamos a preguntar por Pulsador

RONDA CLRF DEC

CALL MOSTRAR

GOTO SUBE

;************************

;***RUTINA MOSTRAR***

;************************

MOSTRAR BCF PORTA,1

BSF PORTA,0 ; Se selecciona el display de UNIDAD

MOVF UNI,0 ; Y se muestra el dato

CALL TABLA ;Vamos a Tabla para extraer dato

;Equivalente al registro UNIDAD

MOVWF PORTB

CALL RET1mS ;Se espera 1 ms para la

;multiplexación de display

BCF PORTA,0

MOVLW b'11111111'

MOVWF PORTB ;Limpiamos Puerto B

BSF PORTA,1 ; Se selecciona el display de DECENA

MOVF DEC,0 ; Y se muestra el dato

CALL TABLA

MOVWF PORTB

CALL RET1mS ; se espera 1 ms para la multiplexación

BCF PORTA,1

MOVLW B'11111111'

MOVWF PORTB

return

Page 12: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 12 Microcontroladores I

;****************************************************

;****TABLA DE CONVERSIÓN BCD A 7 SEGMENTOS****

;****************************************************

; Esta tabla se encarga de convertir los datos en BCD en formato de 7

;segmentos para visualizarce en los displays. Compare con el ejercicio 2 de la

;guía de ejercicios resueltos del tema 3.

TABLA addwf PCL,F

retlw b'11000000' ;0

retlw b'11111001' ;1

retlw b'10100100' ;2

retlw b'10110000' ;3

retlw b'10011001' ;4

retlw b'10010010' ;5

retlw b'10000010' ;6

retlw b'11111000' ;7

retlw b'10000000' ;8

retlw b'10011000' ;9

;***********************

;***RUTINA 0,5 SEG****

;***********************

; Esta rutina permite que entre la activacion de la señal sonora y su

; desactivacion exista 0,5 segundos de intervalo. Está generada por el

; PDEL

;***********************

RET05S clrf conta

ret5 movlw .4 ; 1 set numero de repeticion (B)

movwf PDel0 ; 1 |

PLoop1 movlw .186 ; 1 set numero de repeticion (A)

movwf PDel1 ; 1 |

PLoop2 clrwdt ; 1 clear watchdog

decfsz PDel1, 1 ; 1 + (1) es el tiempo 0 ? (A)

goto PLoop2 ; 2 no, loop

decfsz PDel0, 1 ; 1 + (1) es el tiempo 0 ? (B)

goto PLoop1 ; 2 no, loop

PDelL1 goto PDelL2 ; 2 ciclos delay

PDelL2 clrwdt ; 1 ciclo delay

Page 13: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 13 Microcontroladores I

;*******************************************

;***Rutina para eliminar rebotes por software***

;***RETARDO 5 mS**************************

;***Extraida del PDEL************************

RET5mS movlw .6 ; 1 set numero de repeticion (B)

movwf PDel2 ; 1 |

PLoop3 movlw .207 ; 1 set numero de repeticion (A)

movwf PDel3 ; 1 |

PLoop4 clrwdt ; 1 clear watchdog

decfsz PDel3, 1 ; 1 + (1) es el tiempo 0 ? (A)

goto PLoop4 ; 2 no, loop

decfsz PDel2, 1 ; 1 + (1) es el tiempo 0 ? (B)

goto PLoop3 ; 2 no, loop

PDelL8 goto PDelL9 ; 2 ciclos delay

PDelL9 clrwdt ; 1 ciclo delay

return ; 2+2 Fin.

;*********************************************************

;***Rutina de 1 ms utilizado en la multiplexacion de los displays***

;*********************************************************

;Nota: Esta rutina es opcional, se puede utilizar perfectamente la misma de

;los 5 ms para la multiplexación de los displays.

RET1mS MOVLW D'248' ; 1 set numero de repeticion

MOVWF PDelX5 ; 1 |

PLoopX CLRWDT ; 1 clear watchdog

DECFSZ PDelX5,1 ; 1 + (1) es el tiempo 0 ?

GOTO PLoopX ; 2 no, loop

PDelLX1 GOTO PDelLX2 ; 2 ciclos delay

PDelLX2 CLRWDT ; 1 ciclo delay

RETURN ; 2+2 Fin.

end

Page 14: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 14 Microcontroladores I

3. Diseñe un semáforo de 4 esquinas. Utilice tabla. Diseño libre.

SOLUCIÓN:

Paso 1. Enunciado y delimitación del Hardware: Ya disponemos del

enunciado, pero al ser diseño libre, debemos asignarle los pines de entrada

y salida de control del microcontrolador.

Una sugerencia es la siguiente:

ENTRADA ¿Qué pin

Asignamos?

SALIDA ¿Qué pin

Asignamos?

Pulsador

INICIO

RA4 Salidas a los semáforos

1 2 y 3

RB0 – RB7

Salida a los semáforos

3 y 4

RA0 –RA3

El hardware quedaría delimitado de la siguiente forma:

OSC1/CLKIN16

RB0/INT6

RB17

RB28

RB39

RB410

RB511

RB612

RB713

RA017

RA118

RA21

RA32

RA4/T0CKI3

OSC2/CLKOUT15

MCLR4

U?

PIC16F84A

VDD

INICIO

INICIO

RA0

R?1k

RA0

RA1RA1RA2

RA2RA3

RA3RB0RB1RB2

RB0

RB1

RB2

RB3RB4RB5

RB3

RB4

RB5

RB6

RB7

RB6RB7

X?CRYSTAL

C1

22p

C2

22p

VSS

SEMÁFORO

CASCAJAL

S1

S4S3

S2

Page 15: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 15 Microcontroladores I

Paso 2. Diagramas de Flujo:

Page 16: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 16 Microcontroladores I

Nota: El PDEL permite generar rutinas de tiempo muy grandes, sin embargo,

como buena práctica podemos tener rutinas de tiempo variables en base a un

registro, como lo observamos en las subrutinas Delay 30 seg y Delay5 seg

Page 17: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 17 Microcontroladores I

Paso 3. Lenguaje ensamblador:

LIST P=16F84A ;Encabezado del programa

INCLUDE P16F84A.INC

CONTA EQU 20H ;Zona de declaraciones

CONTA2 EQU 21H

PDel0 EQU 22H

PDel1 EQU 23H

PDel2 EQU 24H

ORG 00H

GOTO INICIO

INICIO BSF STATUS,5 ;Banco 1 para configurar puertos

CLRF TRISB ;Se configura puerto A y B

MOVLW B'10000'

MOVWF TRISA

BCF STATUS,5 ;Banco 0 para trabajar con los puertos

CLRF CONTA ;Inicializamos los registros y puertos

CLRF CONTA2

CLRF PORTA

CLRF PORTB

EMPIEZA BTFSC PORTA,4 ;Preguntamos si se presionó “INICIO”

GOTO EMPIEZA ;No. Esperamos

NCICLO CALL MOSTRAR ;Llamamos rutina “MOSTRAR”

CALL DELAY30S ;Esperamos 30 segundos

INCF CONTA,1 ;Incrementamos contador de estados

CALL MOSTRAR

CALL DELAY5S ;Esperamos 5 segundos

MOVLW 07H

SUBWF CONTA,0 ;Si contador=7 reinicia ciclo

BTFSS STATUS,2

GOTO SUBE

CLRF CONTA

GOTO NCICLO

SUBE INCF CONTA,1

GOTO NCICLO

Page 18: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 18 Microcontroladores I

MOSTRAR MOVF CONTA,0 ;Se mueve Contador a W

CALL TABLA1 ;Se extrae de la TABLA 1 el valor

MOVWF PORTB ;a mostrar por el Puerto B

MOVF CONTA,0 ;Luego se extraerá de la TABLA2

CALL TABLA2 ;El valor a mostrar por el Puerto A

MOVWF PORTA

RETURN

;***********************************

;***Rutina de retardo de 30 segundos***

;***********************************

DELAY30S CLRF CONTA2 ;CONTA2, va a contar hasta 30, que

OTROCALL RET1S ; y en cada cuenta se espera 1 segundo

MOVLW D'29' ;Lo que nos dará la rutina de 30 segundos

SUBWF CONTA2,0

BTFSS STATUS,2

GOTO PREVIO

RETURN

PREVIO INCF CONTA2,1

GOTO OTRO

;***********************************

;***Rutina de retardo de 5 segundos***

;***********************************

DELAY5S CLRF CONTA2 ;CONTA2, va a contar hasta 5, que

OTRO2 CALL RET1S ; y en cada cuenta se espera 1 segundo

MOVLW D'4' ;Lo que nos dará la rutina de 5 segundos

SUBWF CONTA2,0

BTFSS STATUS,2

GOTO PREVIO2

RETURN

PREVIO2 INCF CONTA2,1

GOTO OTRO2

Page 19: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 19 Microcontroladores I

;***Rutina de retardo de 1 segundos extraida del PDEL ***

RET1S movlw .14 ; 1 set numero de repeticion (C)

movwf PDel0 ; 1 |

PLoop0 movlw .72 ; 1 set numero de repeticion (B)

movwf PDel1 ; 1 |

PLoop1 movlw .247 ; 1 set numero de repeticion (A)

movwf PDel2 ; 1 |

PLoop2 clrwdt ; 1 clear watchdog

decfsz PDel2, 1 ; 1 + (1) es el tiempo 0 ? (A)

goto PLoop2 ; 2 no, loop

decfsz PDel1, 1 ; 1 + (1) es el tiempo 0 ? (B)

goto PLoop1 ; 2 no, loop

decfsz PDel0, 1 ; 1 + (1) es el tiempo 0 ? (C)

goto PLoop0 ; 2 no, loop

PDelL1 goto PDelL2 ; 2 ciclos delay

PDelL2 clrwdt ; 1 ciclo delay

return ; 2+2 Fin.

;***La tabla 1 contiene el valor que se va a mostrar por el Puerto B***

TABLA1 ADDWF PCL,1

RETLW 4CH

RETLW 4AH

RETLW 61H

RETLW 51H

RETLW 09H

RETLW 89H

RETLW 49H

RETLW 49H

;***La tabla 2 contiene el valor que se va a mostrar por el Puerto A***

TABLA2 ADDWF PCL,1

RETLW 02H

RETLW 02H

RETLW 02H

RETLW 02H

RETLW 03H

RETLW 02H

RETLW 08H

RETLW 04H

END

Page 20: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 20 Microcontroladores I

4. Se desea diseñar un sistema de protección para una línea de

ensamblaje que contiene 4 máquinas soldadoras.

M1 (RA0) M2 (RA1) M3 (RA2) M4 (RA3)

Máquina activa= 1 Máquina inactiva= 0

* Cada máquina tiene dos leds que indican si están funcionando:

(Led Verde=ON) o si están apagadas (Led Rojo=ON).

* Si ninguna máquina está activa, debe activarse adicionalmente una señal

sonora (RA4).

Nota: Vamos a realizar el mismo problema que hicimos en la guía de ejercicio

anterior, pero ahora con el uso de una tabla, veamos si el problema se

simplifica o no.

Page 21: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 21 Microcontroladores I

SOLUCIÓN:

Paso 1. Enunciado y delimitación del Hardware: si el enunciado y el hardware

ya se nos ha proporcionado, saltaremos este paso.

Paso 2. Diagramas de Flujo:

INICIO

ConfigurarPuerto A y B

SI

¿W=B'01011010'?

ALARMA= ON

1

TABLA

W + PCL → PCL

Return

Retornamos con el valoren W extraido de la tabla

correspondiente a lossensores de Puerto A

1

ALARMA= OFF

1

NO

PuertoA → W

TABLA

W → PuertoB

¿Todas lasmáquinas están

apagadas?

Page 22: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 22 Microcontroladores I

Paso 3. Lenguaje Ensamblador:

list P=16F84A ;Encabezado del programa

include P16F84A.inc

org 00H

goto INICIO

INICIO bsf STATUS,5 ;Configuramos Puerto A y B

movlw 0FH

movwf TRISA

clrf TRISB

bcf STATUS,5

clrf PORTB

bcf PORTA,4

CICLO movf PORTA,0 ;Leemos los sensores

andlw 0FH ;Aplicamos enmascaramiento

call TABLA ;Llamamos la tabla

movw PORTB ;Sacamos el dato hacia los indicadores

sublw 5AH ;Preguntamos si todas las máquinas=OFF

btfss STATUS,2

goto NOALARMA ;No. Mantenemos Alarma=OFF

ALARMA bsf PORTA,4 ;Si. Alarma=ON

goto CICLO ;Vamos a explorar los sensores de nuevo

NOALARMA bcf PORTA,4

goto CICLO

TABLA addwf PCL,1

DT 5AH, 59H, 56H, 55H, 6AH, 69H, 66H, 65H, 9AH,

99H, 96H, 95H, 0AAH, 0A9H, 0A6H, 0A5H

;DT es una directiva para declarar tablas y tiene el mismo efecto que usar el

retlw K:

; retlw 5AH ;Nota: estos son los valores que corresponden

; retlw 59H ;a la combinación de sensores y leds de salida

; retlw 56H

; retlw 55H

; retlw 6AH

; retlw 69H

; retlw 66H

Page 23: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 23 Microcontroladores I

; retlw 65H

; retlw 9AH

; retlw 99H

; retlw 96H

; retlw 95H

; retlw 0AAH

; retlw 0A9H

; retlw 0A6H

; retlw 0A5H

END

Nota: Este programa posee 24 líneas si utilizamos la directiva DT y 40 si

utilizamos las instrucciones retlw k, mientras que el realizado en la guía de

ejercicios anterior posee 67 líneas, lo que demuestra que se simplifica en

código y lógica de trabajo.

El uso de las tablas tiene aplicaciones muy importantes en los

microcontroladores.

El circuito de las máquinas en el Proteus quedaría así:

OSC1/CLKIN16

RB0/INT6

RB17

RB28

RB39

RB410

RB511

RB612

RB713

RA017

RA118

RA21

RA32

RA4/T0CKI3

OSC2/CLKOUT15

MCLR4

U1

PIC16F84A

C1

22p

C2

22p

X?CRYSTAL

R41k

VDD

VSS

VSS

R51k

VDD

VSS

R61k

VDD

VSS

R11k

VDD

VSS

VDD

MÁQUINA 1 MÁQUINA 2 MÁQUINA 3 MÁQUINA 4

E1

E1

E2 S1 S2

E2S1S2

DA

LED-GREENDB

LED-REDDC

LED-GREENDD

LED-REDDE

LED-GREENDF

LED-REDDG

LED-GREENDV

LED-RED

RA

330RRB

330R

RC

330RRD

330R

RE

330RRF

330R

RG

330RRV

330R

VSS

RA4

D2LED-RED

RA4

SW1

SW-SPST

SW2

SW-SPST

SW3

SW-SPST

SW4

SW-SPST

RZ100

VDD

RZ1

100R

MÁQUINA 1

MÁQUINA 2

MÁQUINA 3

MÁQUINA 4

Page 24: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita

5. Diseñe un circuito de control para un estacionamiento del Centro

Comercial IUT PLAZA cuya capacidad es de 50 vehículos. Diseño

libre.

Consta de dos sensores de entrada (E1 y E2)

Consta de dos sensores de salida (S1 y S2)

Consta de un led que indica si hay puesto (HP) y uno que indica si no hay

puesto (NHP)

Consta de dos Displays para visualizar el número de vehículos dentro del

estacionamiento.

El programa debe cumplir con las siguientes condiciones:

Se realizará un monitoreo constante sobre los sensores de entrada y

sobre los sensores de salida.

Si se llega a la cifra de 50 vehículos, se enciende el led de “NO HAY

PUESTO” y se coloca un cerrojo a la espera de que salga un vehículo.

ESTACIONAMIENTO

IUT PLAZA

CASILLA

(μC PIC)Hay Puesto

No Hay Puesto

E2 S2

24

E1

S1

Microcontroladores I

Page 25: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 25 Microcontroladores I

SOLUCIÓN:

Paso 1. Enunciado y delimitación del Hardware: Ya disponemos del

enunciado, pero debemos asignarle los pines de entrada y salida de control del

microcontrolador.

Una sugerencia es la siguiente:

ENTRADA ¿Qué pin

Asignamos?

SALIDA ¿Qué pin

Asignamos?

Sensor

Entrada 1

RB4 Salida Decodificar

7448

RA0 – RA3

Sensor

Entrada 2

RB5 Salida Multiplexación

Displays

RB0 y RB1

Sensor

Salida 1

RB6 Led Hay Cupo RB2

Sensor

Salida 2

RB7 Led No hay Cupo RB3

El hardware quedaría delimitado de la siguiente forma:

R21k

OSC1/CLKIN16

RB0/INT6

RB17

RB28

RB39

RB410

RB511

RB612

RB713

RA017

RA118

RA21

RA32

RA4/T0CKI3

OSC2/CLKOUT15

MCLR4

U1

PIC16F84A

C1

22p

C2

22p

X?CRYSTAL

R41k

VDD

VSS

VSS

VSS

A7

QA13

B1

QB12

C2

QC11

D6

QD10

BI/RBO4

QE9

RBI5

QF15

LT3

QG14

U2

74LS48

Q12N3904 Q2

2N3904

R31k

VSS

R51k

VDD

VSS

R61k

VDD

VSS

R11k

VDD

VSS

VDD

ENTRADA 1 ENTRADA 2 SALIDA 1 SALIDA 2

R22

330R

R33

330R

D1LED-GREEN

D2LED-RED

HAY CUPO

NO HAY CUPO

RBORB1RB2RB3

RB2

RB3

RB1RBO

E1

E1

E2 S1 S2

E2S1S2

Page 26: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 26 Microcontroladores I

Paso 2. Diagramas de Flujo:

INICIO

ConfigurarPuerto A y B

Declarar registros:UNI, DEC, PDel0, PDel1

¿SensorEnt1=0?

UNI=UNI+1 ¿UNI=9?

NO

SI SI

NO

Inicializar registros:UNI, DEC

Puerto A y B

MOSTRAR

Delay 5 ms

UNI=0

DEC=DEC+1¿DEC=

4?

DEC=51

1

1

NO

SI

NO

SI

Led HC=ONLed NHC=OFF

¿SensorEnt2=0?

¿SensorEnt1=1?

MOSTRAR

¿SensorSal1=0?

¿SensorSal2=0?

Led HC=OFFLed NHC=ON

A3 A4

Delay 5 ms

¿SensorEnt2=1?

SI

NO

SI

¿SensorSal1=0?

UNI=UNI-1¿UNI=

0?

NO

SI SI

NO

Delay 5 ms

UNI=9

DEC=DEC-1¿DEC=

0?

1

NO

SI

NO

SI

¿SensorEnt2=0?

¿SensorSal=1?

Delay 5 ms

¿SensorEnt2=1?

SI

NO

SI

Led HC=OFFLed NHC=ON

1Led HC=OFFLed NHC=ON UNI=0

DEC=0

1

1

NO

NO

NO

NO

SI SI

A3 A4

Leyenda:Sensor Ent1 y Ent2: Sensores de entradas 1 y 2Sensor Sal1 y Sal2= Sensores de salida 1 y 2

Led HC= Led Hay CupoLed NHC= Led No Hay CupoLógica de los sensores=

1= Desactivado0=Activado

Page 27: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 27 Microcontroladores I

Paso 3. Lenguaje Ensamblador:

List P=16F84A ;Encabezado del Programa

include P16F84A.inc

UNI equ 20H ;Zona de declaraciones

DEC equ 21H ;UNI y DEC se mostrarán en los Displays

PDel0 equ 22H ;PDel0 y PDel1 generarán el retardo

PDel1 equ 23H ;de 5 ms

org 00H

goto INICIO

INICIO bsf STATUS,5 ;Banco 1 para configurar Puerto A y B

CLRF TRISA

MOSTRAR

UNI → PORTA

Display UNI= ONDisplay DEC= OFF

Delay 5 ms

Return

Delay 5 ms

La sacamos del PDEL

Return

DEC → PORTA

Display UNI= OFFDisplay DEC= ON

Delay 5 ms

Display UNI= OFFDisplay DEC= OFF

Page 28: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 28 Microcontroladores I

MOVLW B'11110000'

MOVWF TRISB

bcf STATUS,5 ;Banco 0 para trabajar con los puertos

EXP clrf UNI ;Inicializamos los registros

clrf DEC ; y los leds

bcf PORTB,3 ;LED NHC=OFF

bsf PORTB,2 ;LED HC=ON

ENT1 call MOSTRAR ;Mostramos nro de carros en los displays

ENT11 btfsc PORTB,4 ;ENTRADA 1?

goto ENT2 ;No. Vamos a explorar Entrada 2

AQUI1 call DELAY5ms ;Si. Colocamos antirrebote

btfss PORTB,4 ;PASÓ EL CARRO?

goto AQUI1 ;No. Esperamos

SUBE MOVF UNI,0 ;Si. Vamos a incrementar nro de carros

SUBLW .9

BTFSS STATUS,2 ;UNI=9?

GOTO SUBEU ;INCREMENTA UNIDAD

CLRF UNI

MOVF DEC,0

SUBLW .4 ;DEC=5?

BTFSS STATUS,2

GOTO SUBED ;INCREMENTA DECENA

MOVLW .5 ;Si hemos llegado a 50 vehículos

MOVWF DEC ;Lo meteremos en un cerrojo

bSf PORTB,3 ;LED NHC, indicando que no hay cupo

bCf PORTB,2 ;LED HC

CERROJO CALL MOSTRAR ;Mostramos nro de carros en los displays

BTFSC PORTB,6 ;SALIO UN VEHICULO? En sensor SAL1

GOTO CERR2 ;No. Preguntamos por sensor SAL2

GOTO AQUI3 ;SI, A DECREMENTAR en SAL1

CERR2 CALL MOSTRAR ;Mostramos nro de carros en los displays

BTFSC PORTB,7 ;SALIO UN VEHICULO?

GOTO CERROJO ;Vamos a preguntar si salió un carro.

GOTO AQUI4 ;SI, A DECREMENTAR en SAL2

SUBEU INCF UNI,1 ;Incrementamos UNIDAD

GOTO ENT1 ;Vamos a monitorear las entradas

Page 29: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 29 Microcontroladores I

SUBED INCF DEC,1 ;Incrementamos la DECENA

GOTO ENT1 ;Vamos a monitorear las entradas

ENT2 btfsc PORTB,5 ;ENTRADA 2?

goto SAL1 ;A CHEQUEAR SALIDA 1

AQUI2 call DELAY5ms ;Introducimos antirebote

btfss PORTB,5 ;Pasó el carro?

goto AQUI2 ;Esperamos

GOTO SUBE ;Vamos a incrementar el nro de carros

SAL1 BTFSC PORTB,6 ;SALIDA 1

GOTO SAL2 ;Vamos a chequear salida 2

AQUI3 CALL DELAY5ms

BTFSS PORTB,6 ;Salió el carro?

GOTO AQUI3 ;Esperamos

BAJA MOVF UNI,0 ;Vamos a decrementar el nro de carros

SUBLW 00H ;UNI=0?

BTFSS STATUS,2

GOTO BAJAU ;A DECREMENTAR UNIDAD

MOVLW .9

MOVWF UNI

MOVF DEC,0

SUBLW 00H

BTFSS STATUS,2

GOTO BAJAD ;A DECREMENTAR DECENA

CLRF UNI ;Si el nro de carros es cero

CLRF DEC ;Mantendremos ese valor.

GOTO ENT1

BAJAU DECF UNI,1

bcf PORTB,3 ;LED NHC=OFF

bsf PORTB,2 ;LED HC=ON

GOTO ENT1

BAJAD DECF DEC,1

bcf PORTB,3 ;LED NHC=OFF

bsf PORTB,2 ;LED HC=ON

GOTO ENT1

SAL2 BTFSC PORTB,7 ;SALIDA 2

GOTO ENT1 ;A chequear entrada 1 nuevamente

AQUI4 CALL DELAY5ms

BTFSS PORTB,7 ;Salió el carro?

Page 30: GUÍA DE EJERCICIOS RESUELTOS TEMA 3

Prof. Luis Zurita 30 Microcontroladores I

GOTO AQUI4 ;Esperamos

GOTO BAJA ;Vamos a decrementar el nro de carros

;***********************************************************

;***Subrutina para Mostrar UNIDAD y DECENA en los Displays***

;***********************************************************

MOSTRAR MOVF UNI,0

MOVWF PORTA

BCF PORTB,0

CALL DELAY5ms

BSF PORTB,0

BSF PORTB,1

MOVF DEC,0

MOVWF PORTA

BCF PORTB,1

CALL DELAY5ms

BSF PORTB,0

BSF PORTB,1

CALL DELAY5ms

return

;******************************************************

;***Subrutina de retardo de 5 mseg, generada por el PDEL***

;******************************************************

DELAY5ms movlw .6 ; 1 set numero de repeticion (B)

movwf PDel0 ; 1 |

PLoop1 movlw .207 ; 1 set numero de repeticion (A)

movwf PDel1 ; 1 |

PLoop2 clrwdt ; 1 clear watchdog

decfsz PDel1, 1 ; 1 + (1) es el tiempo 0 ? (A)

goto PLoop2 ; 2 no, loop

decfsz PDel0, 1 ; 1 + (1) es el tiempo 0 ? (B)

goto PLoop1 ; 2 no, loop

PDelL1 goto PDelL2 ; 2 ciclos delay

PDelL2 clrwdt ; 1 ciclo delay

return ; 2+2 Fin.

end ;Fin del programa