Tarea3 procesadores: PID en FRDM-KL25Z con botones incrementales
Dependencies: TextLCD-modificad mbed
DebouncedIn.cpp@0:6a56212dd414, 2015-04-08 (annotated)
- Committer:
- obifandres
- Date:
- Wed Apr 08 15:19:22 2015 +0000
- Revision:
- 0:6a56212dd414
Tarea 3 procesadores: PID en FKL26Z con botones de incrementales
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
obifandres | 0:6a56212dd414 | 1 | #include "DebouncedIn.h" |
obifandres | 0:6a56212dd414 | 2 | #include "mbed.h" |
obifandres | 0:6a56212dd414 | 3 | |
obifandres | 0:6a56212dd414 | 4 | /* |
obifandres | 0:6a56212dd414 | 5 | * Constructor |
obifandres | 0:6a56212dd414 | 6 | */ |
obifandres | 0:6a56212dd414 | 7 | DebouncedIn::DebouncedIn(PinName in) |
obifandres | 0:6a56212dd414 | 8 | : _in(in) { |
obifandres | 0:6a56212dd414 | 9 | |
obifandres | 0:6a56212dd414 | 10 | // reset all the flags and counters |
obifandres | 0:6a56212dd414 | 11 | _samples = 0; |
obifandres | 0:6a56212dd414 | 12 | _output = 0; |
obifandres | 0:6a56212dd414 | 13 | _output_last = 0; |
obifandres | 0:6a56212dd414 | 14 | _rising_flag = 0; |
obifandres | 0:6a56212dd414 | 15 | _falling_flag = 0; |
obifandres | 0:6a56212dd414 | 16 | _state_counter = 0; |
obifandres | 0:6a56212dd414 | 17 | |
obifandres | 0:6a56212dd414 | 18 | // Attach ticker |
obifandres | 0:6a56212dd414 | 19 | _ticker.attach(this, &DebouncedIn::_sample, 0.005); |
obifandres | 0:6a56212dd414 | 20 | } |
obifandres | 0:6a56212dd414 | 21 | |
obifandres | 0:6a56212dd414 | 22 | void DebouncedIn::_sample() { |
obifandres | 0:6a56212dd414 | 23 | |
obifandres | 0:6a56212dd414 | 24 | // take a sample |
obifandres | 0:6a56212dd414 | 25 | _samples = _samples >> 1; // shift left |
obifandres | 0:6a56212dd414 | 26 | |
obifandres | 0:6a56212dd414 | 27 | if (_in) { |
obifandres | 0:6a56212dd414 | 28 | _samples |= 0x80; |
obifandres | 0:6a56212dd414 | 29 | } |
obifandres | 0:6a56212dd414 | 30 | |
obifandres | 0:6a56212dd414 | 31 | // examine the sample window, look for steady state |
obifandres | 0:6a56212dd414 | 32 | if (_samples == 0x00) { |
obifandres | 0:6a56212dd414 | 33 | _output = 0; |
obifandres | 0:6a56212dd414 | 34 | } |
obifandres | 0:6a56212dd414 | 35 | else if (_samples == 0xFF) { |
obifandres | 0:6a56212dd414 | 36 | _output = 1; |
obifandres | 0:6a56212dd414 | 37 | } |
obifandres | 0:6a56212dd414 | 38 | |
obifandres | 0:6a56212dd414 | 39 | |
obifandres | 0:6a56212dd414 | 40 | // Rising edge detection |
obifandres | 0:6a56212dd414 | 41 | if ((_output == 1) && (_output_last == 0)) { |
obifandres | 0:6a56212dd414 | 42 | _rising_flag++; |
obifandres | 0:6a56212dd414 | 43 | _state_counter = 0; |
obifandres | 0:6a56212dd414 | 44 | } |
obifandres | 0:6a56212dd414 | 45 | |
obifandres | 0:6a56212dd414 | 46 | // Falling edge detection |
obifandres | 0:6a56212dd414 | 47 | else if ((_output == 0) && (_output_last == 1)) { |
obifandres | 0:6a56212dd414 | 48 | _falling_flag++; |
obifandres | 0:6a56212dd414 | 49 | _state_counter = 0; |
obifandres | 0:6a56212dd414 | 50 | } |
obifandres | 0:6a56212dd414 | 51 | |
obifandres | 0:6a56212dd414 | 52 | // steady state |
obifandres | 0:6a56212dd414 | 53 | else { |
obifandres | 0:6a56212dd414 | 54 | _state_counter++; |
obifandres | 0:6a56212dd414 | 55 | } |
obifandres | 0:6a56212dd414 | 56 | |
obifandres | 0:6a56212dd414 | 57 | // update the output |
obifandres | 0:6a56212dd414 | 58 | _output_last = _output; |
obifandres | 0:6a56212dd414 | 59 | |
obifandres | 0:6a56212dd414 | 60 | } |
obifandres | 0:6a56212dd414 | 61 | |
obifandres | 0:6a56212dd414 | 62 | |
obifandres | 0:6a56212dd414 | 63 | |
obifandres | 0:6a56212dd414 | 64 | // return number of rising edges |
obifandres | 0:6a56212dd414 | 65 | int DebouncedIn::rising(void) { |
obifandres | 0:6a56212dd414 | 66 | int return_value = _rising_flag; |
obifandres | 0:6a56212dd414 | 67 | _rising_flag = 0; |
obifandres | 0:6a56212dd414 | 68 | return(return_value); |
obifandres | 0:6a56212dd414 | 69 | } |
obifandres | 0:6a56212dd414 | 70 | |
obifandres | 0:6a56212dd414 | 71 | // return number of falling edges |
obifandres | 0:6a56212dd414 | 72 | int DebouncedIn::falling(void) { |
obifandres | 0:6a56212dd414 | 73 | int return_value = _falling_flag; |
obifandres | 0:6a56212dd414 | 74 | _falling_flag = 0; |
obifandres | 0:6a56212dd414 | 75 | return(return_value); |
obifandres | 0:6a56212dd414 | 76 | } |
obifandres | 0:6a56212dd414 | 77 | |
obifandres | 0:6a56212dd414 | 78 | // return number of ticsk we've bene steady for |
obifandres | 0:6a56212dd414 | 79 | int DebouncedIn::steady(void) { |
obifandres | 0:6a56212dd414 | 80 | return(_state_counter); |
obifandres | 0:6a56212dd414 | 81 | } |
obifandres | 0:6a56212dd414 | 82 | |
obifandres | 0:6a56212dd414 | 83 | // return the debounced status |
obifandres | 0:6a56212dd414 | 84 | int DebouncedIn::read(void) { |
obifandres | 0:6a56212dd414 | 85 | return(_output); |
obifandres | 0:6a56212dd414 | 86 | } |
obifandres | 0:6a56212dd414 | 87 | |
obifandres | 0:6a56212dd414 | 88 | // shorthand for read() |
obifandres | 0:6a56212dd414 | 89 | DebouncedIn::operator int() { |
obifandres | 0:6a56212dd414 | 90 | return read(); |
obifandres | 0:6a56212dd414 | 91 | } |