0% found this document useful (0 votes)
22 views32 pages

GPIO

The document provides an overview of the GPIO (General Purpose Input/Output) configuration for the LPC2148 microcontroller, detailing the available pins across different ports. It specifies that Port 0 has 29 usable pins (28 bidirectional and 1 output only) and Port 1 has 16 bidirectional pins, totaling 45 GPIOs. Additionally, it includes examples of how to configure, read, and write to these GPIO pins using specific registers and functions.

Uploaded by

Tirth Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views32 pages

GPIO

The document provides an overview of the GPIO (General Purpose Input/Output) configuration for the LPC2148 microcontroller, detailing the available pins across different ports. It specifies that Port 0 has 29 usable pins (28 bidirectional and 1 output only) and Port 1 has 16 bidirectional pins, totaling 45 GPIOs. Additionally, it includes examples of how to configure, read, and write to these GPIO pins using specific registers and functions.

Uploaded by

Tirth Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

LPC2148 - GPIO

Parallel Port – Port 0


• Port 0 = 32 Bits  (P0.0 to P0.31)
• P0.24, P0.26 and P0.27 are not available
• So (32-3) = 29 Pins are Available

• Out of 29 Pins, 28 Pins  Bidirectional,


Pin 31  O/P Only
Parallel Port – Port 1
• Port 1 = 16 Bits  P1.16 to P1.31
• P1.0 to P1.15 of port 1 are
• ====================================
• P0 = 29 (28 Bidirectional + 1 output only)
• P1 = 16 (16 Bidirectional)
• Total = 45 GPIOs
• ====================================
Parallel Port

P0 = 29 Pins
P1 = 16 Pins
-------------------
Total = 45 Pins
--------------------
Pins Used For GPIO in our Board

P0
P0.8- P0.15

P1
P1.16- P1.23

P2
P1.24- P1.31
SFRs Used

P0 P1
• IODIR0  to set the direction • IODIR1
• IOSET0 to set a Pin • IOSET1
• IOCLR 0 to clear a Pin • IOCLR1
• IOPIN0 to Read a Pin • IOPIN1
IODIR
1  Output 0  Input
• IODIR controls the 'direction' of the GPIO pin.
• You use this register to set a GPIO pin to
either Input (0) or Output (1).
• To Configure GPIO 0.10 to 'Output' (1).
GPIO0_IODIR |= (1 << 10);
• To Configure GPIO 0.10 to ‘Input' (0).
GPIO0_IODIR &= ~(1 << 10);
IODIR0
1  Output
0  Input

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

1 0

P0.1 = Output

P0.0 = Input

Writing a 1  Pin is configured as Output


Writing a 0  Pin is configured as Input
IOSET and IOCLR
• If GPIO pin is set as Output (using the IODIR
register), then
• IOSET is used to set GPIO pin to 'high'
(providing a small 3.3V electrical output)
• IOCLR is used to clear it to 'low' (providing a
connection to GND).
IOSET0
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

1 0

Pin P0.1 = 3.3 V

No Effect

Writing a 1  Pin is set to High


Writing a 0  No Effect
IOCLR0
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

1 0

Pin P0.1 = 0 V

No Effect

Writing a 1  Pin is cleared to Low


Writing a 0  No Effect
A small Example
• // Make sure GPIO 0.10 and 0.11 are set to
output
• IODIR0 |= (1 << 10) | (1 << 11);
• // Turn the LEDs off using IOCLR (which gives a
GND connection)
• IOCLR0 |= (1 << 10) | (1 << 11);
• // Turn the LEDs on using IOSET (which supplies
3.3V
• IOSET0 |= (1 << 10) | (1 << 11);
IOPIN
• Regardless of whether GPIO pin's direction is
set to Input or Output,
• IOPIN register is used to read the current
'state' of every GPIO pin(all 32 pins in GPIO).
• A 1 value means that the pin is currently
'high', and a 0 value means the pin is currently
'low'.
A Small Example
int getPinState(int pinNumber)
{
// Read the current state of all pins in GPIO block 0
int pinBlockState = GPIO0_IOPIN;
// Read the value of 'pinNumber'
int pinState = (pinBlockState & (1 << pinNumber)) ? 1 : 0;
// Return the value of pinState
return pinState;
}
Example Programs
LED Blink Program
#include<lpc21xx.h>
void delay(void);
int main()
{
IODIR0=0xFFFFFFFF;

while(1)
{
IOSET0=0xFF00;
delay();
IOCLR0=0xFF00;
delay();
}
}

void delay(void)
{
int i;
for(i=0;i<=600000;i++);

}
Pins & Ports

P0
P0.8- P0.15

P1
P1.16- P1.23

P2
P1.24- P1.31
Ports & Pins

ARM P1.23-P1.31 ARM P1.16-P0.23 ARM P0.8-P0.15

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 Pins

P2 P1 P0

For our convenience


GPIO.h
List of Functions
void writepin(int pinnumber,int pinvalue);

void writeport(int portnumber,int portvalue);

int readpin(int pinnumber);

int readport(int portnumber);

void delay(void);

pinnumber = (8 to 31)
portnumber = (0 to 2)
writepin
void writepin(int pinnumber,int pinvalue)
{
// P0 = P0.8 - P0.15
if (pinnumber >= 8 && pinnumber <= 15 )
{
IODIR0 = IODIR0 | (1 << pinnumber);
if (pinvalue)
IOSET0 = IOSET0 | (1 << pinnumber);
else
IOCLR0 = IOCLR0 | (1 << pinnumber);
}

// P1 = P1.16 - P1.23
if (pinnumber >= 16 && pinnumber <= 31 )
{
IODIR1 = IODIR1 | (1 << pinnumber);
if (pinvalue)
IOSET1 = IOSET1 | (1 << pinnumber);
else
IOCLR1 = IOCLR1 | (1 << pinnumber);
}
}
writeport
void writeport(int portnumber,int portvalue)
{
int i; else if (portnumber == 2)
if (portnumber == 0)
{ {
//P0 ==> P0.8 to P0.15 //P2 ==> P1.24 to P1.31
for (i=0;i<8;i++)
{ for (i=0;i<8;i++)
if (portvalue & 0x01 << i) {
writepin(i+8,1); if (portvalue & 0x01 << i)
else writepin(i+24,1);
writepin(i+8,0);
} else
} writepin(i+24,0);
}
else if (portnumber == 1)
{ }
//P1 ==> P1.16 to P1.23 else
for (i=0;i<8;i++) {
{
if (portvalue & 0x01 << i) i=0 ; // dummy
writepin(i+16,1); }
else
writepin(i+16,0); }
}
}
readpin
int readpin(int pinnumber)
{
int temp,mask ;
if (pinnumber >= 8 && pinnumber <= 15)
{
mask = ~(0x01 << pinnumber);
IODIR0 = IODIR0 & mask ; // pinnumber = input
temp = IOPIN0 ;
if (temp & (0x01 << pinnumber))
return 1 ;
else
return 0;
}

if (pinnumber >= 16 && pinnumber <= 31)


{
mask = ~(0x01 << pinnumber);
IODIR1 = IODIR1 & mask ; // pinnumber = input
temp = IOPIN1 ;
if (temp & (0x01 << pinnumber))
return 1 ;
else
return 0;
}
}
int readport(int portnumber)
readport
{
int temp ;
if (portnumber == 0 )
{
//P0.8 to P0.15
IODIR0 = IODIR0 & 0xFFFF00FF;
temp = IOPIN0 ;
temp = temp & 0x0000FF00;
return (temp >> 8) ;
}
else if (portnumber == 1 )
{
//P1.16 to P1.23
IODIR1 = IODIR1 & 0xFF00FFFF;
temp = IOPIN1 ;
temp = temp & 0x00FF0000;
return (temp >> 16) ;
}
else if (portnumber == 2 )
{
//P1.24 to P1.31
IODIR1 = IODIR1 & 0x00FFFFFF;
temp = IOPIN1 ;
temp = temp & 0xFF000000;
return (temp >> 24) ;
}
else
temp=0; // dummy
Software delay()

void delay()
{
unsigned int i;
for(i=0;i<200000;i++);
}
Example Programs
Toggle LED @ P0.15, using software delay

C1
#include<LPC214x.h>

1
33pF U1

#include "GPIO.h"
62 19
X1 XTAL1 P0.0/TxD0/PWM1
21
61 P0.1/RxD0/PW M3/EINT0
C2 XTAL2
22
P0.2/SCL0/CAP0.0

int main()
3

2
RTXC1 26
P0.3/SDA0/MAT0..0/EINT1
5 27
RTXC2 P0.4/SCK0/CAP0.1/AD0.6
33pF 29
P0.5/MISO0/MAT0.1/AD0.7 30
57 P0.6/MOSI0/CAP0.2/AD1.0
C3 RST
31

{
P0.7/SSEL0/PW M2/EINT2
33
P0.8/TxD1/PW M4/AD1.1
34
P0.9/RxD1/PW M6/EINT3

1
22pF 35
P0.10/RTS1/CAP1.0/AD1.2
X2 37

while(1)
P0.11/CTS1/CAP1.1/SCL1
38
P0.12/DSR1/MAT1.0/AD1.3
C4 P0.13/DTR1/MAT1.1/AD1.4
39
D2

2
41
P0.14/DCD1/EINT1/SDA1
45

{
P0.15/RI1/EINT2/AD1.5
22pF
46
+3.3V P0.16/EINT0/MAT0.2/CAP0.2 LED-YELLOW
47
P0.17/CAP1.2/SCK1/MAT1.2
53

writepin(15,1);
P0.18/CAP1.3/MISO1/MAT1.3
54
P0.19/MAT1.2/MOSI1/CAP1.2
55
P0.20/MAT1.3/SSEL1/EINT3
1
P0.21/PW M5/AD1.6/CAP1.3
R1 2

delay();
P0.22/AD1.7/CAP0.0/MAT0.0
1K 58
P0.23
9
P0.25/AD0.4/AOUT
10

writepin(15,0);
P0.26/AD0.5
11
P0.27/AD0.0/CAP0.1/MAT0.1
13
P0.28/AD0.1/CAP0.2/MAT0.2
C5 14
P0.29/AD0.2/CAP0.3/MAT0.3
15

delay();
100pF P0.30/AD0.3/EINT3/CAP0.0
17
+3.3V P0.31
16
P1.16/TRACEPKT0
12
P1.17/TRACEPKT1

} 49 8
VBAT P1.18/TRACEPKT2
4
P1.19/TRACEPKT3
63 48
VREF P1.20/TRACESYNC
7 44

}
V3A P1.21/PIPESTAT0
51 40
V3 P1.22/PIPESTAT1
43 36
V3 P1.23/PIPESTAT2
23 32
V3 P1.24/TRACECLK
28
59 P1.25/EXTIN0
VSSA 24
50 P1.26/RTCK
VSS 64
42 P1.27/TDO
VSS 60
25 P1.28/TDI
VSS 56
18 P1.29/TCK
VSS 52
6 P1.30/TMS
VSS 20
P1.31/TRST
LPC2138
Toggle LEDs @ P0, using software delay

#include<LPC214x.h>
#include "GPIO.h" C1 D1

int main() D2

1
33pF U1

P0
LED-YELLOW
X1 62 XTAL1 19

{
P0.0/TxD0/PWM1
61 21
C2 XTAL2 P0.1/RxD0/PW M3/EINT0
22
D3
P0.2/SCL0/CAP0.0 LED-YELLOW
3

2
RTXC1 26
5 P0.3/SDA0/MAT0..0/EINT1
27
RTXC2 P0.4/SCK0/CAP0.1/AD0.6 D4

while(1)
33pF 29 LED-YELLOW
P0.5/MISO0/MAT0.1/AD0.7
57 30
C3 RST P0.6/MOSI0/CAP0.2/AD1.0
31
P0.7/SSEL0/PW M2/EINT2
33
D5
P0.8/TxD1/PW M4/AD1.1 LED-YELLOW
34

1
P0.9/RxD1/PW M6/EINT3

{
22pF 35
P0.10/RTS1/CAP1.0/AD1.2 D6
X2 P0.11/CTS1/CAP1.1/SCL1
37 LED-YELLOW
38
C4 P0.12/DSR1/MAT1.0/AD1.3
39
P0.13/DTR1/MAT1.1/AD1.4 D7

2
41

writeport(0,0x00) ;
P0.14/DCD1/EINT1/SDA1 LED-YELLOW
45
P0.15/RI1/EINT2/AD1.5
22pF
46
D8
+3.3V P0.16/EINT0/MAT0.2/CAP0.2 LED-YELLOW
47
P0.17/CAP1.2/SCK1/MAT1.2

delay();
53
P0.18/CAP1.3/MISO1/MAT1.3
54 LED-YELLOW
P0.19/MAT1.2/MOSI1/CAP1.2
55
P0.20/MAT1.3/SSEL1/EINT3
1
P0.21/PW M5/AD1.6/CAP1.3
R1 2

writeport(0,0xFF) ;
P0.22/AD1.7/CAP0.0/MAT0.0
1K 58
P0.23
9
P0.25/AD0.4/AOUT
10
P0.26/AD0.5

delay();
11
P0.27/AD0.0/CAP0.1/MAT0.1
13
P0.28/AD0.1/CAP0.2/MAT0.2
14
C5 P0.29/AD0.2/CAP0.3/MAT0.3
15
100pF P0.30/AD0.3/EINT3/CAP0.0
17

}
+3.3V P0.31
16
P1.16/TRACEPKT0
12
P1.17/TRACEPKT1
49 8
VBAT P1.18/TRACEPKT2
4

}
P1.19/TRACEPKT3
63 48
VREF P1.20/TRACESYNC
7 44
V3A P1.21/PIPESTAT0
51 40
V3 P1.22/PIPESTAT1
36
43 P1.23/PIPESTAT2
V3 32
23 P1.24/TRACECLK
V3 28
P1.25/EXTIN0
59 24
VSSA P1.26/RTCK
50 64
VSS P1.27/TDO
42 60
VSS P1.28/TDI
25 56
VSS P1.29/TCK
18 52
VSS P1.30/TMS
6 20
VSS P1.31/TRST

LPC2138
Read Pin @ P1.31 & control @ P0.15
#include<LPC214x.h> +3.3V

#include "GPIO.h" C1

1
33pF U1 R2
int main() C2
X1 62
61
XTAL1
XTAL2
P0.0/TxD0/PWM1
P0.1/RxD0/PW M3/EINT0
P0.2/SCL0/CAP0.0
19
21
22
1K

2
3 26

{
RTXC1 P0.3/SDA0/MAT0..0/EINT1
5 27
RTXC2 P0.4/SCK0/CAP0.1/AD0.6
33pF 29
RST P0.5/MISO0/MAT0.1/AD0.7
57 30
C3 P0.6/MOSI0/CAP0.2/AD1.0
31

int mydata;
P0.7/SSEL0/PW M2/EINT2
33
P0.8/TxD1/PW M4/AD1.1
34

1
P0.9/RxD1/PW M6/EINT3
22pF 35
P0.10/RTS1/CAP1.0/AD1.2
X2 37

while(1)
P0.11/CTS1/CAP1.1/SCL1
38
C4 P0.12/DSR1/MAT1.0/AD1.3
39
P0.13/DTR1/MAT1.1/AD1.4 D2

2
41
P0.14/DCD1/EINT1/SDA1
45

{
P0.15/RI1/EINT2/AD1.5
22pF
46 LED-YELLOW
+3.3V P0.16/EINT0/MAT0.2/CAP0.2
47
P0.17/CAP1.2/SCK1/MAT1.2
53

mydata = readpin(31);
P0.18/CAP1.3/MISO1/MAT1.3
54
P0.19/MAT1.2/MOSI1/CAP1.2
55
P0.20/MAT1.3/SSEL1/EINT3
1
P0.21/PW M5/AD1.6/CAP1.3
R1 2

if (mydata == 0)
P0.22/AD1.7/CAP0.0/MAT0.0
1K 58
P0.23
9
P0.25/AD0.4/AOUT
10
P0.26/AD0.5

writepin(15,1);
11
P0.27/AD0.0/CAP0.1/MAT0.1
13
P0.28/AD0.1/CAP0.2/MAT0.2
14
C5 P0.29/AD0.2/CAP0.3/MAT0.3
15
100pF P0.30/AD0.3/EINT3/CAP0.0

else
17
+3.3V P0.31
16
P1.16/TRACEPKT0
12
P1.17/TRACEPKT1

writepin(15,0);
49 8
VBAT P1.18/TRACEPKT2
4
P1.19/TRACEPKT3
63 48
VREF P1.20/TRACESYNC
7 44
V3A P1.21/PIPESTAT0

}
51 40
V3 P1.22/PIPESTAT1
43 36
V3 P1.23/PIPESTAT2
23 32
V3 P1.24/TRACECLK
28
P1.25/EXTIN0

}
59 24
VSSA P1.26/RTCK
50 64
VSS P1.27/TDO
42 60
VSS P1.28/TDI
25 56
VSS P1.29/TCK
18 52
VSS P1.30/TMS
6 20
VSS P1.31/TRST
LPC2138
Read Pin @ P1.31 & control @ P0.15

#include<LPC214x.h> +3.3V

#include "GPIO.h" C1

1
33pF U1 R2
int main() C2
X1 62
61
XTAL1
XTAL2
P0.0/TxD0/PWM1
P0.1/RxD0/PW M3/EINT0
P0.2/SCL0/CAP0.0
19
21
22
1K

2
3 26

{
RTXC1 P0.3/SDA0/MAT0..0/EINT1
5 27
RTXC2 P0.4/SCK0/CAP0.1/AD0.6
33pF 29
RST P0.5/MISO0/MAT0.1/AD0.7
57 30
C3 P0.6/MOSI0/CAP0.2/AD1.0
31

int mydata;
P0.7/SSEL0/PW M2/EINT2
33
P0.8/TxD1/PW M4/AD1.1
34

1
P0.9/RxD1/PW M6/EINT3
22pF 35
P0.10/RTS1/CAP1.0/AD1.2
X2 37

while(1)
P0.11/CTS1/CAP1.1/SCL1
38
C4 P0.12/DSR1/MAT1.0/AD1.3
39
P0.13/DTR1/MAT1.1/AD1.4 D2

2
41
P0.14/DCD1/EINT1/SDA1
45

{
P0.15/RI1/EINT2/AD1.5
22pF
46 LED-YELLOW
+3.3V P0.16/EINT0/MAT0.2/CAP0.2
47
P0.17/CAP1.2/SCK1/MAT1.2
53

mydata = readpin(31);
P0.18/CAP1.3/MISO1/MAT1.3
54
P0.19/MAT1.2/MOSI1/CAP1.2
55
P0.20/MAT1.3/SSEL1/EINT3
1
P0.21/PW M5/AD1.6/CAP1.3
R1 2

if (mydata == 0)
P0.22/AD1.7/CAP0.0/MAT0.0
1K 58
P0.23
9
P0.25/AD0.4/AOUT
10
P0.26/AD0.5

writepin(15,1);
11
P0.27/AD0.0/CAP0.1/MAT0.1
13
P0.28/AD0.1/CAP0.2/MAT0.2
14
C5 P0.29/AD0.2/CAP0.3/MAT0.3
15
100pF P0.30/AD0.3/EINT3/CAP0.0

else
17
+3.3V P0.31
16
P1.16/TRACEPKT0
12
P1.17/TRACEPKT1

writepin(15,0);
49 8
VBAT P1.18/TRACEPKT2
4
P1.19/TRACEPKT3
63 48
VREF P1.20/TRACESYNC
7 44
V3A P1.21/PIPESTAT0

}
51 40
V3 P1.22/PIPESTAT1
43 36
V3 P1.23/PIPESTAT2
23 32
V3 P1.24/TRACECLK
28
P1.25/EXTIN0

}
59 24
VSSA P1.26/RTCK
50 64
VSS P1.27/TDO
42 60
VSS P1.28/TDI
25 56
VSS P1.29/TCK
18 52
VSS P1.30/TMS
6 20
VSS P1.31/TRST
LPC2138
Read Port @ P0 & control @ P1,P2
#include<LPC214x.h>
#include "GPIO.h" +3.3v +3.3v +3.3v +3.3v +3.3v +3.3v +3.3v +3.3v

int main() R2 R3 R4 R5 R6 R7 R8 R9
10k

{ C1

int mydata;

1
U1
X1
C2

while(1)

2
{
C3

P0

1
X2
C4

2
mydata = readport(0); D9

D10
R1
D11

if (mydata < 128) C5


D12

D13

D14
P1
writeport(1,mydata); D15

D16

else D17

D18

writeport(2,mydata); D19

}
D20

D21

D22
P2
D23

D24

}
LED-YELLOW

You might also like