Department of Electronics and Computer Science
TE(ETRX) SEM-V
Microcontrollers & Applications
By
Dr. Arun Chavan
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
What is Embedded C ?
The C Programming Language provided low level memory access using an
uncomplicated compiler (a software that converts programs to machine code)
and achieved efficient mapping to machine instructions.
The C Programming Language became so popular that it is used in a wide range
of applications ranging from Embedded Systems to Super Computers.
Embedded C Programming Language, which is widely used in the development
of Embedded Systems, is an extension of C Programming Language. The
Embedded C Programming Language uses the same syntax and semantics of
the C Programming Language like main function, declaration of data types,
defining variables, loops, functions, statements, etc.
The extension in Embedded C from standard C Programming Language include
I/O Hardware Addressing, accessing address spaces, etc.
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C Data types
Bits
Data Type Range
(Bytes)
bit 1 0 or 1 (bit addressable RAM)
signed int 16 (2) -32768 to +32767
unsigned int 16 (2) 0 to 65535
signed char 8 (1) -128 to +127
unsigned char 8 (1) 0 to 255
float 32 (4) ±1.175494E-38 to ±3.402823E+38
sbit 1 0 or 1 (bit addressable sfr)
sfr 8 (1) Addresses (80h to FFh)
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Modifiers in Embedded C
These are keywords in C to modify the default properties of int and char data
types. There are 4 modifiers in C as follows.
1. Short (2 Bytes)
It limits user to store small integer values from -32768 to +32767. It can be used
only on int data type.
short int myShortIntegerValue = 18;
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Modifiers in Embedded C
2. Long (4 Byte) & Long Long (8 Byte)
It allows user to store very large number (something like 9 Million/Trillion) from -
9223372036854775808 to 9223372036854775807. Syntax “long long” can also be
used instead of “long int”.
long long int myLongIntegerValue = 827337203685421584;
3. signed
It is default modifier of int and char data type if no modifier is specified. It says that
user can store negative and positive values.
signed int myNegativeIntegerValue = -544;
signed int mypositiveIntegerValue = 544; /* Both statements have same meant even
without "signed" modifier*/
4.unsigned
When user intends to store only positive values in the given data type (int and char).
unsigned int myIntegerValue = 486;
8051 Programming in Embedded C
Qualifiers in Embedded C
Two types of qualifiers available in C , 'const' and 'volatile'.
'Const' qualifier will impose a restriction on the variable, such a way that its value
can't be changed or modified. The main use of 'const' is to define constants in your
program; so that it's value can't be changed.
Please take a look at the example given below to understand the use case of
'const' qualifier.
For example:
#include <stdio.h>
// PI is defined as 'const' so that you may not change it's value accidentally.
const float PI = 3.14;
{
return PI * radius * radius;
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Qualifiers in Embedded C
If a variable is declared as 'volatile', its value can be changed from outside the
program.
Let's look at it with the help of an example using 'volatile' keyword.
For example:
#include <stdio.h>
volatile int signal = 0;
{
while (signal == 0)
{
}
return 1;
} Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Macros and Functions in Embedded C
Macros are pre-processed which means that all the macros would be processed
before your program compiles. However, functions are not pre-processed but
compiled.
Macro Function
Macro is Preprocessed Function is Compiled
No Type Checking is done in Macro Type Checking is Done in Function
Using Function keeps the code length
Using Macro increases the code length
unaffected
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Macros and Functions in Embedded C
Macro Function
Speed of Execution using Macro is Speed of Execution using Function is
Faster Slower
Before Compilation, macro name is During function call, transfer of
replaced by macro control takes place
Macros are useful when small code is Functions are useful when large code
repeated many times is to be written
Macro does not check any Compile-
Function checks Compile-Time Errors
Time Errors
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Interrupt Service Routine (ISR) in Embedded C
An ISR (also called an interrupt handler) is a software process invoked by an
interrupt request from a hardware device. It handles the request and sends it to
the CPU, interrupting the active process. When the ISR is complete, the process is
resumed.
interrupt void isr(void)
{
/* normal C code here */
};
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to toggle Pins of Port 2
#include<reg 51.h>
void main()
{
int x;
while (1)
{
P2=0x00;
for (x=0;x<=5000;x++) //software delay
P2=0xff;
for (x=0;x<=5000;x++) //software delay
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to toggle pin 1 of Port 1 continuously
without affecting other pins of Port 1
#include<reg 51.h>
sbit mybit=P1^1; // Pin 1 of Port 1 named as mybit
void main()
{
int x;
while (1)
{
mybit=0; //clear pin 1 of port 1
for (x=0;x<=5000;x++) //software delay
mybit=1; //set pin 1 of port 1
for (x=0;x<=5000;x++) //software delay
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to monitor Port 1 pin 0, if it is high then make Port 1 =
00H, else make Port 2=00H
#include<reg 51.h>
sbit mybit=P1^0; // Pin 0 of Port 1 named as mybit
void main()
{
while (1)
{
if (mybit==1) // check if P1.0=1
{
P1=0x00; //if yes P1=00h
}
else // otherwise
{
P2=0x00H; //P2=00h
}
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to convert ASCII digit 6 and 8 to packed
BCD and send it on Port 0
#include<reg 51.h>
void main()
{
unsigned char data1 = 0x36;
unsigned char data2 = 0x38;
P0 = (data2 & 0x0f) + ((data1 & 0x0f) << 4);
while (1)
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Embedded C program to switch on the alarm connected to P1.7, when the lift
door is open. The lift door opening is sensed by the sense of P1.1
#include<reg 51.h>
sbit door=P1^1; // Pin 1 of Port 1 named as door
sbit alarm=P1^7; // Pin 7 of Port 1 named as alarm
void main()
{
int x;
alarm = 0;
while (1)
{
if (door==1) // checking P1.1 pin
{
alarm = 1;
for (x=0;x<=5000;x++) // software delay
alarm = 0;
}
} Dr. Arun Chavan, Department of Electronics
} and Computer Science
8051 Programming in Embedded C
Program for LED Blinking using 8051 Microcontroller
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
Program for LED Blinking using 8051 Microcontroller
#include<reg51.h> //header file//
void main() //the program execution start point//
{
unsigned int i; //data type//
while(1) //for continuous loop//
{
P1=0x55; //send the hex value to port 1//
for(i=0;i<40000;i++) //normal delay//
P1=0xAA; //send the hex value to port 1//
for(i=0;i<40000;i++) //normal delay//
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
WAP to generate the delay of 500 us and 1 sec using T1M1(timer 1 in mode 1) and
T0M1(timer 0 in mode 1) respectively, assume the oscillator frequency as 12 MHz.
#include<reg51.h> #include<reg51.h>
void delay() void delay()
{ {
TMOD=0x10; int count=0;
TL1=0x0c; while(count!=20);
TH1=0xfe; {
TR1=1; TMOD=0x01;
while (TF1==0); TL0=0xb0;
TR1=0; TH0=0x3c;
TF1=0; TR0=1;
} while(TF0==0);
TR0=0;
TF0=0;
count++;
}
}
Dr. Arun Chavan, Department of Electronics
and Computer Science
8051 Programming in Embedded C
WAP to transmit the character ‘S’ to the serial port, use 9600 as the baud rate
(assume the oscillator frequency as 12 MHz)?
#include<reg51.h>
void main()
{
while(1)
{
SCON=0x40; //program the serial port//
PCON=0x00; // program SMOD bit = 0 //
TMOD=0x20; //program timer 1 in mode 2//
TL1=0xfd; // load the count for 9600 baud//
TH1=0xfd; //load the count for 9600 baud//
TR1=1; //timer 1 ON//
SBUF=’S’; //write the character in the register//
while(TI==0); //check TI for ‘Transmit Interrupt’//
TI=0; // reset TI when found set//
TR1=0; //timer 1 OFF//
}
} Dr. Arun Chavan, Department of Electronics
and Computer Science
Assignment no - 7
Short Questions (2 Marks)
1. List the Embedded C data types
2. What are Modifiers in Embedded C ?
3. What are Qualifiers in Embedded C ?
4. What is ISR in Embedded C ?
Descriptive Questions (5 Marks)
1. Differentiate between ‘Macros’ & ‘Functions’
2. Write Embedded C program to switch on the alarm connected to P1.7, when the lift door is
open. The lift door opening is sensed by the sense of P1.1
3. Write Embedded C program to toggle P1.1 at the regular interval of 5 msec.
Descriptive Questions (10 Marks)
1. Write Embedded C program for LED blinking at the regular interval of 2 secs. Assume the
oscillator frequency as 12 MHz and LEDs connected to Port 1.
2. Write Embedded C program to transmit character ‘A’ through the serial port at the baud
rate of 2400. Assume the oscillator frequency as 12 MHz.
Thank You
Dr. Arun Chavan, Department of Electronics
and Computer Science