Monday, January 21, 2013

C library for microcontrollers - How to use..??

using C library for 8051
Most embedded systems are currently being developed in C language. The main advantage of programming microcontroller in C language is that we can develop our own embedded library in C and use it anytime. Lots of development tools are available to write source code in embedded C for example keil, mikroC, SDCC etc.

I am gonna upload some of the important embedded C library which you can directly use in your project with little or no modification. I will recommend please go through every tiny part of my library especially the declaration part of pins, ports and variables because this will help you in designing your circuit diagram and developing your algorithm. So before going to this article lets have some clear understanding about C library.

What is C library..??

Every C library contains variables and functions which are responsible perform some process or action defined by users and return some value or data. The major component of library are....

  • Header File (having ".h" extension)
  • Standard C file (having ".c" extension)

1) Header File:  Header file consists variable declaration and the function declaration (prototype) which are being defined in C file. If you want to use your header file in more than one file than variables should be declare by "extern" keyword in header file. Functions can also be declared by "extern" keyword

2) C file: The C file contains the definition of function which are declared in header file and may or may not be return some value to the source file. If function is returning any value then it must be stored in variable which having specific data type i.e. same as data type of return variable at the time of function call.

How to use C library of ThE nIk'S wOrLd NiK's RuLeS..?? 

Here i am only going to discuss about Keil. Download library files from link given at the bottom of this post and unzip it. Now paste both the files i.e. header file and C file in your project directory.

Now in Project Workspace right click on Source Group 1 and then click on Add File to Group 'Source Group 1'

Adding files to source group in keil
Adding ".C" file in Source Group
Now one dialogue box appear in that dialogue box select "C Source File (*.c)" from "Files of Type". So it only shows the file having ".C" extension. Select the file which you want to add and click on "Add" button and then click on "Close" button.

Selecting ".C" file to be add in Source Group
Selecting ".C" file to add in Source Group
Verify the Project Workspace window now showing the file in your source group. Make sure that it must have extension of ".c"

So we are done with our one file i.e. C source file of library. Lets come to adding the header file. It is very simple but make sure that both this file must be available in your current project directory. To add the header file just simply write the following line of code in your main source file (The file having main function).

#include<name_of_header_file.h>

here name_of_header_file must be replaced by the actual file name.

To verify whether the file added accurately and working perfectly click on "Translate Current File" in Build tool. After finishing process header file must be appear under the main source file.

After complete procedure your code should look like similar to below piece of code.
#include<reg51.h> // Standard header file for 8051 family
#include<name_of_header_file.h> // replace "name_of_header_file" with actual file name

// Function Prototyping..No need for the functions of library

// Global variable declaration...

// Pin and Ports declaration...

void main()
{
    // Code statements...

    While(1)   // Repeat forever..
    { 

       // Code statements...

    }

} 

For download library file click here...  and navigate to list of libraries.

If you having any query or doubt feel free to use comment...

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.