PortIn

mbed OS 2 and mbed OS 5

This is the handbook for mbed OS 2. If you’re working with mbed OS 5, please see the new handbook and API References. For the latest PortIn API, please see PortIn.

The PortIn interface is used to read an underlying GPIO port as one value. This is much faster than BusIn as you can read a port in one go, but much less flexible as you are constrained by the port and bit layout of the underlying GPIO ports.

A mask can be supplied so only certain bits of a port are used, allowing other bits to be used for other interfaces.

Hello World!

Import program

00001 // Switch on an LED if any of mbed pins 21-26 is high
00002  
00003 #include "mbed.h"
00004  
00005 PortIn     p(Port2, 0x0000003F);   // p21-p26
00006 DigitalOut ind(LED4);
00007  
00008 int main() {
00009     while(1) {
00010         int pins = p.read();
00011         if(pins) {
00012             ind = 1;
00013         } else {
00014             ind = 0;
00015         }
00016     }
00017 }

API

Import library

Public Member Functions

  PortIn (PortName port, int mask=0xFFFFFFFF)
  Create an PortIn , connected to the specified port.
int  read ()
  Read the value currently output on the port.
void  mode (PinMode mode)
  Set the input pin mode.
  operator int ()
  A shorthand for read()

Interface

The PortIn Interface can use any pin with a blue label, as long as all the pins used are in the same GPIO port

/media/uploads/chris/pinout-thumbnails.jpg
See the Pinout page for more details