File tree Expand file tree Collapse file tree 8 files changed +120
-0
lines changed Expand file tree Collapse file tree 8 files changed +120
-0
lines changed Original file line number Diff line number Diff line change
1
+ # DigitalInOut example
2
+
3
+ The DigitalInOut API can be used to both read and write a digital pin. Use the output() and input() function calls to switch modes and then use just like you would DigitalIn or DigitalOut.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ # DigitalIn example
2
+
3
+ This example shows how to use digital input to monitor push button state.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ # DigitalIn example
2
+
3
+ This example shows how to use digital inputs to implement logical functions.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ # DigitalOut example
2
+
3
+ This example shows how to use DigitalOut to drive the LED.
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments