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.

Forum Activity
pravin_35 Avatar
Posted By pravin_35
Thu Jul 31 2014, 01:49 AM
attach is the code for lcd may be need a little ch...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:45 PM
@ sermeteraythank you for sharing your experience ...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:38 PM
@ Peter_ParkerHC06 by default baud rate is 9600try...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:25 PM
attach is the code for lcd may be need a little ch...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:21 PM
@ pravin_35both compilers are almost sameif you ha...
pravin_35 Avatar
Posted By pravin_35
Wed Jul 30 2014, 07:38 AM
http://extremeelectronics.co.in/pic16f877a-tutoria...
NexusRevZ Avatar
Posted By NexusRevZ
Wed Jul 30 2014, 02:31 AM
Ok, I have edited the code, it's updated above wit...
NexusRevZ Avatar
Posted By NexusRevZ
Wed Jul 30 2014, 02:10 AM
Oh tahnk you so much for the reply. I will do imme...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Jul 30 2014, 01:56 AM
You cannot move and read data from EEPROM immediat...
pravin_35 Avatar
Posted By pravin_35
Wed Jul 30 2014, 12:39 AM
Remove all power connections to the circuit at the...

Conversation

Thu Jul 31 2014, 04:32 PM
1
-1'
Thu Jul 31 2014, 04:32 PM
1
1
NexusRevZ
Tue Jul 29 2014, 09:32 PM
Anyone good in Asm lang?
ajay_bhargav
Mon Jul 28 2014, 12:34 AM
@ Anshu Raj sorry we do not take such requests.
Sun Jul 27 2014, 12:23 AM
Anshu Raj
sir plz send the automatic collage alram bells in 2 hrs bells ring. For microcontroller programme plz sir
ajay_bhargav
Thu Jul 24 2014, 01:06 AM
@R.ABHISHEK where? I dont see it. is it in forum?
Wed Jul 23 2014, 05:49 AM
R.ABHISHEK
sir,pls message on microcontroller 8051 assambly language new program
Wed Jul 23 2014, 05:47 AM
R.ABHISHEK
sir,pls message on 8051 microcontroller assambly language program new quastions
ajay_bhargav
Tue Jul 22 2014, 09:18 AM
@Pragya Android application is taken from google play. Only Author has its source with him. Application is only used in this project.
Tue Jul 22 2014, 04:58 AM
Pragya
plz send me 8051 code and android code for android based device controller via Bluetooth .Here in this project I am using Hc-06 Bluetooth module

Downloads

Comments

risko
Thu Jul 31 2014, 03:53 PM
ismaelfogaca
Thu Jul 31 2014, 10:50 AM
NexusRevZ
Tue Jul 29 2014, 09:29 PM
kritz22
Mon Jul 28 2014, 10:33 AM
Abbasans786
Mon Jul 28 2014, 03:15 AM
Imran.pk
Fri Jul 25 2014, 07:50 PM
PavanKh
Fri Jul 25 2014, 12:56 AM
vinosentinal
Mon Jul 21 2014, 08:28 PM

Online

Guests: 29, Members: 0 ...

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

Members: 37486
Newest member: risko