Ejercicio uno del primer trabajo practico de la materia Sistemas Embebidos 2018
Dependencies: antirrebote mbed
ej1.cpp@0:009746e1ce2d, 2018-06-12 (annotated)
- Committer:
- Tom_87
- Date:
- Tue Jun 12 17:12:27 2018 +0000
- Revision:
- 0:009746e1ce2d
programa que controla el titileo de un led por medio de un pulsador
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tom_87 | 0:009746e1ce2d | 1 | #include "mbed.h" //Si se toca el pulsador comienza la secuencia de "Blinky LED" |
Tom_87 | 0:009746e1ce2d | 2 | #include "antirrebote.h" //Si se vuelve a presionar, se apaga el LED |
Tom_87 | 0:009746e1ce2d | 3 | //Declaración de valores de estados |
Tom_87 | 0:009746e1ce2d | 4 | enum {ON = 0, |
Tom_87 | 0:009746e1ce2d | 5 | OFF = 1, |
Tom_87 | 0:009746e1ce2d | 6 | COMP = 1, |
Tom_87 | 0:009746e1ce2d | 7 | INC = 0, |
Tom_87 | 0:009746e1ce2d | 8 | S_OFF = 0, |
Tom_87 | 0:009746e1ce2d | 9 | S_WAIT, |
Tom_87 | 0:009746e1ce2d | 10 | S_ON, |
Tom_87 | 0:009746e1ce2d | 11 | S_OUT = 0, |
Tom_87 | 0:009746e1ce2d | 12 | S_LON, S_LOFF}; |
Tom_87 | 0:009746e1ce2d | 13 | |
Tom_87 | 0:009746e1ce2d | 14 | //Seteo de entradas y salidas |
Tom_87 | 0:009746e1ce2d | 15 | DigitalIn Puls(PTC0); //Este tiene una configuracion PULL_UP (Presionado => "0"/ NO Presionado => "1") |
Tom_87 | 0:009746e1ce2d | 16 | DigitalOut LedRojo(LED1); |
Tom_87 | 0:009746e1ce2d | 17 | |
Tom_87 | 0:009746e1ce2d | 18 | Ticker wait_puls, wait_led; //Seteo de nombres de objetos tipo "Ticker" |
Tom_87 | 0:009746e1ce2d | 19 | AntReb puls_detect; |
Tom_87 | 0:009746e1ce2d | 20 | |
Tom_87 | 0:009746e1ce2d | 21 | //Prototipos de funciones |
Tom_87 | 0:009746e1ce2d | 22 | |
Tom_87 | 0:009746e1ce2d | 23 | void SM_BLINKING_LED(); |
Tom_87 | 0:009746e1ce2d | 24 | void interrupt_puls(); |
Tom_87 | 0:009746e1ce2d | 25 | void interrupt_led(); |
Tom_87 | 0:009746e1ce2d | 26 | //Seteo de valores iniciales de variables que funcionan como memorias |
Tom_87 | 0:009746e1ce2d | 27 | int blink = OFF, |
Tom_87 | 0:009746e1ce2d | 28 | state_pul = 0, |
Tom_87 | 0:009746e1ce2d | 29 | state_b, |
Tom_87 | 0:009746e1ce2d | 30 | timeP_complete = INC, |
Tom_87 | 0:009746e1ce2d | 31 | timeL_complete = INC; |
Tom_87 | 0:009746e1ce2d | 32 | char hab; |
Tom_87 | 0:009746e1ce2d | 33 | |
Tom_87 | 0:009746e1ce2d | 34 | int main(void){ //Función de los objetos |
Tom_87 | 0:009746e1ce2d | 35 | char Pulso; |
Tom_87 | 0:009746e1ce2d | 36 | LedRojo=1; |
Tom_87 | 0:009746e1ce2d | 37 | wait_puls.attach(&interrupt_puls,0.01); //Llamará a la función "interrupt_puls" cada 0.1s (100ms) |
Tom_87 | 0:009746e1ce2d | 38 | wait_led.attach(&interrupt_led,0.5); //Llamará a la función "interrupt_led" cada 0.5s (500ms) |
Tom_87 | 0:009746e1ce2d | 39 | |
Tom_87 | 0:009746e1ce2d | 40 | |
Tom_87 | 0:009746e1ce2d | 41 | while(true){ //Función infinita (LOOP) |
Tom_87 | 0:009746e1ce2d | 42 | //Llamado de las funciones |
Tom_87 | 0:009746e1ce2d | 43 | puls_detect.setPin(PTC7); |
Tom_87 | 0:009746e1ce2d | 44 | Pulso = puls_detect.antiRebote(); |
Tom_87 | 0:009746e1ce2d | 45 | if (Pulso==1) |
Tom_87 | 0:009746e1ce2d | 46 | blink= !blink; |
Tom_87 | 0:009746e1ce2d | 47 | SM_BLINKING_LED(); |
Tom_87 | 0:009746e1ce2d | 48 | } |
Tom_87 | 0:009746e1ce2d | 49 | } |
Tom_87 | 0:009746e1ce2d | 50 | // ------------------------------------------------------------------------------------------------------- |
Tom_87 | 0:009746e1ce2d | 51 | //A partir de este comentario se realizarán las definiciones de cada función |
Tom_87 | 0:009746e1ce2d | 52 | |
Tom_87 | 0:009746e1ce2d | 53 | //Máquina de estados de un "Debounce Switch" (Pulsador Antirrebote) |
Tom_87 | 0:009746e1ce2d | 54 | void SM_ANTI_REBOTE(void){ //Este switch variará según el estado del pulsador |
Tom_87 | 0:009746e1ce2d | 55 | |
Tom_87 | 0:009746e1ce2d | 56 | } |
Tom_87 | 0:009746e1ce2d | 57 | //Máquina de estados del "Blinky LED" (LED Titilando) |
Tom_87 | 0:009746e1ce2d | 58 | void SM_BLINKING_LED(void){ |
Tom_87 | 0:009746e1ce2d | 59 | switch(state_b) |
Tom_87 | 0:009746e1ce2d | 60 | { |
Tom_87 | 0:009746e1ce2d | 61 | case S_OFF: |
Tom_87 | 0:009746e1ce2d | 62 | LedRojo = 1; |
Tom_87 | 0:009746e1ce2d | 63 | if(blink == ON) |
Tom_87 | 0:009746e1ce2d | 64 | state_b = S_ON; |
Tom_87 | 0:009746e1ce2d | 65 | break; |
Tom_87 | 0:009746e1ce2d | 66 | |
Tom_87 | 0:009746e1ce2d | 67 | case S_ON: |
Tom_87 | 0:009746e1ce2d | 68 | if (timeL_complete == COMP){ |
Tom_87 | 0:009746e1ce2d | 69 | LedRojo = !LedRojo; |
Tom_87 | 0:009746e1ce2d | 70 | timeL_complete = INC; |
Tom_87 | 0:009746e1ce2d | 71 | } |
Tom_87 | 0:009746e1ce2d | 72 | if(blink == OFF) |
Tom_87 | 0:009746e1ce2d | 73 | state_b=S_OFF; |
Tom_87 | 0:009746e1ce2d | 74 | break; |
Tom_87 | 0:009746e1ce2d | 75 | } |
Tom_87 | 0:009746e1ce2d | 76 | |
Tom_87 | 0:009746e1ce2d | 77 | } |
Tom_87 | 0:009746e1ce2d | 78 | //Esta función hará posbile el parpadeo del LED |
Tom_87 | 0:009746e1ce2d | 79 | void interrupt_led(void){ |
Tom_87 | 0:009746e1ce2d | 80 | timeL_complete = COMP; |
Tom_87 | 0:009746e1ce2d | 81 | } |
Tom_87 | 0:009746e1ce2d | 82 | //Esta función hará posible el antirrebote del pulsador |
Tom_87 | 0:009746e1ce2d | 83 | void interrupt_puls(void){ |
Tom_87 | 0:009746e1ce2d | 84 | puls_detect.DebTime(); |
Tom_87 | 0:009746e1ce2d | 85 | } |