Ejemplo de LED Blink con máquinas de estados (mbed / S08)
Revision 0:b9a539dbd0fe, committed 2018-03-19
- Comitter:
- diegocode
- Date:
- Mon Mar 19 17:39:32 2018 +0000
- Child:
- 1:d488a7a0c9e1
- Commit message:
- Ejemplo de Blink con ME (mbed / S08 / etc.)
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Mar 19 17:39:32 2018 +0000
@@ -0,0 +1,112 @@
+#include "mbed.h"
+
+/*
+* Ejemplo 01 - Máquinas de estados
+*
+* LED rojo 0.1s encendido y 0.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 TO_LED_OFF_MS 900 / TICK_MS
+#define TO_LED_ON_MS 100 / 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);
+
+ // Inicialización de GPIO y TPM1 para S08
+ /*
+ LED = 0;
+ _LED = 1;
+
+ TPM1SC = 0b01001011; // ftpm = 1us
+ TPM1MOD = 9999; // overflow cada 1ms
+ */
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Mar 19 17:39:32 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/aa5281ff4a02 \ No newline at end of file