Debounce_Library
Revision 3:df73a4aa4ff8, committed 2018-06-12
- Comitter:
- Tom_87
- Date:
- Tue Jun 12 17:11:31 2018 +0000
- Parent:
- 0:16d62113f1d5
- Commit message:
- Programa para cotrolar titileo d un led con un pulsador
Changed in this revision
antirrebote.cpp | Show annotated file Show diff for this revision Revisions of this file |
antirrebote.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/antirrebote.cpp Thu May 03 23:32:45 2018 +0000 +++ b/antirrebote.cpp Tue Jun 12 17:11:31 2018 +0000 @@ -1,56 +1,77 @@ #include "antirrebote.h" -enum{PULS_NA, PULS_FLC1, PULS_A, PULS_FLC2}; +enum {PRESS=0, NO_PRESS=1, S_NO_PRESS, S_DETECT_PULS, S_DEB1, S_PRESS, S_RETENTION, S_DEB2, DEB_TIME = 20}; -AntReb::~AntReb(){ - } -AntReb::AntReb(){ - e_PULS = PULS_NA; - } +AntReb::~AntReb() +{ +} +AntReb::AntReb() +{ + state = S_NO_PRESS; + deb_time = DEB_TIME; + hab=0; + out=0; +} void AntReb::setPin(DigitalIn pin) { - P_PULS =pin.read(); + Pulsador = pin.read(); +} +void AntReb::DebTime() +{ + if(deb_time>0) + deb_time--; + else + deb_time = DEB_TIME; } -pinEstado_t AntReb::antiRebote(){ - pinEstado_t StateOut; +pinEstado_t AntReb::antiRebote() +{ - switch(e_PULS){ + switch(state) { + case S_NO_PRESS: default: - case PULS_NA: - StateOut = APAGADO; - if(P_PULS == 0) { - e_PULS = PULS_FLC1; - } + if (Pulsador == PRESS) { + state = S_DETECT_PULS; + } + break; + + case S_DETECT_PULS: + if (Pulsador == PRESS && deb_time < 20){ + hab=1; + state = S_DEB1; + } else if(Pulsador == NO_PRESS) + state = S_NO_PRESS; break; - case PULS_FLC1: - StateOut = APAGADO; - if(P_PULS == 0) { - e_PULS = PULS_A; - StateOut= ENCENDIDO; - }else if(P_PULS == 1) { - e_PULS = PULS_NA; + + case S_DEB1: + if (deb_time == 0 && hab == 1) { + state = S_PRESS; + hab=0; + } break; - case PULS_A: - StateOut =APAGADO; - if(P_PULS == 1) { - e_PULS = PULS_FLC2; - } + + case S_PRESS: + out = 1; + state= S_RETENTION; break; - case PULS_FLC2: - StateOut = APAGADO; - if(P_PULS == 0) { - e_PULS = PULS_A; - }else if(P_PULS == 1) { - e_PULS = PULS_NA; - + + case S_RETENTION: + out = 0; + if (Pulsador == NO_PRESS) + state = S_DEB2; + break; + + case S_DEB2: + if (deb_time == 20) + hab=1; + else if (deb_time == 0 && hab == 1) { + state = S_NO_PRESS; + hab=0; } break; } - return StateOut; + return out; } - -
--- a/antirrebote.h Thu May 03 23:32:45 2018 +0000 +++ b/antirrebote.h Tue Jun 12 17:11:31 2018 +0000 @@ -2,7 +2,6 @@ #define _ANTIRREBOTE_H_ #include "mbed.h" -enum{NO_APRETADO,ENCENDIDO=0, APRETADO, APAGADO = 1}; typedef int pinEstado_t; @@ -14,14 +13,12 @@ ~AntReb(); pinEstado_t antiRebote(); - void setPin(DigitalIn pin); + void DebTime(); private: - - - char e_PULS; - char P_PULS; + char state, Pulsador, deb_time, hab; + pinEstado_t out; };