0% found this document useful (0 votes)
8 views9 pages

LPC17xx GPIO Control Examples

The document contains multiple C programs for controlling various hardware components using the LPC17xx microcontroller. It includes code for a buzzer, relay, stepper motor, square wave generator, triangular wave generator, LED control, and a 7-segment display. Each program initializes the system and implements a loop to control the respective hardware based on specific conditions or inputs.

Uploaded by

Sagar Shidesh
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)
8 views9 pages

LPC17xx GPIO Control Examples

The document contains multiple C programs for controlling various hardware components using the LPC17xx microcontroller. It includes code for a buzzer, relay, stepper motor, square wave generator, triangular wave generator, LED control, and a 7-segment display. Each program initializes the system and implements a loop to control the respective hardware based on specific conditions or inputs.

Uploaded by

Sagar Shidesh
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

Buzzer

include "lpc17xx.h"
int main (void)
{
uint32_t j;

SystemInit();

LPC_GPIO1->FIODIR |= 0x08000000; // P1.27 = Outputs

while(1)
{
LPC_GPIO1->FIOCLR = 0x08000000; // Turn-off buzzer
while(LPC_GPIO2->FIOPIN & 0x00000800);
while(!(LPC_GPIO2->FIOPIN & 0x00000800 ));
LPC_GPIO1->FIOSET = 0x08000000;
while(LPC_GPIO2->FIOPIN & 0x00000800);
while(!(LPC_GPIO2->FIOPIN & 0x00000800 ));

}
}
Relay

#include "lpc17xx.h"
int main (void)
{
uint32_t j;

SystemInit();

LPC_GPIO1->FIODIR |= 0x10000000; // P1.28 = Outputs

while(1)
{
LPC_GPIO1->FIOCLR = 0x10000000; // Turn-off relay
while(LPC_GPIO2->FIOPIN & 0x00000800);
while(!(LPC_GPIO2->FIOPIN & 0x00000800 ));
LPC_GPIO1->FIOSET = 0x10000000;
while(LPC_GPIO2->FIOPIN & 0x00001000);
while(!(LPC_GPIO2->FIOPIN & 0x00001000 ));

}
}
Stepper Motor

#include "lpc17xx.h"
void delay();
void delay()
{
int i,j;
for(i=0;i<0xff;i++)
for(j=0;j<0x400;j++);
}
int main (void)
{
char rotate=0;
SystemInit();
LPC_GPIO0->FIODIR |= 0x00078000;
while(1)
{
if(rotate==1)
{
LPC_GPIO0->FIOPIN = 0x00008000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00010000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00020000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00040000 ;
delay();
}
else
{
LPC_GPIO0->FIOPIN = 0x00040000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00020000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00010000 ;
delay();
LPC_GPIO0->FIOPIN = 0x00008000 ;
delay();
}

if(!(LPC_GPIO2->FIOPIN & 0x00000800))


{
while(!(LPC_GPIO2->FIOPIN & 0x00000800));
rotate=1;
}
else if(!(LPC_GPIO2->FIOPIN & 0x00001000))
{
while(!(LPC_GPIO2->FIOPIN & 0x00001000));
rotate=0;
}
}
}
Square Wave

#include <lpc17xx.h>
void delay_ms(unsigned int ms)
{
unsigned int i,j;

for(i=0;i<ms;i++)
for(j=0;j<20;j++);
}

/* start the main program */


int main()
{
SystemInit();
LPC_GPIO0->FIODIR = 0x04000000; //Configure the PORT0 pins as OUTPUT;
while(1)
{
LPC_GPIO0->FIOSET = 0x04000000; // Make P0.26 Port pins as high
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x04000000; // Make P0.26 Port pins as low
delay_ms(200);
}
}
Triangular Wave

#include "LPC17xx.h"
uint32_t result=0x00000040,val;
int main()
{

SystemInit();
LPC_PINCON->PINSEL1 |= 0x02<<20;

while(1)
{
while(1)
{
result=result+128;
val =result &0x0001FFC0;
LPC_DAC->DACR=val;
if(val>=0x0000FFC0)
{
break;
}
}
while(1)
{
result=result-128;
val =result &0x0001FFC0;
LPC_DAC->DACR=val;
if(val<=0x00000040)
{
break;
}
}
}
LED ON/OFF

#include "lpc17xx.h"
#define SwitchPinNumber 11
#define LedPinNumber 19
#define Led1PinNumber 20

int main (void)


{

uint32_t switchStatus;
LPC_GPIO1->FIODIR = 0x07f80000; /* [Link] defined as Outputs */
LPC_GPIO1->FIOCLR = 0x07f80000; /* turn off all the LEDs */
LPC_GPIO2->FIODIR = 0x00000000; /* [Link] defined as Outputs */

while(1)
{

/* Turn On all the leds and wait for one second */


switchStatus = (LPC_GPIO2->FIOPIN>>SwitchPinNumber) & 0x01 ; // Read the switch status

if(switchStatus == 1) //Turn ON/OFF LEDs depending on switch status


{
LPC_GPIO1->FIOPIN = (1<<LedPinNumber);
LPC_GPIO1->FIOPIN = (0<<Led1PinNumber);
}
else
{
LPC_GPIO1->FIOPIN = (0<<LedPinNumber);
LPC_GPIO1->FIOPIN = (1<<Led1PinNumber);
}
}
}
7 Segment Display

#include <lpc17xx.h>

void delay_ms(unsigned int ms)


{
unsigned int i,j;

for(i=0;i<ms;i++)
for(j=0;j<40000;j++);
}

/* start the main program */


int main()
{
SystemInit(); //Clock and PLL configuration

LPC_GPIO0->FIODIR = 0x000001f7; //Configure the PORT0 pins as OUTPUT;

while(1)
{

LPC_GPIO0->FIOSET = 0x00000004; // Make all the Port pins as high

LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000100;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x000001e1;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000090;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x000000c0;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000061;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000042;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000002;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x000001e0;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000000;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000040;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000020;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000003;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000112;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000081;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000012;
delay_ms(200);
LPC_GPIO0->FIOCLR = 0x000001f3;
LPC_GPIO0->FIOSET = 0x00000032;
delay_ms(200);

}
}

You might also like