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 working on 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 you’re 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 zero’s and one’s and is written in Hexadecimal format, 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 to produce a HEX file at the end. This HEX file will be then used by the ‘burner’ to 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 2147483647
unsigned 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 the two 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 be used 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 been declared.

To understand the difference between those types, consider the following example source code where we start by declaring three ‘unsigned char’ variables, and one ‘signed char’ and then perform some simple operations:

unsigned char a,b,c;
signed char d;
a = 100;
b = 200;
c = a - b;
d = a - b;

In that program the values of ‘c’ will be equal to ’155′! and not ‘-100′ as you though, because the variable ‘c’ is an unsigned type, and when a the value to be stored in a variable is bigger than the maximum value range of this variable, it overflows and rolls back to the other limit. Back to our example, the program is trying to store ‘-100′ in ‘c’, but since ‘c’ is unsigned, its range of values is from ’0 to 255′ so, trying to store a value below zero, will cause the the variable to overflow, and the compiler will subtract the ‘-100′ from the other limit plus 1, from ’255 + 1′ giving ’156′. We add 1 to the range because the overflow and roll back operation from 0 to 255 counts for the subtraction of one bit. On the other hand, the value of ‘d’ will be equal to ‘-100′ as expected, because it is a ‘signed’ variable. Generally, we try to avoid storing value that are out of range, because sometime, even if the compiler doesn’t halt on that error, the results can be sometimes totally un-expected.

Note that in the C programming language, any code line is ended with a semicolon ‘;’, except for the lines ending with brackets ‘{‘ ‘}’.

Like in any programming language, the concept of a variables ‘array’ can also be used for microcontrollers programming. an array is like a table or a group of variables of the same type, each one can be called by a specific number, for example an array can be declared this way:

char display[10];

This will create a group of 10 variables. Each one of them is accessible by its number, example:

display[0] = 100;
display[3] = 60;
display[1] = display[0] - display[3];

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 using it will cause an error in the compiler.

Constants

Sometimes, you want to store a very large amount of constant values, that wouldn’t fit 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, your responsibility 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 unsigned char 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) that dramatically 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 this is 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 to machine language. For example, you could define the following:

#define LED1 P1_0

With the definition above, the compiler will replace every occurrence of LED1 by P1_0. This makes your code much more easier to read, especially when the new names you give make more sense.

You could also define a numeric constant value like this:

#define led_on_time 184

Then, each time you write led_on_time, it will be replaced by 184. Note that this is not a variable and accordingly, you cannot write something like:

led_on_time = 100; //That's wrong, you cannot change a constant's value in code.

The utility of using defined constants, appears when you want to adjust some delays in your code, or some constant variables that are re-used many times within the code: With a predefined constant, you only change it’s value once, and it’s applied to the whole code. that’s for sure apart from the fact that a word like led_on_time is much more comprehensive than simply ‘184‘!

Along this tutorial you will see how port names, and special function registers are used exactly as variables, to control input/output operations and other features of the microcontroller like timers, counters and interrupts.

Mathematical & logic operations

Now that you know how to declare variables, it is time to know how to handle them in your program using mathematical and logic operations.

Mathematical operations

The most basic concept about mathematical operations in programming languages, is the ‘=’ operator which is used to store the content of the expression at its right, into the variable at its left. For example the following code will store the value of ‘b’ into ‘a’ :

a = b;

And subsequently, the following expression in totally invalid:

5 = b;

Since 5 in a constant, trying to store the content of ‘b’ in it will cause an error.

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 advanced functions in your equations like Sin, Cos and Tan trigonometric functions, absolute values 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, and you can read the line:

extern float cos (float val);

from this line you can deduce that the ‘cos’ function returns a float data type, and takes 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 those functions:

Function
Description
char cabs (char val);
Return an the absolute value of a char variable.
int abs (int val);
Return an the absolute value of a int variable.
long labs (long val);
Return an the absolute value of a long variable.
float fabs (float val);
Return an the absolute value of a float variable.
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 of val
float log (float val);
Returns the natural logarithm of val
float log10 (float val);
Returns the common logarithm of val
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 (float val);
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, float y);
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

You can also perform logic operations with variables, like AND, OR and NOT operations, 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, it’s easier to look at the bits of a 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 have exactly the same effect than the previous one (because 240 = F0 in HEX):

P1 = P1 & 240;

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 0×81, which is ’10000001′ in binary. Recalling the 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 next section.

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. this can be done using the following two operators:

Operator
Description
>>
Shift to the right
<<
Shift to the left

The syntax is is quite intuitive, for example:

P1 = 0x01; // After that operation, in binary, P1 = 0000 0001
P1 = (P1 << 7) // After that operation, in binary P1 = 1000 0000

You can clearly notice that the content of P1 have been shifted 8 steps to the left.

Conditions and loops

In most programs, it is required at a certain time, to differentiate between different situations, to make decision according to specific input, or to direct the flow of the code depending on some criteria. All the above situation describe an indispensable aspect of programming: ‘conditions’. In other words, this feature allows to execute a block of code only under certain conditions, and otherwise execute another code block or continue with the flow of the program.

The most famous way to do that is to use the ‘if’ statement, according to the following 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 between those two brackets will be executed if and only if the expression is valid. The expression can be any combination of mathematical and logical expressions, as you can see in the following example:

if ( (P1 == 0) & (a <= 128) ){
...
code to be executed
...
}

Notice the use of the two equal signs (==) between two variables or constants, In C language, this means that you are asking whether P1 equals 0 or not. writing this expression with only one equal sign, would cause the the compiler to store 0 in P1. This issue is a source of logical error for many beginners in C language, this error wont generate any alert from the compiler and is very hard to identify in a big program, so pay attention, it can save you lot of debugging time. Otherwise it is clear that in that previous example, the code block is only executed if both the two expressions are true. Here is a list of all the operators you can use to write an expression describing a certain condition:

Operator
Description
==
Equal to
<, >
Smaller than, bigger than.
<=, >=
Smaller than or equal to, bigger than or equal to.
!=
Not equal to

The ‘If’ code block can get a little more sophisticated by introducing the ‘else’ and ‘else if’ statement. Observe the following example source code:

if (expression_1) {
...
code block 1
...
}else if(expression_2) {
...
code block 2
...
}else if(expression_3) {
...
code block 3
...
}else{
...
code block 4
...
}

Here, There are four different code blocks, only one shall be executed if and only if the corresponding condition is true. The last code block will only be executed if none of the previous expression is valid. Note that you can have as many ‘else if’ blocks as you need, each one with its corresponding condition, BUT you can only have one ‘else’ block, which is completely logical. However you can chose not to have and ‘else’ block at all if you want.

There are some other alternatives to the ‘if…else’ code block, that can provide faster execution speeds, but also have some limitations and restrictions like the ‘Select…case’ code block. For now, it is enough to understand the ‘if…else’ code block, whose performance is quite fair and have a wide range of applications.

Another very important tool in the programming languages is the loop. In C language like in many others, loops are usually restricted to certain number of loops like in the ‘for’ code block or restricted to a certain condition like the ‘while’ block.

Let’s start with the ‘for’ code block, which is a highly controllable and configurable loop. consider the following example source code:

for(i=0;i<10;i++){

P0 = i;

}

Here the code between the the two brackets ‘{‘ ‘}’ will be be executed a certain number of times, each time with the counting variable ‘i’ increasing by 1 according to the statement ‘i++’. The code will keep looping as long as the condition ‘i<10′ is true. Usually the counting value ‘i’ is reused in the body of the loop, which makes the particularity of this loop. The ‘for’ loop functioning can be recapitulated by the following syntax:

for(start;condition;step){
...
code block
...
}

Where start represents the start value assigned to the count value before the loop begins. The condition is the expression that is is to remain true for the loop to continue; as long as this conditions is satisfied, the code will keep looping. Finally, step is the increase or decrease of the counting variable, it can be any statement that changes its value, whether by an addition or subtraction.

The second type of loop that we are going to study is the ‘while’ loop. the syntax of this one is simpler than the previous one, as you can observe in the following example source code, that is equivalent to the previous method:

while(i < 10){
P0 = i;
i = i +1;
}

Here there is only one parameter to be defined, which is the condition to keep this loop alive, which is ‘i < 10′ in our example. Then, it is the responsibility of the programmer to design the software carefully to provide an exit for that loop, or to make it an infinite loop. Both techniques are commonly used in microcontroller programs, as you shall see later on along this tutorial.

Functions

Functions are way of organizing your code, reducing its size, and increasing its overall performance, by grouping relatively small parts of code to be reused many times in the same program. A new function can be created according to the following syntax:

Function_name(parameter_1, Parameter_2, Parameter_3){
...
function body
...
return value //optional
...
}

This is the general form of a function. The number of parameters of the function can be more than the three parameters of the examples above, as it can be zero, all depends on the type and use of the function. The function’s body is usually a sub program that implies the parameters to produce the required result. some functions will also generate an output, like the cos() function, through the ‘return’ command, which will output the value next to it. Usually the ‘return’ command is used at the end of the function.

A very common use of functions without return value is to create delays in a software, consider the following function:

delay(unsigned int y){
unsigned int i;
for(i=0;i<y;i++){
;
}
}

In this last piece of code a function named ‘delay’ is created, with an unsigned integer ‘y’ as a parameter, and implying a locally defined unsigned int ‘i’. the function will repeat a loop for a couple hundreds or thousand of times to generate precise delays in a program. A function like this can be called from anywhere in the program according to the following syntax:

delay(30000);

this line of code would cause the program to pause for approximately one second on a 12 MHz clock on a 8051 microcontroller.

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 be as 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 this syntax:

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 program must contain a function named ‘main’ which is the place where the program’s 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 it’s followed, sometimes it’s not, however, it is imperative for this category of programming that this 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:

  1. Headers Includes and constants definitions
    In 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.
  2. Variables declarations
    More 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 they consume more memory space.
  3. Functions’ body
    Here 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.
  4. Initialization
    The 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.
  5. Infinite loop
    An 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 the other 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 part of the tutorial, but for now it is important to concentrate on the programming to summarize the notions discussed above.

#include <REGX52.h>
#include <math.h>

delay(unsigned int y){
    unsigned int i;
    for(i=0;i<y;i++){;}
} 

main(){
    while(1){
        delay(30000);
        P1_0 = 0;
        delay(30000);
        P1_0 = 1;
    }
}

After including basic headers for the SFR definitions of the 8952 microcontroller (REGX52.h) and for mathematical functions (math.h), a function named ‘delay’ is created, which is simple a function to create a delay controlled via the parameter ‘y’. Then comes the main function, with an infinite loop (the condition for that loop to remain will always be satisfied as it is ’1′). Inside that loop, the pin number 0 of port 1 is constantly turned ON and OFF with a delay of approximately one second.

As you will see in the next part, A simple circuit can be constructed and a LED can be connected to the pin P1_0 to see how software and hardware adjustments can affect the behavior of you circuits.

Using the KEIL environment

KEIL uVision is the name of a software dedicated to the development and testing of a family of microcontrollers based on 8051 technology, like the 89S52 which we are going to use along this tutorial. You can can download an evaluation version of KEIL at their website: http://www.keil.com/c51/. Most versions share merely the same interface, this tutorial uses KEIL C51 uVision 3 with the C51 compiler v8.05a.

To create a project, write and test the previous example source code, follow the following steps:

  • Open Keil and start a new project:

2.8.A

  • You will prompted to chose a name for your new project, Create a separate folder 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′:

figure 2.8.B

  • From the list at the left, seek for the brand name ATMEL, then under ATMEL, select AT89S52. You will notice that a brief description of the device appears on the right. Leave the two upper check boxes unchecked and click OK. 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 codeclick No.
  • Click File, New, and something similar to the following window should appear. The box named ‘Text1′ is where your code should be written later.

figure 2.8.C

  • Now you have to click ‘File, Save as’ and chose a file name for your source code 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 the left as shown in the following screen shot:

figure 2.8.D

  • 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, eventually ‘code.c’ 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 micro-controller.
  • You can then start to write the source code in the window titled ‘code.c’ then before testing your source code, you have to compile your source code, and correct eventual syntax errors. In KEIL IDE, this step is called ‘rebuild all targets’ and has this icon:  .

figure 2.8.E

  • You can use the output window to track eventual syntax errors, but also to check 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 circled in the following figure:

figure 2.8.F

  • 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 you’re 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 start building a real hardware project in the next part.

Previous part: Introduction to 8051 micro-controllers Next part: Basic input/output operations

Comments ( 1 )

  • Akshay says:

    Sir I want to make a Program in keil for seven segment by using a button ….When i press button 1 digit should show and again if i press button 2 digit should show and so on can u tel me the programing code to how to use button as toggle….

Leave A Comment

Your email address will not be published. Required fields are marked *