0% found this document useful (0 votes)
235 views7 pages

GPIO Pin Toggle Example Code

The document contains 14 code snippets that each toggle a GPIO pin on an LPC11xx microcontroller by toggling its state in an infinite loop. Each snippet configures the GPIO clock, sets the pin direction as output, and continuously sets the pin high and low with delays in between. This toggles the pin to generate a square wave on the corresponding physical pin of the microcontroller.

Uploaded by

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

GPIO Pin Toggle Example Code

The document contains 14 code snippets that each toggle a GPIO pin on an LPC11xx microcontroller by toggling its state in an infinite loop. Each snippet configures the GPIO clock, sets the pin direction as output, and continuously sets the pin high and low with delays in between. This toggles the pin to generate a square wave on the corresponding physical pin of the microcontroller.

Uploaded by

crsarin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include "LPC11xx.

h"
int main(void) {
//Toggle a GPIO (FBD48 pin 48, PIO3_3)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO3_3
&= ~(0x10);
pull-up (sec 7.4.42)
LPC_GPIO3->DIR
|= (1<<3);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO3->DATA
|= (1<<3);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO3->DATA
&= ~(1<<3);
for(i=0; i<0xFFFFF; ++i);

//set pin high (sec 12.3.1)


//delay
//set pin low (sec 12.3.1)

}
return 0 ;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
PCB)

//Toggle a GPIO (FBD48 pin 23, PIO0_7, drives the LED on the LPCExpresso

LPC_SYSCON->SYSAHBCLKCTRL
3.5.14)
LPC_IOCON->PIO0_7
pull up (sec 7.4.19)
LPC_GPIO0->DIR
output (sec 12.3.2)

|= (1<<6);

//enable clock GPIO (sec

&= ~(0x10);

//NOT NECESSARY, turn off

|= (1<<7);

//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO0->DATA
|= (1<<7);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO0->DATA
&= ~(1<<7);
for(i=0; i<0xFFFFF; ++i);
}
return 0 ;
}

//set pin high (sec 12.3.1)


//delay
//set pin low (sec 12.3.1)
//delay

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 24, PIO2_9)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO2_9
&= ~(0x10);
pull up (sec 7.4.20)
LPC_GPIO2->DIR
|= (1<<9);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO2->DATA
|= (1<<9);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO2->DATA
&= ~(1<<9);
for(i=0; i<0xFFFFF; ++i);

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
return 0 ;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 19, PIO2_4, PIN IS MISLABELLED ON SILKSCREEN)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
//enable clock GPIO (sec
3.5.14)
LPC_IOCON->PIO2_4
&= ~(0x10);
//NOT NECESSARY, turn off
pull up (sec 7.4.15)
LPC_GPIO2->DIR
|= (1<<4);
//set pin direction to
output (sec 12.3.2)
unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO2->DATA
|= (1<<4);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO2->DATA
&= ~(1<<4);
for(i=0; i<0xFFFFF; ++i);

}
return 0 ;

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 22, PIO0_6)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO0_6
&= ~(0x10);
pull up (sec 7.4.18)
LPC_GPIO0->DIR
|= (1<<6);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO0->DATA
|= (1<<6);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO0->DATA
&= ~(1<<6);
for(i=0; i<0xFFFFF; ++i);

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
return 0 ;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 27, PIO0_8)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO0_8
&= ~(0x10);
pull up (sec 7.4.23)
LPC_GPIO0->DIR
|= (1<<8);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO0->DATA
|= (1<<8);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO0->DATA
&= ~(1<<8);
for(i=0; i<0xFFFFF; ++i);

}
return 0 ;

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 31, PIO2_11)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO2_11
&= ~(0x10);
pull up (sec 7.4.27)
LPC_GPIO2->DIR
|= (1<<11);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO2->DATA
|= (1<<11);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO2->DATA
&= ~(1<<11);
for(i=0; i<0xFFFFF; ++i);

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
return 0 ;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 36, PIO3_0)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO3_0
&= ~(0x10);
pull up (sec 7.4.32)
LPC_GPIO3->DIR
|= (1<<0);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO3->DATA
|= (1<<0);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO3->DATA
&= ~(1<<0);
for(i=0; i<0xFFFFF; ++i);
}
return 0 ;

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////

#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 37, PIO3_1)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO3_1
&= ~(0x10);
pull up (sec 7.4.33)
LPC_GPIO3->DIR
|= (1<<1);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO3->DATA
|= (1<<1);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO3->DATA
&= ~(1<<1);
for(i=0; i<0xFFFFF; ++i);

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
return 0 ;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 38, PIO2_3)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO2_3
&= ~(0x10);
pull up (sec 7.4.34)
LPC_GPIO2->DIR
|= (1<<3);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO2->DATA
|= (1<<3);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO2->DATA
&= ~(1<<3);
for(i=0; i<0xFFFFF; ++i);
}
return 0 ;

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"

int main(void) {
//Toggle a GPIO (FBD48 pin 43, PIO3_2)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO3_2
&= ~(0x10);
pull up (sec 7.4.38)
LPC_GPIO3->DIR
|= (1<<2);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO3->DATA
|= (1<<2);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO3->DATA
&= ~(1<<2);
for(i=0; i<0xFFFFF; ++i);

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
return 0 ;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {
//Toggle a GPIO (FBD48 pin 45, PIO1_5)
LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO1_5
&= ~(0x10);
pull up (sec 7.4.39)
LPC_GPIO1->DIR
|= (1<<5);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO1->DATA
|= (1<<5);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO1->DATA
&= ~(1<<5);
for(i=0; i<0xFFFFF; ++i);
}
return 0 ;

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
#include "LPC11xx.h"
int main(void) {

//Toggle a GPIO (FBD48 pin 13, PIO2_1)


LPC_SYSCON->SYSAHBCLKCTRL
|= (1<<6);
3.5.14)
LPC_IOCON->PIO2_1
&= ~(0x10);
pull up (sec 7.4.9)
LPC_GPIO2->DIR
|= (1<<1);
output (sec 12.3.2)

//enable clock GPIO (sec


//NOT NECESSARY, turn off
//set pin direction to

unsigned int i = 0;
while(1){

//infinite loop

LPC_GPIO2->DATA
|= (1<<1);
for(i=0; i<0xFFFFF; ++i);
LPC_GPIO2->DATA
&= ~(1<<1);
for(i=0; i<0xFFFFF; ++i);

}
return 0 ;

//set pin high (sec 12.3.1)


//arbitrary delay
//set pin low (sec 12.3.1)
//arbitrary delay

Common questions

Powered by AI

Each code example uses a similar methodology: configuring the clock for GPIO operations, optionally disabling the pull-up resistor configuration, setting the GPIO pin as output, and implementing an infinite loop to toggle the pin's state with delays in between. This approach ensures a consistent control structure for managing GPIO pins, which can be adapted to different pins and ports through minor modifications in register selections and pin semantics .

The code snippets explicitly force the pull-up resistor configuration to a known state by clearing the relevant bit in the IOCON register, although the document states this step might not be necessary. This intentional configuration empowers developers to ensure consistent behavior across different hardware settings or initial states, regardless of default conditions that might vary between manufacturing batches or erroneous initial states. Ensuring a stable starting state prevents unexpected behaviors due to unnoticed initial pin configurations .

The code snippets are designed to toggle General Purpose Input/Output (GPIO) pins on a microcontroller platform utilizing LPC11xx series for various pin configurations. These examples aim to blink LEDs connected to the specific GPIO pins by setting them high and low, implemented using an infinite loop with arbitrary delay intervals to form the visible blinking action .

The line `LPC_IOCON->PIOX_X &= ~(0x10);` is designed to clear the bit related to the pull-up resistor configuration in the IOCON register, effectively disabling it. The document mentions this line as unnecessary because it indicates the code configuration might not require changing the existing pull-up state for the functionality desired, possibly due to either internal chip defaults or external circuit considerations .

Every code snippet begins by enabling the master clock for the GPIO block by writing to the SYSAHBCLKCTRL register. This step is fundamental because, in microcontroller operations, peripherals do not function unless their corresponding clocks are enabled. The clock provides the necessary timing reference for the operations of the GPIO pins such as setting high or low states, ensuring that the pin control tasks are paced and synchronized correctly with the rest of the system operations. Neglecting to enable the clock may leave the GPIO pins non-functional or unpredictable .

The code employs a for-loop that creates an arbitrary delay in each iteration of the main infinite loop. After setting the GPIO pin to high, the delay loop runs. Once the loop completes, the GPIO pin is set to low, followed by another execution of the delay loop before returning to the beginning of the infinite loop. Thus, these delay loops ensure the pin toggles at regular, albeit arbitrary, intervals {.

The pin direction is set using the line `LPC_GPIOX->DIR |= (1<<N);`, where `X` and `N` correspond to the specific GPIO port and pin number respectively. This operation ensures that the particular pin is configured as an output, which is necessary for driving LEDs or other external devices as intended in the code. Setting pin direction is crucial because it informs the microcontroller's hardware whether the pin should emit data or read input signals .

If the delay loop condition were altered, it would directly affect the frequency of the toggle operation, thereby changing the flashing rate of connected LEDs or the response time of connected devices. A shorter loop might cause the GPIO pin to toggle rapidly, potentially beyond the observable threshold for an LED, while a longer loop would slow down the blinking appearance. Adjusting the loop condition could also impact current consumption and reliability, especially if timing synchronization with other operations is crucial .

Hardware discrepancies like a mislabelled silkscreen can lead to incorrect hardware connections, misunderstanding of component layouts, or erroneous debugging. Users might incorrectly associate the wrong pin with its intended function, causing logical errors in circuit design or testing. Addressing this requires verifying pin configurations through the microcontroller's datasheet or schematics instead of relying solely on silkscreen print, providing corrections in documentation or labeling on the board for accurate reference .

This line enables the clock for the GPIO block in the microcontroller by setting the 6th bit of the SYSAHBCLKCTRL register. By enabling this clock, you ensure that the GPIO operation is timed correctly and the pins can be set high or low as intended .

You might also like