General purpose ports in ARM MCU

Microcontrollers aren’t imaginable without interacting with other devices like indicators, input devices, or other off-chip devices. For this, every MCU has I/O pins that are used to interact with the external world.

General-purpose I/O ports can be accessed via registers that provide enhanced features or simply via port registers. Port registers are allocated to the ARM bus so that fast possible timing can be achieved. Control of individual bits is possible using a single instruction. All port registers are byte and half-word addressable. After MCU reset, all I/O ports are set to input.

Let’s take the LPC2000 series ARM microcontroller LPC2148. It has two 32 bit general purpose I/O ports PORT0 and PORT1. PORT0 has 30 pins for input/output operations, where one is only for output. PORT1 has 16 available pins for GPIO.

Each GPIO pins is controlled by four registers:

  • IOPIN – port pin value. Depending on pin direction settings, the value from this register can be read;
  • IOSET – Writing “1” to this register sets the port value to the high state while writing 0 does not affect. This register works in conjunction with IOCLR;
  • IOCLR – This register is opposite to IOSET. Writing the “1” value to it will set the port pin to a low state. Writing zero will not affect;
  • IODIR – this register controls the direction of each port pin. If the bit is is et “1” pin will be as output and if bit “0” then pin will be set to input. After reset, all pins are set to input.

Bellow is a simplified routine of simple Led flasher.

#define IOPINS016 0x10000 //16th pin
int main(void)
{
sysInit(); //system init routine: PLL, MAM- not discussed here
IO0CLR = (1<<IOPINS016); // clear 16th pin output
IO0SET = (1<<IOPINS016); // set the ONE to 16th pin
IO0DIR =(1<<IOPINS016); // set the output bit direction
for (;;)
{
IO0CLR = (1<<IOPINS016); // 16th pin Low output
_delay(900000); //delay
IO0SET = (1<<IOPINS016); // 16th pin high
_delay(900000);
}
return 0;
}

One Comment:

  1. Hello.I’d like to thank you for your good tutorial. I have a problem about GPIOs in KEIL UVISION .when I write a simple program which just contains a “main method”, all or some of pins of GPIOs are in high state.
    for example what is shown in debugger:
    FIO3DIR=0X0;
    FIO3SET= 0X0;
    FIO3CLR=0X0;
    FIO3SPIN=0x078000FF;
    I don’t know why.
    Thank you.

Leave a Reply