Simple example to drive a servo on pin 1. A serial input at 57600 baud send a percentage from 0 to 100 (followed by whitespace) and a pulse goes out to drive an RC servo to one end of travel to another. Example for my DDJ blog at http://www.ddj.com/embedded.
Revision 0:10d20e0f582e, committed 2014-05-12
- Comitter:
- wd5gnr
- Date:
- Mon May 12 01:28:06 2014 +0000
- Commit message:
- First commit;
Changed in this revision
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
meter.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 10d20e0f582e mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon May 12 01:28:06 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file
diff -r 000000000000 -r 10d20e0f582e meter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meter.cpp Mon May 12 01:28:06 2014 +0000 @@ -0,0 +1,30 @@ + +#include "mbed.h" + +PwmOut out0(P0_8); + + +Serial host(USBTX, USBRX); + + + + + +int main(void) +{ + host.baud(57600); // TODO read a digital input to select high/low baud + out0.period_ms(20); + out0.pulsewidth_us(2600); + + while(1) + { + unsigned int n=200; + host.scanf("%d",&n); // read from PC + if (n>100) continue; // make sure in range 0-100 + n=((unsigned int) (1600.0*n/100.0)); // get a number from 0 to 1600 + n=1600-n; // reverse motion direction so now it is 1600 to 0 + out0.pulsewidth_us(n+1000); // add 1000 so that total number is 1000 to 2600 (or 1mS to 2.6mS) + } +} + +