1. Introduction

Most tutorials use pre made libraries. Sometimes it is needed to make a new library for your own project. This tutorial explains how to make a custom library for the Simplecortex. Also it explains how to read the datasheet and use functions of the Simplecortex without the libraries.
2. Libraries

A library exists of two files, a code file and a header file. They should have the same name, Tutlibrary for example. The code file is called Tutlibray.c and the header file Tutlibrary.h. The code file is the file containing the actual code that has to be executed and the header files is just a list with all instructions of the library inside. To add new files to a project:
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 = Multiplyadd2(A); For the library 2 files need to be made in the project. A C file and an H file as explained above.
The code for the library has to be placed in a function like this:

void Multiplyadd2( void )

Void means emptiness, if a instruction doesn’t need to send any data back void can be used. The variable we want to modify needs to be send with the instruction and the instruction has to send a value back. To change the instruction it can use variables from the main code it has to be changed to:

void Multiplyadd2(uint32_t Variable)

Instead of uint32_t any variable can be used. Multiple variables can be used too:
(uint32_t Variable, uint16_t Variable2, Char char3)
The command for the above line is: A = Multiplyadd2(Variable, Variable2, Char3); For the command to multiply and add 2 to a variable it’s just needed to use 1 variable, the one that needs to be multiplied etc.
The instruction also has to send the value back after it is done with the calculations. To do this the void in the beginning has to be changed in a variable:
uint32_t Multiplyadd2(uint32_t Variable)
Now the actual code can be added to the instruction:
uint32_t Multiplyadd2(uint32_t Variable)
Variable = Variable * 2;
Variable = Variable + 2;
return Variable;
The return instruction sends the Variable back after the calculations are done. The .c file also needs some includes. In the top of the .c file the following includes needs to be added:

#include "lpc17xx.h"
#include "type.h"
#include "Tutlibrary.h"

For CoIDE type.h has to be replaced with LPC_types.h. These includes are needed so the library knows what microcontroller is used and that variables can be used in the library. It is also possible to add other includes for other libraries so those can be used in the new library.
The only thing that is left is add the code for the .h file. In the .h files all instructions that can be used are listed. This example library only has one instruction so the code is:

uint32_t Multiplyadd2(uint32_t Variable);
To use the library just include Tutlibrary.h in the main code and it is ready to use. Making libraries for the Simplecortex works the same as making libraries for any C programming language so google can help a lot if you need more information. The sample libraries can be downloaded from our repository.
Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics