Software para realizar control PID en tarjeta Freescale KL25Z con ENCODER y Display 16x2

Dependents:   PID_Encoder

Committer:
cmorab
Date:
Wed Nov 20 17:32:26 2013 +0000
Revision:
0:13e5c41f8363
Software para realizar control PID en tarjeta Freescale KL25Z con ENCODER  y Display 16x2;

Who changed what in which revision?

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