laboratorio no 4
TRANSCRIPT
![Page 1: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/1.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 1/14
MICROCONTOLADORESINFORME DE PRÁCTICA 04
Uso del keypad y LCD
En mikrobasic pro
I. TRABAJO EN LABORATORIO: Para cada uno de los incisos realizar:
Proyecto MikroBasic
Código fuente (impreso)
Diagrama de flujo (impreso)
Simulación en ISIS-Proteus (confirmación con firma)
Grabado del microcontrolador
Armado y funcionamiento en PROTOBOARD (confirmación con firma)
a) Candado electrónico: se ingresa un código de apertura del candado de 3 dígitos. Si el código
es correcto el LCD debe mostrar el mensaje “ABIERTO”, caso contrario el mensaje será
“CERRADO”.
Código fuente: program CANDADO
symbol Alerta=PortB.0dim Tecla, i, j, valor as bytedim TeclaT as string[3]dim vec_dato, nvec_dato as string[3]
' KeyPad module
dim keypadPort as byte at PORTD' Lcd module connections
dim LCD_RS as sbit at RC4_bitLCD_EN as sbit at RC5_bitLCD_D4 as sbit at RC0_bitLCD_D5 as sbit at RC1_bitLCD_D6 as sbit at RC2_bitLCD_D7 as sbit at RC3_bitLCD_RS_Direction as sbit at TRISC4_bitLCD_EN_Direction as sbit at TRISC5_bitLCD_D4_Direction as sbit at TRISC0_bit
LCD_D5_Direction as sbit at TRISC1_bitLCD_D6_Direction as sbit at TRISC2_bitLCD_D7_Direction as sbit at TRISC3_bit
' End Lcd module connections
main:ANSEL = 0ANSELH = 0C1ON_bit = C2ON_bit = 0portb=$00TrisB=%11111110
![Page 2: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/2.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 2/14
Keypad_Init()Lcd_Init()Lcd_Cmd(_LCD_CLEAR)Lcd_Cmd(_LCD_CURSOR_OFF)
Lcd_Out(1, 1, "CANDADO ELECTRONICO")delay_ms(500)
Lcd_Cmd(_LCD_Clear)Lcd_Out(1, 1, " INTRODUZCA")delay_ms(500)Lcd_Out(1, 1, "CODIGO SECRETO:")Lcd_Cmd(_LCD_second_row)
i=0while (i<3)
Tecla = 0while Tecla=0Tecla = Keypad_Key_Click()
wendvec_dato[i]=Tecla
BytetoStr(Tecla,TeclaT)Lcd_Out_cp(TeclaT)i=i+1
wenddelay_ms(1000)Lcd_Out(1, 1, "CODIGO GUARDADO....")Lcd_Out(2, 1, "**********************")delay_ms(1000)
while (1)Lcd_Cmd(_LCD_CLEAR)Lcd_Out(1,1,"Introduzca clave:")Lcd_Out(2,1," ")
i=0while(i<3)
Tecla = 0while Tecla=0Tecla = Keypad_Key_Click()
wendnvec_dato[i]=TeclaBytetoStr(Tecla,TeclaT)Lcd_Out_cp(TeclaT)i=i+1
wenddelay_ms(1000)
valor=0for j=0 to 2if(vec_dato[j]=nvec_dato[j]) then
valor= valor +1end if
next j
if (valor=3) thenLcd_Out(1,1,"**********************")Lcd_Out(2,1," ABIERTO =) ")Delay_ms(1000)
![Page 3: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/3.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 3/14
Alerta=1Delay_ms(3000)Alerta=0
elseLcd_Out(2,1," CERRADO =(")Delay_ms(2000)
end if
wendend.
Diagrama de flujo:
b) Calculadora básica: se ingresa dos números de un digito (del 0 al 9), posteriormente ingresa
una operación a realizar: +, -, *, /, finalmente el LCD muestra el resultado de la operación.
Código fuente:program CALCULADORA
dim Tecla, i, dato1, dato2, res as bytedim TeclaT, ress as string[3]
' KeyPad moduledim keypadPort as byte at PORTD' Lcd module connections
dim LCD_RS as sbit at RC4_bitLCD_EN as sbit at RC5_bitLCD_D4 as sbit at RC0_bitLCD_D5 as sbit at RC1_bitLCD_D6 as sbit at RC2_bitLCD_D7 as sbit at RC3_bitLCD_RS_Direction as sbit at TRISC4_bitLCD_EN_Direction as sbit at TRISC5_bit
CANDADO
Configuración de puertos
Guardar la clave secreta
Mostrar en el LCD
ABIERTO
Comparar si el código
in resado es el correcto
CERRADO
![Page 4: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/4.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 4/14
LCD_D4_Direction as sbit at TRISC0_bitLCD_D5_Direction as sbit at TRISC1_bitLCD_D6_Direction as sbit at TRISC2_bitLCD_D7_Direction as sbit at TRISC3_bit
' End Lcd module connections
main:ANSEL = 0
ANSELH = 0C1ON_bit = C2ON_bit = 0Keypad_Init()Lcd_Init()Lcd_Cmd(_LCD_CLEAR)Lcd_Cmd(_LCD_CURSOR_OFF)
while (1)Lcd_Out(1, 1, " CALCULADORA BASICA ")Lcd_Out(2, 1, " Introducir datos: ")delay_ms(1000)Lcd_Cmd(_LCD_Clear)
DATO1:Lcd_Out(1, 3, "DATO 1:")delay_ms(500)i=0Tecla = 0
while Tecla=0Tecla = Keypad_Key_Click()
wendif Tecla <= 9 then
if i<1 thendato1=Teclai=1BytetoStr(Tecla,TeclaT)Lcd_Out(1,11,TeclaT)
delay_ms(1000)elsegoto DATO2 end if
elsegoto DATO1end if
DATO2:Lcd_Out(2, 3, "DATO 2:")delay_ms(500)i=0Tecla = 0
while Tecla=0Tecla = Keypad_Key_Click()wend
if (Tecla <= 9) thenif i<1 then
dato2=Teclai=1BytetoStr(Tecla,TeclaT)Lcd_Out(2,11,TeclaT)delay_ms(1000)
![Page 5: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/5.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 5/14
elsegoto OPERACIONend if
elsegoto DATO2 end if
OPERACION:Tecla=0
while Tecla=0Tecla = Keypad_Key_Click()
wendselect case Tecla
case 16res= dato1 + dato2Lcd_Out(2, 17, "+")
case 15res = dato1 - dato2Lcd_Out(2, 17, "-")
case 14res= dato1 * dato2Lcd_Out(2, 17, "*")
case 13res = dato1 / dato2Lcd_Out(2, 17, "/")
end select
delay_ms(1000)Lcd_Cmd(_LCD_Clear)Lcd_Out(1, 1, "El resultado es: ")BytetoStr(res,ress)Lcd_Out(2, 7, ress)delay_ms(1000)
wendend.
Diagrama de flujo: CALCULADORABASICA
Configuración de puertos
Insertar Datos de o eración
Mostrar el resultado
en el LCD
Datos >=9??
Si
+ - * /
operar
Ele ir o eración
![Page 6: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/6.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 6/14
c) Número secreto: se ingresa un número de dos dígitos, se compara con el número secreto. Si el
numero ingresado es mayor se muestra en el LCD “Mayor” si es menor se muestra “Menor”. El
proceso se repite hasta encontrar el número secreto, en cuyo caso el LCD muestra “Numero
secreto”.
Código fuente:program NUMSEC
dim Tecla, i, dato1, numsec as bytedim TeclaT as string[3]
' KeyPad module
dim keypadPort as byte at PORTD' Lcd module connections
dim LCD_RS as sbit at RC4_bitLCD_EN as sbit at RC5_bitLCD_D4 as sbit at RC0_bitLCD_D5 as sbit at RC1_bitLCD_D6 as sbit at RC2_bitLCD_D7 as sbit at RC3_bitLCD_RS_Direction as sbit at TRISC4_bitLCD_EN_Direction as sbit at TRISC5_bitLCD_D4_Direction as sbit at TRISC0_bitLCD_D5_Direction as sbit at TRISC1_bitLCD_D6_Direction as sbit at TRISC2_bitLCD_D7_Direction as sbit at TRISC3_bit
' End Lcd module connections
main:ANSEL = 0ANSELH = 0
C1ON_bit = C2ON_bit = 0Keypad_Init()Lcd_Init()Lcd_Cmd(_LCD_CLEAR)Lcd_Cmd(_LCD_CURSOR_OFF)
numsec=24 ' se puede cambiar
Lcd_Out(1, 1, " BUSCA EL ")Lcd_Out(2, 1, " NUMERO SECRETO ")delay_ms(1500)
while (1)dato1=0
Lcd_Cmd(_LCD_Clear)Lcd_Out(2,3," buscalo... ")Tecla = 0
while Tecla=0Tecla = Keypad_Key_Click()
wendif Tecla <= 9 then
dato1=dato1 + Tecla*10BytetoStr(Tecla,TeclaT)Lcd_Out(1,7,TeclaT)delay_ms(500)
![Page 7: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/7.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 7/14
end if
Tecla = 0while Tecla=0
Tecla = Keypad_Key_Click()wend
if Tecla <= 9 thendato1= dato1 + Tecla
BytetoStr(Tecla,TeclaT)Lcd_Out(1,10,TeclaT)delay_ms(500)
end if
if dato1 > numsec thenLcd_Out(2,3," Mayor ")delay_ms(1000)elseif dato1 < numsec then
Lcd_Out(2,3," Menor ")delay_ms(1000)
else
Lcd_Out(2,3,"Numero secreto")delay_ms(1000)end if
end ifdelay_ms(1000)
wendend.
Diagrama de flujo:NUMEROSECRETO
Configuración de puertos
Insertar el número secreto
de dos cifras
“Número Secreto”
si
“mayor”
Leer un dato
por teclado
Numsecret =dato leído??
Numsecret >dato leído??
si
“menor”
![Page 8: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/8.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 8/14
DIAGRAMA ESQUEMÁTICO IMPLEMENTADO:
![Page 9: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/9.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 9/14
II. TRABAJO DE INVESTIGACION:Detallar (incluir código ejemplo en MikroBasic Pro) el procedimiento de escritura y lectura de la
memoria EEPROM de datos en el PIC16F887, de tal manera que pueda almacenar en esta memoria los
datos correspondientes al código de apertura del “Candado electrónico” y al “Numero Secreto”.
Candado electrónico:program CAND_EE
dim Tecla, i, j, valor, v as bytedim TeclaT as string[3]dim vec_dato, nvec_dato as string[3]
' KeyPad module
dim keypadPort as byte at PORTD' Lcd module connections
dim LCD_RS as sbit at RC4_bitLCD_EN as sbit at RC5_bitLCD_D4 as sbit at RC0_bitLCD_D5 as sbit at RC1_bitLCD_D6 as sbit at RC2_bitLCD_D7 as sbit at RC3_bitLCD_RS_Direction as sbit at TRISC4_bitLCD_EN_Direction as sbit at TRISC5_bitLCD_D4_Direction as sbit at TRISC0_bitLCD_D5_Direction as sbit at TRISC1_bitLCD_D6_Direction as sbit at TRISC2_bitLCD_D7_Direction as sbit at TRISC3_bit
' End Lcd module connections
main:ANSEL = 0
ANSELH = 0C1ON_bit = C2ON_bit = 0portb=$00TrisB=%11111110Keypad_Init()Lcd_Init()Lcd_Cmd(_LCD_CLEAR)Lcd_Cmd(_LCD_CURSOR_OFF)
if(EEPROM_Read(0x50)=1) thengoto start
end ifLcd_Out(1, 1, "CANDADO ELECTRONICO")delay_ms(500)Lcd_Cmd(_LCD_Clear)Lcd_Out(1, 1, " INTRODUZCA")delay_ms(500)Lcd_Out(1, 1, "CODIGO SECRETO:")Lcd_Cmd(_LCD_second_row)
i=0while (i<3)
Tecla = 0while Tecla=0Tecla = Keypad_Key_Click()
![Page 10: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/10.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 10/14
wendvec_dato[i]=TeclaBytetoStr(Tecla,TeclaT)Lcd_Out_cp(TeclaT)EEPROM_Write(0x02+i,Tecla)EEPROM_Write(0x50,1)i=i+1
wend
delay_ms(1000)Lcd_Out(1, 1, "CODIGO GUARDADO....")Lcd_Out(2, 1, "**********************")delay_ms(1000)
start:while (1)
Lcd_Cmd(_LCD_CLEAR)
Lcd_Out(1,1,"Introduzca clave:")Lcd_Out(2,1," ")i=0
while(i<3)Tecla = 0while Tecla=0Tecla = Keypad_Key_Click()
wendnvec_dato[i]=Tecla
BytetoStr(Tecla,TeclaT)Lcd_Out_cp(TeclaT)i=i+1
wenddelay_ms(1000)
valor=0for j=0 to 3
if(EEPROM_Read(0x02+j)=nvec_dato[j]) thenvalor= valor +1
end ifnext j
if (valor=3) thenLcd_Out(1,1,"**********************")
Lcd_Out(2,1," clave correcta =) ")Delay_ms(1000)Alerta=1
Delay_ms(3000)Alerta=0
elseLcd_Out(2,1," clave incorrecta =(")
Delay_ms(2000)end if
wendend.
Número Secreto:program NUMSEC_EE
dim Tecla, i, valor, j as byte
![Page 11: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/11.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 11/14
dim TeclaT as string[3]dim vec_dato, nvec_dato as string[3]
' KeyPad module
dim keypadPort as byte at PORTD' Lcd module connections
dim LCD_RS as sbit at RC4_bitLCD_EN as sbit at RC5_bit
LCD_D4 as sbit at RC0_bitLCD_D5 as sbit at RC1_bitLCD_D6 as sbit at RC2_bitLCD_D7 as sbit at RC3_bitLCD_RS_Direction as sbit at TRISC4_bitLCD_EN_Direction as sbit at TRISC5_bitLCD_D4_Direction as sbit at TRISC0_bitLCD_D5_Direction as sbit at TRISC1_bitLCD_D6_Direction as sbit at TRISC2_bitLCD_D7_Direction as sbit at TRISC3_bit
' End Lcd module connections
main:
ANSEL = 0ANSELH = 0C1ON_bit = C2ON_bit = 0Keypad_Init()Lcd_Init()Lcd_Cmd(_LCD_CLEAR)Lcd_Cmd(_LCD_CURSOR_OFF)
if(EEPROM_Read(0x50)=1) thengoto start
end if
Lcd_Out(1, 1, " INTRODUCE ")Lcd_Out(2, 1, " NUMERO SECRETO ")
delay_ms(1000)Lcd_Cmd(_LCD_CLEAR)i=0
while (i<2)Tecla = 0while Tecla=0Tecla = Keypad_Key_Click()
wendvec_dato[i]=TeclaBytetoStr(Tecla,TeclaT)Lcd_Out_cp(TeclaT)EEPROM_Write(0x02+i,Tecla)EEPROM_Write(0x50,1)
i=i+1wenddelay_ms(1000)Lcd_Out(1, 1, "NUMERO GUARDADO....")Lcd_Out(2, 1, "**********************")delay_ms(1000)
start:while(1)
Lcd_Cmd(_LCD_CLEAR)Lcd_Out(1, 1, " BUSCA EL ")
![Page 12: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/12.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 12/14
Lcd_Out(2, 1, " NUMERO SECRETO ")delay_ms(1500)
Lcd_Cmd(_LCD_CLEAR)i=0while(i<2)
Tecla = 0while Tecla=0Tecla = Keypad_Key_Click()
wendnvec_dato[i]=Tecla
BytetoStr(Tecla,TeclaT)Lcd_Out_cp(TeclaT)i=i+1
wenddelay_ms(1000)
valor=0for j=0 to 1
if(EEPROM_Read(0x02+j)=nvec_dato[j]) thenvalor= valor +1
end if
next jif (valor=2) thenLcd_Out(1,1,"**********************")Lcd_Out(2,1," NUMERO SECRETO =) ")
Delay_ms(1000)elseif(nvec_dato[0]=EEPROM_Read(0x02)) then
if(nvec_dato[1]>EEPROM_Read(0x02+1)) thenLcd_Out(1,1,"**********************")Lcd_Out(2,1," mayor ")Delay_ms(1000)
elseLcd_Out(1,1,"**********************")Lcd_Out(2,1," menor ")
Delay_ms(1000)end if
elseif(nvec_dato[0]>EEPROM_Read(0x02)) thenLcd_Out(1,1,"**********************")Lcd_Out(2,1," mayor ")Delay_ms(1000)elseLcd_Out(1,1,"**********************")Lcd_Out(2,1," menor ")Delay_ms(1000)
end ifend if
end ifwendend.
EEPROM_WritePrototipo //Para PIC16
sub procedure EEPROM_Write(dim Dirección as byte, dim Dato as byte)
//Para PIC18
sub procedure EEPROM_Write(dim Direccion as word, dim Dato as byte)
![Page 13: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/13.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 13/14
Retorna Nada
Descripción Escribe un dato en la dirección específica. El parámetro de dirección es dependiente de MCU;
para la familia PIC16 es de tipo byte, y para la familia PIC18 es de tipo word. Para PIC18
MCUs con más localizaciones de datos EEPROM, esto es responsabilidad de programar el
registro SFR EEADRH apropiadamente.
Sepa que todas las interrupciones estarán deshabilitadasdurante la ejecución de las rutina de
EEPROM_Write (GIE bit del registro INTCON será limpiado). La rutina pondrá ese bit a uno enla salida.
Requiere Requiere el modulo EEPROM.
Asegurar mínimo 20ms de retardo entre usos sucesivos de las rutinas EEPROM_Write y
EEPROM_Read. Aunque PIC escribirá el valor correcto, el EEPROM_Read podría devolver un
resultado indefinido.
Ejemplo EEPROM_Write($32, $AA)
EEPROM_ReadPrototipo //Para PIC16
sub function EEPROM_Read(dim Dirección as byte) as byte
// Para PIC18 sub function EEPROM_Read(dim Direccion as word) as byte
Retorna Retorna un byte de una dirección específica.
Descripción Lee los datos de memorias específicas. El parámetro de dirección depende de MCU; para la
familia PIC16 es de tipo byte, y para la familia PIC18 es de tipo word. For PIC18 MCUs con
mas localizaciones de datos EEPROM esto es responsabilidad de programar el registro SFR
EEADRH apropiadamente.
Requiere Requiere el modulo EEPROM.
Asegurar mínimo 20ms de retardo entre usos sucesivos de las rutinas EEPROM_Write y
EEPROM_Read. Aunque PIC escribirá el valor correcto, el EEPROM_Read podría devolver un
resultado indefinido.
Ejemplo tmp = EEPROM_Read($3F)
CONCLUSIONES:Se logro comprender el uso del teclado matricial junto al LCD, viendo las aplicaciones que se le
pueden dar.
Se realizo la implementación en hardware del ejemplo de la práctica observando un resultado bueno, y
un funcionamiento correcto.
Se logro realizar los programas de los incisos a), b) y c) en simulación, comprobando su
funcionamiento correcto, con un poco de demora en el análisis de los mismos, pero fue posibleentendiendo cada uno de los pasos a seguir.
Se logro comprender como se utilizaba la librería de mikroBasic para la escritura y lectura de la
memoria EEPROM del microcontrolador.
De la misma manera se logro entender el uso de las múltiples librerías de MikroBasic PRO,
utilizándolas del modo adecuado para las aplicaciones necesarias.
![Page 14: Laboratorio No 4](https://reader031.vdocuments.pub/reader031/viewer/2022020811/55cf9ddb550346d033af8de1/html5/thumbnails/14.jpg)
7/16/2019 Laboratorio No 4
http://slidepdf.com/reader/full/laboratorio-no-4-5634f93ba9898 14/14