In this post, we will see how to make a timer that works for hours, using a PIC 16F628A programmed in BASIC language in the MICROCODE program.
MATERIALS:
We will use the following materials for this project:
- 1 PIC 16F628A
- 1 LCD 16x2
- 3 Push buttons
- 3 Resistors of 1k
- 1 Resistor of 220 Ohms
- 1 LED diode
- 1 Potentiometer of 10k
WIRING DIAGRAM:
EXPLANATION OF THE CODE:
'****************************************************************
'* Name : encXhoras.BAS *
'* Author : Esteban Carrillo - www.edeptec.com *
'* Notice : Copyright (c) 2022 *
'* : All Rights Reserved *
'* Date : 10/02/2021 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
CMCON = 7
trisa =%011'Declaramos los puertos a0 y a1 como entradas, y el puerto a2 como salida.
porta = 0
'Nombramos los puertos a0 y a1 como pulsadorSumar y pulsadorIniciar respectivamente, haciendo referencia a los botones.
pulsadorSumar VAR porta.0
pulsadorIniciar VAR porta.1
'Nombramos el puerto a2 como led
led VAR porta.2
'CREAMOS VARIAS VARIABLES
' Tres de tipo BYTE y dos de tipo WORD
tiempoProgramado VAR BYTE
tiempoTranscurrido VAR BYTE
y VAR BYTE
tiempoActual VAR WORD
tiempoLimite var WORD
y = 1' Le asignamos a la variable y el valor de 1
' Declaramos pines de conexión del lcd
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
'Creamos un loop: cicloProgramarTiempo
cicloProgramarTiempo:
' Cada vez que presionemos el pulsadorSumar:
'* Sumaremos la variable tiempoProgramado, esta variable ira de 0 a 24
'* Si la variable tiempoProgramado es mayor a 24 entonces tiempoProgramado = 0
IF pulsadorSumar = 1 THEN
PAUSE 500
tiempoProgramado = tiempoProgramado + 1
IF tiempoProgramado > 24 THEN
tiempoProgramado = 0
ENDIF
ENDIF
LCDOUT $fe, 1,$fe, 2, "PROGRAMAR TIEMPO"
LCDOUT $fe,$C5, DEC tiempoProgramado 'Imprimimos el valor de la variable tiempoProgramado
LCDOUT $fe,$C8, "HORAS"
IF pulsadorIniciar = 1 THEN ' Si pulsamos pulsadorIniciar, nos dirigimos al loop tiempoOn
GOTO iniciarTemporizador
ENDIF
PAUSE 50
GOTO cicloProgramarTiempo' Cerramos el loop cicloProgramarTiempo
iniciarTemporizador:
tiempoLimite = tiempoProgramado * 60 ' *La variable tiempoLimite tomara valores de 60 a 1440.
LCDOUT $fe, 1, $fe, 2, "TIME ON: ",DEC tiempoProgramado, "Hr"' Imprimimos en el LCD el tiempo programado.
LCDOUT $fe,$c0, "ELAPSED T: ",DEC tiempoTranscurrido , "Hr" ' Imprimimos en el LCD el tiempo transcurrido.
FOR tiempoActual = 0 TO tiempoLimite ' Creamos un ciclo for que ira de 0 hasta el valor de la variable tiempoLimite, esta es la pausa durante la cual estará encendido el led
HIGH led ' Activamos led
PAUSE 60000 ' Establecemos una pausa de 1 minuto
IF tiempoActual == y * 60 THEN ' Condición para mostrar en el LCD el tiempoLimite transcurrido.
y = y + 1
tiempoTranscurrido =(tiempoActual/60)
LCDOUT $fe,$c0, "ELAPSED tiempoProgramado: ",DEC tiempoTranscurrido, "Hr"
ENDIF
NEXT tiempoActual
' Una vez que se haya terminado el ciclo for
LOW LED ' Desactivamos led
tiempoProgramado = 0 ' Enceramos la variable tiempoProgramado
LCDOUT $fe, 1 ' limpiamos el LCD
GOTO cicloProgramarTiempo ' Retornamos al loop cicloProgramarTiempo.
EXPLANATION OF THE FOR LOOP PART (PAUSE FOR HOURS).
The pauses in microcode, according to what I have tested, can be up to 65,500ms (65.5 seconds - 1 minute 10 seconds), as we can see, it does not allow us to make a single pause that lasts for hours; what we can do is successive pauses, for example PAUSE 65500: PAUSE 65500... but doing this takes up a lot of storage space in the microcontroller, for this reason, we will use a for loop.
Within the for loop, we will set a pause of 60,000 ms, which is equal to 1 minute. To make a pause of 1 hour, the for loop would have to repeat 60 times because 60 times 60,000 is equal to 1 hour (60*60000ms = 3,600,000ms which is equal to 1 hour). To determine the number of repetitions according to the programmed time (value of the variable t), we multiply t * 60 and store the value of this multiplication in the variable time.
tiempo = t * 60 FOR X = 0 TOtiempo PAUSE 60000 NEXT X
I added a condition to show the elapsed hours on the LCD; for this, I did the following:
Within the for loop, we add the following line of codeIF X == y * 60 THEN, as we know the variable "y" was given the value of 1, so when the value of X is equal to 60 (initially), within this condition we have the following.
We add the value of y + 1, with this we will achieve that the next time we enter this condition it will be when the value of X has multiples of 60."60 = 1 hour, 120 = 2 hours, 180 = 3 hours, etc"Each time this occurs, we will divide the value of X by 60; this will give us the number of elapsed hours, and we will store this value in the variable z. Example:
If x = 180, z = 180/60 ==> 180/60 = 3 ==> z = 3, we print this value on the LCD.
IFx == y * 60 THEN y = y + 1 z =(x/60) LCDOUT $fe,$c0,"ELAPSED T: ",DEC z, "Hr" ENDIF
If you want to activate a load of 110 - 220v with this project, I recommend the following post:
Post a Comment
Hello! We're so glad you've made it this far and are reading this article on Edeptec.
This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.
» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?
Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.
Thank you for being part of this learning community! Your participation is what makes this project grow.