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

ExperimenterUK
Thu Nov 20 2014, 06:00 PM
@Mehboob.. I don't think you can do that.
Sat Nov 15 2014, 06:41 AM
Mehboob
Pls can you send me code in c of PWM generation using UART/serial port in 8051.
Fri Nov 14 2014, 02:51 PM
mohammad aqil
NEED A BOOK ON MICROPROCESSOR AND INTERFACING BY D.V.HALL. RIGHT NOW, I SHALL BE VERY THANK FULL TO U....
Thu Nov 13 2014, 02:15 PM
m
please code hex project avr blood pressure monitor
Thu Nov 13 2014, 05:56 AM
Lambo
hi mate, whats the ajay-bhargav? tell us more pls
ExperimenterUK
Wed Nov 12 2014, 12:56 AM
@tmtm000.. have you been able to join ?
ExperimenterUK
Wed Nov 12 2014, 12:54 AM
@Alice..become a member,then send a PM to ajay_bhargav the site owner.
Tue Nov 11 2014, 03:58 AM
Abhishek Bansal
Thanks a lot for your informative tutorials!
Mon Nov 10 2014, 09:03 AM
Alice
Hello, This is Alice from a PCB rototype manufacturer. Our company is interested in your website contents, and wonder if we can advertise our company with payment on your website. There are two ways to advertise: first, if we an get a instant quote section on your main site columns; second, whether we can get a advertising section if you have any avaliable one. If any of above ways avaliable, please contact me with your detailed scheme and advertising charge. Thanks. Alice
Sat Nov 08 2014, 11:58 AM
Mohamed_123sd
Signals and Systems, 2nd Edition by Alan V. Oppenheim Alan S.willsky with S.Hamid Nawab?

Downloads

Comments

sankalp_s
Tue Nov 25 2014, 05:13 PM
dodul
Mon Nov 24 2014, 10:08 PM
VikiPeter
Tue Nov 18 2014, 09:13 AM
gentlboy
Sun Nov 16 2014, 11:13 PM
philipeastbourne
Wed Nov 12 2014, 05:32 AM
astra91
Mon Nov 10 2014, 08:11 AM
francise
Thu Nov 06 2014, 02:55 AM
ashbir
Tue Nov 04 2014, 11:13 PM

Online

Guests: 48, Members: 0 ...

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

Members: 37549
Newest member: sankalp_s
Forum Activity
nomi77 Avatar
Posted By nomi77
Wed Nov 26 2014, 07:05 AM
@ajayI did same procedureconnected my modem with L...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 26 2014, 04:47 AM
Are you manually trying to input data via virtual ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 26 2014, 04:39 AM
Uart is totally different peripheral. Look at seri...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 26 2014, 04:37 AM
I see three different components in your design bu...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 26 2014, 04:33 AM
Are you bound to use assembly? if yes then there w...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Nov 26 2014, 02:33 AM
I assume you mean CMMS as in AT+CMMS, something to...
Max.otto Avatar
Posted By Max.otto
Tue Nov 25 2014, 09:54 AM
Hello all,i was bit confused of my requirment. But...
dodul Avatar
Posted By dodul
Mon Nov 24 2014, 10:11 PM
I know its a bit off topic. But, any help would be...
nomi77 Avatar
Posted By nomi77
Mon Nov 24 2014, 02:44 PM
here is the attached code + simulation please help...
nomi77 Avatar
Posted By nomi77
Mon Nov 24 2014, 02:43 PM
Dear All,I have issue with GSM code, when i get me...