A simple counter using onboard LEDs, plus a screen and a button to display current state and switch between counting in Binary and Graycode. Thanks to Mr. Lerche for the GreyCode counter.
Revision 0:de4ce57020b0, committed 2011-06-29
- Comitter:
- Eggotape
- Date:
- Wed Jun 29 13:22:04 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r de4ce57020b0 DebouncedIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.cpp Wed Jun 29 13:22:04 2011 +0000 @@ -0,0 +1,93 @@ +#include "DebouncedIn.h" +#include "mbed.h" + +/* + * Constructor + */ +DebouncedIn::DebouncedIn(PinName in) + : _in(in) { + + // reset all the flags and counters + _samples = 0; + _output = 0; + _output_last = 0; + _rising_flag = 0; + _falling_flag = 0; + _state_counter = 0; + + // Attach ticker + _ticker.attach(this, &DebouncedIn::_sample, 0.005); +} + +void DebouncedIn::_sample() { + + // take a sample + _samples = _samples >> 1; // shift left + + if (_in) { + _samples |= 0x80; + } + + // examine the sample window, look for steady state + if (_samples == 0x00) { + _output = 0; + } + else if (_samples == 0xFF) { + _output = 1; + } + + + // Rising edge detection + if ((_output == 1) && (_output_last == 0)) { + _rising_flag++; + _state_counter = 0; + } + + // Falling edge detection + else if ((_output == 0) && (_output_last == 1)) { + _falling_flag++; + _state_counter = 0; + } + + // steady state + else { + _state_counter++; + } + + // update the output + _output_last = _output; + +} + + + +// return number of rising edges +int DebouncedIn::rising(void) { + int return_value = _rising_flag; + _rising_flag = 0; + return(return_value); +} + +// return number of falling edges +int DebouncedIn::falling(void) { + int return_value = _falling_flag; + _falling_flag = 0; + return(return_value); +} + +// return number of ticsk we've bene steady for +int DebouncedIn::steady(void) { +return(_state_counter); +} + +// return the debounced status +int DebouncedIn::read(void) { + return(_output); +} + +// shorthand for read() +DebouncedIn::operator int() { + return read(); +} + +
diff -r 000000000000 -r de4ce57020b0 DebouncedIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.h Wed Jun 29 13:22:04 2011 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" + + class DebouncedIn { + public: + DebouncedIn(PinName in); + + int read (void); + operator int(); + + int rising(void); + int falling(void); + int steady(void); + + private : + // objects + DigitalIn _in; + Ticker _ticker; + + // function to take a sample, and update flags + void _sample(void); + + // counters and flags + int _samples; + int _output; + int _output_last; + int _rising_flag; + int _falling_flag; + int _state_counter; + + }; + \ No newline at end of file
diff -r 000000000000 -r de4ce57020b0 TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Wed Jun 29 13:22:04 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#e4cb7ddee0d3
diff -r 000000000000 -r de4ce57020b0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jun 29 13:22:04 2011 +0000 @@ -0,0 +1,33 @@ +#include "mbed.h" +#include "TextLCD.h" +#include "DebouncedIn.h" + +BusOut led(LED4, LED3, LED2, LED1); +unsigned char i; +TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 +DebouncedIn setbutton(p5); + +int main() { + int state; + state = 0; + i = 0; + while(1) { + if (setbutton.rising()) { + state = !state; + } + if (state == 0) { + lcd.printf("Binary: %u\n", i); + led = i; + } + else if (state == 1) { + lcd.printf("Graycode: %u\n", i); + led = i^(i>>1); + } + wait(1); + i = i+1; + if (i == 16) { + i = 0; + } + lcd.cls(); + } +} \ No newline at end of file
diff -r 000000000000 -r de4ce57020b0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jun 29 13:22:04 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912