You are not logged in

Log in into our community

Log in so you acess some hidden content

* Fields are required.

Register into our community!

Sign up now for some good content

* Fields are required.

Lost something?

Enter your username or email to reset your password.

* Fields are required.

General Port Input Output

Posted on : Sun , 02 2014 by : virusi

Theory on GPIO :

One of the most important features of the microcontroller are the input/output pins used for connection with peripherals. Depending on the price of the microcontroller the number of inputs/output pins can vary from microcontroller to microcontroller. Not all IO pins are bidirectional if you want to know if a specific pin is a bidirectional one than you should check microcontroller’s datasheet.

For practical reasons, many I/O pins have two or three functions. If a pin is used as any other function, it may not be used as a general purpose input/output pin. At a given time a pin can only act as either input or output. This means that each pin has distinct direction state. A pin cannot be used for input and output at the same time. You must actually reset the direction state of that pin before attempting to read the pin’s logical state.

The pin’s direction can be controlled by setting an appropriate bit in a direction register. Direction registers are hardware registers in the microcontroller that are also memory mapped into the device’s RAM address space. Rather than accessing the registers by their absolute addresses, the programmer would use the addresses logical name.

If an appropriate bit in the direction register is set and the value in the appropriate PORT register is set, this will cause the voltage on the associated pin to be set to +5V. Setting a bit to low in PORT register causes the output pin to be set to ground (0 volts).

What is the maximum current that can be outputted from a pin? Everything depends on your microcontroller if you are interested in specific numbers than you should check microcontroller’s datasheet. For NXP LPC1768 microcontroller the maximum current that can be outputted from a pin is 50 mA.

If you are interested in the logic value of a specific pin than you should configure your PIN as input by clear the appropriate bit in the direction register and read the value from the PIN register.

What is the maximum input current that can be driven into a pin? The same you should consult microcontroller datasheet. For NXP LPC1768 microcontroller the maximum input current that can be driven by a pin is 40 mA.

LPC1768 Practical Example :

Let’s take a look at a small example. In this example will connect a button to pin and some LED’S to other pins. The main idea is to configure the pin where the button is connected as input and pins where the Led’s are connected as output. When the button is pressed the PIN register will change his value and then we should turn on a LED by setting a specific value in PORT register.
Fig.1 Example Schematic

Let’s analyze the software flow.


1. Configure PIN’S

At init we need to configure PIN 2.11 as input because the button is connected to that PIN and as output the PIN’S form 2.00 to 2.07 :

/* Configure P 2.11 as input (Set 0 to 11 position in the FIODIR register) */
LPC_GPIO2->FIODIR &= ~(1<<11UL); 
/* Configure P 2.00... P 2.07 as output */
LPC_GPIO2->FIODIR |= ((1UL<< 0)|(1UL<< 1)|(1UL<< 2)|(1UL<< 3)
                      |(1UL<< 4)|(1UL<< 5)|(1UL<< 6)|(1UL<< 7)) ;
/* turn off all the LEDs */            
LPC_GPIO2->FIOCLR = 0x000000FF;


1. Read Button value and set current led.

  pinval = ( (LPC_GPIO2->FIOPIN >> 11)&0x01 );
  /* read P 2.11 value */
    /* turn on only current LED */
    LPC_GPIO2->FIOPIN = (1UL< 7) 
      currentLed = 0UL;	


If you are interested in the full example then download GPIO example.

Last updated on Mon , 03 2014

This page is waiting for your comment.

Share and Leave a comment.

You must be logged in to post a comment.

Back to Top