Skip to content

Commit 1df371a

Browse files
author
Qinghao Shi
authored
Merge pull request #90 from maciejbocianski/move_digital_in_out_examples
add Digital In Out examples
2 parents 10693fb + a7f876d commit 1df371a

File tree

8 files changed

+123
-0
lines changed

8 files changed

+123
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# DigitalInOut example
2+
3+
You can use the DigitalInOut API to both read and write to a digital pin:
4+
5+
1. Use the `output()` and `input()` function calls to switch modes.
6+
1. Use just like you would DigitalIn or DigitalOut.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
DigitalInOut mypin(LED1);
9+
10+
int main()
11+
{
12+
// check that mypin object is initialized and connected to a pin
13+
if (mypin.is_connected()) {
14+
printf("mypin is initialized and connected!\n\r");
15+
}
16+
17+
// Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
18+
mypin.mode(PullNone);
19+
20+
while (1) {
21+
// write to pin as output
22+
mypin.output();
23+
mypin = !mypin; // toggle output
24+
ThisThread::sleep_for(500);
25+
26+
// read from pin as input
27+
mypin.input();
28+
printf("mypin.read() = %d \n\r", mypin.read());
29+
ThisThread::sleep_for(500);
30+
}
31+
}

APIs_Drivers/DigitalIn_ex_1/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DigitalIn example
2+
3+
This example shows how to use digital input to monitor push button state.

APIs_Drivers/DigitalIn_ex_1/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
DigitalIn mypin(SW2); // change this to the button on your board
9+
DigitalOut myled(LED1);
10+
11+
int main()
12+
{
13+
// check mypin object is initialized and connected to a pin
14+
if (mypin.is_connected()) {
15+
printf("mypin is connected and initialized! \n\r");
16+
}
17+
18+
// Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
19+
mypin.mode(PullNone);
20+
21+
// press the button and see the console / led change
22+
while (1) {
23+
printf("mypin has value : %d \n\r", mypin.read());
24+
myled = mypin; // toggle led based on value of button
25+
ThisThread::sleep_for(250);
26+
}
27+
}

APIs_Drivers/DigitalIn_ex_2/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DigitalIn example
2+
3+
This example shows how to use digital inputs to implement logical functions.

APIs_Drivers/DigitalIn_ex_2/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
DigitalIn a(D0);
9+
DigitalIn b(D1);
10+
DigitalOut z_not(LED1);
11+
DigitalOut z_and(LED2);
12+
DigitalOut z_or(LED3);
13+
DigitalOut z_xor(LED4);
14+
15+
int main()
16+
{
17+
while (1) {
18+
z_not = !a;
19+
z_and = a && b;
20+
z_or = a || b;
21+
z_xor = a ^ b;
22+
}
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DigitalOut example
2+
3+
This example shows how to use DigitalOut to drive the LED.

APIs_Drivers/DigitalOut_ex_1/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
8+
DigitalOut myled(LED1);
9+
10+
int main()
11+
{
12+
// check that myled object is initialized and connected to a pin
13+
if (myled.is_connected()) {
14+
printf("myled is initialized and connected!\n\r");
15+
}
16+
17+
// Blink LED
18+
while (1) {
19+
myled = 1; // set LED1 pin to high
20+
printf("myled = %d \n\r", (uint8_t)myled);
21+
ThisThread::sleep_for(500);
22+
23+
myled.write(0); // set LED1 pin to low
24+
printf("myled = %d \n\r", myled.read());
25+
ThisThread::sleep_for(500);
26+
}
27+
}

0 commit comments

Comments
 (0)