This is the Article to introduce the programming of ARM Cortex-M3 LPC1768 Microcontroller. First step in this journey is to get yourself introduced to the programming Environment. For programming LPC1768 plenty of free software are available online. For ARM based Microcontrollers it is suggested to go with Keil µVision4. For the beginner, Demo version of Keil is more than enough to start with. Let’s start with the brief introduction to the LPC1768 ARM cortex M3 based Microcontroller.
The LPC 1768 is ARM Cortex- M3 based Microcontrollers for embedded application features in low power consumption and a high level of integration. The ARM Cortex M3 is designed in a such way to enhance debug features and a higher level of system integration. It clocks at a CPU frequency of 100 MHz, and incorporates a 3-stage pipeline and uses a Harvard architecture with separate local instruction and data buses for third bus peripherals. The ARM Cortex- M3 CPU have an internal pre-fetch unit to support speculative branching. The peripheral components include 512KB of flash memory, 64kb of data memory, Ethernet MAC, USB OTG, 4 UART’s, 8-channel general purpose DMA controller, 2 SSP Controllers, 10-bit DAC, Quadrature encoder interface, SPI interface, 3 I2C bus interface, 2 input plus 2 outputs I2S bus interface, 4 general purpose timers, ultra-low power Real-Time Clock (RTC) with separate battery supply, and up to 70 general purpose I/O pins, 6-output general purpose PWM. The LPC1768/66/65/64 are pin-compatible with the 100-pin LPC236x ARM7-based Microcontroller series.
Setting up of Programming and debugging Environment:
First step in this process is to install Keil MDK (Microcontroller Development Kit).In the following series of tutorial we are going to use Keil µVision4 integrated with CooCox Debugger. You can download the Keil IDE from the below link.
Step by Step installation guide is explained in this video.
In the following tutorials we will be using CoLinkEx ARM-JTAG Programmer which is USB programmer.
This Debugger is an open source you can also make one for you. It is very easy to use we have to do two things before starting.
-
Install Drivers for the Debugger.
-
Install Plugin for the Keil IDE.
Install the Driver of CoLinkEx:
You can download the driver from http://www.coocox.org/wiki/coocox/CoLinkEx/CoLinkEx-Support#Software.
Install the driver and then plug in the debugger to the computer USB and check whether it is working properly by seeing the Device manager of the Computer.
Fig. 1: Installing Driver of CoLinkEx for Setting up Programming and debugging Environment in ARM
Install the plugin and integrate with Keil MDK:
You can download the driver from http://www.coocox.org/book/coocox/stop-maintanance-product/MDKPlugin/MDKPlugin
Scroll down to the bottom and download the MDKPlugin.
Open the Setup file and install the Plugin in the Directory where Keil is installed.
Fig. 2: Installing Plugin and integrate with Keil MDK in ARM Cortex M3
Register Configuration (lpc17xx.h):
As LPC1768 is 32-bit architecture which is memory mapped to the location 0x2009 C000 to 0x2009 FFFF. Special Function registers of LPC1768 are defined in lpc17xx.h which can be included in the beginning of the project. Ports (PORT0 – PORT4) are available in LPC1768. Even though every PORT will not have 32 physical pins. A structure is defined in the System file LPC_GPIOn(n= 0,1,2,3) contains all the registers for required for GPIO operation. Refer lpc17xx.h file for more info on the registers.
PINSEL: GPIO Pins Select Register:
PINSEL register is to be configured before using the PIN since almost all the pins has a max of four functions. Below table explains the function for a particular pin using two bits of the PINSEL register.
Value |
Function |
Enumeration |
00 |
Primary (default) function, typically GPIO port |
PINSEL_FUNC_0 |
01 |
First alternate function |
PINSEL_FUNC_1 |
10 |
Second alternate function |
PINSEL_FUNC_2 |
11 |
Third alternate function |
PINSEL_FUNC_3 |
Fig. 3: Bit Values of PINSEL Register to select GPIO in ARM Cortex M3
FIODIR:Fast GPIO Direction Control Register.
This register individually controls the direction of each port pin.
Values |
Direction |
0 |
Input |
1 |
Output |
Fig. 4: Bit Values of FIODIR Register to Control Direction in ARM Cortex M3
FIOSET:Fast Port Output Set Register.
This register controls the state of the output pins. Writing 1s produces highs at the corresponding port pins. Writing 0s has no effect. Reading this register returns the current contents of the port output register not the physical port value.
Values |
FIOSET |
0 |
No Effect |
1 |
Sets High on Pin |
Fig. 5: Bit Values of FIOSET Register to Control state of Pin in ARM Cortex M3
FIOCLR:Fast Port Output Clear Register.
This register controls the state of the output pins. Writing 1s produces lows at the corresponding port pins. Writing 0s has no effect.
Values |
FIOCLR |
0 |
No Effect |
1 |
Sets Low on Pin |
Fig. 6: Bit Values of FIOCLR Register to Control Output Pins in ARM Cortex M3
FIOPIN:Fast Port Pin Value Register.
This register is used for both reading and writing data from/to the PORT.
Output: Writing to this register places corresponding values in all bits of the particular PORT pins.
Input: The current state of digital port pins can be read from this register, regardless of pin direction or alternate function selection (as long as the pins are not configured as an input to ADC).
Note:It is recommended to configure the PORT direction and pin function before using it.
Create a project using Keil uvision4 for LPC1768 Microcontroller:
In this section, we will start creating a project in Keil MDK we have already installed Keil µVision and Co-MDK Plug-in + CoLinkEx Drivers required for the CoLinkEx programming adapter. Let’s get started to create a new µVision project to running LED by adding a simple delay between each LED connected to PORT 2 of LPC1768.
Here I am using third party board which consists of four on-board LEDs connected to PORT 2.
The first thing you have to do is download these files and put together in a folder.
Open “Keil µVision4” default workplace and then go to “Project” –> “New µVision Project” and then browse to the folder you created and give it name as blink and hit on “save”.
Fig. 7: Image Showing Keil µVision4 Default Workplace to Create Project for LPC1768 Microcontroller
Fig. 8: Saving Project Name as Blink in Keil µVision4 for LPC1768 Microcontroller
Popup window will show there we have to select “LPC1768” from NXP and then hit on “ok”
Fig. 9: Selecting LPC1768 from Device Target Window in Keil µVision4
Now it ask you to copy “Startup_LPC1768xx.s” and hit on “Yes”
Fig. 10: Image Showing message on µVision4 Window for Copying LPC1768 to Project Folder
Now right click on “Target 1” in the project pane and click on “Add Existing files to the project”
Fig. 11: Image Showing Source Group Window to Add Files in KEIL uVision Software to program LPC1768
Fig. 12: Selecting Existing File Blink from Source Group Window in KEIL to program LPC1768
At this point we need to configure programming adapter CoLinkEx. So click on “Options for Target” on the menu bar and then go to “Debug” –> select “CooCox Debugger” from drop down menu.
Fig. 13: Image Showing Window to Select the Debugger Options to Configure Programming Adapter CoLinkEx.
then hit on Setting –> choose SYSRESETREQ from drop down menu –> hit on “Ok”
Fig. 14: Selecting SYSRESETREQ from Debugger Options
Now go to “Utilities” –> uncheck “Use Debug Driver” and then choose “CooCox Debugger” from drop down menu.
Here again we have to go to “Setting” –> click on “Add” –> and choose “LPC17xx IAP 512kB Flash” from list and hit on “ok”
Hit on “Build” icon on main menu and once we build successfully without any error. Then simply hit on “Load”.
Now we can see that 4 LED connected to P2 is Running. In the next tutorial we would discuss about a program which includes some Input Function.
Code Description:
ARM programming requires good handling of Bit manipulation in C language. Here is the small note in the introduction of Bit manipulation to a newbie.
C has direct support for bitwise operations that can be used for bit manipulation. In the following examples, n is the index of the bit to be manipulated within the variable bit_fld, which is an unsigned char being used as a bit field. Bit indexing begins at 0, not 1. Bit 0 is the least significant bit.
Set a bit
bit_fld |= (1 << n)
Clear a bit
bit_fld &= ~(1 << n)
Toggle a bit
bit_fld ^= (1 << n)
Test a bit
bit_fld & (1 << n)
LPC_GPIO2->FIODIR = 0x000000FF; /* P2.xx defined as Outputs */
LPC_GPIO2->FIOCLR = 0x000000FF; /* turn off all the LEDs */
The above two lines is to SET the PORT 2 pins as output and clearing it. LPC_GPIO2 structure consists of FIODIR,FIOCLR and FIOSET members.
for(i = 0; i < 8; i++)
{
LPC_GPIO2->FIOSET = 1 << i;
for(j = 1000000; j > 0; j–);
}
The above for loop is used to set the bits of PORT 2 one by one using set a bit, which is mentioned above in the bit manipulation types.
You may also like:
Project Source Code
###The codes are linked in Description ###
Project Components
Project Video
Filed Under: ARM, Electronic Projects
Filed Under: ARM, Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.