Вы находитесь на странице: 1из 59

Microcontroller 8051/89S52

Embedded C Programming

Getting Stated with Keil Software


01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101

Research
Design Lab

Introduction to Embedded C
Interfacing Examples
Mini Projects on GSM, Xbee & Bluetooth

www.researchdesignlab.com
Email: sales@researchdesignlab.com I www.researchdesignlab.com
An ISO 9001- 2008 Certified Company

Index
Embedded Systems 8051/89S52 Microcontroller-Introduction
Keil-Introduction
Creating a new project
Creating a new source file
Creating HEX for the Part
Testing Program in Debugger
Running the Keil Debugger (Simulation)
LED Blinking using 8051 Microcontroller and Keil AT89S52
LCD Keypad interfacing using 8051 Microcontroller and Keil AT89S52
LCD Keypad and Relays interfacing using 8051 Microcontroller and Keil AT89S52
Serial Communication
Simple Serial interfacing using 8051 Microcontroller and Keil AT89S52
Bluetooth and Relays interfacing using 8051 Microcontroller and Keil AT89S52
Keypad and XBee interfacing using 8051 Microcontroller and Keil AT89S52
LEDs and XBee interfacing using 8051 Microcontroller and Keil AT89S52
GSM Modem and LCD interfacing using 8051 Microcontroller and Keil AT89S52
Analog to digital conversion in 8051 Microcontroller and Keil-AT89S52

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1

1
2
2
5
9
12
14
17
19
23
28
30
34
37
41
44
52

WWW.RESEARCHDESIGNLAB.COM

Introduction

Embedded Systems
8051/89S52 Microcontroller
Overview:
The 8051 microcontroller is one of the most
popular general purpose microcontrollers in
use today. The 8051 is an 8-bit microcontroller
which means that most available operations
are limited to 8 bits. 8051 chips are used in a
wide variety of control systems, telecom
applications, robotics as well as in the
automotive industry.

Ports:
There are 4 8-bit ports: P0, P1, P2 and P3.
PORT P1 (Pins 1 to 8): The port P1 is a general
purpose input/output port which can be used
for a variety of interfacing tasks. The other
ports P0, P2 and P3 have dual roles or
additional functions associated with them
based upon the context of their usage. The
port 1 output buffers can sink/source four TTL
inputs. When 1s are written to portn1 pins are
pulled high by the internal pull-ups and can be
used as inputs.

Fig A.
Pin description of 8051

PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional functions such
as, serial transmit and receive pins, 2 external interrupt pins, 2 external counter inputs, read and
write pins for memory access.
PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port when no external
memory is present, but if external memory access is required then PORT P2 will act as an address bus
in conjunction with PORT P0 to access external memory. PORT P2 acts as A8-A15, as can be seen from
fig 1.1
PORT P0 (pins 32 to 39)P0 can be used as a general purpose 8 bit port when no external memory is
present, but if external memory access is required then PORT P0 acts as a multiplexed address and
data bus that can be used to access external memory in conjunction with PORT P2. P0 acts as AD0AD7, as can be seen from fig 1.1

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P1

WWW.RESEARCHDESIGNLAB.COM

Keil
Keil provides a broad range of development tools like ANSI C compiler, macro assembler, debuggers
and simulators, linkers, IDE, library managers, real-time clock operating systems and evaluation
boards for 8051.
Install Keil by following the instructions sets provided in your software.

Creating a new project


1) Open Keil.
2) Select New Vision Project from the Project Menu.

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P2

WWW.RESEARCHDESIGNLAB.COM

Tips
IR Obstacle
Sensor

Quick Overview

3) Name the project Led1.


4) Click on the Save Button.

Based on a simple basic


Idea, this IR obstacle
sensor, is easy to build,
easy to calibrate and
still, it provides a
detection range of 1030 cm. This sensor can
be used for most indoor
applications where no
important ambient light
is present. It is the same
principle in ALL InfraRed proximity sensors.
The basic idea is to send
infra red light through
IR-LEDs, which is then
reflected by any object
in front of the sensor.

Code & Schematic


For more details
www.researchdesignlab.com

5) The device window will be displayed. Select the manufacturer of the IC.
Here let us use ATMEL AT89S52
6) Double Click on Atmel.
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P3

WWW.RESEARCHDESIGNLAB.COM

7) Scroll down and select AT89S52

Tips
Carbon
Monoxide
Sensor

Quick Overview
MQ-7 gas sensor
composed by micro
AL2O3 ceramic tube, Tin
Dioxide (SnO2) sensitive
layer , measuring
electrode and heater
are fixed into a crust
made by plastic and
stainless steel net. The
heater provides
necessary work
conditions for work of
sensitive components.

Code & Schematic


For more details
www.researchdesignlab.com

8) Click OK
9) Choose No.

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P4

WWW.RESEARCHDESIGNLAB.COM

Creating a
new source file
1)Click File Menu and
select New.

2) A new window will


open up in the Keil IDE.
3) Let us write a simple
code that would toggle
the status of Port 1 with
a small delay.

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P5

WWW.RESEARCHDESIGNLAB.COM

4) Click on File menu


and select Save as
5) Name the file
Led1_blinking.c

6) Click the Save Button


7) In the Project
Workspace window,
click on the +
symbol in front of
Target1.

Quick Overview

Tips
Code & Schematic
For more details
www.researchdesignlab.com

Gas Sensor

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P6

This is a simple-to-use liquefied petroleum gas (LPG) sensor,


suitable for sensing LPG (composed of mostly propane and
butane) concentrations in the air. Used in gas leakage
detecting equipments for detecting of LPG, iso-butane,
propane, LNG combustible gases. If output goes above a
preset range the output is low else high in idle condition.

WWW.RESEARCHDESIGNLAB.COM

8) Below that Source Group 1 would appear, right click on it.

Tips
Digital Soil
Moisture
Sensor

Quick Overview
This sensor can be used
to test the moisture of
soil, when the soil is
having water shortage,
the module output is at
high level, else the
output is at low level.
By using this sensor one
can automatically water
the flower plant, or any
other plants requiring
automatic watering
technique. Module
triple output mode,
digital output is simple,
analog output more
accurate, serial output
with exact readings.

9) Click on Add Files


to Group Source
Group 1
10) Select
Led1_blinking.c
11) Click Add button
12) Click Close button.
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P7

WWW.RESEARCHDESIGNLAB.COM

13) Click Close button.


Expand the Source
Group 1 in the tree
menu to ensure that
the file was added to
the project.

Tips
Code & Schematic
For more details
www.researchdesignlab.com

Digital
Heart Beat
Sensor

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P8

Quick Overview
This heart beat sensor is designed to give
digital output of heart beat when a finger is
placed on it. When the heartbeat detector is
working, the top-most LED flashes with each
heart beat. This digital output can be
connected to micro controller directly to
measure the Beats Per Minute (BPM) rate. It
works on the principle of light modulation by
blood flow through finger at each pulse.
Module dual output mode, digital output is
simple, serial output with exact readings.

WWW.RESEARCHDESIGNLAB.COM

Creating HEX for the


Part
1) Right click on Target
1 in Tree menu
2) Select Options for
Target Target 1

Tips
Analog
Light
Intensity
Sensor

Quick Overview
Light dependent resistor
(LDR), suitable for use in
projects which require a
device or circuit to be
automatically switched
on or off in darkness or
light. As the amount of
light falling on this LDR
increases, its resistance
decreases. The light
detector itself is just
5mm in diameter.Analog
output more accurate.

3) Select Target Tab


4) Change Xtal (Mhz) from 33.0 to 11.0592

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P9

Code & Schematic


For more details
www.researchdesignlab.com

WWW.RESEARCHDESIGNLAB.COM

5) Select Output Tab


6) Click on Create
Hex File check box
7) Click OK Button

8. Click on Project
Menu and select
Rebuild all Target
Files

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P10

WWW.RESEARCHDESIGNLAB.COM

9. In the Build Window it should report 0 Errors (s), 0 Warnings

10. You are now ready to Program your Part

Quick Overview

Tips
Code & Schematic
For more details
www.researchdesignlab.com

Digital
Vibration
Sensor
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P11

This basic piezo sensor can be used in anti-theft


devices, electronic locks, mechanical equipment
vibration detection, sound gesture application and
detection range bull's-eye counts vibration testing
occasions. These vibration levels could be given to
any controller/processor and necessary decisions
could be taken through it. Module triple output
mode, digital output is simple, analog output more
accurate, serial output with exact readings.

WWW.RESEARCHDESIGNLAB.COM

Testing Program in Debugger


1) Click on the File Menu and select Save

Tips
Digital
Light
Intensity
Sensor

Quick Overview

2)Click on Project Menu and select Rebuild all Target Files

Light dependent resistor


(LDR), suitable for use in
projects which require a
device or circuit to be
automatically switched
on or off in darkness or
light. As the amount of
light falling on this LDR
increases, its resistance
decreases. The light
detector itself is just
5 m m i n d i a m e t e r.
Module triple output
mode, digital output is
simple, analog output
more accurate, serial
output with exact
readings.

Code & Schematic


For more details
www.researchdesignlab.com

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P12

WWW.RESEARCHDESIGNLAB.COM

3) In the Build Window it should report 0 Errors (s), 0 Warnings


4) Click on Debug Menu and Select Start/Stop Debug Session.

Tips
Serial
Ultrasonic
Distance
Measure

Quick Overview

5) If you are using a free version of Keil the dialog appears. Click OK.

Ultrasonic Distance
Sensor comes with an
ASCII serial O/P and
provided optimum
ranging & detection of
long to short distance
ranges. Owing to their
stable, precise, noncontact and accurate
distance measurements
from about 2 cm to 4
meters.Compactly
designed, easy usable,
high ranging and easily
interfaced upon micro
controllers RX and TX
pin.
Code & Schematic
For more details
www.researchdesignlab.com

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P13

WWW.RESEARCHDESIGNLAB.COM

Running the Keil Debugger (Simulation)


1) The Keil Debugger should be now running.

Tips
Ultrasonic
Ranging
Sensor

Quick Overview

2) Click on Peripherals. Select I/O Ports, Select Port 1.

Ultrasonic sensor
provides stable and
accurate distance
measurements from
2cm to 450cm. It has an
focus of less than 15
degrees and an accuracy
of about 2mm.

Code & Schematic


For more details
www.researchdesignlab.com

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P14

WWW.RESEARCHDESIGNLAB.COM

3) A new window
should port will pop
up. This represents
the Port and Pins

Tips
Ultrasonic
Obstacle
Sensor

Quick Overview
Used to detect the move
of human or object.
Suitable for indoor and
outdoor burglar proof
application, vehicle
a p p l i c a t i o n , AT M
survillence camera etc.

Code & Schematic


For more details
www.researchdesignlab.com

4) To execute your code Click Run. The Parallel Port 1 Box status
should change as a continuous loop.

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P15

WWW.RESEARCHDESIGNLAB.COM

5) To exit out, first Click on Debug Menu and Select Stop Running

Tips
Gyro
Sensor

Quick Overview
The L3G4200DTR is a
low-power, three-axis
angular rate sensor, able
t o
p r o v i d e
unprecedented
stablility of zero rate
level and sensitivity over
temperature and time.It
includes a sensing
element and an IC
interface capable of
providing the measured
angular rate to the
external world through a
digital interface
(I2C/SPI).

Code & Schematic


For more details
www.researchdesignlab.com

6) And then Click on


Debug Menu and
Select Start/Stop
Debug Session

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P16

WWW.RESEARCHDESIGNLAB.COM

Learn embedded C programming in 8051


LED Blinking using 8051 Microcontroller and Keil AT89S52
Circuit and Working:
Fig.1 shows the circuit of simple
8051 Microcontroller interfaced
with LEDs.
Here are 3 simple programs for
controlling LEDs through simple
Embedded C programming in
Microcontroller.
Program 1 shows how to control
the entire port by toggling 8
LEDs.
Program 2 shows how to control
single I/O pin of the controller
one by one just like a Decimal
counter output.

Testing:
1) Write the program as shown below and generate the hex
file by the instructions provided in chapter 1.
2) Burn the code with the help of burner to the controller.
3) Power ON your microcontroller and check the result as per
your program.
Components/modules required :
1) 8051 project board (assembled/non assembled kit).
2) 5V DC source.
3) 8 LEDs.
4) Resistors (1Kx8).
5) IC AT89S52.
6) 8051 IC burner.
7) Connectors and cables.

Fig 1. Circuit Diagram for LCD and 1x4 keypad interfacing


RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P17

WWW.RESEARCHDESIGNLAB.COM

Program 1:
#include<reg52.h> // special function register
//declarations for the
// intended 8051 derivative
#define LEDPORT P2 //Defining Port 2 as the
//'LEDPORT'
// Function prototype declaration
void delay();
//Main Code
void main()
{
//Set Port 2 all bits to 0
P2=0X00;
//infinite loop
while(1)
{
//Set LEDPORT all bits to 1
LEDPORT =0XFF;
// Wait for a small delay
delay();
//Set LEDPORT all bits to 0
LEDPORT =0X00;
// Wait for small delay
delay();
}
}
// Delay Routine
void delay()
{
// larger the value of x
unsigned int x=60000;
//the more is the delay.
// executes this statement
while (x--) ;
// until x decrements to 0
}
Program 2:
#include<reg52.h>
void delay();

//special function register declarations


//for the intended 8051 derivative
// Function prototype declaration

sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit

//Define
//Define
//Define
//Define
//Define
//Define
//Define
//Define

LED0=P2^0;
LED1=P2^1;
LED2=P2^2;
LED3=P2^3;
LED4=P2^4;
LED5=P2^5;
LED6=P2^6;
LED7=P2^7;

Port
Port
Port
Port
Port
Port
Port
Port

Pin
Pin
Pin
Pin
Pin
Pin
Pin
Pin

P2.0 as LED0
P2.1 as LED1
P2.2 as LED2
P2.3 as LED3
P2.4 as LED4
P2.5 as LED5
P2.6 as LED6
P2.7 as LED7

void main()//Main Code


{
//Set Port 2 all bits to 0
P1=0x00;
// Continuous loop
while(1)
{
//Turn ON LED0
LED0=1;
//Wait for a small delay
delay();
//Turn ON LED1
LED1=1;
//Wait for a small delay
delay();
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P18

Tips
Current
Sensor 20A

Quick Overview
The ACS712 provides
economical and precise
solutions for AC or DC
current sensing in
industrial, commercial,
and communications
systems. The device
package allows for easy
implementation by the
c u s t o m e r. Ty p i c a l
applications include
motor control, load
detection and
m a n a g e m e n t ,
switchmode power
supplies, and
overcurrent fault
protection. The device
is not intended for
a u t o m o t i v e
applications.
Code & Schematic
For more details
www.researchdesignlab.com

WWW.RESEARCHDESIGNLAB.COM

LED2=1;
delay();
LED3=1;
delay();
LED4=1;
delay();
LED5=1;
delay();
LED6=1;
delay();
LED7=1;
delay();
P1=0x00;
delay();

//Turn ON LED2
//Wait for a small
//Turn ON LED3
//Wait for a small
//Turn ON LED4
//Wait for a small
//Turn ON LED5
//Wait for a small
//Turn ON LED6
//Wait for a small
//Turn ON LED7
//Wait for a small

delay
delay
delay
delay
delay

LCD Keypad interfacing using 8051


Microcontroller and Keil AT89S52
Circuit and Working:
Fig.2 shows the circuit of simple 8051
Microcontroller interfaced with LCD and
1x4 Keypad.
Here is a simple program for interfacing
LCD and keypad through simple
Embedded C programming in
Microcontroller.

delay

//Turn OFF all LED's


//Wait for a small delay

}
}
// Delay Routine
void delay()
{
unsigned int x=60000 // larger the value of x the
//more is the delay.
// executes this statement
while (x--);
//until x decrements to 0
}

Program 3 demonstrates how to display in


a LCD when an event occurs like a key is
being pressed.
Components/modules required:
1) 8051 project board
(assembled/non assembled kit).
2) 5V DC source.
3) LCD interfacing Module
4) 4 Keys keypad
5) IC AT89S52.
6) 8051 IC burner.
7) Connectors and cables.

Fig 2. Circuit Diagram for LCD and 1x4 keypad interfacing


RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P19

WWW.RESEARCHDESIGNLAB.COM

Program 3:
#include<reg51.h>

//special function register declarations


//for the intended 8051 derivative
// LCD connections
//Define Port 2 as LCD Data pins.
#define LCD_PORT P2
//Register Select is connected to Port 3 pin 5
sbit rs=P3^5;
//Enable is connected to Port 3 pin 7
sbit en=P3^7;
//Data Pin D7 is connected to Port 2 pin 7
sbit D7=P2^7;
//Read/Write is connected to Port 3 pin 6
sbit rw=P3^6;
// Keypad connections
//Switch 1 is connected to Port 0 pin 3
sbit key1=P0^3;
//Switch 2 is connected to Port 0 pin 2
sbit key2=P0^2;
//Switch 3 is connected to Port 0 pin 1
sbit key3=P0^1;
//Switch 4 is connected to Port 0 pin 0
sbit key4=P0^0;
// Call function declarations
//This Function checks whether the LCD is
void busy();
//ready to receive next byte
void CMD_WRT(unsigned char); //This Function is used to write commands
//into the LCD
void LCD_WRT(unsigned char *); //This Function is used to write Strings into
//the LCD
void DATA_WRT(unsigned char); //This Function is used to write a byte of data
//into the LCD
//Call function declarations for delay
void DELAY();
// MAIN CODE
void main()
{
unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80}; // LCD Initialization
//commands, hex codes
// Variable declarations
unsigned char TEMP,i;
for(i=0;i<=4;i++)
{
TEMP=CMD[i];
//Write the commands into the LCD
CMD_WRT(TEMP);
}
//This command clears the LCD screen
CMD_WRT(0X01);
//This moves the cursor to the beginning of the 1st
CMD_WRT(0X80);
//line
//Writes the text 'RDL' in the LCD
LCD_WRT("RDL");
//These delay's will hold the above text 'RDL' for
DELAY();
//some time
DELAY();
DELAY();
DELAY();
// Whatever comes next will execute forever
//Continuous loop
while(1)
{

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P20

WWW.RESEARCHDESIGNLAB.COM

//Check whether switch 1 is being pressed


if (key1 ==0)
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Key 1 is pressed"); //Writes the text within quotes in the LCD
//Wait until the switch has been released
while(key1==0);
}
//Check whether switch 2 is being pressed
else if (key2 ==0)
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Key 2 is pressed"); //Writes the text within quotes in the LCD
//Wait until the switch has been released
while(key2==0);
}
//Check whether switch 3 is being pressed
else if (key3 ==0)
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Key 3 is pressed"); //Writes the text within quotes in the LCD
//Wait until the switch has been released
while(key3==0);
}
//Check whether switch 4 is being pressed
else if (key4 ==0)
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Key 4 is pressed"); //Writes the text within quotes in the LCD
//Wait until the switch has been released
while(key4==0);
}
else
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("No key Pressed"); //Writes the text within quotes in the LCD
// A small delay for relaxation
DELAY();
DELAY();
DELAY();
}
}
}
//This Function checks whether the LCD is ready to receive next byte
void busy()
{
D7=1;
rs=0;
rw=1;

//Keep D7 pin to High


//Keep RS to Low to select command register
//RW=1 for read

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P21

WWW.RESEARCHDESIGNLAB.COM

while(D7!=0)
{
en=0;
en=1;
}
}

//Monitor D7 pin until it gets low


//Provide a latch pulse from low to high to EN

//This Function is used to write commands into the LCD


void CMD_WRT(unsigned char val)
{
//Execute busy function to know whether the LCD is
busy();
//ready to receive any data/command
//Put the variable val into LCD_PORT which is
LCD_PORT=val;
//connected to LCD data pins
//Keep RS to Low to select command register
rs=0;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}
//This Function is used to write Strings into the LCD
void LCD_WRT(unsigned char *string)
{
//increment from the beginning of the string until a
while(*string)
//null character is detected (end of the string)
//separates a single byte from the string
DATA_WRT(*string++);
}
//This Function is used to write a byte of data into the LCD
void DATA_WRT(unsigned char ch)
{
//Execute busy function to know whether the LCD is
busy();
//ready to receive any data/command
//Put the variable ch into LCD_PORT which is
LCD_PORT = ch;
//connected to LCD data pins
//Keep RS to High to select Data register
rs=1;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}
//function for delay routine
void DELAY()
{
// larger the value of X and Y the more is the
unsigned int X=60000,Y=60000;
//delay.
//executes this statement until X decrements to 0
while(X--);
//executes this statement until Y decrements to 0
while(Y--);
}

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P22

WWW.RESEARCHDESIGNLAB.COM

LCD Keypad and Relays interfacing using 8051


Microcontroller and Keil AT89S52
Circuit and Working:
Fig.3.1 and Fig.3.2 shows the circuit of simple 8051
Microcontroller interfaced with LCD, 1x4 Keypad and 4 Relays.
Here is a simple Embedded C program for interfacing 4 Relays
to a 8051 Microcontroller which could be controlled by a key
press event through a 1x4 keypad, the result or state of the
relays being displayed on the LCD interfaced along with this.
Program 4 enables a user to toggle the state of relays by
pressing a key consequently the result gets displayed on the
LCD interfaced.

Components/modules required:
1) 8051 project board
(assembled/non assembled kit).
2) 12V and 5V DC source.
3) LCD interfacing Module
4) 4 Keys keypad
5) 4 Relay Interfacing Board 12V
6) IC AT89S52.
7) 8051 IC burner.
8) Connectors and cables

Fig 3.1.
Circuit Diagram for
Relay LCD and 1x4
keypad interfacing
- Part 1.

Fig 3.2.
Circuit Diagram for Relay LCD
and 1x4 keypad interfacing
- Part 2.

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P23

WWW.RESEARCHDESIGNLAB.COM

Program 4:
#include<reg52.h>

#define LCD_PORT P2
sbit rs=P3^5;
sbit en=P3^7;
sbit D7=P2^7;
sbit rw=P3^6;

sbit
sbit
sbit
sbit

sbit
sbit
sbit
sbit

//special function register declarations


//for the intended 8051 derivative
// LCD connections
//Define Port 2 as LCD Data pins.
//Register Select is connected
//to Port 3 pin 5
//Enable is connected
//to Port 3 pin 7
//Data Pin D7 is connected
//to Port 2 pin 7
//Read/Write is connected
//to Port 3 pin 6

key1=P0^3;
key2=P0^2;
key3=P0^1;
key4=P0^0;

// Keypad connections
//Switch 1 is connected
//Switch 2 is connected
//Switch 3 is connected
//Switch 4 is connected

Relay1=P1^3;
Relay2=P1^2;
Relay3=P1^1;
Relay4=P1^0;

// Relay Connections
//Relay 1 is connected to Port 1 pin 3
//Relay 2 is connected to Port 1 pin 2
//Relay 3 is connected to Port 1 pin 1
//Relay 4 is connected to Port 1 pin 0

to
to
to
to

Port
Port
Port
Port

0
0
0
0

pin
pin
pin
pin

3
2
1
0

// Call function declarations


//This Function checks whether the
//LCD is ready to receive next byte
CMD_WRT(unsigned char); //This Function is used to write commands
// into the LCD
LCD_WRT(unsigned char *); //This Function is used to write Strings
//into the LCD
DATA_WRT(unsigned char); //This Function is used to write a byte
//of data into the LCD
//Call Function declarations for delay
DELAY();

void busy();
void
void
void
void

// MAIN CODE
void main()
{
unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80};
unsigned char TEMP,i;
P1=0X00;
for(i=0;i<=4;i++)
{
TEMP=CMD[i];
CMD_WRT(TEMP);
}

// LCD Initialization
// commands, hex codes
// Variable declarations
//Set Port 1 all bits to 0

//Write the commands into the LCD

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P24

WWW.RESEARCHDESIGNLAB.COM

//This command clears the LCD screen


CMD_WRT(0X01);
//This moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
//Writes the text 'RDL' in the LCD
LCD_WRT("RDL");
//These delay's will hold the above text 'RDL' for some time
DELAY();
DELAY();
DELAY();
DELAY();
//Continuous loop
while(1)
{
if (key1 ==0 && Relay1==0) //Check whether switch 1 is being pressed
//and Relay1 is OFF
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Relay 1 is ON"); //Writes the text within quotes in the LCD
//Turn ON Relay1
Relay1=1;
//Wait until the switch has been released
while(key1==0);
}
else if (key1 ==0 && Relay1==1) //Check whether switch 1 is being pressed
//and Relay1 is ON
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Relay 1 is OFF"); //Writes the text within quotes in the LCD
//Turn OFF Relay1
Relay1=0;
//Wait until the switch has been released
while(key1==0);
}
else if (key2 ==0 && Relay2==0) //Check whether switch 2 is being pressed
//and Relay2 is OFF
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Relay 2 is ON"); //Writes the text within quotes in the LCD
//Turn ON Relay2
Relay2=1;
//Wait until the switch has been released
while(key2==0);
}
else if (key2 ==0 && Relay2==1) //Check whether switch 2 is being pressed
//and Relay2 is ON
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Relay 2 is OFF"); //Writes the text within quotes in the LCD
//Turn OFF Relay2
Relay2=0;
//Wait until the switch has been released
while(key2==0);
}
else if (key3 ==0 && Relay3==0) //Check whether switch 3 is being pressed
//and Relay3 is OFF
{
//Clears the LCD screen
CMD_WRT(0X01);
//Moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("Relay 3 is ON"); //Writes the text within quotes in the LCD
//Turn ON Relay3
Relay3=1;
//Wait until the switch has been released
while(key3==0);
}

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P25

WWW.RESEARCHDESIGNLAB.COM

else if (key3 ==0 && Relay3==1)


{
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("Relay 3 is OFF");
Relay3=0;
while(key3==0);
}
else if (key4 ==0 && Relay4==0)
{
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("Relay 4 is ON");
Relay4=1;
while(key4==0);
}
else if (key4 ==0 && Relay4==1)
{
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("Relay 4 is OFF");
Relay4=0;
while(key4==0);
}
else
{
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("Press any key..");
DELAY();
}
}
}

//Check whether switch 3 is being pressed


//and Relay3 is ON
//Clears the LCD screen
//Moves the cursor to the beginning of the 1st line
//Writes the text within quotes in the LCD
//Turn OFF Relay3
//Wait until the switch has been released
//Check whether switch 4 is being pressed
//and Relay4 is OFF
//Clears the LCD screen
//Moves the cursor to the beginning of the 1st line
//Writes the text within quotes in the LCD
//Turn ON Relay4
//Wait until the switch has been released
//Check whether switch 4 is being pressed
//and Relay4 is ON
//Clears the LCD screen
//Moves the cursor to the beginning of the 1st line
//Writes the text within quotes in the LCD
//Turn OFF Relay4
//Wait until the switch has been released

//Clears the LCD screen


//Moves the cursor to the beginning of the 1st line
//Writes the text within quotes in the LCD
// A small delay for relaxation

//This Function checks whether the LCD is ready to receive


//next byte
void busy()
{
D7=1;
rs=0;
rw=1;
while(D7!=0)
{
en=0;
en=1;
}
}

//Keep D7 pin to High


//Keep RS to Low to select command register
// RW=1 for read
//Monitor D7 pin until it gets low
//Provide a latch pulse from low to high to EN

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P26

WWW.RESEARCHDESIGNLAB.COM

//This Function is used to write commands into the LCD


void CMD_WRT(unsigned char val)
{
//Execute busy function to know whether the LCD is
busy();
//ready to receive any data/command
//Put the variable val into LCD_PORT which is
LCD_PORT=val;
//connected to LCD data pins
//Keep RS to Low to select command register
rs=0;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}
//This Function is used to write Strings into the LCD
void LCD_WRT(unsigned char *string)
{
// increment from the beginning of the string until
while(*string)
//a null character is detected (end of the string)
//separates a single byte from the string
DATA_WRT(*string++);
}
//This Function is used to write a byte of data into the LCD
void DATA_WRT(unsigned char ch)
{
//Execute busy function to know whether the LCD
busy();
//is ready to receive any data/command
//Put the variable ch into LCD_PORT which is
LCD_PORT = ch;
//connected to LCD data pins
//Keep RS to High to select Data register
rs=1;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}
//Function for delay routine
void DELAY()
{
unsigned int X=60000;
while(X--);
}

// larger the value of X the more is the delay.


//executes this statement until X decrements to 0

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P27

WWW.RESEARCHDESIGNLAB.COM

Serial Communication
Asynchronous serial data communication is widely used for character-oriented transmissions. Each
character is placed in between start and stop bits, this is called framing. Block-oriented data
transfers use the synchronous method. The start bit is always one bit, but the stop bit can be one or
two bits. The start bit is always a 0 (low) and the stop bit(s) is 1 (high).
We need a line driver (voltage converter) to convert the R232s signals to TTL voltage levels that will
be acceptable to 8051s TxD and RxD pins.
The baud rate of 8051 system should match the baud rate of the PCs COM port.

Fig 4.1
Rs232 to TTL
Conversion

Fig 4.2
Serial Transmission
of Character A

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P28

WWW.RESEARCHDESIGNLAB.COM

SM0, SM1
They determine the framing of
data by specifying the number
of bits per character, and the
start and stop bits.
REN (receive enable)
It is a bit-addressable register
When it is high, it allows 8051 to Fig 4.3: Serial Mode Selector
receive data on RxD pin If low,
and should be picked up before
data is framed with start and
the receiver is disable.
it is lost.
stop bits.
4. TR1 is set to 1 to start timer 1
TI (transmit interrupt)
Programming
the
8051
to
5.
TI is cleared by (TI=0;) TI
When 8051 finishes the transfer
transfer
character
bytes
instruction
of 8-bit Character It raises TI
6. The character byte to be
flag to indicate that it is ready serially.
1.
TMOD
register
is
loaded
with
transferred serially is written
to transfer another byte TI bit
t
h
e
v
a
l
u
e
T
M
O
D
=
0
X
2
0
,
into
SBUF register
is raised at the beginning of the
indicating the use of timer 1 in 7. The TI flag bit is monitored
stop bit
mode 2 (8-bit auto-reload) to
with the use of instruction
set
baud
rate.
while (TI==0); to see if the
RI (receive interrupt)
character has been
When 8051 receives data 2. The TH1 is loaded with one of
the
values
to
set
baud
rate
for
transferred
completely.
serially via RxD, it gets rid of the
serial
data
transfer
8.
To
transfer
the next byte, go
start and stop bits and places
to step 5
the byte in SBUF register It 3. The SCON register is loaded
with
the
value
50H,
indicating
raises the RI flag bit to indicate
serial mode 1, where an 8-bit
that a byte has been received

Programming the 8051 to receive character bytes serially


1. TMOD register is loaded with the value TMOD=0X20, indicating the use of timer 1 in mode2
(8-bit auto-reload) to set baud rate
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value SCON=0X50,indicating serial mode 1, where an 8bit data is framed with start and stop bits
4. TR1 is set to 1 to start timer 1
5. RI is cleared by RI=0; RI instruction
6. The RI flag bit is monitored with the use of instruction while(RI==0); to see if an entire
character has been received yet.
7. When RI is raised, SBUF has the byte, its contents are moved into a safe place
8. To receive the next character, go to step 5

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P29

WWW.RESEARCHDESIGNLAB.COM

Simple Serial interfacing using 8051 Microcontroller and Keil AT89S52


Circuit and Working:
Fig.5 shows the circuit of simple 8051 Microcontroller interfaced with LEDs.
Here is a simple Embedded C program for interfacing 8 LEDs to a 8051 Microcontroller
which could be turned ON or OFF by sending few serial commands.
Program 5 enables a user to turn ON/OFF a series of LEDs by sending serial data. The
program is designed in such a way that a serial command A1 will turn ON the first LED
and A0 will turn of the same LED. Similarly B1 will turn ON the second LED and B0 will
turn of the same LED. This will continue for the remaining 6 LEDs. i.e. H1 and H0 would
turn ON and OFF last LED (8th LED) respectively. You can enter the inputs in any serial
window monitor software like Hyperterminal, Putty etc. Also you could design a GUI in
software like Matlab, .NET etc. which could be used to control these LEDs.
Components/modules required :
1)
8051 project board with RS232 interface (assembled/non assembled kit).
2)
5V DC source.
3)
8 LEDs.
4)
Resistors (1Kx8).
5)
IC AT89S52.
6)
8051 IC burner.
7)
Connectors and cables.

Fig. 5 Circuit Diagram for Serial and LED interfacing

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P30

WWW.RESEARCHDESIGNLAB.COM

#include<reg52.h>
void delay();
sbit LED0=P2^0;
sbit LED1=P2^1;
sbit LED2=P2^2;
sbit LED3=P2^3;
sbit LED4=P2^4;
sbit LED5=P2^5;
sbit LED6=P2^6;
sbit LED7=P2^7;
unsigned char byte1,byte2;

//special function register declarations


//for the intended 8051 derivative
// Function prototype declaration
//Define Port Pin P2.0 as
//Define Port Pin P2.1 as
//Define Port Pin P2.2 as
//Define Port Pin P2.3 as
//Define Port Pin P2.4 as
//Define Port Pin P2.5 as
//Define Port Pin P2.6 as
//Define Port Pin P2.7 as
// Variable declarations
// MAIN CODE

LED0
LED1
LED2
LED3
LED4
LED5
LED6
LED7

void main()
{
TMOD=0X20;
SCON=0X50;
TH1=0XFD;
TR1=1;
delay();
TI=0;
SBUF='S';
while (TI==0);
TI=0;
delay();
P2=0x00;
while(1)
{
RI=0;
while(RI==0);
byte1=SBUF;
RI=0;
while(RI==0);
byte2=SBUF;
RI=0;
delay();
delay();
if(byte1=='A')
{
if(byte2=='1')
{

//Serial Initialization
//use Timer 1, mode 2
//indicating serial mode 1, where an 8-bit
//data is framed with start and stop bits
//9600 baud rate
//Start timer
//Wait for a delay for serial initialization to finish
// Transmit 'S' to check whether the setup is ready
//Forcibly change the Transmit Interrupt Flag of 8051 to 0
//Move 'S' to serial buffer memory
//Wait until TI flag is set by hardware when an entire byte
//has been transmitted
// Forcibly clear TI flag
//A small delay for relaxation
//Set Port 2 all bits to 0
// continuous loop
//Forcibly clear the Receive Interrupt Flag of 8051 to 0
//Wait until RI flag is set by hardware when an entire byte
//has been received
//Move the received byte of data into variable 'byte1'
//Forcibly clear RI flag
//Wait until RI flag is set by hardware when an entire byte
//has been received
//Move the received byte of data into variable 'byte2'
//Forcibly clear RI flag

//Check whether the 1st byte of


//data is 'A'
//Check whether the 2nd byte of
//data is '1'

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P31

WWW.RESEARCHDESIGNLAB.COM

LED0=1;
delay();
}
else if(byte2=='0')
{
LED0=0;
delay();
}
}
else if(byte1=='B')
{
if(byte2=='1')
{
LED1=1;
delay();
}
else if(byte2=='0')
{
LED1=0;
delay();
}
}
else if(byte1=='C')
{
if(byte2=='1')
{
LED2=1;
delay();
}
else if(byte2=='0')
{
LED2=0;
delay();
}
}
else if(byte1=='D')
{
if(byte2=='1')
{
LED3=1;

//Turn ON LED0
//Wait for a small delay
//Check whether the 2nd byte of
//data is '0'
//Turn OFF LED0
//Wait for a small delay

//Check whether the 1st byte of


//data is 'B'
//Check whether the 2nd byte of
//data is '1'
//Turn ON LED1
//Wait for a small delay

Tips
SOIC to DIP
Adapter 8-Pin

//Check whether the 2nd byte of


//data is '0'
//Turn OFF LED1
//Wait for a small delay
Quick Overview

//Check whether the 1st byte of


//data is 'C'
//Check whether the 2nd byte of
//data is '1'
//Turn ON LED2
//Wait for a small delay
//Check whether the 2nd byte of
//data is '0'
//Turn OFF LED2
//Wait for a small delay

//Check whether the 1st byte of


//data is 'D'
//Check whether the 2nd byte of
//data is '1'
//Turn ON LED3
}

Adapter for standard 8


SOIC SMD Parts to
convert to standard 8 Pin
DIP size.This SOIC
breakout board is a PCB
which will interface an
SOIC package to 0.1"
(2.54mm) headers which
can be used on
breadboards for
prototyping your
projects.Simply solderon your 8-pin SOIC formfactor IC, along with
some 0.1-inch-pitch
headers, and you will
have a usable,
breadboard-friendly
unit.

Code & Schematic


For more details
www.researchdesignlab.com

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P32

WWW.RESEARCHDESIGNLAB.COM

delay();
}
else if(byte2=='0')
{
LED3=0;
delay();
}
}
else if(byte1=='E')
{
if(byte2=='1')
{
LED4=1;
delay();
}
else if(byte2=='0')
{
LED4=0;
delay();
}
}
else if(byte1=='F')
{
if(byte2=='1')
{
LED5=1;
delay();
}
else if(byte2=='0')
{
LED5=0;
delay();
}
}
else if(byte1=='G')
{
if(byte2=='1')
{
LED6=1;
delay();
}
else if(byte2=='0')
{
LED6=0;
delay();
}
}
else if(byte1=='H')
{

//Wait for a small delay


//Check whether the 2nd byte of
//data is '0'
//Turn OFF LED3
//Wait for a small delay

//Check whether the 1st byte of


//data is 'E'
//Check whether the 2nd byte of
//data is '1'
//Turn ON LED4
//Wait for a small delay

Tips
Current
Sensor 05A

//Check whether the 2nd byte of


//data is '0'
//Turn OFF LED4
//Wait for a small delay
Quick Overview

//Check whether the 1st byte of


//data is 'F'
//Check whether the 2nd byte of
//data is '1
//Turn ON LED5
//Wait for a small delay
//Check whether the 2nd byte of
//data is '0'
//Turn OFF LED5
//Wait for a small delay

//Check whether the 1st byte of


//data is 'G'
//Check whether the 2nd byte of
//data is '1'
//Turn ON LED6
//Wait for a small delay
//Check whether the 2nd byte of
//data is '0'
//Turn OFF LED6
//Wait for a small delay

The ACS712 provides


economical and precise
solutions for AC or DC
current sensing in
industrial, commercial,
and communications
systems. The device
package allows foreasy
implementation by the
c u s t o m e r. Ty p i c a l
applicationsinclude
motor control, load
detection and
m a n a g e m e n t ,
switchmode power
supplies, and
overcurrent fault
protection. The device
is not intended for
a u t o m o t i v e
applications.
Code & Schematic
For more details
www.researchdesignlab.com

//Check whether the 1st byte of


//data is 'H'

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P33

WWW.RESEARCHDESIGNLAB.COM

if(byte2=='1')
{
LED7=1;
delay();
}
else if(byte2=='0')
{
LED7=0;
delay();
}
}
else
{
P2=0x00;

//Check whether the 2nd byte of


//data is '1'
//Turn ON LED7
//Wait for a small delay
//Check whether the 2nd byte of
//data is '0'
//Turn OFF LED7
//Wait for a small delay

//Set Port 2 all bits to 0 if any


//other variable has been received
//Wait for a small delay

delay();
}
}
}
// Delay Routine
void delay()
{
// larger the value of x
unsigned int x=60000;
//the more is the delay.
// executes this statement
while (x--);
//until x decrements to 0
}

Bluetooth and Relays interfacing


using 8051 Microcontroller and
Keil AT89S52
Circuit and Working:
Fig.6 shows the circuit of simple
8051 Microcontroller interfaced
with Bluetooth and 4 relays.
Program 6 demonstrates how to
receive data through Bluetooth.
Components/modules required :
1) 8051 project board
(assembled/non assembled kit).
2) 5V and 12V DC source.
3) Bluetooth Module.
4) 12V 4 Relay board.
5) IC AT89S52.
6) 8051 IC burner.
7) Connectors and cables.

Fig .6 Circuit Diagram for Bluetooth and 4 Relay interfacing


RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P34

WWW.RESEARCHDESIGNLAB.COM

Program 6:
#include<reg52.h>

//special function register declarations


//for the intended 8051 derivative
//Function prototype declaration
void delay();
// Relay Connections
//Relay 1 is connected to Port 2 pin 3
sbit Relay1=P2^3;
//Relay 2 is connected to Port 2 pin 2
sbit Relay2=P2^2;
//Relay 3 is connected to Port 2 pin 1
sbit Relay3=P2^1;
//Relay 4 is connected to Port 2 pin 0
sbit Relay4=P2^0;
unsigned char byte1,byte2; // Variable declarations
// MAIN CODE
void main()
{
//Serial Initialization
//use Timer 1, mode 2
TMOD=0X20;
//indicating serial mode 1,where an 8-bit data
SCON=0X50;
//is framed with start and stop bits
//9600 baud rate
TH1=0XFD;
//Start timer
TR1=1;
//Wait for some time for serial initialization to finish
delay();
// Transmit 'S' to check whether the setup is ready
//Forcibly change the Transmit
TI=0;
//Interrupt Flag of 8051 to 0
//Move 'S' to serial buffer memory
SBUF='S';
//Wait until TI flag is set by hardware
while (TI==0);
//when an entire byte has been transmitted
// Forcibly clear TI flag
TI=0;
//A small delay for relaxation
delay();
//Set Port 2 all bits to 0
P2=0x00;
while(1)
{
RI=0;
while(RI==0);
byte1=SBUF;
RI=0;
while(RI==0);
byte2=SBUF;
RI=0;
if(byte1=='1')
{
if(byte2=='N')
{
Relay1=1;
}

// continuous loop
//Forcibly clear the Receive
//Interrupt Flag of 8051 to 0
//Wait until RI flag is set by hardware
//when an entire byte has been received
//Move the received byte of data into variable 'byte1'
//Forcibly clear RI flag
//Wait until RI flag is set by hardware
//when an entire byte has been received
//Move the received byte of data into variable 'byte2'
//Forcibly clear RI flag
//Check whether the 1st byte of data is '1'
//Check whether the 2nd byte of data is 'N'
//Turn ON Relay1

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P35

WWW.RESEARCHDESIGNLAB.COM

else if(byte2=='F')
{
Relay1=0;
}
}
else if(byte1=='2')
{
if(byte2=='N')
{
Relay2=1;
}
else if(byte2=='F')
{
Relay2=0;
}
}
else if(byte1=='3')
{
if(byte2=='N')
{
Relay3=1;
}
else if(byte2=='F')
{
Relay3=0;
}
}
else if(byte1=='4')
{
if(byte2=='N')
{
Relay4=1;
}
else if(byte2=='F')
{
Relay4=0;
}
}
else if(byte1=='X')
{
if(byte2=='N')
{
P2=0xFF;
}
else if(byte2=='F')
{
P2=0x00;
}
}

//Check whether the 2nd byte of data is 'F'


//Turn OFF Relay1

//Check whether the 1st byte of data is '2'


//Check whether the 2nd byte of data is 'N'
//Turn ON Relay2
//Check whether the 2nd byte of data is 'F'
//Turn OFF Relay2

//Check whether the 1st byte of data is '3'


//Check whether the 2nd byte of data is 'N'
//Turn ON Relay3
//Check whether the 2nd byte of data is 'F'
//Turn OFF Relay3

//Check whether the 1st byte of data is '4'


//Check whether the 2nd byte of data is 'N'
//Turn ON Relay4
//Check whether the 2nd byte of data is 'F'
//Turn OFF Relay4

//Check whether the 1st byte of data is 'X'


//Check whether the 2nd byte of data is 'N'
//Turn ON all the Relays
//Check whether the 2nd byte of data is 'F'
//Turn OFF all the Relays

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P36

WWW.RESEARCHDESIGNLAB.COM

else
{
P2=0x00;
}
}
}

//Clear Port 2 all bits to 0 if any other variable has been received

void delay()
{
unsigned int x=60000;
while (x--);
}

//Function for delay routine


//Delay Routine
// larger the value of x the more is the delay.
// executes this statement until x decrements to 0

Keypad and XBee interfacing using 8051 Microcontroller and Keil AT89S52
Circuit and Working:
Fig.7 shows the circuit of simple 8051 Microcontroller interfaced with XBee and 1x4 Keypad.
Program 7 demonstrates how to send data wirelessly when a key is being pressed.
Components/modules required :
1) 8051 project board (assembled/non assembled kit).
2) 5V DC source.
3) XBee(S2)
4) XBee power supply board
5) 1X4 keys keypad.
6) IC AT89S52.
7) 8051 IC burner.
8) Connectors and cables.

Fig 7. Circuit Diagram for LCD and 1x4 keypad interfacing


RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P37

WWW.RESEARCHDESIGNLAB.COM

Program 7:
#include<reg51.h>

//special function register declarations


//for the intended 8051 derivative
// Keypad connections
//Switch 1 is connected to Port 0 pin 3
//Switch 2 is connected to Port 0 pin 2
//Switch 3 is connected to Port 0 pin 1
//Switch 4 is connected to Port 0 pin 0
//Call Function declarations for delay

sbit key1=P0^3;
sbit key2=P0^2;
sbit key3=P0^1;
sbit key4=P0^0;
void DELAY();
void main()
{
unsigned char flag1=0,flag2=0,flag3=0,flag4=0; //Variable declarations
//Serial Initialization
//use Timer 1, mode 2
TMOD=0X20;
//indicating serial mode 1, where an 8-bit data is
SCON=0X50;
//framed with start and stop bits
//9600 baud rate
TH1=0XFD;
//Start timer
TR1=1;
//Wait for a delay for serial initialization to finish
DELAY();
// Transmit 'S' to check whether the setup is ready
//Forcibly clear the Transmit Interrupt Flag of 8051 to 0
TI=0;
//Move 'S' to serial buffer memory
SBUF='S';
//Wait until TI flag is set by hardware when an
while (TI==0);
//entire byte has been transmitted
//Forcibly clear TI flag
TI=0;
//A small delay for relaxation
DELAY();
//Set Port 1 all bits to 0
P1=0X00;
DELAY();
//Continuous loop
while(1)
{
//Check whether switch 1 is being pressed
if (key1 ==0 && flag1==0)
//flag1 is Low
{
//Move 'A' to serial buffer memory
SBUF='A';
//Wait until TI flag is set by hardware when an
while (TI==0);
//entire byte has been transmitted
// Forcibly clear TI flag
TI=0;
//Set flag1
flag1=1;
//Wait until the switch has been released
while(key1==0);
//A small delay for relaxation
DELAY();
}
else if (key1 ==0 && flag1==1)//Check whether switch 1 is being pressed
//and flag1 is Low
{
//Move 'B' to serial buffer memory
SBUF='B';
//Wait until TI flag is set by hardware when an
while (TI==0);
//entire byte has been transmitted
// Forcibly clear TI flag
TI=0;
//Clear flag1
flag1=0;
//Wait until the switch has been released
while(key1==0);
//A small delay for relaxation
DELAY();
}
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P38

WWW.RESEARCHDESIGNLAB.COM

else if (key2 ==0 && flag2==0)


{
SBUF='C';
while (TI==0);
TI=0;
flag2=1;
while(key2==0);
DELAY();
}
else if (key2 ==0 && flag2==1)
{
SBUF='D';
while (TI==0);
TI=0;
flag2=0;
while(key2==0);
DELAY();
}
else if (key3 ==0 && flag3==0)
{
SBUF='E';
while (TI==0);
TI=0;
flag3=1;
while(key3==0);
DELAY();
}
else if (key3 ==0 && flag3==1)
{
SBUF='F';
while (TI==0);
TI=0;
flag3=0;
while(key3==0);
DELAY();
}
else if (key4 ==0 && flag4==0)
{
SBUF='G';
while (TI==0);

//Check whether switch 2 is being pressed


//and flag2 is Low
//Move 'C' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted
//Forcibly clear TI flag
//Set flag2
//Wait until the switch has been released
//A small delay for relaxation
//Check whether switch 2 is being pressed
//and flag2 is High
//Move 'D' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted
//Forcibly clear TI flag
//Clear flag2
//Wait until the switch has been released
//A small delay for relaxation
//Check whether switch 3 is being pressed
//and flag3 is Low
//Move 'E' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted
//Forcibly clear TI flag
//Set flag3
//Wait until the switch has been released
//A small delay for relaxation
//Check whether switch 3 is being pressed
//and flag3 is High
//Move 'F' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted
// Forcibly clear TI flag
//Clear flag3
//Wait until the switch has been released
//A small delay for relaxation
//Check whether switch 4 is being pressed
//and flag4 is Low
//Move 'G' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P39

WWW.RESEARCHDESIGNLAB.COM

TI=0;
flag4=1;
while(key4==0);
DELAY();
}
else if (key4 ==0 && flag4==1)
{
SBUF='H';
while (TI==0);
TI=0;
flag4=0;
while(key4==0);
DELAY();
}
}
}

// Forcibly clear TI flag


//Set flag4
//Wait until the switch has been released
//A small delay for relaxation
//Check whether switch 4 is being pressed
//and flag4 is High
//Move 'H' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted
// Forcibly clear TI flag
//Clear flag4
//Wait until the switch has been released
//A small delay for relaxation

//Function for delay routine


void DELAY()
{
unsigned int X=60000;
while(X--);
}

Tips
Code & Schematic
For more details

// larger the value of X the more is the delay.


// executes this statement until / X decrements to 0

3 Axis Accelerometer
Quick Overview

www.researchdesignlab.com

3-axis accelerometer to now have an on-board 3.3V regulator making it a perfect choice for interfacing with a 5V microcontroller
such as the . This breakout comes with 3 analog outputs for X, Y and Z
axis breakout board. The ADXL335 is the latest and greatest from
Analog Devices, known for their exceptional quality MEMS devices.
The VCC takes up to 5V in and regulates it to 3.3V with an output pin.
The analog outputs are ratiometric: that means that 0g measurement
output is always at half of the 3.3V output (1.65V), -3g is at 0v and 3g
is at 3.3V with full scaling in between. Fully assembled and tested.
The XYZ filter capacitors are 0.1uF for a 50 Hz bandwidth

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P40

WWW.RESEARCHDESIGNLAB.COM

LEDs and XBee interfacing using 8051 Microcontroller and Keil AT89S52
Circuit and Working:
Fig.8 shows the circuit of simple 8051 Microcontroller interfaced with XBee and 8 LEDs.
Program 8 demonstrates how to receive serial data wirelessly and toggle the state of a LED.

Components/modules required :
1) 8051 project board (assembled/non assembled kit).
2) 5V DC source.
3) XBee(S2)
4) XBee power supply board
5) 8 LEDs.
6) Resistors (1Kx8).
7) IC AT89S52.
8) 8051 IC burner.
9) Connectors and cables.

Fig. 8 Circuit Diagram for XBee and LED interfacing

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P41

WWW.RESEARCHDESIGNLAB.COM

Program 8:
#include<reg52.h>
void delay();

//special function register declarations


//for the intended 8051 derivative
// Function prototype declaration

sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit

//Define
//Define
//Define
//Define
//Define
//Define
//Define
//Define

LED0=P2^0;
LED1=P2^1;
LED2=P2^2;
LED3=P2^3;
LED4=P2^4;
LED5=P2^5;
LED6=P2^6;
LED7=P2^7;

Port
Port
Port
Port
Port
Port
Port
Port

Pin
Pin
Pin
Pin
Pin
Pin
Pin
Pin

P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7

as
as
as
as
as
as
as
as

LED0
LED1
LED2
LED3
LED4
LED5
LED6
LED7

unsigned char byte1,byte2; // Variable declarations


// MAIN CODE
void main()
{
TMOD=0X20;
SCON=0X50;
TH1=0XFD;
TR1=1;
delay();
TI=0;
SBUF='S';
while (TI==0);
TI=0;
delay();
P2=0x00;
while(1)
{
RI=0;
while(RI==0);
byte1=SBUF;
RI=0;
if(byte1=='A')
{
LED0=1;
LED4=1;
}
else if(byte1=='B')

//Serial Initialization
//use Timer 1, mode 2
//indicating serial mode 1, where an 8-bit
//data is framed with start and stop bits
//9600 baud rate
//Start timer
//Wait for some time for serial initialization to finish
// Transmit 'S' to check whether the setup is ready
//Forcibly change the Transmit Interrupt
//Flag of 8051 to 0
//Move 'S' to serial buffer memory
//Wait until TI flag is set by hardware
//when an entire byte has been transmitted
// Forcibly clear TI flag
//A small delay for relaxation
//Set Port 2 all bits to 0
// continuous loop
//Forcibly clear the Receive Interrupt
//Flag of 8051 to 0
//Wait until RI flag is set by hardware
//when an entire byte has been received
//Move the received byte of data into variable 'byte1'
//Forcibly clear RI flag
//Check whether the received byte of data is 'A'
//Turn on LED0
//Turn on LED4
//Check whether the received byte of data is 'B'

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P42

WWW.RESEARCHDESIGNLAB.COM

{
LED0=0;
LED4=0;
}
else if(byte1=='C')
{
LED1=1;
LED5=1;
}
else if(byte1=='D')
{
LED1=0;
LED5=0;
}
else if(byte1=='E')
{
LED2=1;
LED6=1;
}
else if(byte1=='F')
{
LED2=0;
LED6=0;
}
else if(byte1=='G')
{
LED3=1;
LED7=1;
}
else if(byte1=='H')
{
LED3=0;
LED7=0;
}
else
{
P2=0x00;
delay();
}
}
}
void delay()
{
unsigned int x=60000;
while (x--);
}

//Turn off LED0


//Turn off LED4
//Check whether the received byte of data is 'C'
//Turn on LED1
//Turn on LED5
//Check whether the received byte of data is 'D'
//Turn off LED1
//Turn off LED5
//Check whether the received byte of data is 'E'
//Turn on LED2
//Turn on LED6
//Check whether the received byte of data is 'F'
//Turn off LED2
//Turn off LED6
//Check whether the received byte of data is 'G'
//Turn on LED3
//Turn on LED7
//Check whether the received byte of data is 'H'
//Turn off LED3
//Turn off LED7

//Set Port 2 all bits to 0


//if any other variable has been received
//Wait for a small delay

// Delay Routine
// larger the value of x the more is the delay.
// executes this statement until x decrements to 0

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P43

WWW.RESEARCHDESIGNLAB.COM

GSM Modem and LCD interfacing using 8051 Microcontroller and Keil AT89S52
Circuit and Working:
Fig.9 shows the circuit of simple 8051 Microcontroller interfaced with GSM Modem and LCD. The
Modem sends an SMS every time you turn on your microcontroller. Following this the modem will
be waiting for any message to be received, once a message has been received, the message will
be displayed on the LCD.
Program 9 demonstrates how to initialize GSM modem through AT commands via a serial
interface and send/receive a SMS through it.
Please note: GSM modem needs to be turned on at least 10 seconds before you turn on the micro
controller (GSM takes a few seconds to turn on)

Fig.9
Circuit Diagram
for GSM Modem
and 16X2 LCD

Components/modules required :
1)8051 project board (assembled/non assembled kit).
2)5V and 12V,1A DC source.
3)GSM Module.
4)16X2 LCD interfacing module.
5)IC AT89S52.
6)8051 IC burner.
7)Connectors and cables.
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P44

WWW.RESEARCHDESIGNLAB.COM

Program 9:
#include"reg52.h"

//Special function register declarations


//for the intended 8051 derivative
//Define CR as 13
#define CR 0X0D
//Define LF as 10
#define LF 0X0A
//Define EOM as CNTRL+Z
#define EOM 0X1A
// LCD connections
//Define Port 2 as LCD Data pins
#define LCD_PORT P2
//Register Select is connected to Port 3 pin 5
sbit rs=P3^5;
//Read/Write is connected to Port 3 pin 6
sbit rw=P3^6;
//Enable is connected to Port 3 pin 7
sbit en=P3^7;
//Data Pin D7 is connected to Port 2 pin 7
sbit D7=P2^7;
// Call function declarations
//This Function checks whether the LCD is ready to
void busy();
//receive next byte
void CMD_WRT(unsigned char); //This Function is used to write commands
//into the LCD
void LCD_WRT(unsigned char *); //This Function is used to write Strings
//into the LCD
void DATA_WRT(unsigned char); //This Function is used to write a byte of
//data into the LCD
void TRANSMIT(unsigned char *); //This Function is used to write Strings into
//the serial Port
void transmit_byte(unsigned char ); //This Function is used to write a byte of
//data into the serial Port
//This function separates the text message from
void READ_SMS(void);
//the SMS received
void SEND_CMD(unsigned char *BASE_ADD,unsigned char COUNT);
//This function sends GSM commands via the serial interface
void SEND_CRLF(unsigned char); //This function writes a 8 bit hex value into
//the serial interface
unsigned char recv_byte (void); //This function receives a byte of data
//through the serial interface
//This function waits for a character 'K'(of OK) which
void RX_REPLY();
//the GSM modem replies for its commands
//This function is used to hit enter into the GSM
void ENTER(void);
//modem whenever required
//Call Function declarations for delay
void DELAY();
// Global variable declaration and initialization
unsigned char count12=0,message[50];
unsigned char byte1,k,temp;
unsigned char Test_Text[]="GSM Testing";
// GSM commands declaration
//GSM Attention command
unsigned char code CMD_1[]="AT";
unsigned char code CMD_3[]="AT+CMGF=1"; //GSM text initialization
//command
unsigned char code CMD_4[]="AT+CMGD=1"; //Delete previous SMS
//command
unsigned char code CMD_9[]="AT+CMGS=\"7411001407\""; //Edit the 10
//digit destination number here
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P45

WWW.RESEARCHDESIGNLAB.COM

// MAIN CODE
void main()
{
unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80}; //LCD Initialization
//commands, hex codes
//Local variable declarations
unsigned char I,TEMP,count=0;
//Initialization of LCD
for(i=0;i<=4;i++)
{
TEMP=CMD[i];
//Write the commands into the LCD
CMD_WRT(TEMP);
}
//This command clears the LCD screen
CMD_WRT(0X01);
//This moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
//Writes the text 'RDL' in the LCD
LCD_WRT(" RDL ");
//These delay's will hold the above text 'RDL' for some
DELAY();
//time on the LCD
DELAY();
DELAY();
//This command clears the LCD screen
CMD_WRT(0X01);
//This moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
//Writes the text 'Initializing GSM' in the LCD
LCD_WRT("Initializing GSM");
DELAY();
DELAY();
//Serial Initialization
//Use Timer 1, mode 2
TMOD=0X20;
//Indicating serial mode 1, where an 8-bit data is
SCON=0X50;
//framed with start and stop bits
//9600 baud rate
TH1=0XFD;
//Start timer
TR1=1;
//Wait for some time for serial initialization to finish
DELAY();
SEND_CMD(CMD_1,2);
DELAY();
ENTER();
RX_REPLY();
DELAY();

//Enter GSM Attention command


//A small delay for relaxation
//Hit enter
//Wait for GSM modem to respond
//A small delay for relaxation

CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("GSM Initialized!");
DELAY();

//This command clears the LCD screen


//This moves the cursor to the beginning of the 1st line
//Writes the text 'GSM Initialized ' in the LCD
//A small delay for relaxation

CMD_WRT(0XC0);

//This moves the cursor to the beginning of the


//2nd line
//Writes the text 'Sending SMS..' from the
//current pointer of LCD
//A small delay for relaxation

LCD_WRT("Sending SMS..");
DELAY();

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P46

WWW.RESEARCHDESIGNLAB.COM

SEND_CMD(CMD_3,9);
DELAY();
ENTER();
RX_REPLY();
DELAY();

//Enter GSM text initialization command


//A small delay for relaxation
//Hit enter
//Wait for GSM modem to respond
//A small delay for relaxation

SEND_CMD(CMD_4,9);

//Enter Delete previous SMS command to free up


//space for new SMS
//A small delay for relaxation
//Hit enter
//Wait for GSM modem to respond
//A small delay for relaxation

DELAY();
ENTER();
RX_REPLY();
DELAY();
SEND_CMD(CMD_9,20);
DELAY();
ENTER();
DELAY();
TRANSMIT(Test_Text);
DELAY();
transmit_byte(EOM);
RX_REPLY();
DELAY();
CMD_WRT(0X01);
CMD_WRT(0X80);
LCD_WRT("TEXT SENT!!");
DELAY();
while(1)
{
DELAY();
CMD_WRT(0XC0);
LCD_WRT("WAITING..");
DELAY();

//Enter the command for sending the SMS to a


//destination number
//A small delay for relaxation
//Hit enter
//A small delay for relaxation
//Enter the text within the previously declared
//variable Test_Text
//A small delay for relaxation
//Enter ASCII equivalent of CNTRL+Z
//Wait for GSM modem to respond
//A small delay for relaxation
//This command clears the LCD screen
//This moves the cursor to the beginning of the 1st line
//Writes the text 'TEXT SENT!!' in the LCD
//A small delay for relaxation
//Continuous loop
//A small delay for relaxation
//This moves the cursor to the beginning of the 2nd line
//Writes the text 'WAITING..' from the current pointer
//of LCD
//A small delay for relaxation

READ_SMS(); //Call 'READ_SMS()' function which waits until a SMS has been
//received and separates the text message from it
//A small delay for relaxation
DELAY();
}
}
//This Function checks whether the LCD is ready to receive next byte
void busy()
{
//Keep D7 pin to High
D7=1;
//Keep RS to Low to select command register
rs=0;
// RW=1 for read
rw=1;
//Monitor D7 pin until it gets low
while(D7!=0)
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P47

WWW.RESEARCHDESIGNLAB.COM

SEND_CRLF(LF);

//Send the value LF(10) to the GSM modem via the


//serial interface

}
//Function used for sending an 8 bit value to the serial interface
void SEND_CRLF(unsigned char CRLF)
{
//Move the contents of the variable 'CRLF' into the
SBUF=CRLF;
//serial buffer
//Wait until TI flag is set by hardware when an entire
while(TI==0);
//byte has been transmitted
//Forcibly clear TI flag
TI=0;
}
//Function used to send a string of data at 'BASE_ADD' pointer
//having a string length of variable 'COUNT'
void SEND_CMD(unsigned char *BASE_ADD,unsigned char COUNT)
{
//Local variable declaration
unsigned char I;
//for loop till the end of the string length
for(I=0;I<COUNT;I++)
{
//Move the byte of data to serial buffer currently
SBUF=*BASE_ADD;
//located at the address location of the pointer
//Wait until TI flag is set by hardware when an entire
while(TI==0);
//byte has been transmitted
//Forcibly clear TI flag
TI=0;
//Increment the address to point the data of
BASE_ADD++;
//the next location
}
}
//This function waits for a character 'K'(of OK) which the GSM
//modem replies for its commands
void RX_REPLY()
{
//Local variable declaration and initialization
unsigned char RX=0;
//Breaks this loop only when 'K' has been received, till then
while(RX!='K')
//the variable 'RX' keeps waiting for 'K' to be received
{
//Wait until RI flag is set by hardware when an entire byte
while(RI==0);
//has been received
//Move the received byte of data into variable 'RX'
RX=SBUF;
//Forcibly clear RI flag
RI=0;
}
}
// Function used to transmit a string of data into the serial interface
void TRANSMIT(unsigned char *string)
{
//execute the next statement till the end of the string
while(*string)
//Pass the 8 bit data located at the address location
transmit_byte(*string++);
//of the pointer to the function 'transmit_byte'
}
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P48

WWW.RESEARCHDESIGNLAB.COM

{
en=0;
en=1;
}
}

//Provide a latch pulse from low to high to EN

//This Function is used to write commands into the LCD


void CMD_WRT(unsigned char val)
{
//Execute busy function to know whether the LCD is
busy();
//ready to receive any data/command
//Put the variable val into LCD_PORT which is
LCD_PORT=val;
//connected to LCD data pins
//Keep RS to Low to select command register
rs=0;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}
//This Function is used to write Strings into the LCD
void LCD_WRT(unsigned char *string)
{
// increment from the beginning of the string until a
while(*string)
//null character is detected (end of the string)
// separates a single byte from the string
DATA_WRT(*string++);
}
//This Function is used to write a byte of data into the LCD
void DATA_WRT(unsigned char ch)
{
//Execute busy function to know whether the LCD
busy();
//is ready to receive any data/command
//Put the variable val into LCD_PORT which is
LCD_PORT = ch;
//connected to LCD data pins
//Keep RS to High to select Data register
rs=1;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}
//Function for delay routine
void DELAY()
{
unsigned int X=60000,Y=60000; // larger the value of X and Y the more is
//the delay.
// executes this statement until X decrements to 0;
while(X--);
// executes this statement until Y decrements to 0;
while(Y--);
}
//Function used for sending ENTER command to the GSM Modem
void ENTER(void)
{
//Send the value CR(13) to the GSM modem via the
SEND_CRLF(CR);
//serial interface

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P49

WWW.RESEARCHDESIGNLAB.COM

// Function used to transmit a string of data into the serial interface


void TRANSMIT(unsigned char *string)
{
while(*string)
//execute the next statement till the end of the string
transmit_byte(*string++);
//Pass the 8 bit data located at the address location
}
// of the pointer to the function 'transmit_byte'
//Function used for sending an 8 bit data present on the variable
// 'byte' to the serial interface
void transmit_byte(unsigned char byte)
{
SBUF=byte;
//Move the contents of the variable 'byte' to serial buffer memory
while(!TI);
//Wait until TI flag is set by hardware when an entire byte has
//been transmitted
TI=0;
//Forcibly clear TI flag
}
//This function waits until a SMS has been received and separates the text
//message from it and displays it on the LCD
void READ_SMS(void)
{
unsigned char code CMD_5[]="AT+CMGR=1"; //GSM read SMS command
unsigned char byte,i=0, flag=0;
//Local Variables declaration and initialization
byte=0;
//Assign any value to byte other than '+'
while(byte!='+')
//Breaks this loop only when '+' has been received, till then
//the variable 'byte' keeps waiting for '+' to be received
{
byte=recv_byte();
//Keep checking for the data received in the
}
//call function 'recv_byte'
DELAY();
//A small delay for relaxation
DELAY();
//A small delay for relaxation
SEND_CMD(CMD_5,9);
//Enter the command used for reading a SMS
ENTER();
//Hit enter
//The string that comes next includes quotes(") along with other
//information like time, date, number etc. of the received SMS
for(i=0;i<7;i++)
//Wait for at least 8 such quotes(")
{
while(byte!='"') //Breaks this loop only when '"' has been received, till then
//the variable 'byte' keeps waiting for '"' to be received
{
byte=recv_byte();
//Keep checking for the data received in the call
//function 'recv_byte'
}
byte=0;
//Assign any value to byte other than '"'
}
byte=0; //Assign any value to byte other than '13'
while(byte!=13) //Breaks this loop only when '13' has been received, till then
//the variable 'byte' keeps waiting for '13' to be received
{
byte=recv_byte();
//Keep checking for the data recieved in the
}
//call function 'recv_byte'
RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P50

WWW.RESEARCHDESIGNLAB.COM

byte=0;
count12=0;
while(byte!=13)

//Assign any value to byte other than '13'


//Initialize the text character counter to zero
//Breaks this loop only when '13' has been received, till then
//the variable 'byte' keeps waiting for '13' to be received

{
byte=recv_byte();
message[count12]= byte;
count12++;
}
count12--;
CMD_WRT(0X01);
CMD_WRT(0X80);
DELAY();
for (k=1;k<count12;k++)
{
temp=message[k];
DATA_WRT(temp);
DELAY();
}
count12=0;
DELAY();
SEND_CMD(CMD_4,9);
DELAY();
ENTER();
RX_REPLY();
DELAY();
}

//Keep checking for the data received in the


//call function 'recv_byte'
//Move the received byte of data into the
//array 'message'
//Increment the array
//Decrement the text character counter by 1 to
//eliminate the last value received i.e '13'
//This command clears the LCD screen
//This moves the cursor to the beginning of the 1st line
//A small delay for relaxation

//Separate out individual characters of the message


//string to variable 'temp'
//Display the individual characters on the LCD 1 by 1
//A small delay for relaxation
//Clear the text character counter back to zero.
//A small delay for relaxation
//Enter Delete previous SMS command to free up
//space for new SMS
//A small delay for relaxation
//Hit enter
//Wait for GSM modem to respond
//A small delay for relaxation
//This function receives a byte of data through the serial interface
//and returns
//it back to the function where it was called

unsigned char recv_byte()


{
unsigned char D;
while(RI!=1);
D = SBUF;
RI=0;
return(D);
}

//Local variable declaration


//Wait until RI flag is set by hardware when an
//entire byte has been received
//Move the received byte of data into variable 'D'
//Forcibly clear RI flag
//Return the value to the called function where it was called

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P51

WWW.RESEARCHDESIGNLAB.COM

Analog to digital conversion in 8051/89c52 Microcontroller and Keil - ADC0804

Circuit and Working:


Fig 10. shows the circuit of simple 8051 Microcontroller interfaced with LCD Display and analog
to digital converter IC ADC0804. After the connections are done properly, you will be able to
view the digital value on the LCD display.
Program 10 demonstrates how to read the analog values through IC ADC0804. The output of this
IC will be an 8 bit value, this 8 bit value would be connected to a microcontroller. The
microcontroller is programmed in such a way to read the port pin values and display its
equivalent decimal value on the LCD display.
Components/modules required:
1) 8051 project board (assembled/non assembled kit). 4)
2) 5V DC source.
5)
3) LCD interfacing Module.
6)
7)

IC ADC0804 with the circuit


connections as shown in Fig 10.2
IC AT89S52.
8051 IC burner.
Connectors and cables.

Fig10.1: Circuit Diagram for LCD and ADC interfacing-Part 1.


RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P52

WWW.RESEARCHDESIGNLAB.COM

Fig 10.2: Circuit Diagram for LCD and ADC interfacing-Part 2.


Programme 10
#include<reg52.h>
#include<intrins.h>
#define adc_port P1

//Define Port 1 as ADC port Data pins

#define LCD_PORT P2
sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;
sbit D7=P2^7;

// LCD connections
//Define Port 2 as LCD Data pins
//Register Select is connected to Port 3 pin 5
//Read/Write is connected to Port 3 pin 6
//Enable is connected to Port 3 pin 7
//Data Pin D7 is connected to Port 2 pin 7

void busy();
void CMD_WRT(unsigned char);
void LCD_WRT(unsigned char *);
void DATA_WRT(unsigned char);

// Call function declarations


//This Function checks whether the LCD is ready to receive
//next byte
//This Function is used to write commands into the LCD
//This Function is used to write Strings into the LCD
//This Function is used to write a byte of data into the LCD

void CONVERT_DISPLAY(unsigned char); //This Function is used to convert Hex data to Decimal
//equivalent and write into the LCD
void DELAY();

//Call Function declarations for delay

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P53

WWW.RESEARCHDESIGNLAB.COM

sbit rd=P3^0;
sbit wr=P3^1;

//RD of ADC0804 IC is connected to Port 3 pin 0


//WR of ADC0804 IC is connected to Port 3 pin 1

sbit intr=P3^3;
void read();
void conv();

//INTR of ADC0804 IC is connected to Port 3 pin 3

unsigned int adc_value;


void main()
{
unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80}; //LCD Initialization commands, hex codes
//Local variable declarations
unsigned char i,TEMP;
//Initialization of lcd
for(i=0;i<=4;i++)
{
TEMP=CMD[i];
CMD_WRT(TEMP);
}

//Write the commands into the LCD

//This command clears the LCD screen


CMD_WRT(0X01);
//This moves the cursor to the beginning of the 1st line
CMD_WRT(0X80);
LCD_WRT("ADC0804..RDL. "); //Writes the text 'ADC0804..RDL.' in the LCD
while(1)
{
CMD_WRT(0XC4);

//cursor on second line

conv();

// call conv to convert anolog voltage to digtal 8 bit


// call read toi read 8 bit digtal value

read();

CONVERT_DISPLAY(adc_value);

//convert and display

DELAY();
DELAY();
DELAY();
}
}
//Function for delay routine
void DELAY()
{
unsigned int X=60000;
while(X--);

// larger the value of X and Y the more is the delay.


// executes this statement until X decrements to 0;

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P54

WWW.RESEARCHDESIGNLAB.COM

//This Function checks whether the LCD is ready to receive next byte
void busy()
{
D7=1;
rs=0;
rw=1;
while(D7!=0)
{
en=0;
en=1;
}
}

//Keep D7 pin to High


//Keep RS to Low to select command register
// RW=1 for read
//Monitor D7 pin until it gets low
//Provide a latch pulse from low to high to EN

//This Function is used to write commands into the LCD


void CMD_WRT(unsigned char val)
{
busy(); //Execute busy function to know whether the LCD is ready to receive any data/command
LCD_PORT=val; //Put the variable val into LCD_PORT which is connected to LCD data pins
//Keep RS to Low to select command register
rs=0;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}

//This Function is used to write Strings into the LCD


void LCD_WRT(unsigned char *string)
{
//increment from the beginning of the string until a
while(*string)
//null character is detected (end of the string)
DATA_WRT(*string++); // separates a single byte from the string
}
//This Function is used to write a byte of data into the LCD
void DATA_WRT(unsigned char ch)
{
//Execute busy function to know whether the LCD is ready to receive any
busy();
//data/command
//Put the variable val into LCD_PORT which is connected to LCD data pins
LCD_PORT = ch;
//Keep RS to High to select Data register
rs=1;
// RW=0 for write
rw=0;
//Provide a latch pulse from High to Low to EN
en=1;
en=0;
}

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P55

WWW.RESEARCHDESIGNLAB.COM

//This Function is used to convert Hex data to Decimal equivalent


//and write into the LCD
void CONVERT_DISPLAY(unsigned char d)
{
unsigned char dig1,dig2,dig3,dig[3];
//Local variable declarations
unsigned char x;
//Local variable declarations
unsigned char temp;
//Local variable declarations
temp=d;
//Move the Hex value 'd' to a variable 'temp'
temp=temp/10;
dig1=d%10;
//Get the last number
dig2=temp%10;
//Get the 2nd last number
dig3=temp/10;
//Get the first number
//Concatenate all the value into a single variable
dig[0]=dig3;
dig[1]=dig2;
dig[2]=dig1;
CMD_WRT(0XCA);
for(x=0;x<3;x++)
{
temp=dig[x]|0x30;
DATA_WRT(temp);
}

//or'ing with 0x30 will give


//the ASCII equivalent of the decimal value
//Write the 8 bit data into the LCD

}
void conv() {
wr = 0;
_nop_ ();
_nop_ ();
_nop_ ();
wr = 1;

//Make WR low

while (intr);
}

//Wait for INTR to go low

//nop delay
//Make WR high

void read() {
rd = 0;
_nop_ ();
_nop_ ();
_nop_ ();
adc_value = adc_port;
rd = 1;

//Make RD low

//Read ADC port


//Make RD high

RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 I P56

WWW.RESEARCHDESIGNLAB.COM

01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101

Research
Design Lab

IF YOU
THINK THAT
PROJECTS ARE FUN
est Em
b

rg
La

urce Porta
l

www.researchdesignlab.com

ded Open
ed

So

DO IT YOURSELF

Visit our website for Live demo and documents of DIY Kits project

Sensors, Modules, Robotics & DIY Kits


Soil-Moisture Sensor

STA 401A Motor Driver

Power Line Modem

Serial 8 Channel
AC 230V SSR and Dimmer

Compass HMC5883L

FT232 BreakOut Board

Bluetooth Module

4 Channel Isolated
Relay Board

RDL - UNO ATMEGA 328

GSM Modem

Raspberry Pi Case

Digital Dimmer

3 Axis Accelorometer

Bread Board Power Supply

Gas Sensor

Gyro Sensor

Carbon Monoxide Sensor

IR Obstacle Sensor

RFID Reader - USB

ZIGBEE

PIC Project Board

Real Time Clock

ATMEL Project Board (Red)

LCD KEYPAD Shield

USB PIC Programmer

8051 Programmer - USB

Joystick Shield

SMD Adapter

Ultrasonic
Distance Measure

10 min. Voice Playback


and Recorder Kit

Bi-Directional
Logic Level Converter

GSM Modem Arduino Compatible

LiFi TX

Xbee USB
Adapter with FT232RL

Micro SD Memory Card


interface for 5V MCU

Data Logger Shield

(Visible Light Communication)

PCF8591 AD/DA
Converter Module

Bread Board Shield

Bluetooth
Development Board

Xbee Adapter
Development Board

Raspberry Pi
Expansion Board

Serial 3 Channel
AC 230V SSR and Dimmer

4 Channel
Opto-Isolated Board

DTMF Decoder

PIR Motion Sensor

8 Relay
Interfacing Board 12V

Finger Print Sensor


(Serial Data)

Hall Effect Current


Sensor 5A/20A

researchdesignlab.com, C/o Isoft


P: +91 824 4272407

Dealers: Goal Technologies

Mangalore - P: 0 824 4251407, 0 824 4261407


Udupi/Manipal - P: 0 820 4296777, 0 820 4293444

Email: sales@researchdesignlab.com I www.researchdesignlab.com