ARM is 32-bit architecture and provides 32 bit GPIO ports. In this tutorial, we are going to cover about GPIO pins, how to use them, how to configure GPIO registers and an example how microcontroller can interact with outside world with GPIO pins. For this tutorial we are taking LPC1769 as reference and with the use of CMSIS library.
In order to get started with GPIO ports, we need to look into the five ‘registers’ that controls the port pins: FIODIR, FIOMASK, FIOPIN, FIOSET and FIOCLR. Each of these registers is explained in detail below with some basic examples of how they work
This word accessible register is used to control the direction of the pins when they are configured as GPIO port pins. Direction bit for any pin must be set according to the pin functionality. For example, if we want to use our GPIO pin to send signals ‘out’ from the microcontroller to some external device, we need to set the pin as output (‘1’).
Consider the below example to understand more about this register. Suppose we want to set 0th pin of port0 as input and 0th pin of port1 as output, the code will be as follows.
LPC_GPIO0->FIODIR=0x0;
LPC_GPIO1->FIODIR=0x1;
The first line shows how to set the 0th pin of port0 as input to receive information from the outside world. In the second line, we set the 0th pin of port1 as output to send information to the outside world.
We can configure more than one pins as input or output by just setting the register values.
LPC_GPIO0->FIODIR=0x0;
LPC_GPIO1->FIODIR=0xFF;
GPIO port output Set register FIOSET (FIO0SET to FIO4SET)
This register is used to produce a HIGH level output at the port pins configured as GPIO in an OUTPUT mode. Writing 1 produces a HIGH level at the corresponding port pins. Writing 0 has no effect. If any pin is configured as an input or a secondary function, writing 1 to the corresponding bit in the FIOxSET has no effect.
GPIO port output Clear register FIOCLR (FIO0CLR to FIO4CLR)
This register is used to produce a LOW level output at port pins configured as GPIO in an OUTPUT mode. Writing 1 produces a LOW level at the corresponding port pin and clears the corresponding bit in the FIOxSET register. Writing 0 has no effect. If any pin is configured as an input or a secondary function, writing to FIOxCLR has no effect.
LPC_GPIO2->FIOCLR = 0x000000FF;
This line of code clear the port2 (configured as output port using FIODIR) lowest 8 bits.
If your GPIO pin is set as Output (set using the FIODIR register), you can use FIOSET to set your GPIO pin to high or FIOCLR to set it to low.
GPIO port Pin value register FIOPIN (FIO0PIN to FIO4PIN)
You can use the FIOPIN register to read the current logic state of every GPIO pin in the pin block regardless whether the pin is configured as input or output.
FIOPIN returns the current state of ALL 32 pins in the pin block, you have to do a little bit of extra work to determine the value of one specific pin. That we can know by using this line of code.
Value = LPC_GPIO0->FIOPIN & 0x03