Ejemplo de LED Blink con máquinas de estados (mbed / S08)

Dependencies:   mbed

main.cpp

Committer:
diegocode
Date:
2018-03-19
Revision:
1:d488a7a0c9e1
Parent:
0:b9a539dbd0fe

File content as of revision 1:d488a7a0c9e1:

#include "mbed.h"

/*
* Ejemplo 01 - Máquinas de estados
*
* LED rojo 0.1s encendido y 1.9s apagado
* Con máquinas de estados.
*
*/

// constantes para configurar Ticker 
#define     TICK_MS         10

// constantes para tiempo on y tiempo off (En ms)
#define     LED_T_ON_MS     100
#define     LED_T_OFF_MS    1900

#define     TO_LED_OFF_MS  LED_T_OFF_MS / TICK_MS
#define     TO_LED_ON_MS   LED_T_ON_MS / TICK_MS

// Defines para GPIO en S08
/*
#define     LED     PTAD_PTAD0
#define     _LED     PTADD_PTADD0
*/

// Define estado para apagado y encendido
// (El RGB on-board de FRDM es AC => encendido si LO)
#define     APAGADO     1
#define     ENCENDIDO   0

// Estados de la máquina de estados
enum { 
    LED_OFF, 
    LED_ON
};

// 
Ticker tick;

// Declara LED como DigitalOut correspondiente a LED Rojo
DigitalOut LED(LED_RED);

// variable para time out encendido y apagado
unsigned int LED_tout = 0;

// Variable de estado
char LED_estado = LED_OFF;

// Máquina de estados
void LED_Step();
void LED_Tick();

// inicialización del HW
void init_mcu();

int main()
{
    init_mcu();

    for(;;) {
        LED_Step();
    }
}

// Máquina de estados
void LED_Step(){
    switch(LED_estado){
        default:  // si estado no definido => LED_OFF
        case LED_OFF:  // LED_OFF
            LED = APAGADO;     // salidas: Apaga LED
            if (LED_tout == 0) {    // transición: Si timeout apagado...
                LED_estado = LED_ON;        // próximo estado
                LED_tout = TO_LED_ON_MS;    // timeout <- timeout ON
            }
            break;
            
        case LED_ON:   // LED_ON 
            LED = ENCENDIDO;    // salidas: Enciende LED
            if (LED_tout == 0) {    // transición: Si tiemeout encendido...
                LED_estado = LED_OFF;       // próximo estado
                LED_tout = TO_LED_OFF_MS;   // timeout <- timeout OFF
            }            
            break;
    }
}

// Para actualizar timeout de ME_LED
void LED_Tick() {
    if (LED_tout > 0) 
        LED_tout--;
}

// ISR de TPM1 Overflow para MCU S08
/*
__interrupt VectorNumber_tpm1ovf tpm_overflow(){
    TPM1SC_TOF = 0;
    
    LED_tick();
}
*/

void init_mcu(){
  // ticker: ejecuta LED_Tick cada TICK_MS ms
  tick.attach(&LED_Tick, TICK_MS / 1000.0);
  
  // Inicialización de GPIO y TPM1 para S08
  /*  
    LED = 0;
    _LED = 1;
    
    TPM1SC = 0b01001011; // ftpm = 1us
    TPM1MOD = 9999; // overflow cada 1ms
   */
}