Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 /* 00004 * Ejemplo 01 - 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 // variable para time out encendido y apagado 00045 unsigned int LED_tout = 0; 00046 00047 // Variable de estado 00048 char LED_estado = LED_OFF; 00049 00050 // Máquina de estados 00051 void LED_Step(); 00052 void LED_Tick(); 00053 00054 // inicialización del HW 00055 void init_mcu(); 00056 00057 int main() 00058 { 00059 init_mcu(); 00060 00061 for(;;) { 00062 LED_Step(); 00063 } 00064 } 00065 00066 // Máquina de estados 00067 void LED_Step(){ 00068 switch(LED_estado){ 00069 default: // si estado no definido => LED_OFF 00070 case LED_OFF: // LED_OFF 00071 LED = APAGADO; // salidas: Apaga LED 00072 if (LED_tout == 0) { // transición: Si timeout apagado... 00073 LED_estado = LED_ON; // próximo estado 00074 LED_tout = TO_LED_ON_MS; // timeout <- timeout ON 00075 } 00076 break; 00077 00078 case LED_ON: // LED_ON 00079 LED = ENCENDIDO; // salidas: Enciende LED 00080 if (LED_tout == 0) { // transición: Si tiemeout encendido... 00081 LED_estado = LED_OFF; // próximo estado 00082 LED_tout = TO_LED_OFF_MS; // timeout <- timeout OFF 00083 } 00084 break; 00085 } 00086 } 00087 00088 // Para actualizar timeout de ME_LED 00089 void LED_Tick() { 00090 if (LED_tout > 0) 00091 LED_tout--; 00092 } 00093 00094 // ISR de TPM1 Overflow para MCU S08 00095 /* 00096 __interrupt VectorNumber_tpm1ovf tpm_overflow(){ 00097 TPM1SC_TOF = 0; 00098 00099 LED_tick(); 00100 } 00101 */ 00102 00103 void init_mcu(){ 00104 // ticker: ejecuta LED_Tick cada TICK_MS ms 00105 tick.attach(&LED_Tick, TICK_MS / 1000.0); 00106 00107 // Inicialización de GPIO y TPM1 para S08 00108 /* 00109 LED = 0; 00110 _LED = 1; 00111 00112 TPM1SC = 0b01001011; // ftpm = 1us 00113 TPM1MOD = 9999; // overflow cada 1ms 00114 */ 00115 }
Generated on Tue Jul 12 2022 23:07:43 by
1.7.2