Amruta (talk) 11:24, 17 March 2015 (IST)


In this tutorial we will be looking at accessing the GPIO of ARM. In this tutorial series we will be using LPC1768 cortex ARM M3. We will be simply reading a switch and making a LED act accordingly.


Schematic

Code

In this code, we will demonstrate LED and Switch interfacing with the LPC1768.

We will monitor the switch condition and will glow LED when switch is pressed.

#include "lpc17xx.h"   // NXP Library containing all port abd register definations
#include "stdutils.h"  // Explore Embedded library
 
#define LED		29	 //P1.29
#define SWITCH         28	 //P1.28
int main()
{
        SystemInit();
 
	LPC_PINCON->PINSEL3 = 0x00;			   // Configure Pin as GPIO
	util_BitSet(LPC_GPIO1->FIODIR,LED);	   	   // Set pin direction as output
	util_BitClear (LPC_GPIO1->FIODIR,SWITCH);	   // Set pin direction as input
 
	while(1)
	{		
		if (util_IsBitCleared (LPC_GPIO1->FIOPIN,SWITCH))	// check if switch is pressed
			util_BitSet (LPC_GPIO1->FIOSET,LED);	   // turn on LED	
		else
			util_BitSet (LPC_GPIO1->FIOCLR,LED);	// turn off LED	
	}
}

Basics

lpc17xx header file

This is peripheral access header file for NXP LPC17xx device series. It contains all required register and peripheral definitions which one requires in order to access any peripheral.

If you take a look at this header file, you will notice that all registers associated with a peripheral are clubbed together in a structure. So to access a register we need to use the respective structure


e.g. LPC_PINCON->PINSELx for PINSEL register,
LPC_GPIOx->FIOSET for GPIO register, etc.

PINSEL and GPIO

PINCONNECT
PINSELx (Pin function select register)

Before using any port pin, it is required to configure it function.

The PINSEL registers control the functions of device pins as shown below :

PINSEL0 to PINSEL9 values Function
00 Primary (default) function, typically GPIO port
01 First alternate function
10 Seconnd alternate function
11 Third alternate function

To select a function of a pin, 2 bits are required. So there are 2 PINSEL registers for each port, one for pins 0 to 15 and another for pins 16 to 31.

GPIO

FIODIR(Fast GPIO Port Direction control register)

This register individually controls the direction of each port pin.

Values Direction
0 Input
1 Output
FIOPIN (Fast Port Pin value register )

The current state of digital port pins can be read from this register, regardless of pin direction or alternate function selection (as long as pins are not configured as an input to ADC).

Writing to this register places corresponding values on digital port pins

FIOSET (Fast Port Output Set register ) and FIOCLR (Fast Port Output Clear register )

These registers controls the state of output pins

Values FIOSET FIOCLR
0 No Effect No Effect
1 Sets High on Pin Sets Low on Pin
FIOMASK (Fast Mask register )

Writes, sets, clears, and reads to port (done via writes to FIOPIN, FIOSET, and FIOCLR, and reads of FIOPIN) alter or return only the bits enabled by zeros in this register.

As its default value is zero, no need to configure it here.