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

Lecture_14_15

The document provides examples of GPIO programming for configuring and controlling pins on Port 0 of a microcontroller. It includes various examples of setting pins as output and driving them high or low, along with embedded C code snippets for turning LEDs on and off. Additionally, it demonstrates how to use functions to manipulate pin settings and values effectively.

Uploaded by

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

Lecture_14_15

The document provides examples of GPIO programming for configuring and controlling pins on Port 0 of a microcontroller. It includes various examples of setting pins as output and driving them high or low, along with embedded C code snippets for turning LEDs on and off. Additionally, it demonstrates how to use functions to manipulate pin settings and values effectively.

Uploaded by

mainahibataunga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Embedded

System and
Design
D R M A N OJ TO LA N I ( P H D -IIIT A L L A H A BA D )

A S S I STAN T P RO F E S S OR ( D E PA RTME NT O F I CT )
GPIO Programming & Examples

Lets say we want to set PIN 3 on Port 0 as output. It can be done in following ways:
GPIO Programming & Examples

Lets say we want to set PIN 3 on Port 0 as output. It can be done in following ways:
Example #1)
Consider that we want to configure Pin 4 of Port 0 i.e P0.4 as Ouput and want to drive it High(Logic
1). This can be done as :
Example #1)

Consider that we want to configure Pin 4 of Port 0 i.e P0.4 as Ouput and want to drive it High(Logic
1). This can be done as :
Example #2)
Making output configured Pin 17 High of Port 0 i.e P0.17 and then Low can be does as follows:
Example #2)
Making output configured Pin 17 High of Port 0 i.e P0.17 and then Low can be does as follows:
Example #3)
Configuring P0.5 and P0.11 as Ouput and Setting them High:
Example #3)
Configuring P0.5 and P0.11 as Ouput and Setting them High:
Example #4)
Configuring 1st 8 Pins of Port 0 (P0.0 to P0.7) as Output and Setting them High:
Example #4)
Configuring 1st 8 Pins of Port 0 (P0.0 to P0.7) as Ouput and Setting them High:
GPIO (General Purpose Input/Output)
Ex: Send 0xA5 to P0.15-P0.8 without affecting values on the remaining pins.
This can be accomplished in several ways
Configure P0.1 with Function-01 and P0.15 with Function-02, Value to be loaded to PINSEL

Answer: PINSEL0=1<<2 | 2<<30

Configure P0.17 with Function-03 and P0.30 with Function-01, Value to be loaded to PINSEL

Answer: PINSEL1=3<<2 | 1<<28

Configure P2.3 with Function-01 and P2.14 with Function-02, Value to be loaded to PINSEL

Answer: PINSEL4=1<<6 | 2<<28

Configure P2.17 with Function-03 and P2.5 with Function-01, Value to be loaded to PINSEL

Answer: PINSEL5=3<<2
PINSEL4=1<<10

Send 0xA5 to P0.15-P0.8 without affecting values on the remaining pins. Use FIOMASK and FIOPIN
Answer: FIOMASK=0xFFFF00FF;
FIOPIN=0x0000A500;
Initial pins instruction 0x78560743. Send 0xA5 to P0.23-P0.16 without affecting values on the remaining pins (Instructions). Use
FIOMASK and FIOPIN and find the value of the pins instruction after execution

Answer: FIOMASK=0xFF00FFFF;
FIOPIN=0x00A50000;
Pin o/p after instruction 0x78A50743
LPC_GPIO0->FIOPIN | = 3F<<20

0 0 1 1 1 1 1 1

LPC_GPIO0->FIOPIN | = 2E<<4

0 0 1 0 1 1 1 0
Write an embedded C program to turn ON and OFF LEDs P0.11
connected to P0.11 – P0.4

P0.10
#include <LPC17xx.h>
L P0.9
Variable Initialization
P
int main(void) C P0.8
{
1
i/o Pin selection and Pin Direction 7 P0.7
6
while(1)
{
8 P0.6

Pin Operation P0.5

} P0.4
}
GPIO (General Purpose Input/Output)
P0.11
Write an embedded C program to turn ON and OFF LEDs connected to P0.11 – P0.4
#include <LPC17xx.h>
unsigned int j; P0.10
unsigned long LED = 0x00000FF0;

int main(void) L P0.9


{
SystemInit(); P
SystemCoreClockUpdate();
C P0.8
LPC_PINCON->PINSEL0 = 0x00000000; // P0.15-P0.0 GPIO 1
LPC_GPIO0->FIODIR = 0x00000FF0; // P0.11-P0.4 as output
7 P0.7
while(1)
{ 6
LPC_GPIO0->FIOSET = LED; // SET P0.11-P0.4
for(j=0;j<10000;j++); // Delay
8 P0.6

LPC_GPIO0->FIOCLR = LED; // CLEAR P0.11-P0.4


for(j=0;j<10000;j++); //Delay P0.5
}
}
P0.4

LPC_GPIO0->FIOPIN= ~(LPC_GPIO0->FIOPIN & 0x00000FF0);


for(j=0;j<10000;j++); //Delay
GPIO (General Purpose Input/Output)
Write an embedded C program to turn ON and OFF LEDs connected to P0.11 – P0.4 P0.11
#include <LPC17xx.h>
unsigned int j;
unsigned int LED = 0x0FF0; P0.10
int main(void)
{ L P0.9
SystemInit();
SystemCoreClockUpdate(); P
LPC_PINCON->PINSEL0 = 0x00000000; // P0.15-P0.0 GPIO
C P0.8
LPC_GPIO0->FIODIRL = 0x0FF0; // P0.11-P0.4 as output 1
while(1) 7 P0.7
{
LPC_GPIO0->FIOSETL = LED; // SET P0.11-P0.4 6
for(j=0;j<10000;j++); // Delay 8 P0.6
LPC_GPIO0->FIOCLRL = LED; // CLEAR P0.11-P0.4
for(j=0;j<10000;j++); //Delay
P0.5
}
}
P0.4
LPC_GPIO0->FIOPINL= ~(LPC_GPIO0->FIOPINL & 0x0FF0);
for(j=0;j<10000;j++); //Delay
LED = 0x00000010

0000 0000 0000 0000 0000 0000 0001 0000

LED<<=1

OR

LED=LED<<1

After Execution

LED = 0x00000020

0000 0000 0000 0000 0000 0000 0010 0000


GPIO (General Purpose Input/Output)

8 bit Johnson Counter on LEDs


00000000
#include <LPC17xx.h>
00000001
unsigned int i,j; 00000011
unsigned long LED = 0x00000010; 00000111
00001111
00011111
int main(void) 00111111
{ 01111111
SystemInit() 11111111
11111110
SystemCoreClockUpdate(); 11111100
11111000
11110000
LPC_PINCON->PINSEL0 = 0 11100000
;Configure Port0 pins P0.4-P0.11 ;as GPIO 11000000
10000000
LPC_GPIO0->FIODIR = 0x00000FF0; 00000000
;Configure P0.4-P0.11 as output
GPIO (General Purpose Input/Output)
while(1)
{
LED = 0x00000010; Initial value on LED
for(i=1;i<9;i++) //ON the LED's serially
{
LPC_GPIO0->FIOSET = LED;

for(j=0;j<10000;j++);
LED <<= 1;
}

LED = 0x00000010;

for(i=1;i<9;i++) //OFF the LED's serially


{
LPC_GPIO0->FIOCLR = LED
for(j=0;j<10000;j++);
LED <<= 1;
}

}
}

You might also like