este código permite cambiar los valores de las variables para el PID mediante un enconder, ademas pulsando el botón del mismo enconder, cambia la posición del cursor y se genera un sonido.

Dependencies:   QEI TextLCD mbed

Fork of pid-TAREA4-ENCODER by Wilmar Cr

Committer:
lopjohn_26
Date:
Tue Apr 08 19:02:44 2014 +0000
Revision:
1:9ca362d07dd0
Parent:
0:dd68101b1b99
sonido del bot?n encoder

Who changed what in which revision?

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