You are here:
Eugene Maker Space Wiki
>
Sites Web
>
EugMakerSpace
>
ElectronicDoorLock
>
SampleCode
(19 Nov 2011,
ClifCox
)
(raw view)
E
dit
A
ttach
---+!! Sample Code mostly for the Mbed %TOC% ---++ Simple Hello World <verbatim> #include "mbed.h" DigitalOut myled(LED1); int main() { while(1) { myled = 1; puts("Hello!"); wait(0.2); myled = 0; wait(1); } } </verbatim> ---++ Servo Control This lets you move a servo back and forth with s and d keys. <verbatim> // Hello World to sweep a servo through its full range // Control a R/C model servo #include "mbed.h" Serial pc(USBTX, USBRX); // tx, rx PwmOut servo(p21); float pos = 0.0015; int main() { servo.period(0.020); // servo requires a 20ms period pc.printf("Press 's' to turn CCW, 'd' to turn it CW\n\r"); while(1) { char c = pc.getc(); if((c == 's') && (pos > 0.0005)) { pos -= 0.00001; // pos = 0.0005; servo.pulsewidth(pos); } if((c == 'd') && (pos < 0.0024)) { pos += 0.00001; // pos = 0.0024; servo.pulsewidth(pos); } pc.printf("Pulse= %f ms\n\r", pos); } } </verbatim> ---++ Analog In This lets you measure a voltage on a pin. <verbatim> #include "mbed.h" AnalogIn ain(p20); Serial pc(USBTX, USBRX); // tx, rx int main() { while (1){ pc.printf("Analog in= %f V \n\r", 3.306*ain); wait(.2); } } </verbatim> ---++ Beeper Sweep This lets you sweep a Piezo buzzer's frequency though a range with the s and d keys. <verbatim> // Hello World to sweep a beeper through its full range // Control a R/C model beeper #include "mbed.h" Serial pc(USBTX, USBRX); // tx, rx PwmOut beeper(p21); float freq = 1000; // start with 1kHz freq int main() { beeper.period(1/freq); // beeper requires a 20ms period beeper.pulsewidth(0.5/freq); // and a 50% Duty cycle pc.printf("Press 's' to lower, or 'd' to raise the frequency \n\r"); while(1) { char c = pc.getc(); if((c == 's')) { freq -= 50; // freq = 0.0005; beeper.period(1/freq); beeper.pulsewidth(0.5/freq); } if((c == 'd')) { freq += 50; // freq = 0.0024; beeper.period(1/freq); beeper.pulsewidth(0.5/freq); } if (c == 'm') { beeper.pulsewidth(0); } pc.printf("Frequency= %f ms\n\r", freq); } } </verbatim> ---++ LED Brightness Sweep Cycles the LED brightness smoothly up and down through all 16 levels using the SPI controlled !MAX6957ANI+. <verbatim> #include "mbed.h" SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8); #define CONFIG 0x04 #define GLBCUR 0x02 #define DISTEST 0x07 #define PCONF04 0x09 #define PCONF24 0x0E #define PORTS24 0x58 Serial pc(USBTX, USBRX); // tx, rx int read(int reg) { int ret; reg |= 0x80; cs = 0; spi.write(reg); spi.write(0); cs = 1; cs =0; spi.write(0); ret = spi.write(0); cs = 1; return ret; } int write(int add, int data) { cs = 0; spi.write(add); spi.write(data); cs = 1; return 1; } int main() { // Setup the spi for 8 bit data, high steady state clock, // second edge capture, with a 1MHz clock rate spi.format(8,3); spi.frequency(20000000); int ret; write(CONFIG, 1); // Enable the Max 6957 write(PCONF24, 0); // Set p24 - p27 to led drivers write(PORTS24, 0x0f); // Turn on p24 - p27 // write(DISTEST, 1); // Enable display test ret = read(CONFIG); pc.printf("CONFIG register = 0x%X\n\r", ret); ret = read(PCONF24); pc.printf("PCONF24 register = 0x%X\n\r", ret); int cur = 0, inc = 1; while (1) { for(;(inc == 1 && cur <= 15) || (inc == -1 && cur >=0); cur+=inc) { write(GLBCUR, cur); wait(.05); } inc *=-1; cur+=inc; } write(DISTEST, 0); // Disable display test } </verbatim> ---++ Ether Port Test Prints the MAC address of any received packets <verbatim> #include "mbed.h" Ethernet eth; Serial pc(USBTX, USBRX); int main() { char buf[0x600]; while(1) { int size = eth.receive(); if(size > 0) { eth.read(buf, size); pc.printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\n\r", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); pc.printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n\r", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); } wait(1); } } </verbatim> -- Main.ClifCox - 19 Nov 2011
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r2
<
r1
|
B
acklinks
|
V
iew topic
|
Edit
w
iki text
|
M
ore topic actions
Topic revision: r2 - 19 Nov 2011 - 04:50:22 -
ClifCox
Sites
Log In
or
Register
Sites Web
Create New Topic
Index
Search
Changes
Notifications
Statistics
Preferences
Webs
Main
Sandbox
Sites
System
Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Eugene Maker Space Wiki?
Send feedback