Professional Documents
Culture Documents
To
GPIOs
1
Outline of the presentation
GPIOs
GPIOs Registers
GPIOs Registers configuration ALP point of view
GPIOs Registers configuration C language
programming point of view
2
Peripherals Map
Memory Mapped
I/O Mapping
For GPIOs
0x2009 C000
To
0x200A 0000
3
Pin Configuration of LPC1768
4
GPIOs
• LPC1768 is an ARM Cortex-M3 based MCU by Phillips/NXP
and has plenty of General Purpose Input Output pins to play with.
• Pins on LPC176x Microcontrollers are grouped into Ports.
- LPC1768 has 5 ports viz. Port 0 to 4.
- The associated registers for each port are grouped into
a structure with the following naming convention :
LPC_GPIOx , where x is the port number.
• From the programming point of view
-These ports are 32-bit wide i.e. a maximum 32 pins
- But each port may have a few or many pins are ‘reserved’.
- In Port 0 Pins 12, 13, 14 & 31 are not available.
- In Port 1 Pins 2, 3, 7, 6, 5, 11, 12, & 13 are not available.
- In Port 2 only pins 0 to 13 are available and rest are reserved.
- In Port 3 only pins 25, 26 are available and rest are reserved.
- Finally in Port 4 only 28, 29 are available and rest are reserved.
5
• The naming convention for Pins on MCU is ‘Px.y’
- where ‘x’ is the port number (0,1,2,3 or 4 in our case
since we have only 5 ports to play within lpc1768)
- and ‘y’ is simply the pin number in port ‘x’.
- For example:
P0.7 refers to Pin number 7 of Port 0
P2.11 refers to Pin number 11 in Port 2.
6
GPIO: Registers
Registers to control GPIO functionality through fast Interface
AHB Bus
• FIODIR: Fast GPIO Port Direction control register (0=in, 1=out).
- This register individually controls the direction of each port pin.
• FIOMASK: Fast Mask register for port.
- Writes, sets, clears, and reads (done via writes to FIOPIN,
FIOSET, and FIOCLR, and reads of FIOPIN)
- To alter or return only the bits enabled by 0 in this register.
• FIOPIN: Fast Port Pin value register using FIOMASK.
- Current state of digital port pins can be read from this
register, regardless of pin direction or alternate function selection
• FIOSET: Fast Port Output Set register using FIOMASK.
- Writing 1s produces highs at the corresponding port pins.
• FIOCLR: Fast Port Output Clear register using FIOMASK.
- Writing 1s produces lows at the corresponding port pins.
7
GPIOs: General Purpose I/O
Peripherals->GPIO Fast Interface
8
GPIO: Registers Address
FIO0DIR equ 0x2009c000 FIO1DIR equ 0x2009c020
FIO0MASK equ 0x2009c010 FIO1MASK equ 0x2009c030
FIO0PIN equ 0x2009c014 FIO1PIN equ 0x2009c034
FIO0SET equ 0x2009c018 FIO1SET equ 0x2009c038
FIO0CLR equ 0x2009c01c FIO1CLR equ 0x2009c03c
FIO2DIR equ 0x2009c040 FIO3DIR equ 0x2009c060
FIO2MASK equ 0x2009c050 FIO3MASK equ 0x2009c070
FIO2PIN equ 0x2009c054 FIO3PIN equ 0x2009c074
FIO2SET equ 0x2009c058 FIO3SET equ 0x2009c078
FIO2CLR equ 0x2009c05c FIO3CLR equ 0x2009c07c
FIO4DIR equ 0x2009c080
FIO4MASK equ 0x2009c090
FIO4PIN equ 0x2009c094
FIO4SET equ 0x2009c098
FIO4CLR equ 0x2009c09c 9
;ARM ALP to display sum on port0
area reset,data,readonly
export __Vectors
__Vectors ldr r0,=0x12345678
dcd 0 ldr r1,=0x11111111
dcd Reset_Handler
area hello,code,readonly adds r2,r0,r1
FIO0DIR equ 0x2009c000
rev r3,r2
FIO0MASK equ 0x2009c010
FIO0PIN equ 0x2009c014 ldr r4,=sum
FIO0SET equ 0x2009c018 str r3,[r4]
FIO0CLR equ 0x2009c01c
entry ldr r5,=FIO0DIR
export Reset_Handler
ldr r6,=0xffffffff ; o/p port
Reset_Handler
str r6,[r5] ;
defined as LPC_GPIO0.
12
setting, Clearing and Toggling in ARM Embedded C
Setting
LPC_GPIO0->FIOPIN |= (1<<3); //set P0.3 to digital 1
LPC_GPIO1->FIOPIN |= (0xFF<<16); // Set P1 (bits 16-23) to digital 1
Clearing
LPC_GPIO0->FIOPIN &=~ (1<<3); //set P0.3 to digital 0
LPC_GPIO1->FIOPIN &= ~(0xFF<<16); // Set P1 (bits 16-23) to digital 0
Toggling
LPC_GPIO1->FIOPIN ^= 1 << 29; // Toggle the LED
13
PCONP Register (power control for peripheral registers)
14
15
• LPC17xx MCUs have Peripheral Power Control feature
- Which allows individual peripherals to be turned off
for power saving using PCONP Register
- Which is a member of LPC_SC (system control) structure.
16
Pin Function Setting
17
PINSEL register
• By default, after Power-On or Reset :
- All pins of all ports are set as GPIO
- The different functions that any particular pin
provides can be selected by setting appropriate
value in the PINSEL register for the corresponding pin.
• Each pin on any port has 2 corresponding bits
in PINSEL register.
- The first 16 pins (0-15) on a given port will have
a corresponding 32 bit PINSEL register and the rest 16
bits will have another register.
- For example bits 0 & 1 in PINSEL0 are used to
select function for Pin 1 of Port 0, bits 2 & 3 in PINSEL0
are used to select function for PIN 2 of port 0 and so on.
18
Bit Value Function
00 GPIO Function
1st alternate
01
function
2nd alternate
10
function
3rd alternate
11
function
19
Example:
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)); //
LPC_PINCON->PINSEL0 |=(2<<6) 20
Pin modes of Port pins
21
Pull –up Resistor
22
Pull –down Resistor
23
24
• LPC1768 MCU supports 4 pin modes.
Bit Value Pin Mode
00 On chip pull-up resistor enabled
01 Repeater Mode (Retains its last state if it is configured
as an input and is not driven externally)
10 Tri-State mode, (neither pull-up nor pull-down)
11 On chip pull-down resistor enabled
- These include the internal (on-chip) pull-up and pull-down
resistor modes and a special operating mode.
- Pull-up and Pull-down resistors prevent the inputs
from 'floating' by either pulling them to logic HIGH or LOW.
- The state of the inputs is therefore always defined.
-The PINMODE and PINMODE_OD registers together are
used to control the pin mode.
- A total of 3 bits are used to control the mode of a any pin,
2 bits from PINMODE and 1 bit from PINMODE_OD register.
25
• Hence we have a total of 10 PINMODE registers and
a total of 5 PINMODE_OD.
26
27
28
Example:
• 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);
LPC_PINCON->PINMODE3 &= ~(3<<15);
/* Making pull register low */
LPC_PINCON->PINMODE3 |= (1<<15);
/* Configuring Pull register */
LPC_PINCON->PINMODE1 &= ~0x003FC000;
/* Pull up the register */
LPC_PINCON->PINMODE1 |= (2<<14)|(2<<16)|(2<<18)|(2<<20);
/*pull down the register */
29
GPIO Registers
30
1. FIODIR (Pin Direction Setting)
31
Pin is Set as Output
. i) FIOSET
• It is 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.
Example
Turn Pin 0.3 to high
LPC_GPIO0 –> FIOSET |= (1<<3);
If we set LPC_GPIOn –> FIOSET bit to ‘0’ there is no effect.
ii) FIOCLR
• It is used to drive an ‘output’ configured pin to Logic 0 i.e LOW.
• Writing Zero does NOT have any effect and hence
it cannot be used to drive a pin to Logic 1.
Turn Pin 0.3 to low
LPC_GPIO0 –> FIOCLR |= (1<<3);
If we set LPC_GPIOn –> FIOCLR bit to ‘0’ there is no effect 32
FIOPIN (Pin is Set to Input)
• This register can be used to Read or Write values directly
to the pins.
• Regardless of the direction set for the particular pins
it gives the current start of the GPIO pin when read.
35
Programming of GPIOs
Step1: Power-up GPIOs using PCONP Register
(optional, since on-reset GPIOs are turned-on)
LPC_SC->PCONP |=(1<<15);
36
Step3: Select on-chip pull-up or pull-down resistor mode
using PINMODE register
(prevent the inputs from floating)
LPC_PINCON –> PINMODE0 = 0x0000;
// Port0 I/Os (15:0) default pull-up
LPC_PINCON –> PINMODE0 &= ~ ((1<<10) | (1<<5));
// particular I/O pins, in this case digitally 5th and 10th pins of
Port0 default pull-up
LPC_PINCON –> PINMODE0 &= ~ (0xff<<0);
// particular I/O pins, in this case 7,6,5,4,3,2,1 and 0th pins of
Port0 default pull-up
LPC_PINCON –> PINMODE0 = 0xffff;
// Port0 I/Os (15:0) pull-down
LPC_PINCON –> PINMODE0 |= ((1<<10) | (1<<5));
// particular I/O pins, in this case digitally 5th & 10th pins of Port0
pull-down
LPC_PINCON –> PINMODE0 |= (0xff<<0);
// particular I/O pins, in this case 7,6,5,4,3,2,1 and 0th pins of 37
Step4: Configure particular GPIOs either as input or output pins
using FIODIR register
LPC_GPIO0 –> FIODIR = 0x00000000;
// all Port0 I/Os (31:0) default input pins
LPC_GPIO0 –> FIODIR &= ~ ((1<<10) | (1<<5));
// particular I/O pins, in this case digitally 5th and 10th pins of Port0
default input pins
LPC_GPIO0 –> FIODIR &= ~ (0xff<<0);
// particular I/O pins, in this case 7,6,5,4,3,2,1 and 0th pins of Port0
default input pins
LPC_GPIO0 –> FIODIR = 0xffffffff;
// all Port0 I/Os (31:0) output pins
LPC_GPIO0 –> FIODIR |= ((1<<10) | (1<<5));
// particular I/O pins, in this case digitally 5th and 10th pins of Port0
output pins
LPC_GPIO0 –> FIODIR |= (0xff<<0);
// particular I/O pins, in this case 7,6,5,4,3,2,1 and 0th pins of Port0
38
Step5: Enable all bits of FIOSET, FIOCLR and FIOPIN registers
using FIOMASK register
(0 means enable and 1 means disable)
(optional, since on-reset, all bits of FIOMASK register are 0s )
LPC_GPIO0 –> FIOMASK = 0x00000000;
// all bits default enable
LPC_GPIO0 –> FIOMASK &= ~ ((1<<10) | (1<<5));
// particular bits, in this case digitally 5th and 10th bits default
enable
LPC_GPIO0 –> FIOMASK &= ~ (0xff<<0);
// particular bits, in this case 7,6,5,4,3,2,1 and 0th bits default
enable
LPC_GPIO0 –> FIOMASK = 0xffffffff;
//all bits disable
LPC_GPIO0 –> FIOMASK |= ((1<<10) | (1<<5));
// particular bits, in this case digitally 5th and 10th bits disable
LPC_GPIO0 –> FIODIR |= (0xff<<0);
// particular bits, in this case 7,6,5,4,3,2,1 and 0th bits disable 39
Step6: Make GPIOs pin status HIGH using FIOSET register
40
Step7: Make GPIOs pin status LOW using FIOCLR register
41
Step8: Send data through GPIOs configured as output pins
using FIOPIN register
42