Recent changes
Order
tag order
CMSIS RTOS
RTOS
mbed Website Releases
Help
mbed NXP LPC1768
Firmware
Homepage
From the mbed microcontroller Handbook.  

PortInOut

/media/uploads/mbedofficial/digitalout_interfaces.png

The PortInOut interface is used to read and write an underlying GPIO port as one value. This is much faster than BusInOut as you can write 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!

Code

// Toggle all four LEDs

#include "mbed.h"

// LED1 = P1.18  LED2 = P1.20  LED3 = P1.21  LED4 = P1.23
#define LED_MASK 0x00B40000

PortInOut ledport(Port1, LED_MASK);

int main() {
    int v = ledport;
    ledport.output();
    while(1) {
        ledport = LED_MASK;
        wait(1);
        ledport = 0;
        wait(1);
    }
}

API

PortInOutA multiple pin digital in/out used to set/read multiple bi-directional pins
Functions
PortInOutCreate an PortInOut, connected to the specified port
writeWrite the value to the output port
readRead the value currently output on the port
outputSet as an output
inputSet as an input
modeSet the input pin mode
operator=A shorthand for write
operator int()A shorthand for read
class PortInOut
A multiple pin digital in/out used to set/read multiple bi-directional pins
PortInOut(PortName port,  
int mask =  0xFFFFFFFF)
Create an PortInOut, connected to the specified port
void write(int value)
Write the value to the output port
int read()
Read the value currently output on the port
void output()
Set as an output
void input()
Set as an input
void mode(PinMode mode)
Set the input pin mode
PortInOut& operator= (int value)
A shorthand for write
operator int()
A shorthand for read

Related




calendar Page history
Last modified 16 Oct 2010, by   user Simon Ford   tag No tags | 4 comments  

4 comments on PortInOut:

30 Mar 2011

Is there a way to change the masks on the fly? It seems like with a port you wouldn't always want to write to all of the pins at once. It doesn't look like this supports the &= or |= operators.

I could just make multiple PortInOut objects for each mask I want to use. But I'm not really sure if changing one from input to output mode would change all of the port objects.

Is there a more elegant solution, or a lower level way to do this on mbed? Creating a separate PortInOut object for each mask doesn't seem ideal.

30 Mar 2011

Hi Jimmy,

Whilst you cant use the port &= x or port |= x shorthand, you can still do port = port & x or port = port | x. You are correct that the mask is only created once, as it impacts also things like pin mode setup etc, so I think it is best that way.

Simon

19 Aug 2011

port Port to connect to (Port0-Port5) mask A bitmask to identify which bits in the port should be included (0 - ignore)

Port0 to 5 share the I/O configuration pins or they have assigned pins? Thanks

12 Nov 2011

if I understand this correctly: if I want to read or write 8 bits at once without finagling bits in 'unnatural' ways my only choice is port 0.4-11. This would be on mbed pins 30,29,8,7,6,5,28,27.

I could do 6 bits at once port 2.0-6 and 4 bits at once on port 0.23-26 or port0.15-18.

Please login to post comments.