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.

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.

#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.

Conversation

ajay_bhargav
Sat Aug 30 2014, 12:15 PM
@sufiyan, You can use solenoid lock in place of LED.
Fri Aug 29 2014, 12:17 AM
sufiyan
please can you tell me which type of lock activator should i connect in place of LED? can i use selenoid lock actuator
ajay_bhargav
Thu Aug 28 2014, 06:34 AM
Please post questions in forum
Thu Aug 28 2014, 01:44 AM
yednekachew
can you give a project made bay proteus software
Mon Aug 25 2014, 07:12 AM
Vinod Soni
Hi Sir, How can i make this RFID with ATMega16/32 controller
pavanjain
Sat Aug 23 2014, 05:10 AM
Sir i want to download the hex file in P89v51rd2 microcontroller using max232 IC but we not load the program in IC
Tue Aug 19 2014, 04:51 PM
allen ndumu
please i will like to know if the problem of speed control of dc motor using keypad and l298 was if yes can i have the complete project send it to the address i really need to see it
Tue Aug 19 2014, 10:47 AM
Amit Bisht
sir i want to have some trending projects on which i could work related to ATmega 16 microcontroller
ExperimenterUK
Sat Aug 16 2014, 04:16 PM
@diligent . create a thread
Sat Aug 16 2014, 03:17 PM
diligent
in assembly langauage programming..

Downloads

Comments

bobparihar
Mon Sep 01 2014, 06:19 AM
vijit
Sun Aug 31 2014, 08:33 AM
[email protected]
Sat Aug 30 2014, 04:45 PM
enisi
Wed Aug 27 2014, 06:08 AM
sadalakshmi
Wed Aug 27 2014, 12:39 AM
Paecake
Sun Aug 24 2014, 04:07 PM
pavanjain
Sat Aug 23 2014, 02:33 AM
samchovatiya
Thu Aug 21 2014, 02:30 PM

Online

Guests: 44, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 01:37 AM

Members: 37504
Newest member: bobparihar
Forum Activity
bobparihar Avatar
Posted By bobparihar
Tue Sep 02 2014, 01:59 AM
iam using EM-18 reader module.. but i want only to...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Sep 01 2014, 10:59 PM
What is your graph displaying ?I'm assuming it is ...
Peter_Parker Avatar
Posted By Peter_Parker
Mon Sep 01 2014, 06:19 PM
Just to add to this,I've connected this now to a C...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Sep 01 2014, 07:28 AM
Which RFID module are you using? Post link to the ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Sep 01 2014, 07:28 AM
We prefer to use English language on our forums. B...
bobparihar Avatar
Posted By bobparihar
Mon Sep 01 2014, 06:33 AM
i am using a RFID module, i want to connect reader...
ajay_bhargav Avatar
Posted By ajay_bhargav
Sat Aug 30 2014, 12:03 PM
Forgot to attach files. This has source for both R...
ajay_bhargav Avatar
Posted By ajay_bhargav
Sat Aug 30 2014, 11:56 AM
I found a very good working source but its for PIC...