GPIO in Cortex-M3 LPC1768 Microcontroller is the most basic peripheral. GPIO, General Purpose Input Output is what let’s your microcontroller be something more than a weak auxiliary processor. With it you can interact with physical world, connecting up other devices and turning your microcontroller into something useful. GPIO has two fundamental operating modes, input and output. Input let’s you read the voltage on a pin, to see whether it’s held low(0V) or high(3V) and deal with that information programatically. Output let’s you set the voltage on a pin, again either high or low.
Every pin on LPC1768 can be used as GPIO pin and can be independently set to act as input or output. In next tutorial we’ll get you into how to achieve these goal. I mean reading the status of switch and making LED blink. But for now we only have to look at basics, which is very important to understand before we go and build application. Depending on LPC17xx version the pinout maybe different. Here we’ll focus on 100 pin LPC1768 as an example. Please keep LPC1768 User Manual with you [Chapter: 9, Page No:129]. pins on LPC1768 are divided into 5 groups (PORTs) starting from 0 to 4. Pin naming convention: P0.0 (group 0, pin 0) or (port 0, pin 0). Each pin has 4 operating modes: GPIO(default), 1st alternate function, 2nd alternate function, 3rd alternate function. Almost all GPIO pins are powered automatically so we don’t need to turn them on always. Let’s have a look at details about configuration of these GPIO port pins.
1. Pin Function Setting
The LPC_PINCON register controls operating mode of these pin.
LPC_PINCON –> PINSEL0 [1:0] control PIN 0.0 operating mode.
[Page No: 102, Table: 74].
LPC_PINCON –> PINSEL0 [31:30] control PIN 0.15 operating mode.
LPC_PINCON –> PINSEL1 [1:0] control PIN 0.16 operating mode.
LPC_PINCON –> PINSEL1 [29:28] control PIN 0.30 operating mode.
LPC_PINCON –> PINSEL2 [1:0] control PIN 1.0 operating mode.
LPC_PINCON –> PINSEL2 [31:30] control PIN 1.15 operating mode
LPC_PINCON –> PINSEL3 [1:0] control PIN 1.16 operating mode
LPC_PINCON –> PINSEL3 [31:30] control PIN 1.31 operating mode
LPC_PINCON –> PINSEL9 [25:24] control PIN 4.28 operating mode
LPC_PINCON –> PINSEL9 [27:26] control PIN 4.29 operating mode
NOTE: some register bits are reserved and are not used to control a pin for example,
LPC_PINCON –> PINSEL9 [23:0] are reserved.
LPC_PINCON –> PINSEL9 [31:28] are reserved.
|01||1st alternate function|
|10||2nd alternate function|
|11||3rd alternate function|
To set pin 0.3 as GPIO (set corresponding bit to 00)
LPC_PINCON –> PINSEL0 &= ~ ((1<<7) | (1<<6));
To set pin 0.3 as ADC channel 0.6 (2nd alternate function, set corresponding bit to 10)
LPC_PINCON –> PINSEL0 &= ((1<<7) | (0<<6)); // you may omit (0<<6)
For reference follow [Page No: 117, Table: 80]
2. Pin Direction Setting
Register LPC_GPIOn –> FIODIR [31:0] control the pin input/output, where ‘n’ stands for pin group (0-4). To set a pin as output, set the corresponding bit to ‘1’. To set a pin as input, set the corresponding bit to ‘0’, by default, all pins are set as input (all bits are 0).
To set 0.3 as output
LPC_GPIO –> FIODIR |= (1<<3);
3. Pin is Set as Output
A pin digital high/low setting
LPC_GPIOn –> FIOSET is used to turn a pin to HIGH. Register LPC_GPIOn –> FIOCLR is used to turn a pin to low. To turn a pin to digital ‘1’ (high), set the corresponding bit of LPC_GPIOn –> FIOSET to 1. To turn a pin to digital ‘0’ (low), set the corresponding bit of LPC_GPIOn –> FIOCLR to 1.
Turn Pin 0.3 to high
LPC_GPIO0 –> FIOSET |= (1<<3);
If we set LPC_GPIOn –> FIOSET bit to ‘0’ there is no effect.
Turn Pin 0.3 to low
LPC_GPIO0 –> FIOCLR |= (1<<3);
If we set LPC_GPIOn –> FIOCLR bit to ‘0’ there is no effect.
4. Pin is Set to Input
- Read a Pin Value
Register LPC_GPIOn –> FIOPIN stores the current pin state. The corresponding bit is ‘1’ indicates that the pin is driven high.
To read current state of Pin 0.3
Value = ((LPC_GPIO0 –> FIOPIN & (1<<3)) >> 3);
Note: write 1/0 to corresponding bit in LPC_GPIOn –> FIOPIN can change the output of the pin to 1/0 but it is not recommended. We should use LPC_GPIOn –> FIOSET and GPIOn –> FIOCLR instead.
- Pin Internal Pull up Setting
Register LPC_PINCON –> PINMODEn is used to set up a pin internal pull-up.
LPC_PINCON –> PINMODE0 [1:0] control P0.0 internal pull-up
LPC_PINCON –> PINMODE0 [31:30] control P0.15 internal pull-up,
Please see LPC_PINCON –> PINSELn for the full list [Page No: 114].
Bit Value Pin Mode 00 On chip pull-up resistor enabled 01 Repeater Mode 10 Tri-State mode, (neither pull-up nor pull-down) 11 On chip pull-down resistor enabled
By default all pins which are set as input has internal pull-up on (00).
To disable internal pull-up on pin 0.3
LPC_PINCON –> PINSEL0 |= (1<<7);