UNIVERSIDAD DEL VALLE
EXAMEN PREVIO
Estudiante: Mauricio Ross Revollo Fecha: 06/10/2025
Carrera: IET Asignatura: Microprocesadores II
Docente: Ing. Pérez Villarroel Gerson Laboratorio:
Título de la Practica: Manejo del ADC N° Practica: 3
Firma Estudiante: Firma Docente:
Parte 4.1
Enunciado:
Escriba un programa simple que realice una conversión analógica digital de un voltaje de entrada
obtenido a partir de un divisor de voltaje, luego el dato de conversión deberá ser desplegado en un
LCD en tiempo real para su visualización mostrando el valor del voltaje con tres decimales.
Código:
#include "main.h"
#include "stdio.h"
/* --- Variables globales --- */
ADC_HandleTypeDef hadc1;
char mostrar[12];
uint8_t u = 0, d = 0, c = 0, c2 = 0;
uint16_t valor = 0;
uint16_t voltaje = 0;
/* --- Prototipos de funciones --- */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
void LCDEscribirNibble(uint8_t dato);
void LCDComando(uint8_t cmd);
void LCDDatos(uint16_t data);
void LCDInicializar(void);
void LCDIrAXY(uint8_t x, uint8_t y);
void LCDImprimir(char *str);
/* --- Función principal --- */
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_ADC1_Init();
HAL_ADC_Start(&hadc1);
LCDInicializar();
LCDIrAXY(0, 0);
LCDImprimir("LAB 3 VOLTAJE:");
LCDIrAXY(0, 1);
LCDImprimir("0.00 Volts");
while (1)
{
HAL_ADC_PollForConversion(&hadc1, 100);
valor = HAL_ADC_GetValue(&hadc1);
/* Conversión de valor ADC a voltaje (ajustar constante si es necesario) */
voltaje = (valor) / 1.3445;
c = voltaje % 10;
d = (voltaje / 10) % 10;
u = (voltaje / 100) % 10;
c2 = voltaje / 1000;
sprintf(mostrar, "%i,%i%i%i Volts", c2, u, d, c);
LCDIrAXY(0, 1);
LCDImprimir(mostrar);
}
}
/* --- Funciones LCD --- */
/**
* @brief Envía un nibble al bus de datos del LCD
*/
void LCDEscribirNibble(uint8_t dato)
{
GPIOA->ODR = (GPIOA->ODR & 0x0F) | (dato & 0xF0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // EN = 1
HAL_Delay(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // EN = 0
}
/**
* @brief Envía un comando al LCD
*/
void LCDComando(uint8_t cmd)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); // RS = 0
LCDEscribirNibble(cmd);
HAL_Delay(10);
LCDEscribirNibble(cmd << 4);
HAL_Delay(10);
}
/**
* @brief Envía datos (caracteres) al LCD
*/
void LCDDatos(uint16_t data)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); // RS = 1
LCDEscribirNibble(data);
HAL_Delay(10);
LCDEscribirNibble(data << 4);
HAL_Delay(10);
}
/**
* @brief Posiciona el cursor en la coordenada X,Y del LCD
*/
void LCDIrAXY(uint8_t x, uint8_t y)
{
uint8_t firstCharAdr[] = {0x80, 0xC0, 0x94, 0xD4};
LCDComando(firstCharAdr[y] + x);
HAL_Delay(10);
}
/**
* @brief Imprime una cadena de caracteres en el LCD
*/
void LCDImprimir(char *str)
{
uint8_t i = 0;
while (str[i] != 0)
{
LCDDatos(str[i]);
i++;
}
}
/**
* @brief Inicializa el LCD en modo de 4 bits
*/
void LCDInicializar(void)
{
/* Configuración de pines de datos y control */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7,
GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1 | GPIO_PIN_2, GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1 | GPIO_PIN_2, GPIO_PIN_RESET);
/* Secuencia de inicialización */
LCDEscribirNibble(0x30);
HAL_Delay(5);
LCDEscribirNibble(0x30);
HAL_Delay(10);
LCDEscribirNibble(0x30);
HAL_Delay(10);
LCDEscribirNibble(0x20); // Cambiar a modo 4 bits
LCDComando(0x28); // 4 bits, 2 líneas, 5x8
LCDComando(0x0E); // Display ON, cursor ON
LCDComando(0x01); // Clear display
HAL_Delay(2);
LCDComando(0x06); // Incrementar dirección
}
Simulación: