Criando Biblioteca para LPC1768- (Parte 2/21)

Creating Library for LPC1768- (Part 2/21)

This is another article that introduces the programming of ARM Cortex-M3 LPC1768 microcontroller. Most tutorials will use pre-made libraries in the following tutorials. Sometimes it is necessary to create a new library for your own project. This tutorial explains how to make a custom library in Keil and also includes a library built for the GPIO functions of the LPC1768. The environment setup for ARM cortex M3 development is well discussed in this article.

The LPC 1768 is an ARM Cortex-M3-based microcontroller for embedded application capabilities with low power consumption and a high level of integration. The ARM Cortex M3 is designed in a way to enhance debugging capabilities and a higher level of system integration. It runs 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 has an internal prefetch unit to support speculative branches. Peripheral components include 512 KB flash memory, 64 KB data memory, Ethernet MAC, USB OTG, 4 UARTs, 8-channel general-purpose DMA controller, 2 SSP controllers, 10-bit DAC, quadrature encoder interface, SPI interface, 3 bus I2C interface, 2-input plus 2-output I2S bus interface, 4 general-purpose timers, ultra-low power real-time clock (RTC) with separate battery supply, and up to 70 user I/O pins General, 6-output general-purpose PWM. The LPC1768/66/65/64 are pin compatible with the 100-pin LPC236x ARM7 based series of microcontrollers.

Libraries:

There is a library of two files, a code file and a header file. They should have the same name, mat_lib for example. The code file is called mat_lib.c and the header file mat_lib.h. The code file is the file that contains the actual code that should be executed and the header files are just a list with all the library instructions inside. To add new files to a project:

Adicionar novos arquivos para projeto em LPC1768

Fig. 1: Add new files to project in LPC1768

For this example, we will make the following code in a library:

A = A * 2;

A = A + 2;

The code simply multiplies a variable by 2 and then adds 2. The instruction will be named A = Multiply sum2(A); For the library it is necessary to create 2 files in the project. AC file and an H file as explained above.

The library code should be placed in a function like this:

empty Multiplyadd2(empty)

{

//instruction

}

Void means empty, if an instruction does not need to send any data back, void can be used. The variable we want to modify needs to be sent with the instruction and the instruction must send a value back. To change the instruction one can use variables from the main code to which it should be changed:

void Multiplyadd2 (variable uint32_t)

Instead of uint32_t any variable can be used. Multiple variables can also be used:

(Variable uint32_t, Variable uint16_t2, Char char3)

The command for the line above is : A = Multiplyaddition2(Variable, Variable2, Character3); For the command to multiply and add 2 to a variable, just use 1 variable, the one that needs to be multiplied, etc.

The instruction must also send the value back after the calculations are complete. To do this, the void at the beginning must be changed into a variable:

uint32_t Multiplyadd2(variable uint32_t)

{

}

Now the actual code can be added to the statement:

uint32_t Multiplyadd2(variable uint32_t)

{

Variable = Variable * 2;

Variable = Variable + 2;

return variable;

}

The return statement sends the variable back after the calculations are complete. The .c file also needs some additions. At the top of the .c file, the following includes need to be added:

#include “lpc17xx.h”

#include “types.h”

#include “mat_lib.h”

These inclusions are necessary so that the library knows which microcontroller is used and which variables can be used in the library. You can also add other includes for other libraries so that they can be used in the new library.

The only thing left is to add the code from the .h file. The .h files list all the instructions that can be used. This example library has only one instruction, so the code is:

uint32_t Multiplyadd2(variable uint32_t);

To use the library, simply include Tutlibrary.h in the main code and it will be ready to use. Creating libraries for the LPC1768 works the same way as creating libraries for any C programming language.

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. You can start by downloading the project files and try out the library made for the LPC1768's GPIO.

Code.rar

Code description:

ARM programming requires good handling of bit manipulation in C language. Here is a small note on introducing bit manipulation for 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 character being used as the bit field. Bit indexing starts at 0, not 1. Bit 0 is the least significant bit.

Define a little

bit_fld = (1 <

Clean up a little

bit_fld &= ~(1 <

Alternate a little

bit_fld ^ = (1 << n)

Test a little

bit_fld & (1 <

The GPIO library provided above includes a total of 7 functions that can be used to complete GPIO actions easily.

· GPIOSetDir(uint32_t portnumber, uint32_t bitPosi, uint32_t dir);

· GPIOSetValue(uint32_t portnumber, uint32_t bitPosi, uint32_t bitVal);

· GPIOSetPull(uint32_t portNum, uint32_t bitPosi, uint32_t dir);

· GPIOGetValue (uint32_t portNum, uint32_t bitPosi);

· GPIOSetInterrupt (uint32_t portNum, uint32_t bitPosi, uint32_t dir);

· GPIOClearInterrupt(void);

· uint32_t GPIOCheckInterrupts ( uint32_t portNum, uint32_t dir);

GPIO ports can be used as input or output, for this tutorial the GPIO port is used as output. Instructions for configuring a GPIO port:

GPIOSetDir(portnum, bitposi, dir);

Portnum is the port number, it can be from 0 to 4 depending on the IO port used. Bitposi is the bit position of the port number; this can be from 0 to 31 depending on the IO port used. Dir is the direction, 0 for entry and 1 for exit. You can also type Input or Output. The IO port used in this tutorial is GPIO-1.25 and is used as output. The correct instruction to configure the IO port is:

GPIOSetDir(1, 25, OUTPUT); or:

GPIOSetDir(1, 25, 1);

To increase or decrease an IO, the command: GPIOSetValue(portnum, bitposi, valor);

Portnum and bitposi are the same as the GPIOSetDir command. The value determines whether an IO is on or off, 0 for Low for off and 1 or High for on. To activate pin IO 1.25 in the command, do the following:

GPIOSetValue(1, 25, HIGH); or

GPIOSetValue(1, 25, 1);

The name of the other functions will explain their use. You can compare these library functions with the LPC1768 datasheet for better understanding.

Project source code

 ###
The codes are linked in Description ###

Circuit diagrams

Circuit Diagram Creating Library for LPC1768

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.