You are viewing an older revision! See the latest version

Digital inputs and outputs

Exercise 1: mbed Hello World

Follow instructions on /handbook/Creating-a-program.

Modify the program to flash other 3 LEDs.

Exercise 2: Using external LEDs

We will be using two schematics in this exercise to compare two different digital output modes. Digital input modes will also be compared.

Note

You will need two LEDs, two 330 Ohm resistors, mechanical switch, some wires, breadboard and of course mbed board to complete this exercise.

Standard digital outputs

Connect the components on the breadboard according to the following schematics:

Conventional digital outputs

Examine the following code:

#include "mbed.h"

DigitalOut green(p5);
DigitalOut red(p6);

int main() {
    while(1) {
        green = 1;
        red = 0;
        wait(0.5);
        green = 0;
        red = 1;
        wait(0.2);
    }
}

Notice that digital output pins (p5 and p6) are declared using DigitalOut class, which does not (yet) provide the option for setting the output mode. The default mode used in this case is standard (conventional) mode, which drives the output pin from internal power supply.

Digital outputs in open drain mode

Connect the components on the breadboard according to the following schematics:

Digital outputs in open drain mode

Examine the following code:

#include "mbed.h"

DigitalInOut green(p22, PIN_OUTPUT, OpenDrain, 1); // The first way of digital output configuration
DigitalInOut red(p21); // The second way of digital output configuration

int main() {
    red.output(); // Addition 1 for the second way of digital output configuration
    red.mode(OpenDrain);  // Addition 2 for the second way of digital output configuration
    red = 1; // Turn red LED off (inverse logic)
    while(1) {
        green = 1;
        red = 0;
        wait(0.5);
        green = 0;
        red = 1;
        wait(0.2);
    }
}

In this way the digital output pins are configured as open drain outputs. A class used for this purpose is DigitalInOut, since the standard class DigitalOut does not (yet) have the option for open drain mode. DigitalInOut class has two constructors, and both of them are used and commented in the example code above.


All wikipages