POINTS TO BE CONSIDERED WHILE WRITING THE EMBEDDED C CODE FOR 8051 MICRO-CONTROLLER

Introduction:

Many real time projects are using embedded systems, because of the increase in technology and everyone is utilizing this embedded product according to their desire.

In short these advancement making the difficult task easy and increase in automation like in industries, transportation, defense, domestic so on. These embedded systems are designed around the micro-controller or microprocessor. Depending on the product the developer will choose the right control unit (micro-controller/microprocessor). In most cases micro-controllers are used for developing embedded systems. Same system can be developed around the microprocessor but there is a limitation of cost and size.

For an instant we can that an embedded system is a electronic or electrical device which accepts inputs and accomplishes the task given using the controlling unit. The below block diagram gives the idea, how an embedded system is look like.

Fig a: Simple block diagram of an embedded system.

What we learn:

Here in this tutorial we concentrate on how to write embedded C code for a basic micro-controller 8051. Because it is the first version of 8-bit micro controllers and it is built around the 8086 microprocessor. Even though 8051 programming can be done by using assembly language, it is difficult to write the 8051 source code because of its length. So everyone is using C language.

The main difference between “C-programming language and Embedded C-programming language” is,

C-programming is used for application designing for computers.

Embedded C-programming Is used for programming the micro controllers for a specific task.

Pre-Requisites:

Before staring the programming, the things you posses is some basic level C-programming methods and the idea of registers and I/O ports of micro controller (8051).

Mostly the developers use “Keil µ Vision” IDE (Integrated Development Environment) for writing code for 8051 or any other controllers.

Let’s see the first programming for 8051 MCU blinking of a single LED. The programming can be done in two methods.

Method 1:

Using the internal start-up library:

#include <regx51.h>//all the registers of 8051 is initialized using this library/header
void delay(unsigned int time){ //writing a delay method for generation of program based delay
unsigned int i,j;
for (i=0; i<time; i++) for(j=0; j<1275; j++);
}
void main()
{
P2=0x00;  //declaring the port2 as output
While(1) {  //this is necessary for real time projects to run again and again
P2=0x01;   //here P2.0 is used for blinking the LED.
delay(1000);
P2=0x00;
}//end of while loop
}//end of main program

Same program can be implemented in another way for the same pin.

#include <regx51.h> //all the registers of 8051 is initialized using this library/header
void delay(unsigned int time){   //writing a delay method for generation of program based delay
unsigned int i,j;
for(i=0;i<time; i++) for(j=0;j<1275;j++);
}
void main()
{
P2^0=0;          //declaring the port2.0 pin as output
While(1){        //this is necessary for real time projects to run again and again
P2^0=1;          //here P2.0 is used for blinking the LED.
delay(1000);
P2^0=0;
} //end of while loop
} //end of main program

Using an identifier for a particular pin

#include <regx51.h>     //all the registers of 8051 is initialised using this library/header
sbit LED_PIN=P2^0;
void delay(unsigned int time){ //writing a delay method for generation of program based delay
Unsigned int i,j;
for (i=0; i<time; i++) for( j=0; j<1275; j++);
}
void main()
{
P2=0x00;       //declaring the port2 as output
While(1){      //this is necessary for real time projects to run again and again
LED_PIN=1;    //here P2.0 is used for blinking the LED.
delay(1000);
LED_PIN=0;
}           //end of while loop
}           //end of main program

Using a “#define” macro

#include <regx51.h>  //all the registers of 8051 is initialized using this library/header
#define LED_PIN P2_0
void delay(unsigned int time){  //writing a delay method for generation of program based delay
unsigned int i,j;
for(i=0; i<time; i++) for (j=0; j<1275; j++);
}

void main()
{
P2=0x00;       //declaring the port 2 as output
While(1){        //this is necessary for real time projects to run again and again
LED_PIN=1; //here P2.0 is used for blinking the LED.
delay(1000);
LED_PIN=0;
}    //end of while loop
}   //end of main program

In this way we write a Embedded C-program for 8051 using default libraries.
Method:
In this method “special function registers (SFR)” are used for writing a code. Here there is no need of using the inbuilt 8051 header.

sfr P2      = 0xA0;
/*sfr is the special function key word used P2 is the name identifier name given to the Port 2 of 8051 and 0xA0 is the address of that particular port*/
void delay(unsigned int time){   //writing a delay method for generation of program based delay
Unsigned int i,j;
For (i=0; i<time; i++) for(j=0; j<1275; j++);
}

void main()
{
P2=0x00;         //declaring the port2 as output
While(1){          //this is necessary for real time projects to run again and again
P2=0x01;         //here P2.0 is used for blinking the LED.
delay(1000);
P2=0x00;
}              //end of while loop
}              //end of main program
Note: While writing the code be aware of using keywords of 8051, and program size must not
exceed the flash size (ROM) 8051. For the SFR refer to the SFR table of 8051 which is given in the datasheet. ”

“Note: There are several IDE are available for the programmers to program the various micro controllers, for the best practice, it is suggested to use KEIL IDE.”

Leave a Reply

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