Embedded C Programming Tutorial with Keil Language

The embedded C is the most popular programming language in the software field for developing electronic gadgets. Each processor is associated with embedded software. Embedded C Programming plays a major role in performing specific functions by the processor. In our day-to-day life we frequently use many electronic devices such as washing machine, mobile phone, digital camera and so on will work based on microcontrollers that are programmed by embedded C.

Embedded System Programming
Embedded System Programming

The C code written is more reliable, portable, and scalable; and in fact, much easier to understand. The first and foremost tool is the embedded software that decides operation of an embedded system. Embedded C programming language is most frequently used for programming the microcontrollers.

Embedded C Programming Tutorial (8051)

For writing the program the embedded designers must have sufficient knowledge on hardware of particular processor or controllers as the embedded C programming is a fully hardware related programming technique.

Programming Tutorial
Programming Tutorial

Earlier, many embedded applications were developed by using assembly level programming. However, they did not provide portability to overcome this problem with the advent of various high level languages like C, COBOL and Pascal. However, it was the C language that got extensive acceptance for embedded systems application development, and it continues to do so.

Embedded System

The embedded system is defined as the combination of embedded C programming software and hardware part majorly consist of microcontrollers and it is intended to perform the specific task. These types of embedded systems are being used in our daily life such as washing machines and video recorders, refrigerators and so on. The embedded system was first introduced by the 8051 microcontroller.

Embedded System
Embedded System

 Introduction to the 8051 Microcontroller

The 8051 microcontroller is a basic microcontroller, it is first introduced by the ‘Intel Corporation’ since 1970. It is developed by the 8086 processor architecture. The 8051 is a family of the microcontroller, which has been developed by different manufactures such as Philips, atmel, dalls, and so on. The 8051 microcontroller has been used in lots of embedded products from small children’s toys to large automotive systems.

8051 Microcontroller
8051 Microcontroller

The 8051 microcontroller is the 8-bit ‘CISC’ architecture. It consists of memories, serial communication, interrupts, input/output ports and timer/counters, built into a single integrated chip, which is programmed to control the peripheral devices which are interfaced with it.The program is stored in the RAM memory of the microcontroller but before writing the program, we must aware of the RAM memory organization of the microcontroller.

Embedded System Programming: Basics Declaration

Every function is a collection of  statements that perform a specific task and the  collection of one or more functions is called a programming language. Every language consists of some basic elements and grammatical rules. The C language programming is  designed to function with character set, variables, data types, constants, keywords, expressions and so on are used to write a C program. All these considered under header file or library file and it is represented as

#include<studio.h>

Embedded C Programming Development
Embedded C Programming Development

The  extension  of the C language is called an Embedded C programming language. As compared with above, the embedded programming in C language has some additional features such as data types and keywords and header file or library file is represented as

#include<microcontroller name. h>

Embedded C Additional Keywords

  • sbit
  • bit
  • SFR
  • volatile
  • macros define

The “sbit” is used for declaring the single PIN of the microcontroller. For example LED is connected to the P0.1 pin, it is not recommended to send the value to the port pin directly, first we have to declare the pin with another variable then after we can use anywhere in the program.

Syntax: sbit a=P0^1; //declares the respective pin with a variable//
a=0x01; //send the value to the port pin//

The “bit” is used for checking the status of the variable.

Syntax: bit c; //declares the bit variable//
c=a; //a value is assigned to the c variable //
if(c==1) //check the condition true or false//

{
…..
……
}

The “SFR” keyword is used to access the SFR registers by another name. The SFR register defined as a special function register, it contains all peripheral related registers by indicating the address. The SFR register is declared by the SFR keyword. The SFR keyword must be in capital letters.

Syntax: SFR port=0x00; //0x00 is a port0 address it is declared by port variable//
Port=0x01; //then send the value to the port0//
delay();
port=0x00;
delay();

The “volatile” keyword is most important keyword in the embedded system development. The variable that declares with the volatile keyword value could not be changed unexpectedly. It can be used in memory mapped peripheral registers, global variables modified by the ISRs. Without using the volatile keyword for transmitting and receiving the data, code error or an optimization error will takes place.

Syntax: volatile int k;

The macro is a name it is used to declare the block of statements as pre-processor directive. Whenever the name is used, it is replaced by the contents of the macro. The macros represent by the #define. The whole port pins are defined by the macros.

Syntax: #define dat Po; //the whole port is declared by a variable//
dat=0x01; //data send to the port0//

Basic Embedded C Programs

The microcontroller programming will differ for each type of operating system. Even though there are many operating systems are existing such as Linux, Windows, RTOS and so on. However, RTOS has several advantages for embedded system development. This article discusses about basic embedded C programming to develop embedded C programming using 8051 microcontroller.

Embedded C Programming Steps
Embedded C Programming Steps
  • LED blinking using with 8051 microcontroller
  • Number Displaying on 7-segment display using 8051 microcontroller
  • Timer/Counter calculations and program using 8051 microcontroller
  • Serial Communication calculations and program using 8051 microcontroller
  • Interrupt Programs using 8051 microcontroller
  • Keypad Programming using 8051 microcontroller
  • LCD Programming with 8051 microcontroller

LED Blinking using 8051 Microcontroller

The LED is a semiconductor device which is used in many applications,mostly for indication purpose. It is finding a huge range of applications as indicators during test to check the validity of results at different stages. They are very cheap and easily available in a variety of shape, color and size. The LEDs are used to design message display boards and traffic control signal lights etc. Here the LEDs are interfaced to the PORT0 of the 8051 microcontroller.

LED Blinking using 8051 Microcontroller
LED Blinking using 8051 Microcontroller

1. 01010101
10101010

#include<reg51.h> //header file//
void main() //the program execution stat point//
{
unsignedint i; //data type//
while(1) //for continuous loop//
{
P0=0x55; //send the hexa value to the port0//
for(i=0;i<30000;i++) //normal delay//
P0=0x3AA; //send the hexa value to the port0//
for(i=0;i<30000;i++) //normal delay//
}
}

2. 00000001

00000010

00000100

.

.

10000000

#include<reg51.h>

void main()

{

unsignedint i;

unsigned char j,b;

while(1)

{

P0=0x01;

b=P0;

for(j-0;j<3000;j++);

for(j=0;j<8;j++)

{

b=b<<1;

P0=b;

for(j-0;j<3000;j++);

}

}

}

3. 00001111

11110000

#include<reg51.h>

void main()

{

unsignedint i;

while(1)

{

P0=0x0F;

for(j-0;j<3000;j++);

P0=0xF0;

for(j-0;j<3000;j++);

}

}

4. 00000001

00000011

00000111

.

.

11111111

#include<reg51.h>

void main()

{

unsignedint i;

unsigned char j,b;

while(1)

{

P0=0x01;

b=P0;

for(j-0;j<3000;j++);

for(j=0;j<8;j++)

{

b=b<<1;

b=b|0x01;

P0=b;

for(j-0;j<3000;j++);

}

}

}

Displaying Numbers on 7-Segment Display using 8051 Microcontroller

The 7-segment displays is the basic electronic displays, which are used in many systems to display the numeric information. It consists of eight LEDs which are connected in sequential manner so as to display digits from 0 to 9, when proper combinations of LEDs are switched on. They can display only one digit at a time.

Displaying Numbers on 7-Segment Display using 8051 Microcontroller
Displaying Numbers on 7-Segment Display using 8051 Microcontroller

1. WAP to display the numbers form ‘0 to F’ on four 7segment displays ?

#include<reg51.h>
sbit a= P3^0;
sbit b= P3^1;
sbit c= P3^2;
sbit d= P3^3;
void main()
{
unsignedchar n[10]={0×40,0xF9,0×24,0×30,0×19,0×12,0×02,0xF8,0xE00,0×10};
unsigned inti,j;
a=b=c=d=1;
while(1)
{
for(i=0;i<10;i++)
{
P2=n[i];
for(j=0;j<60000;j++);
}
}
}

2. WAP to display the numbers from ’00 to 10’ on 7segment displays ?

#include<reg51.h>
sbit a= P3^0;
sbit b= P3^1;
void display1();
void display2();
void delay();
void main()
{
unsignedchar n[10]={0×40,0xF9,0×24,0×30,0×19,0×12,0×02,0xF8,0xE00,0×10};
unsigned inti,j;
ds1=ds2=0;
while(1)
{
for(i=0,i<20;i++)
display1();
display2();
}
}
void display1()
{
a=1;
b=0;
P2=s[ds1];
delay();
a=1;
b=0;
P2=s[ds1];
delay();
}
void display2()
{
ds1++;
if(ds1>=10)
{
ds1=0;
ds2++;
if(ds2>=10)
{
ds1=ds2=0;
}
}
}
void delay()
{
unsignedint k;
for(k=0;k<30000;k++);
}

Timer/Counter Calculations and Program using 8051 Microcontroller

The delay is the one of the important factors in the application software development. However, the normal delay will not give the precious result to overcome this problem for implementing the timer delay. The timers and counters are hardware components of the microcontroller, which is used in many applications to provide the precious time delay with count pulses.The both tasks are implemented by the software technique.

Timer Delay

WAP to generate the 500us time delay using T1M2(timer1 and mode2) ?

#include<reg51.h>

void main()
{
unsigned char i;
TMOD=0x20; //set the timer mode//
for(i=0i<2;i++) //double the time daly//
{
TL1=0x19; //set the time delay//
TH1=0x00;
TR1=1; //timer oN//
While(TF1==0); //check the flag bit//
TF1=0;
}
TR1=0; //timer off//
}

Normal Loop Delay

void delay()

{
unsignedint k;
for(k=0;k<30000;k++);
}

Serial Communication Calculations and Program using 8051 Microcontroller

Serial communication is commonly used for transmitting and receiving the signal. The 8051 microcontroller have consist UART serial communication the signals transmitted and received by the Rx and Tx pins. The UART takes bytes of data and sends the individual bits in a sequential manner. The registers are a way to collect and store the data in the memory. UART is a half-duplex protocol. Half-duplex means transferring and receiving the data, but not at the same time.

Serial Communication Calculations and Program using 8051 Microcontroller
Serial Communication Calculations and Program using 8051 Microcontroller

1. WAP to transmit the character ‘S’ to the serial window use 9600 as the baud rate?

28800 is the maximum baud rate of the 8051 microcontroller

28800/9600= 3

That baud rate ‘3’ is stored in the timers

#include<reg51.h>

void main()

{
SCON=0x50; //start the serial communication//
TNOD=0x20; //selected the timer mode//
TH1=3; // load the baud rate//
TR1=1; //Timer ON//
SBUF=’S’; //store the character in the register//
while(TI==0); //check the interrupt register//
TI=0;
TR1=0; //OFF the timer//
while(1); //continuous loop//
}

2. WAP to receive the data from the hyperterminal and send that data to the PORT 0 of the Microcontroller using 9600 baud?

28800 is the maximum baud rate of the 8051 microcontroller

28800/9600= 3

That baud rate ‘3’ is stored in the timers

#include<reg51.h>

void main()
{
SCON=0x50; //start the serial communication//
TMOD=0x20; //selected the timer mode//
TH1=3; // load the baud rate//
TR1=1; //Timer ON//
PORT0=SBUF; //send the data from SBUF to port0//
while(RI==0); //check the interrupt register//
RI=0;
TR1=0; //OFF the timer//
while(1); //stop the program when character is received//
}

Interrupt Programs using 8051 Microcontroller

The interrupt is a signal that forcing to stop the current program and execute the other program immediately. The 8051 microcontroller provide 6 interrupt, which are internal and external interrupt sources. When the interrupt occurs the microcontroller pause the current task and attend to the interrupt by executing the ISR then microcontroller returns back to the recent task.

WAP to perform left shift operation when timer 0 interrupts occurs then perform the interrupt operation for the P0 in the main function?

#include<reg51.h>

unsigned char b;

void timer0() interrupt 2 //selected timer0 interrupt//
{
b=0x10;
P1=b<<2;
}
void main()
{
unsigned char a,i;
IE=0x82 //enable the timer0 interrupt//
TMOD=0x01;
TLo=0xFC; //interrupt timer//
TH1=0xFB;
TR0=1;
a=0x00;
while(1)
{
for(i=0;i<255;i++)
{
a++;
Po=a;
}
}
}

Keypad Programming using 8051 Microcontroller

The matrix keypad is an analog switching device, which is used in many embedded applications to allow the user to perform the necessary tasks. A matrix keypad consists of an arrangement of switches in matrix format in rows and columns. The rows and columns are connected to the microcontroller such that the row of switches are connected to one pin and switches in each column are connected to another pin, then perform the operations.

Keypad Programming using 8051 Microcontroller
Keypad Programming using 8051 Microcontroller

1. WAP to toggle the LED by pressing the switch

#include<reg51.h>
sbit a=P3^0;
sbit b=P3^1;
sbit c=P3^2;
sbit d=P3^3;
void delay();
void main()
{
while(1)
{
a=0;
b=1;
c=1;
d=1;
delay();
a=1;
b=0;
c=1;
d=1;
void delay()
{
unsigned char i;
TMOD=0x20; //set the timer mode//
for(i=0i<2;i++) //double the time daly//
{
TL1=0x19; //set the time delay//
TH1=0x00;
TR1=1; //timer oN//
While(TF1==0); //check the flag bit//
TF1=0;
}
TR1=0; //timer off//
}

2. WAP to Switch ON the LED by pressing the key ‘1’ on the keypad?

#include<reg51.h>

sbit r1=P2^0;
sbit c1=P3^0;
sbit LED=P0^1;

void main()
{

r1=0;
if(c1==0)
{

LED=0xff;
}
}

3. WAP to display the number 0,1,2,3,4,5 on the seven segment by pressing the respective key on the keypad?

#include<reg51.h>

sbit  r1=P2^0;

sbit  c1=P3^0;

sbit  r2=P2^0;

sbit  c2=P3^0;

sbit a=P0^1;

void main()

{

r1=0; a=1;

if(c1==0)

{

a=0xFC;

}

If(c2==0)

{

a=0x60;

}

if(c3==0)

{

a=0xDA;

}

If(c4==0)

{

a=0xF2;

}

}

LCD Programming with 8051 Microcontroller

The LCD display is an electronic device, which is frequently used in many applications for displaying the information in a text or image format. The LCD is a display that can easily show characters on its screen. The LCD display have consists 8-data lines and 3-control lines which are used to interface to the microcontroller.

LCD Programming with 8051 Microcontroller
LCD Programming with 8051 Microcontroller

WAP to display the “EDGEFX KITS” on LED display ?

#include<reg51.h>
#define kam P0

voidlcd_initi();
voidlcd_dat(unsigned char );
voidlcd_cmd(unsigned char );
void delay();
void display(unsigned char *s, unsigned char r)

sbitrs=P2^0;
sbitrw=P2^1;
sbit en=P2^2;
void main()
{

lcd_initi();
lcd_cmd(0x80);
delay(100);
lcd_cmd(0xc0);
display(“edgefx kits”,11);
while(1);
}

void display(unsigned char *s, unsigned char r)
{
unsignedint w;
for(w=0;w<r;w++)
{
lcd_data(s[w]);
}
}
voidlcd_initi()
{
lcd_cmd(0×01);
delay(100);
lcd_cmd(0×38);
delay(100);
lcd_cmd(0×06);
delay(100);
lcd_cmd(0x0c);
delay(100);
}
voidlcd_dat(unsigned char dat)
{
kam = dat;
rs=1;
rw=0;
en=1;
delay(100);
en=0;
}
}
voidlcd_cmd(unsigned char cmd)
{
kam=cmd;
rs=0;
rw=0;

en=1;
delay(100);
en=0;
}
void delay( unsigned int n)
{

unsignedint a;
for(a=0;a<n;a++);
}

Hope this article gives a basic information about embedded system programming using 8051 microcontroller with a few example programs. For detailed embedded C programming tutorial please post your comments and queries in the comment section below.

32 Comments

  1. A K M KHURSHED ULLAH says:

    Wonderfull and so helpful

    1. Tarun Agarwal says:

      Hello A K M KHURSHED ULLAH,
      Thanks for your compliment.
      For more information please visit our blog again.

  2. abin thomas k says:

    can you help me ? can you write down a example for home automation using 8051

  3. Lovella Thurmer says:

    My brother recommended I might like this web site. He was entirely right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!|

    1. Tarun Agarwal says:

      Hi Lovella Thurmer
      I sincerely appreciate your kind response regarding my article
      And once again please visit our International website http://www.efxkits.com/
      For more details please contact to Mr. Niranjan on +91 9908518885 or you can email us on info@edgefxkits.in

  4. Denae Colbaugh says:

    Hmm it seems like your blog ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to the whole thing. Do you have any tips for novice blog writers? I’d really appreciate it.|

    1. Tarun Agarwal says:

      Hi Denae Colbaugh
      I sincerely appreciate your kind response regarding my article
      And once again please visit our International website http://www.efxkits.com/
      For more details please contact to Mr. Niranjan on +91 9908518885 or you can email us on info@edgefxkits.in

  5. I got basic information about 8051 interfacing. Excellent one

    1. Tarun Agarwal says:

      Hi Uday
      Thank you very much for telling me how much you have enjoyed reading my column
      And once again please visit our domestic website https://www.edgefxkits.com/
      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

  6. Very Useful Information. I’ve bookmarked elprocus.com for my electronic projects. Thanks a lot

    1. Tarun Agarwal says:

      Hi Daniel
      Thank you very much for telling me how much you have enjoyed reading my column
      And once again please visit our domestic website https://www.edgefxkits.com/
      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

  7. Annapurna says:

    hai sir
    can you give me the code for finger print based exam hall authentication using atmega28 MC in embedded c programing

    1. Tarun Agarwal says:

      Hi Annapurna
      Sorry to inform you that we don’t have that project
      Thank you so much for your feedback
      And once again please visit our domestic website https://www.edgefxkits.com/
      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

  8. Shubham kumar says:

    Hi,

    May u plz send me the code the code of automatic railway gate controller.

    Plz.

    1. Tarun Agarwal says:

      HI Shubham kumar
      Sorry to inform you that we don’t have that project, and we cannot provide you like that
      And once again please visit our domestic website https://www.edgefxkits.com/
      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

  9. Really nice tutorial I never seen like this much clear. Thanks I need one help how send data using Spi protocol with 8051.can you help me

    1. Tarun Agarwal says:

      Hi Pavitra

      I sincerely appreciate your kind response regarding my article

      And once again please visit our domestic website https://www.edgefxkits.com/

      For more details please contact to Sathish on +91 8885507011 or you can email us on info@edgefxkits.in

  10. Sir,it’s a fantastic site to get the basic idea for programming the 8051.i want a entire programme for fingerprint authentication with c language can u plz share that for me

  11. prabhakaran says:

    very spr sir………… i like so much thank you sir… u did very good work. sir

  12. hi
    thank you its very clear, i’m working in elevator company here they want me to find program that can remove lock in programs
    what kind of tool that i can use?
    what is the way to find programming used ?
    is there any thing u can help me with this issue
    …. eagerly waiting for your reply

    1. Tarun Agarwal says:

      Hi Vinoth,
      We are really very sorry to inform you that,we don’t do the Commercial based projects.
      Because,We deal with embedded projects.
      And also,these are These are students based projects .This projects can help full to you to get technical knowledge…
      Kindly we suggest you to please visit our website once: https://www.edgefxkits.com/

      For more details please contact Mr.Tarun Agarwal on +91-9908208883.

  13. Woow very nice and clear,I wish you could give me a very good site where i can go and learn this stuff……………..I really appreciated.

    1. Tarun Agarwal says:

      Hi Dhaval,

      Thank you for your appreciation. It really helps us to work hard. Please check our user friendly website http://www.edgefxkits.com for project ideas. .

      Raji (Mobile: +91 9959178000)

  14. nice one..i very happy and please request to detals share with me
    mobile controll home automation

    1. Tarun Agarwal says:
  15. Thanks Tarun! Your tutorials are very well constructed. This site is my favorite place for learning about 8051 microcontroller programming.

    1. Tarun Agarwal says:

      Hi Tulasi,

      Thank you for your appreciation.Also, check the user friendly website http://www.edgefxkits.com for all the latest projects ideas.

      Raji (Mobile: +91 9959178000)

  16. Woow very nice and clear,I wish you could give me a very good site where i can go and learn this stuff……………..I really appreciated.

    1. Tarun Agarwal says:

      Hi Harshini,

      Thank you for your appreciation. It really helps us to work hard. Please check the user friendly website http://www.edgefxkits.com for all the latest projects ideas on Electrical and Electronics.

      Raji (Mobile: +91 9959178000)

Add Comment