// Examen final - Sistemas digitales y microprocesadores
// Gian Luca Ramirez - 1096600
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "pico/binary_info.h"
#include "pico/cyw43_arch.h"
#include "lcd_i2c.h"
// Define los pines para el LCD y el teclado
#define I2C_PORT i2c0
#define SDA_PIN 4
#define SCL_PIN 5
#define ROWS 4
#define COLS 4
uint rowPins[ROWS] = {0, 1, 2, 3}; // Pines de las filas
uint colPins[COLS] = {6, 7, 8, 9}; // Pines de las columnas
// Define las teclas del teclado matricial
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Variables globales
char operand1[32], operand2[32], operator;
int index1 = 0, index2 = 0;
bool secondOperand = false;
lcd_t lcd; // Definir el objeto lcd
// Funciones de inicialización
void init_i2c() {
i2c_init(I2C_PORT, 100 * 1000);
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_down(SDA_PIN);
gpio_pull_down(SCL_PIN);
bi_decl(bi_2pins_with_func(SDA_PIN, SCL_PIN, GPIO_FUNC_I2C));
}
// Función para inicializar los pines del teclado matricial
void init_keypad() {
for (int i = 0; i < ROWS; i++) {
gpio_init(rowPins[i]);
gpio_set_dir(rowPins[i], GPIO_OUT);
gpio_put(rowPins[i], 1);
}
for (int j = 0; j < COLS; j++) {
gpio_init(colPins[j]);
gpio_set_dir(colPins[j], GPIO_IN);
gpio_pull_down(colPins[j]);
}
}
// Función para leer el teclado matricial
char get_key() {
for (int i = 0; i < ROWS; i++) {
gpio_put(rowPins[i], 0);
for (int j = 0; j < COLS; j++) {
if (gpio_get(colPins[j]) == 0) {
while (gpio_get(colPins[j]) == 0);
gpio_put(rowPins[i], 1);
return keys[i][j];
}
}
gpio_put(rowPins[i], 1);
}
return '\0';
}
// Función para manejar la entrada del teclado
void handle_key(char key) {
if (key >= '0' && key <= '9') {
if (!secondOperand) {
operand1[index1++] = key;
operand1[index1] = '\0';
} else {
operand2[index2++] = key;
operand2[index2] = '\0';
}
lcd_putc(&lcd, key);
} else if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
if (index1 > 0 && !secondOperand) {
operator = key;
secondOperand = true;
lcd_putc(&lcd, key);
}
} else if (key == '#') {
// Calcular el resultado
int num1 = atoi(operand1);
int num2 = atoi(operand2);
int result = 0;
switch (operator) {
case 'A': result = num1 + num2; break;
case 'B': result = num1 - num2; break;
case 'C': result = num1 * num2; break;
case 'D': result = num1 / num2; break;
}
char resultStr[16];
sprintf(resultStr, "%d", result);
lcd_clear(&lcd);
lcd_puts(&lcd, resultStr);
index1 = index2 = 0;
operand1[0] = operand2[0] = '\0';
secondOperand = false;
} else if (key == '*') {
// Limpiar la entrada
index1 = index2 = 0;
operand1[0] = operand2[0] = '\0';
secondOperand = false;
lcd_clear(&lcd);
}
}
int main() {
stdio_init_all();
init_i2c();
init_keypad();
// Inicializar el LCD
lcd_init(&lcd, I2C_PORT, 0x27, 16, 2);
lcd_clear(&lcd);
lcd_puts(&lcd, "Calculadora");
while (1) {
char key = get_key();
if (key != '\0') {
handle_key(key);
}
}
return 0;
}