Created
March 12, 2019 22:39
-
-
Save rescurib/b4f966d516008a9afdcb081f0d7a8677 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
/* | |
* File: main.c | |
* Author: Rodolfo E. Escobar Uribe | |
* | |
* Created on 12 de marzo de 2019, 07:16 PM | |
*/ | |
#include "config.h" | |
#include <xc.h> | |
#define _XTAL_FREQ 48000000 | |
//-- Definición de tipo de estados | |
typedef enum { | |
Estado_0, // Estado inicial | |
Estado_1, | |
Estado_2, | |
Estado_3, | |
Estado_4, | |
No_de_Edos | |
} ESTADOS; | |
//-- Variable global de estado | |
ESTADOS Estado_Actual = Estado_0; | |
//-- Estructura de Maquina de Estados (FSM)) | |
typedef struct { | |
ESTADOS Estado; // Estado miembro del conjunto enumerado ESTADOS | |
void (*func)(int); // Acciones asociadas al estado | |
} FSM; | |
//-- Prototipos de funciones de estado | |
void Codigo_EDO_0(int); | |
void Codigo_EDO_1(int); | |
void Codigo_EDO_2(int); | |
void Codigo_EDO_3(int); | |
void Codigo_EDO_4(int); | |
//-- Creación de la estructura de la FSM | |
FSM Maquina_de_Estados[] = { | |
{Estado_0,Codigo_EDO_0}, | |
{Estado_1,Codigo_EDO_1}, | |
{Estado_2,Codigo_EDO_2}, | |
{Estado_3,Codigo_EDO_3}, | |
{Estado_4,Codigo_EDO_4}, | |
}; | |
//-- Funciones de estado | |
void Codigo_EDO_0(int sw){ | |
LATD = 0b10101010; | |
if(sw == 0) | |
Estado_Actual = Estado_1; | |
else | |
Estado_Actual = Estado_4; | |
} | |
void Codigo_EDO_1(int sw){ | |
__delay_ms(1000); | |
LATD = 1; | |
if(sw == 0) | |
Estado_Actual = Estado_2; | |
else | |
Estado_Actual = Estado_4; | |
} | |
void Codigo_EDO_2(int sw){ | |
__delay_ms(1000); | |
LATD = 2; | |
if(sw == 0) | |
Estado_Actual = Estado_3; | |
else | |
Estado_Actual = Estado_1; | |
} | |
void Codigo_EDO_3(int sw){ | |
__delay_ms(1000); | |
LATD = 3; | |
if(sw == 0) | |
Estado_Actual = Estado_4; | |
else | |
Estado_Actual = Estado_2; | |
} | |
void Codigo_EDO_4(int sw){ | |
__delay_ms(1000); | |
LATD = 4; | |
if(sw == 0) | |
Estado_Actual = Estado_1; | |
else | |
Estado_Actual = Estado_3; | |
} | |
void setup(){ | |
TRISBbits.TRISB0 = 0; | |
TRISD = 0x00; | |
ANSELD = 0x00; | |
} | |
void main(void) { | |
int sw = 0; | |
setup(); | |
//-- FSM --- | |
while(1){ | |
sw = PORTBbits.RB0; | |
//if (Estado_Actual<No_de_Edos){ | |
(*Maquina_de_Estados[Estado_Actual].func)(sw); | |
//} | |
//else{ | |
// Operaciones de manejo de exepción por | |
// estado no permitido. | |
//} | |
} | |
//---------- | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment