NPEDUCATIONS, Electronic circuits development, electronics tutorials, microcontroller tutorials and projects, advanced microcontroller (ARM) based tutorials and projects, Embedded c development, Embedded c for ARM cortex M, Intel IoT based projects, IoT projects, CC3200 launch pad projects, MSP430 Launchpad tutorials and projects, Tiva C launch pad tutorials and projects, 8051 tutorials and projects, sensor interfacing with microcontroller tutorials, data communications and networking tutorials, peripheral interfacing with microcontroller, led message scrolling display, arduino based tutorials and projects, intel galileo based tutorials and project, ADC interfacing tutorials, LED Blinking, LCD interfacing, Embedded System tutorials and projects, projects, projects, online Embedded C training

Embedded C program to blink an LED using ARM cortex M processor LPC17xx without using any Header Files

In this tutorial, we are going to look and understand how to write an Embedded C program to blink LED using ARM Cortex M3 processor (LPC1768) without using any processor header files. Here I am using LPC1768-Xplorer base board (from NGX technologies) along with Mbed –Xpresso base board as development board.

Step1: look into Schematic for hardware connection to the LED

In this tutorial, we are going to look and understand how to write an Embedded C program to blink LED using ARM Cortex M3 processor (LPC1768) without using any processor header files. Here I am using LPC1768-Xplorer base board (from NGX technologies) along with Mbed –Xpresso base board as development board.
Figure 1 Schematic Diagram showing LED connection to LPC1768

Step2: Enable GPIO port hardware

Generally, in advanced Microcontrollers (like ARM) there is a possibility to turn ON or OFF different hardware sub sections in it to reduce the power consumption of the processor. This can be done by stopping the power/clock source to that particular hardware section off and this technique is most popularly referred as ‘Clock Gating’. In LPC1768 ARM processor, there is a special function register (SFR) called Power control for Peripheral register (PCONP). Each bit in this PCONP enables or disables one peripheral. i.e., if a peripheral control bit 1, then its corresponding peripheral is enabled otherwise disable. All such SFR are explained in the user guide of LPC17xx. In this tutorial, we need to enable the GPIO hardware in the LPC1768 ARM cortex processor. By looking the user guide of the LPC1768, 15th bit of the PCONP should be 1 in order to enable the GPIO hardware. The below figure2 shows PCONP register configuration according to our requirement.

Figure 2 PCONP register with address
This memory mapped register can be define in embedded c as follows

Embedded C Syntax to define PCONP register:
#define PCONP  (*((volatile unsigned long *)0x400FC0C4))

The above preprocessor instruction requires in some explanation for the beginners. To read a memory mapped special function register content of a Microcontroller in Embedded C, we should take the address of that register (in this case, the PCONP is located at address 0x400FC0C4).

Read_data = 0x400FC0C4;
The above instruction will take the 0x400FC0C4 as data and stores in the variable ‘Read_data’. To read the content of that register, we need to dereference the address as shown below.

Read_data = *(0x400FC0C4);
The above instruction is similar to Read_data = *p, which is a pointer value assigned into the Read_data variable. All ARM cortex registers are 32-bit wide, so we need to type cast the address as ‘unsigned long’.

Read_data = *((unsigned long *) 0x400FC0C4);
The volatile qualifier indicates the compiler that the object pointed by pointer might change even though no instructions in the program appear to change it. This doesn’t make sense for this PCONP register, but this volatile keyword will find its importance when you read the data from a GPIO port data register. Because sometimes you may connect the GPIO data port pin to a push button then the GPIO port data register bits may change when the user presses or release the push button, even though there is no related instructions are executed.

Read_data = *((volatile unsigned long *) 0x400FC0C4);

Step 3: Configure the GPIO Port0 Pins as digital pins

Till now we understand, how to power up individual hardware section in the ARM processor (LPC17xx) using PCONP register. And also we got the coding syntax to declare a memory mapped register. As stated above the ARM cortex M processor is advanced processor in which each port pin may have multiple functionalities. For example, P0.10 in LPC1768 can be used as GPIO, TXD2 for serial communication, SDA2 for I2C communication, and as MAT3.0. To select a particular functionality of a pin, the LPC17xx ARM cortex M processor provides a special multiplexed hardware called “Pin Connect Block”. The pin connect block allows to select a particular function on the pin using some set of SFR registers.

There are three set of special function registers are available to configure the functionality of the pin as shown in the below figure. 
1.       Pin Function Select Registers – These registers are used to configure the single functionality of a pin from multiple functionalities. These registers range from PINSEL0 to PINSEL10, and to configure PORT 0 lower half (0-15) pins PINSEL0 register is used. Similarly, to configure upper half (16-31 pins) of the PORT 0 pins, PINSEL1 register is used. For further ports configuration look into the datasheet.
2.       Pin Mode Select Registers – These registers are used to control the input mode of all ports. i.e., these registers are used to configure the pull up/ pull down resistors of all ports and these registers ranges from PINMODE0-PINMODE9. The on-chip pull-up/ pull-down resistor can be selected for every port regardless of the pin functionality except when I2C and USB configuration. PINMODE0 and PINMODE1 registers are used to control pull-up/ pull-down resister for lower and upper half of the GPIO Port respectively.
3.       Open Drain Pin Mode Selection Registers – These are five registers (ranges from PINMODE_OD0 to PINMODE_OD4) used to control the open drain mode of all ports.

In this simple LED blinking tutorial, the LED pin of LPC17xx (P0.10) is to be configured as simple GPIO pin. To do so, we need to configure the PINSEL0 register 20 and 21 bits as ‘00’ combination and its value become ‘0x00000000’. In LPC17xx, the PINSEL0 register is located at address 0x4002C000. I am not using any Pull-up/Pull-down and open drain configurations here, so PINMODE_OD1 and PINMODE_OD0 registers are not used in this tutorial. 
Figure 5 PINSEL0 register

Embedded C Syntax to define PINSEL0 register:
#define PINSEL0  (*((volatile unsigned long *)0x4002C000))

To load a value 0x00000000 in to PINSEL0, Syntax as follows
PINSEL0 = (unsigned long)0x00000000;

Step4: Configure GPIO Port registers

There are five separate registers available for each port in LPC17xx ARM Cortex processor. These registers will allow operations like read, write and set direction of the data on the GPIO port.
1.       FIODIR – Fast GPIO Port Direction control register: used to control the data direction (‘0’ for Input or ‘1’ for Output) of each port pin.
2.       FIOMASK – Fast MASK register for port: used to mask the GPIO operation (read, write) on particular port pin.
3.       FIOPIN – Fast Port PIN value register: used to read the current state of digital port pins.
4.       FIOSET – Fast Port Output Set register: used to produce high state at output.  Writing 1 produces logic high at output and writing 0 has no effect.
5.       FIOCLR – Fast Port Output Clear Register: used to produce low state at output. Writing 1 produces logic high at output and writing 0 has no effect.

Why the name ‘Fast’ comes to every register in this LPC17xx processor?
All the GPIO in LPC17xx ARM cortex processor connected to Advanced High Speed bus (AHB) for faster access. Hence GPIO registers uses ‘fast’ keyword in their register name.

Now we will configure the GPIO registers for Port0. Firstly, Configuring Port Direction register FIODIR, setting a bit in this register will make that particular pin as output otherwise input as shown below.  Since our LED is an output device which is connected to P0.10 pin of LPC17xx, so we need to set 10th bit as 1 in the FIODIR register as shown below. It is located at address 0x2009C000.


Embedded C Syntax to define FIODIR register:
#define FIODIR (*(volatile unsigned long *) 0x2009C000)

To set a particular bit in the FIODIR register, we can use bitwise operators in Embedded C as follows. The below embedded c syntax (using OR operation) will enable the 10th bit without effecting the other bits.

FIODIR = FIODIR |((unsigned long)1 << 10);  or simply use compound assignment statements - FIODIR|=((unsigned long)1 << 10);

(Unsigned long)1 – the type casting of (unsigned long) will tell the compiler to make the 1 as 32-bit number.

Configuring Port Mask register, a zero in mask register will allow the GPIO operations (read, write), otherwise the operations are not allowed.

Embedded C Syntax to define FIOMASK register:
#define FIODIR (*(volatile unsigned long *) 0x2009C010)

Configuring PORT 0 MASK register 10th bit to zero. The below embedded c syntax (using AND operation) will make the 10th bit zero without effecting the other bits. The tiled operator makes the bit to zero.

FIOMASK &= ~((unsigned long)1 << 10);

The embedded C syntax for FIOSET and FIOCLR are similar to above register declaration.
Now it times to write the complete Embedded C program
1.       Declare any Header files required
2.       Define any preprocessing directives (for SFR register address)
3.       Define any Function Prototypes
4.       Define any Global Variables

/*Preprocessor Declaration for ARM registers*/

#define PCONP    (*((volatile unsigned long *) 0x400FC0C4)) // Power control register
#define PINSEL0  (*((volatile unsigned long *) 0x400FC0C4)) // PORT0 Special Funciton Select Register
#define FIO0DIR  (*((volatile unsigned long *) 0x400FC0C4)) // PORT0 Data Direction register
#define FIOMASK  (*((volatile unsigned long *) 0x400FC0C4)) // PORT0 MASK register
#define FIOSET   (*((volatile unsigned long *) 0x400FC0C4)) // PORT0 BIT SET register
#define FIOCLR   (*((volatile unsigned long *) 0x400FC0C4)) // PORT0 BIT CLR register

/*Function Prototype*/
void delay(unsigned long);

/*Global Vaiablr Declarations*/

/*main function*/

void main()
                // Enable the Clock to Port0 using PCONP register
                PCONP |= ((unsigned long)1<<15);

                // Select simple GPIO functionality of PORT0.10 pin using PINSEL0
                PINSEL0 |= ~((unsigned long)1<<10);

                // Set the PORT0.10 pin to output using FIODIR
                FIODIR |= ((unsigned long)1<<10);

                // Make the mask bit of PORT0.10 to zero
                FIOMASK |= ~((unsigned long)1<<10);

                // super loop to blink LED
                                FIOSET |= ((unsigned long)1<<10);          // LED ON
                                FIOSET |= (~(unsigned long)1<<10);        // LED OFF

void delay (unsigned long value)
                unsigned long i;



Thanks for visiting NPEDUCATIONS. We will contact you soon


Jig Saw said...
This comment has been removed by the author.
jaastark said...

great post. thanks for sharing this information
KineMaster for pc
Free fire for pc

Post a Comment

Thanks for visiting NPEDUCATIONS. We will contact you soon

If you really like this tutorial, Don't forget to give the comment or please subscribe to the RSS feed by submitting your E-mail or like our Facebook page.

Powered by Blogger