0% found this document useful (0 votes)
54 views

GPIO Programming

GPIO pins on microcontrollers like the LPC214x can be used for general purpose input/output applications such as driving LEDs and controlling external devices. The pins are grouped into ports that can be configured via registers to select pin functions and set pin directions. Programming GPIO involves manipulating registers like IOxPIN for pin values, IOxDIR for directions, and IOxSET/IOxCLR for driving pin states high/low to implement applications like blinking LEDs.

Uploaded by

Nikhil Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

GPIO Programming

GPIO pins on microcontrollers like the LPC214x can be used for general purpose input/output applications such as driving LEDs and controlling external devices. The pins are grouped into ports that can be configured via registers to select pin functions and set pin directions. Programming GPIO involves manipulating registers like IOxPIN for pin values, IOxDIR for directions, and IOxSET/IOxCLR for driving pin states high/low to implement applications like blinking LEDs.

Uploaded by

Nikhil Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

GPIO

Applications of GPIO
• General purpose I/O
• • Driving LEDs, or other indicators
• • Controlling off-chip devices
• • Sensing digital inputs
• When getting started in embedded programming, GPIO (viz. General
Purpose Input Output) pins are important.
• The use of GPIO is not limited to driving LEDS but can be also used for
reading digital signal , generating triggers for external components ,
controlling external devices etc….
• Most of the function oriented pins on lpc214x Microcontrollers are
grouped into ports. lpc2148 has 2 ports viz. Port 0 and Port 1.

• Port 0 is a 32 bit wide I/O port (i.e it can be used for max 32 pins
where each pin refers to a corresponding bit) and has dedicated
direction bits for each of the pins present in the port. 28 out of the 32
pins can be used as bi-directional I/O (digital) pins. Pins P0.24 , P0.26
& P0.27 are unavailable for use and Pin P0.30 can be used as output
pin only.
• Port 1 is also a 32 bit wide I/0 port but Pins 0 to 15 i.e P1.0 –
P1.15 are unavailable for use and this port too has a dedicated
direction bit for each of the usable pins.
Pin connect block
• Allows individual pin configuration.
• The purpose of the Pin connect block is to configure the
microcontroller pins to the desired functions.
• In lpc214x MCUs most of the PINS are Multiplexed i.e. these pins can
be configured to provide different functions.
• The functions of the Pins in Port 0 & 1 can be selected by
manipulating appropriate bits in PINSEL0/1/2 registers.
• assigning ’0′ to these registers forces the corresponding pins to be
used as GPIO
• Since by default all pins are configured as GPIOs we dont need to
explicitly assign zero value to PINSELx registers in GPIO programming.

• The purpose of the Pin Connect Block is to configure the


microcontroller pins to the desired functions. The pin connect block
allows selected pins of the microcontroller to have more than one
function.
• Two bits from PINSEL register will be used to select the multi functions
of the pins.
PINSEL0: P0.0-P0.15
PINSEL1: P0.16-P0.31
PINSEL2: P1.16-P1.31
Registers in GPIO Programming
1. IOxPIN (x=port number) (32 bit) : Pin value register-Regardless of the
direction set for the particular pins it gives the current status of the GPIO
pin when read.
2. IOxDIR (32 bit) : This is the GPIO direction control register. Setting a bit
to 0 in this register will configure the corresponding pin to be used as an
Input while setting it to 1 will configure it as Output.
3. IOxSET (32 bit) : This register controls the state of output pins. This
register can be used to drive an ‘output’ configured pin to Logic 1 i.e
HIGH. Writing Zero does NOT have any effect and hence it cannot be used
to drive a pin to Logic 0 i.e LOW. For driving pins LOW IOxCLR is used. 4.
IOxCLR (32 bit) : This register can be used to drive an ‘output’ configured
pin to Logic 0 i.e LOW. It cannot be used to drive a pin to Logic 1. Writing
ones produces lows at the corresponding port pins and clears the
corresponding bits in the IOSET register.
Example-sequential accesses to IOSET and IOCLR
affecting the
same GPIO pin/bit
IO0DIR = 0x0000 0080 ;pin P0.7 configured as output
IO0CLR = 0x0000 0080 ;P0.7 goes LOW
IO0SET = 0x0000 0080 ;P0.7 goes HIGH

• pin P0.7 is configured as an output (write to IO0DIR register). After this,


P0.7 output is set to low (first write to IO0CLR register). Short high
pulse follows on P0.7 (write access to IO0SET), and the final write to
IO0CLR register sets pin P0.7 back to low level.
Example to generate square waveform at
P0.0-P0.3
# include <lpc214x.h>
Int main (void)
{
unsigned int x;
for(;;)
{
IO0DIR=0xFFFFFFFF;
for (x=0;x<4;x++)
IO0SET=0x0000000F;
for (x=0;x<12;x++)
IO0CLR=0x0000000F;
}
}
50% duty cycle on P0.16
# include <lpc214x.h>
Int main (void)
{
unsigned int x;
IO0DIR=0x00010000;
while(1)
{

IO0SET=1<<16;
delay ();
IO0CLR=1<<16;
delay();

}
}
void delay(void)
{
    int z, c;
    c=0;
    for(z=0; z<4000000; z++) // You can edit this as per your needs
    {
        c++;
    }
}

You might also like