
Communication between two serial ports and control direction of motor through the serial terminal.
Dependencies: C12832_lcd mbed-rtos mbed
Revision 0:b8435cfc1bba, committed 2014-04-30
- Comitter:
- bhakti08
- Date:
- Wed Apr 30 19:23:22 2014 +0000
- Commit message:
- The program does the following:; 1. Communication between two serial ports on the MBED; 2. Control ON/OFF of a LED(The pin can be used as direction control of the motor) using the joystick and the serial communication;
Changed in this revision
diff -r 000000000000 -r b8435cfc1bba C12832_lcd.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832_lcd.lib Wed Apr 30 19:23:22 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/dreschpe/code/C12832_lcd/#468cdccff7af
diff -r 000000000000 -r b8435cfc1bba DebouncedIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.cpp Wed Apr 30 19:23:22 2014 +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 b8435cfc1bba DebouncedIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.h Wed Apr 30 19:23:22 2014 +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 b8435cfc1bba main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 30 19:23:22 2014 +0000 @@ -0,0 +1,66 @@ +#include "mbed.h" +#include "rtos.h" +#include "C12832_lcd.h" +#include "DebouncedIn.h" + + +DigitalOut myled(LED1); +DigitalOut dir_pin(LED4); +DebouncedIn dir_fwd(p15); +DebouncedIn dir_rev(p12); +Serial Wifi_Tx(p9,p10); +Serial Wifi_Rx(p28,p27); +Serial pc(USBTX,USBRX); +C12832_LCD lcd; + +bool dir; + +void Serial_Tx(void const *args) { + while (1){ + while (pc.readable()) + { + Wifi_Tx.putc(pc.getc()); + } + Thread::wait(1000); + } +} + +void Serial_Rx(void const *) { + char c; + while (1) { + while (Wifi_Rx.readable()) + { + c = Wifi_Rx.getc(); + lcd.printf("%c",c); + if (c == 'F') + dir = 1; + else if (c == 'R') + dir = 0; + } + Thread::wait (10); + } +} + +void dir_control(void const *) { + while (1) { + if ((dir_fwd || dir) && !dir_rev) { + dir_pin = 1; + dir = 1; + } + else if (dir_rev || !dir) { + dir_pin = 0; + dir = 0; + } + Thread::wait(10); + } + +} +int main() { + Thread serial_tx(Serial_Tx); + Thread serial_rx(Serial_Rx); + Thread Direction_Control(dir_control); + while(1) { + myled = !myled; + Thread::wait(500); + } +}
diff -r 000000000000 -r b8435cfc1bba mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Apr 30 19:23:22 2014 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed-rtos/#f88660a9bed1
diff -r 000000000000 -r b8435cfc1bba mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Apr 30 19:23:22 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file