In depth LED Blink with NXP LPC17xx


This is going to be a very interesting tutorial for us, as being an embedded developer we all start training with LED blinking, or in other words Hello Word. In this tutorial we are going to discuss in depth LED blink with NXP LPC17xx controller.

I assume that you are well aware of embedded C and knowledge of microcontrollers.

#include “LPC17xx.h”

This file is common for all 17xx series controllers. In this header file, interrupts, GPIO pins, protocols definitions and the memory mappings are done here.

LED_Blink

volatileuint32_t msTicks;

volatile tells the compiler not to optimize anything that has to do with the volatile variable.

The main and only reason is, when we are interfacing with hardware, and then we need to use volatile keyword.

uint32_t is coming from the following line.

typedef unsigned long uint32_t ;

If you are coming from C, then you don’t need any introduction about typedef. If not, typedef is a keyword in Cwhich is used to assign alias name for any existing data type.

/*——————————————————————————

SysTick_Handler

*——————————————————————————*/

void SysTick_Handler(void)

{

msTicks++;

}

1. Here we have External Interrupts and Internal Interrupts(system interrupts). For the systeminterrupts, there are already a set of handler functions also defined in our libraries.

Here, SysTick_Handler is the handler function for one of the internal interrupt.

2. So, the next question is when it will be called????

Well, when you configure the systick timer, then for every reload buffer overwritten, it causes aninterrupt, thenthe handler function SysTick_Handler will be called.

3. So, if you want to see from where these handlers are getting called by interrupts??

Then go and open your startup_LPC17xx.h header file. If you observe the vector table address mapping, thereyou will find the system interrupts and external interrupts.

 

/*——————————————————————————

System Delay

*——————————————————————————*/

__INLINE static void Delay (uint32_t dlyTicks)

{

uint32_tcurTicks;

curTicks = msTicks;

while ((msTicks – curTicks) <dlyTicks);

}

This function gives the required delay for us. For every 1 msec, the global variable msTicks will get increments inSysTick_Handler function, so, until the required time gets over, the control will be in while loop which is inDelay function.

/*——————————————————————————

configure LED pins

*——————————————————————————*/

__INLINE static void LED_Config(void)

{

LPC_GPIO1->FIODIR = 0xB0000000; /* LEDs PORT1 are Output */

}

Here, LPC_GPIO1 is a macro. The declaration is in LPC17xx.h header file. This macro consists of address

location which is base address to the port 1. So, by using this structure base address, we can access port direction

register, set register, clear registers. All these structures are defined in the same LPC17xx.h header file.

/*——————————————————————————

Switch on LEDs

*——————————————————————————*/

__INLINE static void LED_On (uint32_t led)

{

LPC_GPIO1->FIOPIN |= (led); /* Turn On LED */

}

As we discuss in the above paragraph, FIOPIN is a structure member which is used to read the output. ‘led’ is

the pin number which is connected to LED. Here, when you receive the pin number, due to ORing operation that

pin gets high which results the LED makes glow.

/*——————————————————————————

Switch off LEDs

*——————————————————————————*/

__INLINE static void LED_Off (uint32_t led)

{

LPC_GPIO1->FIOPIN &= ~(led); /* Turn Off LED */

}

As I said above, when the function receive the pin number, that particular pin become low, so the LED will go off.

To go to next page click here

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (2)
  1. tarik says - Posted: March 16, 2013

    hello,
    what do you advise me,lpcxpresso board or arduino board to begin with embedded systems and are lpcxpresso an open source hard and soft, because i want to use them free

    thanks

  2. jalaja says - Posted: July 19, 2013

    Need more information on this……..

Leave your comment

Your email address will not be published. Required fields are marked *