Sample Code mostly for the Mbed

From Eugene Maker Space
Sample Code
Jump to: navigation, search


Simple Hello World

#include "mbed.h"

[[Digital Out]] myled(LED1);

int main() {
    while(1) {
        myled = 1;
        puts("Hello!");
        wait(0.2);
        myled = 0;
        wait(1);
    }
}

Servo Control

This lets you move a servo back and forth with s and d keys.

// 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

[[Pwm Out]] 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);
    }
}

Analog In

This lets you measure a voltage on a pin.

#include "mbed.h"

[[Analog In]] 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);
    }
}

Beeper Sweep

This lets you sweep a Piezo buzzer's frequency though a range with the s and d keys.

// 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

[[Pwm Out]] 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);
    }
}

LED Brightness Sweep

Cycles the LED brightness smoothly up and down through all 16 levels using the SPI controlled MAX6957ANI+.

#include "mbed.h"

SPI spi(p5, p6, p7); // mosi, miso, sclk
[[Digital Out]] 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 <tt>= 1 && cur <</tt> 15) || (inc == -1 && cur >=0); cur+=inc) {
        write(GLBCUR, cur);
        wait(.05);
    }
    inc *=-1; cur+=inc;
    }
    write(DISTEST, 0); // Disable display test
}

Ether Port Test

Prints the MAC address of any received packets

#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);
    }
}

QC-5000 Test Code

Exercise the Proto-board IO.

// Control the AMC2500 steppers

#include "mbed.h"

// Defines

#define YLEFT  p30
#define YRIGHT p29
#define XFRONT p28
#define XREAR  p27
#define XCLK   p26
#define XDIR   p25
#define YCLK   p24
#define YDIR   p23
#define ZAX    p21
#define RELAY   p22
#define MOSI    p5
#define MISO    p6
#define SCLK    p7
#define STCP    p8  // 74LS595 Latch outputs
#define SCAL    36.4
#define AIN     p20
#define INC     1
#define UP      2
#define CS      4
#define MSK     7

[[Digital In]] yl(YLEFT);
[[Digital In]] yr(YRIGHT);
[[Digital In]] xf(XFRONT);
[[Digital In]] xr(XREAR);
[[Digital Out]] xdir(XDIR);
[[Digital Out]] ydir(YDIR);
[[Digital Out]] *adir = &xdir;
[[Digital In Out]] zax(ZAX);
[[Digital In Out]] relay(RELAY);
SPI spi(MOSI, MISO, SCLK);
[[Digital Out]] latch(STCP);
[[Analog In]] spinv(AIN);

Serial pc(USBTX, USBRX); // tx, r
int ramp(float target, int step, float delay);
int write(unsigned int data);
int vadj(int up);

[[Pwm Out]] xstep(XCLK);
[[Pwm Out]] ystep(YCLK);
[[Pwm Out]] *astep= &xstep;

float sps = 0; // start with a slow steps per second
unsigned int   data =8;

int main() {
    yl.mode([[Pull Up]]);
    yr.mode([[Pull Up]]);
    xf.mode([[Pull Up]]);
    xr.mode([[Pull Up]]);
    zax = 1; zax.output(); zax.mode([[Open Drain]]);
    relay = 1; relay.output(); relay.mode([[Open Drain]]);
    spi.format(8,0);
    spi.frequency(1000000);

    //  ramp(50, 10, 0.1);
    xstep.period(1/sps);            // set a starting steps per second
    xstep.pulsewidth(0.5/sps);      // and a 50% Duty cycle
    write(data);                       // Clear 74LS595 data so all controls are off
    pc.printf("Press 's' for slower, or 'd' for faster, 'c' to change direction.\n\r");

    while(1) {
        char c = pc.getc();
        // Slow down current axis speed
        if((c == 's')) {
            sps -= 50;
            // sps = 0.0005;
            if (sps < 0) sps = 0;
            astep->period(1/sps);
            astep->pulsewidth(0.5/sps);
        }
        // Speed up current axis speed
        if((c == 'd')) {
            sps += 50;
            // sps = 0.0024;
            astep->period(1/sps);
            astep->pulsewidth(0.5/sps);
        }
        // Change current axis direction
        if (c == 'c') {
            float s= sps;

            // pc.printf("Ramping down\n");            
            ramp(0, 200, 0.01);
            *adir = *adir ? 0 : 1;
            ramp(s, 200, 0.01);
        }
        // Stop current axis
        if (c == ' ') {
            ramp(0, 200, 0.01);
            astep->pulsewidth(0);
        }
        if (c == 'l') {
            int p=0;
            if (!yl) { pc.printf("Left "); p=1; }
            if (!yr) { pc.printf("Right "); p=1; }
            if (!xf) { pc.printf("Front "); p=1; }
            if (!xr) { pc.printf("Rear"); p=1; }
            if (p)   { printf("\n\r"); p=0; }
        }
        // Toggle Realay
        if (c == 'r') {
            relay = relay ? 0 : 1;
        }
        // Toggle Motor
        if (c == 'm') {
            if (8 & data) {
                data = ~8&data;
                write(data);
                wait(0.1);
                pc.printf("Spindle Voltage = %f V \n\r", SCAL*spinv);
            } else {
                data = 8|data;
                write(data);
            }
       }
       // Decrease? current Spindle voltage and report.
        if (c == 'n') {
            vadj(0);
            wait(0.1);
            pc.printf("Spindle Voltage = %f V \n\r", SCAL*spinv);
        }
        // Increase? current Spindle voltage and report.
       if (c == ',') {
            vadj(1);
            wait(0.1);
            pc.printf("Spindle Voltage = %f V \n\r", SCAL*spinv);
        }
        // Toggle Vacume
        if (c == 'v') {
            data = (16 & data) ? (~16)&data : 16|data;
            write(data);
        }
        // Toggle Z axis
        if (c == 'z') {
            zax = zax ? 0 : 1;
        }
        // Swich movment to other (Alternate) Axis
        if (c == 'a') {
            float s = sps;
            ramp(0, 200, 0.01);
            astep->pulsewidth(0);
            if (astep == &xstep) { astep = &ystep; adir = &ydir; }
            else { astep = &xstep; adir = &xdir; }
            ramp(s, 200, 0.01);
        }
        // Print current "speed"
        pc.printf("SPS= %f\n\r", sps);
    }
}

int ramp(float target, int step, float delay) {
    target = target < 0 ? 0 : target;
    if (target > sps) step = abs(step);
    else step = -abs(step);

    while( abs(sps - target) > 0.1) {
        wait(delay);
        if (abs(target - sps) >= abs(step)) sps += step;
        else sps = target;
        astep->period(1/sps);
        astep->pulsewidth(0.5/sps);
        // pc.printf("SPS = %f\n\r", sps);
    }
        return 0;
}

int write(unsigned int data) {
    // pc.printf("Writing: %0X\n\r", data);
    latch = 0;
    spi.write(data);
    // wait(0.1);
    latch = 1; // latch = 0;
    return 1;
}

int vadj(int up) {
    data &= ~MSK;
    write(INC|data);
    // wait(.1);
    write(INC|(up ? UP:0)|data);
    // wait(.1);
    write((up ? UP:0)|data);
    // wait(.1);
    write(INC|(up ? UP:0)|data);
    // wait(.1);
    write((up ? UP:0)|data);
    // wait(.1);
    write(INC|(up ? UP:0)|data);
    // wait(.1);
    write((up ? UP:0)|data);
    // wait(.1);
    write(CS|data);
    // wait(.1);
    // data |= INC;
    write(data);
    return 1;
}

-- Main.ClifCox - 19 Nov 2011