3. Writing first C Program
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 that 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.
4. Cont..
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.
5. Cont..
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.
6. To open a new file in Keil C:
Open Keil and Start a new project:
7. Cont..
You will prompted to chose a name for your new project, choose a name and
click save. The following window will appear, where you will be asked to
select a device for Target ‘Target 1′:
8. Cont..
From the list ,select ATMEL and chose your microcontroller .The AT89s52
will be called your ‘Target device’ ,which is the final destination of your
source code. You will be asked whether to copy standard 8051 startup code
click No.
Click file, New and similar to the following window should appear. The blank
space where your code should be written.
9. Cont..
From the list ,select ATMEL and chose your microcontroller .The AT89s52
will be called your ‘Target device’ ,which is the final destination of your
source code. You will be asked whether to copy standard 8051 startup code
click No.
Click file, New and similar to the following window should appear. The blank
space where your code should be written.
10. Cont..
Now, you have to click ‘File ‘,Save as and choose a file name for code ending
with the extension ‘.c’ and click save. Then you have to add this file to your
project work space as shown in the following screen:
11. Cont..
After right-clicking on ‘source group 1‘, click on ‘Add files to group…‘, then you will be
prompted to browse the file to add to ‘source group 1′, chose the file that you just saved and
add it to the source group. You will notice that the file is added to the project tree at the left.
In some versions of this software you have to turn ON manually the option to generate HEX
files. make sure it is turned ON, by right-clicking on target 1, Options for target ‘target 1′,
then under the ‘output‘ tab, by checking the box ‘generate HEX file‘. This step is very
important as the HEX file is the compiled output of your project that is going to be transferred
to the microcontroller.
12. Following layout provides a skeleton of Basic C program:
1.voidmain()
2.{
3. //Your one time initialization code will come here
4.while(1){
5. //while 1 loop
6. //This loop will have all your application code
7. //which will run infinitely
8. }
9.}
13. Writing first C Program
When we are working on controller specific code, then we need to add header
file for that controller.
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.
14. Figure bellow shows the windows context for adding header file to your c file.
15. Write an 8051 C program to send values 00 – FF to port P1:
#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
16. Write an 8051 C program to toggle bit D0 of the port P1 (P1.0)
50,000 times
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
17. Write an 8051 C program to toggle bits of P1 continuously forever
with some delay
//Toggle P1 forever with some delay in between “on” and “off”
#include <reg51.h>
void main(void)
{
unsigned int x;
for (;;) //repeat forever
{
p1=0x55;
for (x=0;x<40000;x++); //delay size
//unknown
p1=0xAA;
for (x=0;x<40000;x++);
}}
18. Writing Hardware specific code
In hardware specific code, we use hardware peripherals like ports, timers and
USART 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.
19. Lets write a simple code to Blink LED on Port1,
Pin1.