Saturday, December 10, 2011

keil C Programming Tutorial: Writing simple C program in Keil

►Basic of a C program

As we already discussed, Keil C is not much different from a normal C program. If you know assembly, writing a C program is not a problem, only thing you have to keep in mind is forget your controller has general purpose registers, accumulators or whatever. But do not forget about Ports and other on chip peripherals and related registers to them.

In basic C, all programs have atleast one function which is entry point for your application that function is named as "main" function. Similarly in keil, we will have a main function, in which all your application specific work will be defined. Lets move further deep into the working of applications and programs.

When you run your C programs in your PC or computer, you run them as a child program or process to your Operating System so when you exit your programs (exits main function of program) you come back to operating system. Whereas in case of embedded C, you do not have any operating system running in there. So you have to make sure that your program or main file should never exit. This can be done with the help of simple while(1) or for(;;) loop as they are going to run infinitely. Following layout provides a skeleton of Basic C program.

CODE:
void main(){
//Your one time initialization code will come here
        while(1){
                //while 1 loop
                //This loop will have all your application code
                //which will run infinitely
        }
}


When we are working on controller specific code, then we need to add header file for that controller. I am considering you have already gone through "Keil Microvision" tutorial. After project is created, add the C file to project. Now first thing you have to do is adding the header file. All you have to do is right click in editor window, it will show you correct header file for your project.

Figure below shows the windows context for adding header file to your c file.

include file
►Writing Hardware specific code

In harware specific code, we use hardware peripherals like ports, timers and uart etc. Do not forget to add header file for controller you are using, otherwise you will not be able to access registers related to peripherals.

Lets write a simple code to Blink LED on Port1, Pin1.

CODE:
#include <REGx51.h> //header file for 89C51
void main(){
        //main function starts

        unsigned int i;
        //Initializing Port1 pin1

        P1_1 = 0; //Make Pin1 o/p

        while(1){
                //Infinite loop main application

                //comes here

                for(i=0;i<1000;i++)
                        ; //delay loop

                P1_1 = ~P1_1;
                //complement Port1.1

                //this will blink LED connected on Port1.1

        }
}


You can now try out more programs. "Practice makes a man perfect".
In next section of this tutorial, we will learn how to mix C and assembly codes.

No comments:

Post a Comment