Diego Codevilla
/
LED_me_uno
Ejemplo de LED Blink con máquinas de estados (mbed / S08)
main.cpp@1:d488a7a0c9e1, 2018-03-19 (annotated)
- Committer:
- diegocode
- Date:
- Mon Mar 19 23:24:44 2018 +0000
- Revision:
- 1:d488a7a0c9e1
- Parent:
- 0:b9a539dbd0fe
Agregado de constantes para t ON y t OFF.; Bug de inicializaci?n de Ticker corregido
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
diegocode | 0:b9a539dbd0fe | 1 | #include "mbed.h" |
diegocode | 0:b9a539dbd0fe | 2 | |
diegocode | 0:b9a539dbd0fe | 3 | /* |
diegocode | 0:b9a539dbd0fe | 4 | * Ejemplo 01 - Máquinas de estados |
diegocode | 0:b9a539dbd0fe | 5 | * |
diegocode | 1:d488a7a0c9e1 | 6 | * LED rojo 0.1s encendido y 1.9s apagado |
diegocode | 0:b9a539dbd0fe | 7 | * Con máquinas de estados. |
diegocode | 0:b9a539dbd0fe | 8 | * |
diegocode | 0:b9a539dbd0fe | 9 | */ |
diegocode | 0:b9a539dbd0fe | 10 | |
diegocode | 0:b9a539dbd0fe | 11 | // constantes para configurar Ticker |
diegocode | 0:b9a539dbd0fe | 12 | #define TICK_MS 10 |
diegocode | 0:b9a539dbd0fe | 13 | |
diegocode | 0:b9a539dbd0fe | 14 | // constantes para tiempo on y tiempo off (En ms) |
diegocode | 1:d488a7a0c9e1 | 15 | #define LED_T_ON_MS 100 |
diegocode | 1:d488a7a0c9e1 | 16 | #define LED_T_OFF_MS 1900 |
diegocode | 1:d488a7a0c9e1 | 17 | |
diegocode | 1:d488a7a0c9e1 | 18 | #define TO_LED_OFF_MS LED_T_OFF_MS / TICK_MS |
diegocode | 1:d488a7a0c9e1 | 19 | #define TO_LED_ON_MS LED_T_ON_MS / TICK_MS |
diegocode | 0:b9a539dbd0fe | 20 | |
diegocode | 0:b9a539dbd0fe | 21 | // Defines para GPIO en S08 |
diegocode | 0:b9a539dbd0fe | 22 | /* |
diegocode | 0:b9a539dbd0fe | 23 | #define LED PTAD_PTAD0 |
diegocode | 0:b9a539dbd0fe | 24 | #define _LED PTADD_PTADD0 |
diegocode | 0:b9a539dbd0fe | 25 | */ |
diegocode | 0:b9a539dbd0fe | 26 | |
diegocode | 0:b9a539dbd0fe | 27 | // Define estado para apagado y encendido |
diegocode | 0:b9a539dbd0fe | 28 | // (El RGB on-board de FRDM es AC => encendido si LO) |
diegocode | 0:b9a539dbd0fe | 29 | #define APAGADO 1 |
diegocode | 0:b9a539dbd0fe | 30 | #define ENCENDIDO 0 |
diegocode | 0:b9a539dbd0fe | 31 | |
diegocode | 0:b9a539dbd0fe | 32 | // Estados de la máquina de estados |
diegocode | 0:b9a539dbd0fe | 33 | enum { |
diegocode | 0:b9a539dbd0fe | 34 | LED_OFF, |
diegocode | 0:b9a539dbd0fe | 35 | LED_ON |
diegocode | 0:b9a539dbd0fe | 36 | }; |
diegocode | 0:b9a539dbd0fe | 37 | |
diegocode | 0:b9a539dbd0fe | 38 | // |
diegocode | 0:b9a539dbd0fe | 39 | Ticker tick; |
diegocode | 0:b9a539dbd0fe | 40 | |
diegocode | 0:b9a539dbd0fe | 41 | // Declara LED como DigitalOut correspondiente a LED Rojo |
diegocode | 0:b9a539dbd0fe | 42 | DigitalOut LED(LED_RED); |
diegocode | 0:b9a539dbd0fe | 43 | |
diegocode | 0:b9a539dbd0fe | 44 | // variable para time out encendido y apagado |
diegocode | 0:b9a539dbd0fe | 45 | unsigned int LED_tout = 0; |
diegocode | 0:b9a539dbd0fe | 46 | |
diegocode | 0:b9a539dbd0fe | 47 | // Variable de estado |
diegocode | 0:b9a539dbd0fe | 48 | char LED_estado = LED_OFF; |
diegocode | 0:b9a539dbd0fe | 49 | |
diegocode | 0:b9a539dbd0fe | 50 | // Máquina de estados |
diegocode | 0:b9a539dbd0fe | 51 | void LED_Step(); |
diegocode | 0:b9a539dbd0fe | 52 | void LED_Tick(); |
diegocode | 0:b9a539dbd0fe | 53 | |
diegocode | 0:b9a539dbd0fe | 54 | // inicialización del HW |
diegocode | 0:b9a539dbd0fe | 55 | void init_mcu(); |
diegocode | 0:b9a539dbd0fe | 56 | |
diegocode | 0:b9a539dbd0fe | 57 | int main() |
diegocode | 0:b9a539dbd0fe | 58 | { |
diegocode | 0:b9a539dbd0fe | 59 | init_mcu(); |
diegocode | 0:b9a539dbd0fe | 60 | |
diegocode | 0:b9a539dbd0fe | 61 | for(;;) { |
diegocode | 0:b9a539dbd0fe | 62 | LED_Step(); |
diegocode | 0:b9a539dbd0fe | 63 | } |
diegocode | 0:b9a539dbd0fe | 64 | } |
diegocode | 0:b9a539dbd0fe | 65 | |
diegocode | 0:b9a539dbd0fe | 66 | // Máquina de estados |
diegocode | 0:b9a539dbd0fe | 67 | void LED_Step(){ |
diegocode | 0:b9a539dbd0fe | 68 | switch(LED_estado){ |
diegocode | 0:b9a539dbd0fe | 69 | default: // si estado no definido => LED_OFF |
diegocode | 0:b9a539dbd0fe | 70 | case LED_OFF: // LED_OFF |
diegocode | 0:b9a539dbd0fe | 71 | LED = APAGADO; // salidas: Apaga LED |
diegocode | 0:b9a539dbd0fe | 72 | if (LED_tout == 0) { // transición: Si timeout apagado... |
diegocode | 0:b9a539dbd0fe | 73 | LED_estado = LED_ON; // próximo estado |
diegocode | 0:b9a539dbd0fe | 74 | LED_tout = TO_LED_ON_MS; // timeout <- timeout ON |
diegocode | 0:b9a539dbd0fe | 75 | } |
diegocode | 0:b9a539dbd0fe | 76 | break; |
diegocode | 0:b9a539dbd0fe | 77 | |
diegocode | 0:b9a539dbd0fe | 78 | case LED_ON: // LED_ON |
diegocode | 0:b9a539dbd0fe | 79 | LED = ENCENDIDO; // salidas: Enciende LED |
diegocode | 0:b9a539dbd0fe | 80 | if (LED_tout == 0) { // transición: Si tiemeout encendido... |
diegocode | 0:b9a539dbd0fe | 81 | LED_estado = LED_OFF; // próximo estado |
diegocode | 0:b9a539dbd0fe | 82 | LED_tout = TO_LED_OFF_MS; // timeout <- timeout OFF |
diegocode | 0:b9a539dbd0fe | 83 | } |
diegocode | 0:b9a539dbd0fe | 84 | break; |
diegocode | 0:b9a539dbd0fe | 85 | } |
diegocode | 0:b9a539dbd0fe | 86 | } |
diegocode | 0:b9a539dbd0fe | 87 | |
diegocode | 0:b9a539dbd0fe | 88 | // Para actualizar timeout de ME_LED |
diegocode | 0:b9a539dbd0fe | 89 | void LED_Tick() { |
diegocode | 0:b9a539dbd0fe | 90 | if (LED_tout > 0) |
diegocode | 0:b9a539dbd0fe | 91 | LED_tout--; |
diegocode | 0:b9a539dbd0fe | 92 | } |
diegocode | 0:b9a539dbd0fe | 93 | |
diegocode | 0:b9a539dbd0fe | 94 | // ISR de TPM1 Overflow para MCU S08 |
diegocode | 0:b9a539dbd0fe | 95 | /* |
diegocode | 0:b9a539dbd0fe | 96 | __interrupt VectorNumber_tpm1ovf tpm_overflow(){ |
diegocode | 0:b9a539dbd0fe | 97 | TPM1SC_TOF = 0; |
diegocode | 0:b9a539dbd0fe | 98 | |
diegocode | 0:b9a539dbd0fe | 99 | LED_tick(); |
diegocode | 0:b9a539dbd0fe | 100 | } |
diegocode | 0:b9a539dbd0fe | 101 | */ |
diegocode | 0:b9a539dbd0fe | 102 | |
diegocode | 0:b9a539dbd0fe | 103 | void init_mcu(){ |
diegocode | 0:b9a539dbd0fe | 104 | // ticker: ejecuta LED_Tick cada TICK_MS ms |
diegocode | 1:d488a7a0c9e1 | 105 | tick.attach(&LED_Tick, TICK_MS / 1000.0); |
diegocode | 0:b9a539dbd0fe | 106 | |
diegocode | 0:b9a539dbd0fe | 107 | // Inicialización de GPIO y TPM1 para S08 |
diegocode | 0:b9a539dbd0fe | 108 | /* |
diegocode | 0:b9a539dbd0fe | 109 | LED = 0; |
diegocode | 0:b9a539dbd0fe | 110 | _LED = 1; |
diegocode | 0:b9a539dbd0fe | 111 | |
diegocode | 0:b9a539dbd0fe | 112 | TPM1SC = 0b01001011; // ftpm = 1us |
diegocode | 0:b9a539dbd0fe | 113 | TPM1MOD = 9999; // overflow cada 1ms |
diegocode | 0:b9a539dbd0fe | 114 | */ |
diegocode | 0:b9a539dbd0fe | 115 | } |