Last active
March 5, 2024 13:24
-
-
Save esmarr58/daa38fba06275a53ffafa91bae2af365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Contador ascedente, descendente y reset con 3 display de 7 segmentos. | |
*/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "driver/gpio.h" | |
#include "esp_log.h" | |
#include "sdkconfig.h" | |
#include "esp_intr_alloc.h" | |
#define A 41 | |
#define B 40 | |
#define C 39 | |
#define D 38 | |
#define E 37 | |
#define F 36 | |
#define G1 35 | |
#define G2 45 | |
#define A3 12 | |
#define A2 11 | |
#define A1 10 | |
#define A0 9 | |
#define botonAscendente 4 | |
#define botonDescendente 7 | |
#define botonReset 6 | |
#define e0 0b0001 | |
#define e1 0b0010 | |
#define e2 0b0100 | |
#define e3 0b1000 | |
#define cero 0xFC | |
#define uno 0x60 | |
#define dos 0xDB | |
#define tres 0xF3 | |
#define cuatro 0x67 | |
#define cinco 0xB7 | |
#define seis 0xBF | |
#define siete 0xE3 | |
#define ocho 0xFF | |
#define nueve 0xE7 | |
#define signoNegativo 0x03 | |
#define signoPositivo 0x00 | |
uint8_t estadoActual = e0; | |
uint8_t estadoFuturo; | |
volatile uint8_t ascendente = 0; | |
volatile uint8_t descedente = 0; | |
volatile uint8_t reset = 0; | |
volatile int16_t cuenta = -251; | |
volatile uint8_t signo = 12; | |
volatile int16_t unidades = 0; | |
volatile int16_t decenas = 0; | |
volatile int16_t centenas = 0; | |
static void IRAM_ATTR funcionAscendente(void *arg) { | |
ascendente = 1; | |
} | |
static void IRAM_ATTR funcionDescendente(void *arg) { | |
descedente = 1; | |
} | |
static void IRAM_ATTR funcionReset(void *arg) { | |
reset = 1; | |
} | |
void display(uint8_t numero){ | |
const uint8_t numeros[12] = {cero, uno, dos, tres, cuatro, cinco, seis, siete, ocho, nueve, signoNegativo, signoPositivo}; | |
gpio_set_level(A, !((numeros[numero] & 0b10000000) >> 7)); | |
gpio_set_level(B, !((numeros[numero] & 0b01000000) >> 6)); | |
gpio_set_level(C, !((numeros[numero] & 0b00100000) >> 5)); | |
gpio_set_level(D, !((numeros[numero] & 0b00010000) >> 4)); | |
gpio_set_level(E, !((numeros[numero] & 0b00001000) >> 3)); | |
gpio_set_level(F, !((numeros[numero] & 0b00000100) >> 2)); | |
gpio_set_level(G1,!((numeros[numero] & 0b00000010) >> 1)); | |
gpio_set_level(G2,!((numeros[numero] & 0b00000001) >> 0)); | |
} | |
void programa1(void * pvParameters){ | |
//Configuracion de las salidas | |
uint8_t tiempo = 0; | |
gpio_reset_pin(A); | |
gpio_reset_pin(B); | |
gpio_reset_pin(C); | |
gpio_reset_pin(D); | |
gpio_reset_pin(E); | |
gpio_reset_pin(F); | |
gpio_reset_pin(G1); | |
gpio_reset_pin(G2); | |
gpio_reset_pin(A3); | |
gpio_reset_pin(A2); | |
gpio_reset_pin(A1); | |
gpio_reset_pin(A0); | |
gpio_set_direction(A, GPIO_MODE_OUTPUT); | |
gpio_set_direction(B, GPIO_MODE_OUTPUT); | |
gpio_set_direction(C, GPIO_MODE_OUTPUT); | |
gpio_set_direction(D, GPIO_MODE_OUTPUT); | |
gpio_set_direction(E, GPIO_MODE_OUTPUT); | |
gpio_set_direction(F, GPIO_MODE_OUTPUT); | |
gpio_set_direction(G1, GPIO_MODE_OUTPUT); | |
gpio_set_direction(G2, GPIO_MODE_OUTPUT); | |
gpio_set_direction(A3, GPIO_MODE_OUTPUT); | |
gpio_set_direction(A2, GPIO_MODE_OUTPUT); | |
gpio_set_direction(A1, GPIO_MODE_OUTPUT); | |
gpio_set_direction(A0, GPIO_MODE_OUTPUT); | |
while (true) { | |
//Contar el tiempo | |
tiempo++; | |
if(tiempo == 199){ | |
} | |
//FSM # 1 | |
//Evaluar los estado | |
if(estadoActual == e0){ | |
estadoFuturo = e1; | |
display(decenas); | |
} | |
else if(estadoActual == e1){ | |
estadoFuturo = e2; | |
display(centenas); | |
} | |
else if(estadoActual == e2){ | |
estadoFuturo = e3; | |
display(signo); | |
} | |
else if(estadoActual == e3){ | |
estadoFuturo = e0; | |
display(unidades); | |
} | |
//Asignamos el estado futuro | |
estadoActual = estadoFuturo; | |
//Visualizar el estadoActual | |
//Se necesitan | |
gpio_set_level(A3, ((estadoActual & 0b1000)>>3)); | |
gpio_set_level(A2, ((estadoActual & 0b0100)>>2)); | |
gpio_set_level(A1, ((estadoActual & 0b0010)>>1)); | |
gpio_set_level(A0, ((estadoActual & 0b0001))); | |
//display(0); | |
usleep(500); | |
} | |
} | |
void programa2(void * pvParameters){ | |
for(;;){ | |
//cuenta++; | |
if(ascendente){ | |
ascendente = 0; | |
cuenta++; | |
} | |
if(descedente){ | |
descedente = 0; | |
cuenta--; | |
} | |
if(reset){ | |
reset = 0; | |
cuenta = 0; | |
} | |
if(cuenta > 250) cuenta = -250; | |
//Visualizar el signo negativo | |
if(cuenta < 0) signo = 10; | |
else signo = 11; | |
//Visualizar el resultado en 3 displays | |
centenas = cuenta/100; | |
if(centenas < 0) centenas = -1*centenas; | |
if(cuenta >= 0) decenas = (cuenta-100*centenas)/10; | |
else decenas = ((-1*cuenta)-100*centenas)/10; | |
if(cuenta >=0 ) unidades = cuenta-100*centenas-10*decenas; | |
else{ | |
unidades = ((-1*cuenta)-100*centenas-10*decenas); | |
} | |
usleep(300000); | |
//Si se activa la bandera de int1 | |
//cuenta++ | |
//sino y si bandera int2 | |
//cuenta-- | |
//sino | |
//cuenta = 0 | |
//esperar 1s | |
} | |
} | |
void app_main(void){ | |
gpio_reset_pin(botonAscendente); | |
gpio_set_direction(botonAscendente, GPIO_MODE_INPUT); | |
gpio_install_isr_service(botonAscendente); | |
gpio_set_intr_type(botonAscendente, GPIO_INTR_NEGEDGE); | |
gpio_isr_handler_add(botonAscendente, funcionAscendente, NULL); | |
gpio_reset_pin(botonDescendente); | |
gpio_set_direction(botonDescendente, GPIO_MODE_INPUT); | |
gpio_install_isr_service(botonDescendente); | |
gpio_set_intr_type(botonDescendente, GPIO_INTR_NEGEDGE); | |
gpio_isr_handler_add(botonDescendente, funcionDescendente, NULL); | |
gpio_reset_pin(botonReset); | |
gpio_set_direction(botonReset, GPIO_MODE_INPUT); | |
gpio_install_isr_service(botonReset); | |
gpio_set_intr_type(botonReset, GPIO_INTR_NEGEDGE); | |
gpio_isr_handler_add(botonReset, funcionReset, NULL); | |
xTaskCreate(programa1, "Programa1", 4048, NULL, tskIDLE_PRIORITY, NULL); | |
xTaskCreate(programa2, "Programa2", 4048, NULL, tskIDLE_PRIORITY, NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment