Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
-
Upload
frederico-maila -
Category
Documents
-
view
236 -
download
0
Transcript of Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
1/24
Part 2: C programming for 8051 using KEIL IDE
If not simpler, the version of the C programming language used for the
microcontroller environment is not very different than standard C when workingon mathematical operations, or organizing your code. The main difference is all
about the limitations of the processor of the 89S52 microcontroller as compared to
modern computers.
Even if youre not very familiar with the C language, this tutorial will introduce all
the basic programming techniques that will be used along this tutorial. It will also
show you how to use the KEIL IDE.
From the C program to the machine language
The C source code is very high level language, meaning that it is far from being at
the base level of the machine language that can be executed by a processor. This
machine language is basically just zeros and ones and is written in Hexadecimalformat, that why they are called HEX files.
figure 2.1.A
There are several types of HEX files; we are going to produce machine code in the
INTEL HEX-80 format, since this is the output of the KEIL IDE that we are going
to use. Figure 2.1.A shows that to convert a C program to machine language, it
takes several steps depending on the tool you are using, however, the main idea is
http://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/c-to-hex.jpg -
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
2/24
to produce a HEX file at the end. This HEX file will be then used by the burnerto write every byte of data at the appropriate place in the EEPROM of the 89S52.
Variables and constants
Variables
One of the most basic concepts of programming is to handle variables. knowing
the exact type and size of a variable is a very important issue for microcontroller
programmers, because the RAM is usually limited is size. There are two main
design considerations to be taken in account when choosing the variables types: the
occupied space in ram and the processing speed. Logically, a variable that occupies
a big number of registers in RAM will be more slowly processed than a small
variable that fits on a single register.
For you to chose the right variable type for each one of your applications, you will
have to refer to the following table:
Data Type Bits Bytes Value Range
bit 1 0 to 1
signed char 8 1 -128 to +127
unsigned char 8 1 0 to 255
signed int 16 2 -32768 to +32767
unsigned int 16 2 0 to 65535
signed long 32 4 -2147483648 to 2147483647unsigned long 32 4 0 to 4294967295
float 32 4 1.175494E-38 to 3.402823E+38
This table shows the number of bits and bytes occupied by each types of variables,
noting that each byte will fit into a register. You will notice that most variables can
be either signed or unsigned unsigned, and the major difference between thetwo types is the range, but both will occupy the same exact space in memory.
The names of the variables shown in the table are the same that are going to beused in the program for variables declarations. Note that in C programming
language, any variable have to be declared to be used. Declaring a variable, will
attribute a specific location in the RAM or FLASH memory to that variable. The
size of that location will depend on the type of the variable that have beendeclared.
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
3/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
4/24
Where display[1] will be equal to 40. Note that display contains 10 different
variables, numbered from 0 to 9. In that previous example, according to the
variable declaration, there is not such variable location as display[10], and usingit will cause an error in the compiler.
Constants
Sometimes, you want to store a very large amount of constant values, that wouldntfit in the RAM or simply would take too much space. you can store this DATA in
the FLASH memory reserved for the code, but it wont be editable, once the
program is burned on your chip. The advantage of this technique is that it can be
used to store a huge amount of variables, noting that the FLASH memory of the
89S52 is 8K bytes, 32 times bigger than the RAM memory. It is, however, yourresponsibility to distribute this memory between your program and your DATA.
To specify that a variable is to be stored in the FLASH memory, we use exactly the
same variable types names but we add the prefix code before it. Example:
code unsignedchar message[500];
This line would cause this huge array to be stored in the FLASH memory. This can
be interesting for displaying messages on an LCD screen.
To access the pins and the ports through programming, there are a number of pre-
defined variables (defined in the header file, as you shall see later) thatdramatically simplifies that task. There are four ports, Port 0 to Port 3, each one of
them can be accessed using the char variables P0, P1, P2 and P3 respectively. In
those char types variables, each one of the 8 bits represents a pin on the port.
Additionally, you can access a single pin of a port using the bit type variables
PX_0 to PX_7, where X takes a value between 0 and 3, depending on the port
being accessed. For example P1_3 is the pin number 3 of port 1.
You can also define your own names, using the #define directive. Note that thisis compiler directive, meaning that the compiler will use this directive to read and
understand the code, but it is not a statement or command that can be translated tomachine language. For example, you could define the following:
#define LED1 P1_0
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
5/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
6/24
You can then perform all kind of mathematical operations, using the operators
+,'-,'* and /. You can also use brackets ( ) when needed. Example:
a =(5*b)+((a/b)*(a+b));
If you include math.h header file, you will be able to use more advancedfunctions in your equations like Sin, Cos and Tan trigonometric functions, absolutevalues and logarithmic calculations like in the following example:
a =(c*cos(b))+sin(b);
To be able to successfully use those functions in your programs, you have to know
the type of variables that those functions take as parameter and return as a result.
For example a Cosine function takes an angle in radians whose value is a float
number between -65535 and 65535 and it will return a float value as a result. You
can usually know those data types from the math.h file itself, for example, the
cosine function, like all the others is declared in the top of the math header file, andyou can read the line:
extern float cos (float val);
from this line you can deduce that the cos function returns a float data type, andtakes as a parameter a float too. (the parameter is always between brackets.). Using
the same technique, you can easily know how to deal with the rest of the functions
of the math header file. the following table shows a short description of thosefunctions:
Function Description
char cabs (char
val);Return an the absolute value of a charvariable.
int abs (int
val);Return an the absolute value of a intvariable.
long labs (long
val);
Return an the absolute value of a long variable.
float fabs (float
val);Return an the absolute value of afloatvariable.
float sqrt (float
val);Returns the square root of a float variable.
float exp (float
val);Returns the value of the Euler number e to the power ofval
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
7/24
Function Description
float log (float
val);Returns the natural logarithm ofval
float log10
(float val);Returns the common logarithm ofval
float sin (float
val);
A set of standard trigonometric functions. They all take angles
measured in radians whose value
have to be between -65535 and 65535.
float cos (float
val);
float tan (float
val);
float asin (float
val);
float acos (floatval);
float atan (float
val);
float sinh (float
val);
float cosh (float
val);
float tanh (float
val);float atan2
(float y, float
x);
This function calculates the arc tan of the ratio y / x, using the
signs of both x and ytodetermine the quadrant of the angle and
return a number ranging from -pi to pi.
float ceil (float
val);
Calculates the smallest integer that is bigger than val. Example:
ceil(4.3) = 5.
float floor
(float val);
Calculates the largest integer that is smaller than val. Example:
ceil(4.8) = 4.
float fmod
(float x, floaty);
Returns the remainder of x / y. For example: fmod(15.0,4.0) = 3.
float pow (float
x, float y);Returns x to the power y.
Logical operations
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
8/24
You can also perform logic operations with variables, like AND, OR and NOToperations, using the following operators:
Operator Description
! NOT (bit level) Example: P1_0 = !P1_0;~ NOT (byte level) Example: P1 = ~P1;
& AND
| OR
Note that those logic operation are performed on the bit level of the registers. To
understand the effect of such operation on registers, its easier to look at the bits ofa variable (which is composed of one or more register). For example, a NOT
operation will invert all the bit of a register. Those logic operators can be used in
many ways to merge different bits of different registers together.
For example, consider the variable P1, which is of type char, and hence stored
in an 8-bit register. Actually P1 is an SFR, whose 8 bits represents the 8 I/O pins of
Port 1. It is required in that example to clear the four lower bits of that register
without changing the state of the four other which may be used by other
equipment. This can be done using logical operators according to the following
code:
P1 = P1 &0xF0;//Adding '0x' before a number indicates that it is a hexadecimal
one
Here, the value of P1 is ANDed with the variable 0xF0, which in the binary base is
11110000. Recalling the two following relations:
1 AND X = X
0 AND X = 0
(where X can be any binary value)
You can deduce that the four higher bits of P1 will remain unchanged, while the
four lower bits will be cleared to 0.
By the way, note that you could also perform the same operation using a decimal
variable instead of a hexadecimal one, for example, the following code will haveexactly the same effect than the previous one (because 240 = F0 in HEX):
P1 = P1 &240;
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
9/24
A similar types of operations that can be performed on a port, is to to set some of
its bits to 1 without affecting the others. For example, to set the first and last bit of
P1, without affecting the other, the following source code can be used:
P1 = P1 |0x81;
Here, P1 is ORed with the value 081, which is 10000001 in binary. Recallingthe two following relations:
1 OR X = 1
0 OR X = X
(where X can be any binary value)
You can deduce that the first and last pins of P1 will be turned on, without
affecting the state of the other pins of port 1. Those are just a few example of the
manipulations that can be done to registers using logical operators. Logic operators
can also be used to define very specific conditions, as you shall see in the nextsection.
The last types of logic operation studied in this tutorial is the shifting. It can be
useful the shift the bit of a register the right or to the left in various situations. thiscan be done using the following two operators:
Operator Description
>> Shift to the right
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
10/24
a block of code only under certain conditions, and otherwise execute another codeblock or continue with the flow of the program.
The most famous way to do that is to use the if statement, according to thefollowing syntax.
if(expression){
...
code to be executed
...
}
It is important to see how the code is organized in this part. The expression is the
condition that shall be valid for the code block to be executed. the code block is
all delimited by the two brackets { and }. In other words, all the code betwe enthose two brackets will be executed if and only if the expression is valid. The
expression can be any combination of mathematical and logical expressions, asyou can see in the following example:
if((P1 ==0)&(a
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
11/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
12/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
13/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
14/24
A common example of a function with a return value, is a function that will
calculate the angle in radian of a given angle in degrees, as all the trigonometric
functions that are included by default take angles in radians. This function can beas the following:
deg_to_rad(float deg){
float rad;
rad =(deg *3.14)/180;
retrun rad;
}
This function named deg_to_rad will take as a parameter an angle in degrees and
output an angle in radians. It can be called in your program according to thissyntax:
angle = deg_to_rad(102,18);
Where angle should be already defined as a float, and where will be stored the
value returned by the function, which is the angle in radians equivalent to 102.18
Another important note about functions in the main function. Any C programmust contain a function named main which is the place where the programs
execution will start. more precisely, for microcontrollers, it were the execution will
start after a reset operation, or when a microcontroller circuit is turned ON. The
main function has no parameters, and is written like this:
main(){
...
code of the main functions
...
}
Organization of a C program
All C programs have this common organization scheme, sometimes its followed,sometimes its not, however, it is imperative for this category of programming thatthis organization scheme be followed in order to be able to develop your
applications successfully. Any application can be divided into the following parts,
noting that is should be written in this order:
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
15/24
A.Headers Includes and constants definitionsIn this part, header files (.h) are included into your source code. those
headers files can be system headers to declare the name of SFRs, to define
new constants, or to include mathematical functions like trigonometric
functions, root square calculations or numbers approximations. Header files
can also contain your own functions that would be shared by various
programs.
B.Variables declarationsMore precisely, this part is dedicated to Global Variables declarations.Variables declared in this place can be used anywhere in the code. Usually
in microcontroller programs, variables are declared as global variables
instead of local variables, unless your are running short of RAM memory
and want to save some space, so we use local variables, whose values will be
lost each time you switch from a function to another. To summarize, global
variables as easier to use and implement than local variables, but theyconsume more memory space.
C.Functions bodyHere you group all your functions. Those functions can be simple ones that
can be called from another place in your program, as they can be called from
an interrupt vector. In other words, the sub-programs to be executed when
an interrupt occurs is also written in this place.
D.InitializationThe particularity of this part is that it is executed only one time when the
microcontroller was just subjected to a RESET or when power is just
switched ON, then the processor continue executing the rest of the program
but never executes this part again. This particularity makes it the perfect
place in a program to initialize the values of some constants, or to define the
mode of operation of the timers, counters, interrupts, and other features of
the microcontroller.
E.Infinite loopAn infinite loop in a microcontroller program is what is going to keep it
alive, because a processor have to be allays running for the system to
function, exactly like a heart have to be always beating for a person to live.
Usually this part is the core of any program, and its from here that all theother functions are called and executed.
Simple C program for 89S52
Here is a very simple but complete example program to blink a LED. Actually it is
the source code of the example project that we are going to construct in the next
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
16/24
part of the tutorial, but for now it is important to concentrate on the programmingto summarize the notions discussed above.
#include
#include
delay(unsignedint y){
unsignedint i;
for(i=0;i
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
17/24
To create a project, write and test the previous example source code, follow thefollowing steps:
Open Keil and start a new project:
2.8.A
You will prompted to chose a name for your new project, Create a separatefolder where all the files of your project will be stored, chose a name and
click save. The following window will appear, where you will be asked to
select a device for Target Target 1:
http://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/keil1.jpg -
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
18/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
19/24
figure 2.8.C
Now you have to click File, Save as and chose a file name for your sourcecode ending with the letter .c. You can name is code.c for example, and
click save. Then you have to add this file to your project work space at theleft as shown in the following screen shot:
http://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/keil3.jpg -
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
20/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
21/24
-
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
22/24
You can use the output window to track eventual syntax errors, but also tocheck the FLASH memory occupied by the program (code = 49) as well as
the registers occupied in the RAM (data = 9). If after rebuilding the targets,
the output window shows that there is 0 error, then you are ready to test
the performance of your code. In KEIL, like in most development
environment, this step is called Debugging, and has this icon: . After
clicking on the debug icon, you will notice that some part of the user
interface will change, some new icons will appear, like the run icon circledin the following figure:
figure 2.8.F
http://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/keil6.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/keil6.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but.jpg -
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
23/24
You can click on the Run icon and the execution of the program will start.In our example, you can see the behavior of the pin 0 or port one, but
clicking on peripherals, I/O ports, Port 1. You can always stop the
execution of the program by clicking on the stop button ( ) and you can
simulate a reset by clicking on the reset button .
You can also control the execution of the program using the following icons:which allows you to follow the execution step by step. Then,
when youre finished with the debugging, you can always return to the
programming interface by clicking again on the debug button ( ).
There are many other features to discover in the KEIL IDE. You will easily
discover them in first couple hours of practice, and the more important of them will
be presented along the rest of this tutorial.
This concludes this second part of the 89S52 tutorial. I now invite you to startbuilding a real hardware project in the next part.
Previous part: Introduction to 8051 micro-
controllers
Next part: Basic input/output
operations
Leave a Reply
Gravatars are enabled.Registerone now!
Submit comment
Stay in touch
http://www.ikalogic.com/part-1-introduction-to-8051-microcontrollers/http://www.ikalogic.com/part-1-introduction-to-8051-microcontrollers/http://www.ikalogic.com/part-1-introduction-to-8051-microcontrollers/http://www.ikalogic.com/part-3-basic-inputoutput-operations/http://www.ikalogic.com/part-3-basic-inputoutput-operations/http://en.gravatar.com/http://en.gravatar.com/http://en.gravatar.com/http://www.addtoany.com/share_savehttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but1.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/step_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/rst_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/stop_but.jpghttp://www.addtoany.com/share_savehttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but1.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/step_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/rst_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/stop_but.jpghttp://www.addtoany.com/share_savehttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but1.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/step_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/rst_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/stop_but.jpghttp://www.addtoany.com/share_savehttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but1.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/step_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/rst_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/stop_but.jpghttp://www.addtoany.com/share_savehttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/debug_but1.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/step_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/rst_but.jpghttp://localhost/var/www/apps/conversion/tmp/scratch_2/Part%202%20%20C%20programming%20for%208051%20using%20KEIL%20IDE%20%20%20IKALOGIC_files/stop_but.jpghttp://en.gravatar.com/http://www.ikalogic.com/part-3-basic-inputoutput-operations/http://www.ikalogic.com/part-3-basic-inputoutput-operations/http://www.ikalogic.com/part-1-introduction-to-8051-microcontrollers/http://www.ikalogic.com/part-1-introduction-to-8051-microcontrollers/ -
7/29/2019 Part 2 C Programming for 8051 Using KEIL IDE IKALOGIC
24/24
Let us inform you when new content is added!It's cool, we hate spam too!
Your e-mail a
Subscribe Unsubscribe
Search
Newsletter
It's cool, we hate spam too!
Your e-mail a
Subscribe Unsubscribe
Get to know us
2012IKALOGICS.A.S. - 1 AVENUE D'ESTER - 87000 LIMOGES -
FRANCE - TEL: +33 (0)5 55 35 80 28 - FAX: +33 (0)9 72 12 58 30
http://www.ikalogic.com/http://www.ikalogic.com/http://www.ikalogic.com/http://ikalogic.cluster006.ovh.net/contact-us/http://www.ikalogic.com/phpBB3/http://www.facebook.com/home.php?sk=group_146938835352437&ap=1http://www.twitter.com/ikalogichttp://ikalogic.cluster006.ovh.net/contact-us/http://www.ikalogic.com/phpBB3/http://www.facebook.com/home.php?sk=group_146938835352437&ap=1http://www.twitter.com/ikalogichttp://ikalogic.cluster006.ovh.net/contact-us/http://www.ikalogic.com/phpBB3/http://www.facebook.com/home.php?sk=group_146938835352437&ap=1http://www.twitter.com/ikalogichttp://ikalogic.cluster006.ovh.net/contact-us/http://www.ikalogic.com/phpBB3/http://www.facebook.com/home.php?sk=group_146938835352437&ap=1http://www.twitter.com/ikalogichttp://www.ikalogic.com/