PortIn
Use the PortIn interface to define which pins of a hardware GPIO port are set as an input and to read the value of those pins. The port name is device specific and defined in the device's PortNames.h
file in the mbed-os/targets
folder.
A bit mask defines which pins of the GPIO port are set as an input (1b
= include, 0b
= ignore). The default mask value is 0xFFFFFFFF
, which sets all pins as an input.
The device-specific PinNames.h
and the respective datasheet or reference manual define the pins associated with a GPIO port.
Note: You can combine pins from different GPIO ports using the BusIn interface. Use PortOut to define which GPIO pins are to be used as digital output.
PortIn class reference
Public Member Functions | |
PortIn (PortName port, int mask=0xFFFFFFFF) | |
Create a PortIn, connected to the specified port. More... | |
int | read () |
Read the value input to the port. More... | |
void | mode (PinMode mode) |
Set the input pin mode. More... | |
operator int () | |
A shorthand for read() More... |
PortIn hello, world
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
// Toggle all four LEDs
// LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED_RED = P1.23
#define LED_MASK 0x00B40000
PortInOut ledport(PortA, LED_MASK);
int main()
{
int v = ledport;
ledport.output();
while (1) {
ledport = LED_MASK;
ThisThread::sleep_for(500);
ledport = 0;
ThisThread::sleep_for(1000);
}
}