Diego Codevilla / Mbed 2 deprecated LED_me_uno_eb

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 /*
00004 * Ejemplo 01.eb - Máquinas de estados
00005 *
00006 * LED rojo 0.1s encendido y 1.9s apagado
00007 * Con máquinas de estados.
00008 *
00009 */
00010 
00011 // constantes para configurar Ticker 
00012 #define     TICK_MS         10
00013 
00014 // constantes para tiempo on y tiempo off (En ms)
00015 #define     LED_T_ON_MS     100
00016 #define     LED_T_OFF_MS    1900
00017 
00018 #define     TO_LED_OFF_MS  LED_T_OFF_MS / TICK_MS
00019 #define     TO_LED_ON_MS   LED_T_ON_MS / TICK_MS
00020 
00021 // Defines para GPIO en S08
00022 /*
00023 #define     LED     PTAD_PTAD0
00024 #define     _LED     PTADD_PTADD0
00025 */
00026 
00027 // Define estado para apagado y encendido
00028 // (El RGB on-board de FRDM es AC => encendido si LO)
00029 #define     APAGADO     1
00030 #define     ENCENDIDO   0
00031 
00032 // Estados de la máquina de estados
00033 enum { 
00034     LED_OFF, 
00035     LED_ON
00036 };
00037 
00038 // 
00039 Ticker tick;
00040 
00041 // Declara LED como DigitalOut correspondiente a LED Rojo
00042 DigitalOut LED(LED_RED);
00043 
00044 //Serial serusb(USBTX, USBRX);
00045 
00046 // variable para time out encendido y apagado
00047 unsigned int LED_tout = 0;
00048 
00049 // Variable de estado
00050 char LED_estado = LED_OFF;
00051 
00052 // Máquina de estados
00053 void LED_Step();
00054 void LED_Tick();
00055 
00056 // inicialización del HW
00057 void init_mcu();
00058 
00059 int main()
00060 {
00061     init_mcu();
00062 
00063     for(;;) {
00064         LED_Step();
00065         
00066     }
00067 }
00068 
00069 // Máquina de estados
00070 void LED_Step(){
00071     //serusb.printf("%d", LED_tout);
00072     if (LED_tout > 0)      // Si no pasó el tiempo...
00073         return;            // ...sale de ME
00074     
00075     switch(LED_estado){
00076         default:  // si estado no definido => LED_OFF
00077         case LED_OFF:                   // estado   LED_OFF
00078             LED = APAGADO;              // salidas: Apaga LED
00079             LED_estado = LED_ON;        // próximo estado
00080             LED_tout = TO_LED_OFF_MS;   // timeout <- timeout ON
00081             break;
00082             
00083         case LED_ON:                    // estado    LED_ON 
00084             LED = ENCENDIDO;            // salidas: Enciende LED
00085             LED_estado = LED_OFF;       // próximo estado
00086             LED_tout = TO_LED_ON_MS;   // timeout <- timeout OFF           
00087             break;
00088     }
00089 }
00090 
00091 // Para actualizar timeout de ME_LED
00092 void LED_Tick() {
00093     if (LED_tout > 0) 
00094         LED_tout--;
00095 }
00096 
00097 // ISR de TPM1 Overflow para MCU S08
00098 /*
00099 __interrupt VectorNumber_tpm1ovf tpm_overflow(){
00100     TPM1SC_TOF = 0;
00101     
00102     LED_tick();
00103 }
00104 */
00105 
00106 void init_mcu(){
00107   // ticker: ejecuta LED_Tick cada TICK_MS ms
00108   tick.attach(&LED_Tick, TICK_MS / 1000.0);
00109  
00110   LED = APAGADO;
00111 
00112   // Inicialización de GPIO y TPM1 para S08
00113   /*  
00114     LED = 0;
00115     _LED = 1;
00116     
00117     TPM1SC = 0b01001011; // ftpm = 1us
00118     TPM1MOD = 9999; // overflow cada 1ms
00119    */
00120 }