You are on page 1of 58

# PU Microcontroller Lab Manual 2017 -18

## PRESIDENCY UNIVERSITY, BENGALURU

SCHOOL OF ENGINEERING

MICROCONTROLLER APPLICATIONS
LAB MANUAL
(ECE263)

Engineering

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 1

PU Microcontroller Lab Manual 2017 -18

SYLLABUS
MICROCONTROLLER APPLICATION LAB

## Sub Code : ECE263 Total Marks: 100

Hrs/ Week : 2 hrs/week
Credit Structure : 0-0-2-1

I. PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array.
2. Arithmetic Instructions – Addition, subtraction, multiplication and division, square, Cube – (16
bits Arithmetic operations – bit addressable).
3. Counters.
4. Boolean & Logical Instructions (Bit manipulations).
5. Code conversion: BCD – ASCII; Decimal - ASCII; HEX - Decimal and Decimal - HEX.
Note: Programming exercise is to be done on 8051 using KEIL .

II. INTERFACING:
Write C programs to interface 8051 chip to Interfacing modules to develop single chip solutions.
6.. Simple Calculator using 6 digit seven segment display and Hex Keyboard interface to 8051.
7. Alphanumeric LCD panel and Hex keypad input interface to 8051.
8. External ADC and Temperature control interface to 8051.
9. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface to 8051;
change the frequency and amplitude.
10. Stepper and DC motor control interface to 8051.
11. Elevator interface to 8051
Note: Programming exercise is to be done on 8051 using KEIL uv3, Microcontroller 8051 kit
and flip 2.4.2 tool.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 2

PU Microcontroller Lab Manual 2017 -18

CONTENTS

## S.No Topic Page No

Introduction 4
1.a Data Transfer 10
1.b: Block Exchange
1.
1.c. Sorting
1.d. Finding Largest / smallest
2.b Multi byte subtraction
2. 2.c Multi byte multiplication
2.d division
2.e Square & Cube
3. Counters 22
4. Boolean & Logical Instructions (Bit manipulations). 23
5.a BCD - ASCII 25
5. 5.c HEX - Decimal
5.c Decimal - HEX
6. Simple Calculator 28
8. ADC and Temperature control 42
DAC Interface 47
11.a Sine Wave
9. 11.b square wave
11.c triangle wave
11.b ramp wave
12.a Stepper Motor 52
10.
12.b DC Motor
11. Elevator Interface 56

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 3

PU Microcontroller Lab Manual 2017 -18

INTRODUCTION

## A microcontroller can be compared to a small stand alone computer; it is a very powerful

device, which is capable of executing a series of preprogrammed tasks and interacting with
other hardware devices. Being packed in a tiny integrated circuit (IC) whose size and weight
is usually negligible, it is becoming the perfect controller for robots or any machines
requiring some kind of intelligent automation. A single microcontroller can be sufficient to
control a small mobile robot, an automatic washer machine or a security system. Any
microcontroller contains a memory to store the program to be executed, and a number of
input/output lines that can be used to interact with other devices, like reading the state of a
sensor or controlling a motor.
Nowadays, microcontrollers are so cheap and easily available that it is common to use them
instead of simple logic circuits like counters for the sole purpose of gaining some design
flexibility and saving some space. Some machines and robots will even rely on a multitude of
microcontrollers, each one dedicated to a certain task. Most recent microcontrollers are 'In
System Programmable', meaning that you can modify the program being executed, without
removing the microcontroller from its place.
Today, microcontrollers are an indispensable tool for the robotics hobbyist as well as for the
engineer. Starting in this field can be a little difficult, because you usually can't understand
how everything works inside that integrated circuit, so you have to study the system
gradually, a small part at a time, until you can figure out the whole image and understand
how the system works.
Comparison Of Microprocessor And Microcontroller
Microprocessor and Microcontroller differ in their functionalities. Microprocessor has more
generalized functions, where as Microcontroller is more specific to its task.
A Microprocessor may not be programmed to handle real-time tasks but Microcontroller in
devices that need to control temperature of water or perhaps measure the temperature of a
room require real time monitoring and therefore with its inbuilt set of instructions the
microcontroller works on its own.
Microprocessor requires constant input by a human such as in a personal computer so that
instructions can be booted. Microprocessor is the memory of the computing machine
whereas the microcontroller integrates the entire computer in a single chip. Not only does it
have the memory embedded in it but also has input and output ports plus peripherals such
as timers and converters. All this can be handled with a single touch.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 4

PU Microcontroller Lab Manual 2017 -18

## The 8051 microcontroller architecture

The 8051 is the name of a big family of microcontrollers. The device which we are going to
use along this tutorial is the 'AT89S52' which is a typical 8051 microcontroller
manufactured by Atmel™. Note that this part doesn't aim to explain the functioning of the
different components of a 89S52 microcontroller, but rather to give you a general idea of the
organization of the chip and the available features, which shall be explained in detail along
this tutorial.
The block diagram provided by Atmel™ in their datasheet showing the architecture the
89S52 device can seem very complicated, and since we are going to use the C high level
language to program it, a simpler architecture can be represented as the figure
This figures shows the main features and components that the designer can interact with.
You can notice that the 89S52 has 4 different ports, each one having 8 Input/output lines
providing a total of 32 I/O lines. Those ports can be used to output DATA and orders do
other devices, or to read the state of a sensor, or a switch. Most of the ports of the 89S52
have 'dual function' meaning that they can be used for two different functions: the first one
is to perform input/output operations and the second one is used to implement special
features of the microcontroller like counting external pulses, interrupting the execution of
the program according to external events, performing serial data transfer or connecting the
chip to a computer to update the software.
Each port has 8 pins, and will be treated from the software point of view as an 8-bit variable
called 'register', each bit being connected to a different Input/Output pin.You can also notice
two different memory types: RAM and EEPROM. Shortly, RAM is used to store variable
during program execution, while the EEPROM memory is used to store the program itself,
that's why it is often referred to as the 'program memory'. The memory organization will be
discussed in detail later.
The special features of the 89S52 microcontroller are grouped in the blue box at the bottom
of figure At this stage of the tutorial, it is just important to note that the 89S52 incorporates
hardware circuits that can be used to prevent the processor from executing various
repetitive tasks and save processing power for more complex calculations. Those simple
tasks can be counting the number of external pulses on a pin, or generating precise timing sequences.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 5

PU Microcontroller Lab Manual 2017 -18

Features of 8051:
The main features of 8051 microcontroller are:
1) RAM – 128 Bytes (Data memory)
2) ROM – 4Kbytes (ROM signify the on – chip program space)
3) Serial Port – Using UART makes it simpler to interface for serial communication.
4) Two 16 bit Timer/ Counter
5) Input/output Pins – 4 Ports of 8 bits each on a single chip.
6) Five Interrupt Sources
7) 8 – bit ALU (Arithmetic Logic Unit)
8) Harvard Memory Architecture – It has 16 bit Address bus (each of RAM and ROM)
and 8 bit Data Bus.
9) 8051 can execute 1 million one cycle instructions per second with a clock frequency of
12MHz.
This microcontroller is also called as “System on a chip” because it has all the features on a
single chip

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 6

PU Microcontroller Lab Manual 2017 -18

AT89C51ED2 MICROCONTROLLER
Description
AT89C51RD2/ED2 is high performance CMOS Flash version of the 80C51 CMOS single
chip 8- bit microcontroller. It contains a 64-Kbyte Flash memory block for code and for data.
The 64-Kbytes Flash memory can be programmed either in parallel mode or in serial mode
with the ISP capability or with software. The programming voltage is internally generated
from the standard VCC pin.
The AT89C51RD2/ED2 retains all of the features of the Atmel 80C51 with 256 bytes of
internal RAM, a 5 source 4-level interrupt controller and three timer/counters. The
AT89C51ED2 provides 2048 bytes of EEPROM for nonvolatile data storage.
In addition, the AT89C51RD2/ED2 has a Programmable Counter Array, an XRAM of 1792
bytes, a Hardware Watchdog Timer, SPI interface, Keyboard, a more versatile serial
channel that facilitates multiprocessor communication (EUART) and a speed improvement
mechanism (X2 Mode).
The fully static design of the AT89C51RD2/ED2 allows reducing system power consumption
by bringing the clock frequency down to any value, including DC, without loss of data.
The AT89C51RD2/ED2 has 2 software-selectable modes of reduced activity and an 8-bit
clock prescaler for further reduction in power consumption. In the Idle mode the CPU is
frozen while the peripherals and the interrupt system are still operating. In the Power-down
mode the RAM is saved and all other functions are inoperative.
The added features of the AT89C51RD2/ED2 make it more powerful for applications that
need pulse width modulation, high speed I/O and counting capabilities such as alarms,
motor control, corded phones, and smart card readers.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 7

PU Microcontroller Lab Manual 2017 -18

INTERUPTS
Five sources of interrupt (both external and internal).
Two External interrupts INTO and INTI are provided with push button switches: these can
also be used as general-purpose switches.
I/O (Port) Lines Four 10-pin connectors for all the 32 I/O lines.
P0,P1 and P2 Port lines are available on a 26-pin connector,
16X2 LCD & SERIAL I/O are also available.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 8

PU Microcontroller Lab Manual 2017 -18

## I. PROCEDURE FOR USING KEIL µVISION3 FOR 8051 PROGRAMMING

1. Create a project folder in a particular students folder
2. Click on keil µvision3 icon in desktop
3. To create project: click on project  new µvision project  in the create new project
window browse for the created project folder  type the project name <pro1.µv2>  click
ok
4. To create a file: goto file menu  click new  type the program in editor window  click
save in File menu  save the file as < filename.asm>
5. To add the files to source group : right click source group in project work space  click on
add files to project  select <filename.asm> file  add  close
6. Build the project: goto Project menu  click Build Project (note: if the target not created
then fix the errors, build again).
7. To run the program: goto Debug menu  click Start/Stop Debug session. Again goto
Debug menu  click RUN for running the program
8. Memory window option:
1. For internal RAM: D:0x40h
2. For external RAM:X:0x8900h
3. For internal ROM:C:0x00

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 9

PU Microcontroller Lab Manual 2017 -18

1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array
1.a) Data Transfer

Aim: To write an ALP to transfer block data from one memory location to another

## Program: 8051 program

ORG 00H
MOV R2,#04H ;No of elements = 4
NXT_LOC:
MOV A,@R0 ; transfer the element in present address
MOV @R1,A
INC R0 ; go to next element address of block1
INC R1 ; go to next element address of block2
; Transfer till the end of an array

END

Result:
I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 10

PU Microcontroller Lab Manual 2017 -18

## 1.b: Block Exchange

Aim: To write an ALP to exchange block data from one memory location to another memory
location

## Program: 8051 program

ORG 00H
MOV R2,#04H ;No of elements = 4
NXT_LOC:
MOV A,@R0 ; exchange the element in present address
XCH A,@R1
MOV @R0,A
INC R0 ; go to next element address of block1
INC R1 ; go to next element address of block2
; Transfer till the end of an array

END

Result:

I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 11

PU Microcontroller Lab Manual 2017 -18

## Program: 8051 program

ORG 00H
MOV R7,#04H ;No of elements
DEC R7 ;no of iterations = no of elements -1
MOV R6,#03H ;no of exchanges = iteration no-1
NXT_NO: MOV A,@R0 ;A= present element in array
INC R0 ;
MOV B,@R0 ; B= next element in array
CJNE A,B,N_E ;compare present with next element
N_E: JNC NO_EXG ; no. are equal/ A>B exchange not required
MOV @R0,A ;exchange the elements
DEC R0
MOV @R0,B
INC R0 ; go to next number address
; Comparison. If all the number are
; Compared go to next iteration
DJNZ R7,NXT_ITN; If all the iterations are over then exit

END

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 12

PU Microcontroller Lab Manual 2017 -18

Result:

I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 13

PU Microcontroller Lab Manual 2017 -18

## Program: 8051 program

ORG 00H
MOV R3,#04H ; N-1 WHERE N LENGTH OF AN ARRAY
MOV A,@R0 ; A= present element in array
NXT_NO: INC R0
MOV B,@R0 ;B= next element in array
CJNE A,B,NT_E ; compare present with next element
SJMP AGB ;A=B
NT_E: JNC AGB ; if carry = 0, then A>B
MOV A,@R0 ;else B>A, move B to A
AGB: DJNZ R3,NXT_NO ; go to next no. In an array till last no.
MOV 50H,A ;A has the largest no save it in internal
;memory

END
Result:
I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 14

PU Microcontroller Lab Manual 2017 -18

## 2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube –

(16 bits Arithmetic operations – bit addressable)

## Aim: To write an ALP to find the sum of 2 16 bit number

Program:

ORG 00H
MOV R0,#40H ;address of Lower byte of 1st number
MOV R1,#50H ; address of Lower byte of 2nd number
MOV A,@R0
MOV 60H,A ; save the lower byte of sum in 60h INC R0
; address of higher byte of 1st number
INC R1 ; address of higher byte of 2nd number
MOV A,@R0 ; add the higher bytes of 2 numbers with
ADDC A,@R1 ; along with carry generated by lower byte
MOV 61H,A ; save the second byte of sum in 61h
MOV 62H,#00H
JNC N_C
MOV 62H,#01H ; save the third byte of sum in 62h
N_C:
END

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 15

PU Microcontroller Lab Manual 2017 -18

Result:
I/P:

O/P:

Program:

ORG 00H

## MOV R0,#40H ;address of Lower byte of 1st number

MOV R1,#50H ; address of Lower byte of 2nd number
MOV A,@R0
SUBB A,@R1 ; subtract lower bytes of 2 numbers
MOV 60H,A ; save the lower byte of difference in 60h
INC R0 ; address of higher byte of 1st number
INC R1 ; address of higher byte of 2nd number
MOV A,@R0 ; subtract the higher bytes of 2 numbers
SUBB A,@R1 ; with along with carry generated by lower
MOV 61H,A ; save the 2nd byte of difference in 61h
MOV 62H,#00H
JNC N_C
MOV 62H,#0FFH ; save the 3rd byte of difference in 62h
N_C: END

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 16

PU Microcontroller Lab Manual 2017 -18

Result:

I/P:

O/P:

Logic:

## ( where B4B3 are 2 bytes of 2nd number & B2B1

B4 B3x B2 B1 2 bytes of 1st number

P2 P1 B3B1
+ P4 P3 B4B1
P6 P5 P1
+ P8 P7 B3B2
P11 P10 P9 P1
+ P13 P12 B4B2
P15 P14 P9 P1
Save in 63h 62h 61h 60h

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 17

PU Microcontroller Lab Manual 2017 -18

ORG 00H
MOV A,20H ;lower byte of 1st number (say B1)
MOV B,40H ;lower byte of 2nd number (say B3)
MUL AB ; B3xB1= product of 16 bit say (P2 P1)
MOV 60H,A ;[60h]=P1
MOV 61H,B ;[61h]=P2

## MOV A,20H ; lower byte of 1st number (say B1)

MOV B,41H ; higher byte of 2nd number (say B4)
MUL AB ; B4xB1= product of 16 bit say (P4 P3)
MOV 61H,A
MOV A,B
MOV 62H,A

MOV A,21H
MOV B,40H
MUL AB
MOV 61H,A
MOV A,B
MOV 62H,A
MOV A,#00
MOV 63H,A

MOV A,21H
MOV B,41H
MUL AB
MOV 62H,A

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 18

PU Microcontroller Lab Manual 2017 -18

MOV A,B
MOV 63H,A
END

Result:
I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 19

PU Microcontroller Lab Manual 2017 -18

2.d Division

ORG 00H
MOV DPTR,#2000H
MOVX A,@DPTR
MOV B,A
MOV DPTR,#2001H
MOVX A,@DPTR

DIV AB
MOV DPTR,#2010H
MOVX @DPTR,A
END

Result:
I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 20

PU Microcontroller Lab Manual 2017 -18

## 2.e Square & Cube

Aim: To write an ALP to find the square & cube of a given 8 bit number

ORG 00H

MOV A,20H
MOV B,20H
MUL AB
MOV 40H,A
MOV 41H,B

MOV A,20H
MOV B,40H
MUL AB
MOV 60H,A
MOV 61H,B

MOV A,20H
MOV B,41H
MUL AB
MOV 61H,A
MOV A,B
MOV 62H,A

END

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 21

PU Microcontroller Lab Manual 2017 -18

Result:

I/P:

O/P:

3. Counters
Aim: To write an ALP to count decimal UP

## Program: 8051 program

ORG 00H

MOV A,40H
NXT_CNT: MOV P1,A
A,#99H ( FOR DECIMAL DOWN)
DA A ; DELETE THIS INSTRUCTION FOR BINARY
COUNTER
ACALL DELAY
CJNE A,41H,NXT_CNT
MOV P1,A
RET

## DELAY: MOV R0,#40H

MOV R1,#0FFH
MOV R2,#0FFH
BACK: DJNZ R2,BACK

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 22

PU Microcontroller Lab Manual 2017 -18

DJNZ R1,BACK
DJNZ R0,BACK
RET
END
Result:

## Program: 8051 program

Y=ABC'+AB'C+AB'C'

ORG 00H
MOV C,20H
ANL C,01H
ANL C,/00H
MOV 8H,C
MOV C,20H
ANL C,/01H
ANL C,00H
ORL C,8
MOV 8,C
MOV C,20H
ANL C,/01H

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 23

PU Microcontroller Lab Manual 2017 -18

ANL C,/00H
ORL C,8
MOV 8,C
END

Result:
Truth Table

A B C Y=ABC'+AB'C+AB'C'

0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 24

PU Microcontroller Lab Manual 2017 -18

5. Code conversion: BCD – ASCII; ASCII – Decimal; HEX - Decimal and Decimal - HEX

## 5.a BCD - ASCII

Aim: To write an ALP to find the ASCII code of given BCD number

ORG 0000
MOV A,40H
ANL A,#0F0H
SWAP A
MOV 41H,A
MOV A,40H
ANL A,#0FH
MOV 42H,A
SJMP \$
END

Result:
I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 25

PU Microcontroller Lab Manual 2017 -18

## 5.b HEX - Decimal

Aim: To write an ALP to find the hexadecimal value of given decimal number

ORG 00H
MOV A,40H
MOV B,#0AH
DIV AB
MOV 43H,B
MOV B,#0AH
DIV AB
MOV 42H,B
MOV 41H,A
SJMP \$
END

Result:
I/P:

O/P:

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 26

PU Microcontroller Lab Manual 2017 -18

## 5.c Decimal - HEX

Aim: To write an ALP to find the hexadecimal value of given decimal number.

ORG 00H
MOV A,40H
MOV B,#10H
DIV AB
MOV R2,B
MOV B,#0AH
MUL AB
MOV 41H,A
SJMP \$
END

Result:
Run 1

Run 2

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 27

PU Microcontroller Lab Manual 2017 -18

6. Simple Calculator using 6 digit seven segment display and Hex Keyboard interface to 8051
Aim: To write a C program to simulate a simple calculator by interfacing 6 digit seven segment
display and key board
P0.3 chk CE / AC
-
P0.2 . = + x %

8051 P0.1 5 6 7 8 9
P0.0
0 1 2 3 4
P1.0
.
.
P1.4

## Fig. Interfacing Diagram

Program:
#include "AT89C51re2.H"
void scan(void);
void get_key(void);
void isr (void);
void delay_ms(unsigned int);
idata unsigned char row,col,key,dg1,dg2,dg3,dg4,dg5,dg6;
unsigned char scan_code[16]={0xEE, 0xED,0xEB, 0xE7,
0xDE, 0xDD, 0xDB, 0xD7,
0xBE, 0xBD, 0xBB, 0xB7,
0x7E, 0x7D, 0x7B, 0x77 };
unsigned char LEDCODE[16]= { 0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71 };
idata unsigned char temp,temp2,temp3,res1,flag,i,result=0;
idata unsigned char ind,temp1,num1=0,num2=0,number;
idata unsigned char operator,df,res12;
idata int result1;
idata unsigned int r,r1;
void isr(void) interrupt 1

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 28

PU Microcontroller Lab Manual 2017 -18

{
TL0 = 0xcc;
TH0 = 0xf8;
if(df==1)
{ //P2 holding position of led from right
P2 =0x50;
P1 = dg4; //P0 holding value to be display
df=2;
}
else if(df ==2)
{
P2 =0x40;
P1 = dg3;
df=3;
}
else if(df ==3)
{
P2 =0x30;
P1 = dg2;
df=4;
}
else if(df ==4)
{
P2 =0x20;
P1 = dg1;
df=5;
}
else if(df ==5)
{
P2 =0x10;
P1 = dg5;
df=1;
}
}

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 29

PU Microcontroller Lab Manual 2017 -18

void main ()
{
TMOD = 0x01;
TL0 = 0xcc;
TH0 = 0xf8;
ET0 = 1;
EA = 1;
TR0 = 1;
df = 1;
ind = 0;
operator = 0x00;
dg1 = 0x00;
dg2 = 0x00;
dg3 = 0x0;
dg4 = 0x0;
dg5= 0x0;
dg6= 0x00;
P3=0x0f;
// get first digit of the first number
do{
get_key();
}while (number>9);
num1 = number;
dg4 = LEDCODE[number]; //display first bit in unit place
dg3=0x00;
display();
delay_ms(500);
// get operator or second digit of the first number
do
{ //get next key
get_key();
}while(number>13);
if(number <10) //if it is less than 10 then it is no
{ //now that is unit value previous value is

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 30

PU Microcontroller Lab Manual 2017 -18

## dg4 = LEDCODE[number]; //in tens place

dg3 = LEDCODE[num1];
num1= num1*10 + number; //whole no is stored in num1
operator = 0;
}
else //if no is greater tha 9 then consider
{ //as a operator
operator =number;
dg4=LEDCODE[operator]; //display in unit place
dg3 = 0x0;
}
//now get operator key
if(operator == 0)
{
do
{
get_key();
}while (number <10 || number >13);
operator = number;
// display operator key
result=LEDCODE[operator];
dg4=LEDCODE[number]; // result = 0x00;
dg3 = 0x0;
}

delay_ms(500);
// get first digit of the second number
do{
get_key();
}while (number>9);

num2 = number;
dg4 = LEDCODE[number]; //display first bit in unit place

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 31

PU Microcontroller Lab Manual 2017 -18

dg3=0x00;
delay_ms(500);
//get second digit of the second number or EXEC key('E')
do
{
get_key();
}while(number>10 && number != 14);//get next key
// is it continuation of second number?
if(number <10) //if it is less than 10 then it is no
{
dg4 = LEDCODE[number];//now that is unit value previous value is
dg3 = LEDCODE[num2]; //in tens place
num2= num2*10 + number; //whole no is stored in num2
// get execute key (.) or second number
do{
get_key();
}
while (number != 14); //get execute key
}
dg6 = LEDCODE[number];
dg3 = 0x0;
dg4 = 0x0;
delay_ms(500);
switch (operator)
{
case 10: result1 = (num1 + num2);
break;
case 11: result1 =(int) (num1 - num2);
break;
case 12: result1 =(int) (num1 * num2);
break;
case 13: result1 = (num1 / num2);
break;

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 32

PU Microcontroller Lab Manual 2017 -18

default :result1 = 5;
break;
}
if(result1<0)
{
result1=~result1+1; //displaying sign
dg5=0x40;
}
dg1 = result1 / 1000; //get thousands bit
result1=result1-(dg1*1000);
dg2 = result1 / 100; //get hundreds bit
result1=result1-(dg2*100);
dg3 = result1 / 10; //get tens bit
result1=result1-(dg3*10);
dg4 = result1 % 10; //get unit bit
dg1=LEDCODE[dg1]; //get led code values to all bits
dg2=LEDCODE[dg2];
dg3=LEDCODE[dg3];
dg4=LEDCODE[dg4];
display_res();
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
here: goto here;
// while(1)
}
void get_key(void)
{

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 33

PU Microcontroller Lab Manual 2017 -18

int i;
flag = 0x00;
while(1)//flag == 0x00)
{
for(row=0;row<4;row++) //find which row is low
{ // store corresponding value in temp3
if( row == 0)
temp3=0xfe;
else if(row == 1)
temp3=0xfd;
else if(row == 2)
temp3=0xfb;
else if(row == 3)
temp3=0xf7;
P0 = temp3;
scan();
delay_ms(10);
if(flag == 0xff)
break;
} // end of for
if(flag == 0xff)
break;
} // end of while
for(i=0;i<16;i++)
{
if(scan_code[i] == res1) //find position of scan code
{ //corresponding to result value
number = i; //that value is assigned to number
break;
}
}
}// end of get_key();
void scan(void)
{

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 34

PU Microcontroller Lab Manual 2017 -18

unsigned char t;
temp2 = P0;
temp2 = temp2 & 0xf0;
if(temp2 != 0xF0) //rOW SELECTION
{
delay_ms(100); //other than 0f then give debounce delay
delay_ms(100);
delay_ms(100);
delay_ms(100);
delay_ms(100);
temp2 = P0;
temp2 = temp2 & 0xF0; //repeat process if there is change
if(temp2 != 0xF0)
{
flag = 0xff;
res1 = temp2;
// t = (temp3 << 4) & 0xf0;
t = temp3 & 0x0F;
res1 = res1 | t;//assign the scan code in res1
}
else
{
flag = 0x00;
}
}
} // end of scan()
void delay_ms(unsigned int r)
{
for(r1=0;r1<r*4;r1++);
}
Result:
Thus the program was executed successfully and the output is seen.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 35

PU Microcontroller Lab Manual 2017 -18

## 7. Alphanumeric LCD panel and Hex keypad input interface to 8051

Aim: To write a C program to interface LCD Panel and 4x4 Hex keypad to 8051

P0.3
C D E F
P0.2
8 9 A B

P0.1 4 5 6 7
8051
P0.0
0 1 2 3
P1.0
P1.1
P1.2
P1.3
P2 P3

LCD Display

## Fig: Interfacing Diagram

Program:
// Takes a key input from key board and displays it on LCD screen and also on the 7 segment display
(U21)
// target Atmel89c51ed2
// This project includes the following files:
// 1. kbdisp.c the source program to keypad
// 2. LCD_routine.c to display the key read

#include "at89c51xd2.h"
void scan(void);
void get_key(void);
void display(void);
void delay_ms(int i);
void uart_init(void);

## // LCD FUNCTION PROTOTYPE

void lcd_init(void);
void lcd_comm(void);
void lcd_data(void);
void clear_lcd(void);

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 36

PU Microcontroller Lab Manual 2017 -18

void delay(int);

## unsigned char temp1=0x00;

unsigned char temp2;

## unsigned char scan_code[16]={0xEE, 0xDE, 0xBE, 0x7E,

0xED, 0xDD, 0xBD, 0x7D,
0xEB, 0xDB, 0xBB, 0x7B,
0xE7, 0xD7, 0xB7, 0x77 };

## unsigned char LED_CODE[16]= {0x3f,0x66,0x7f,0x39,

0x06,0x6d,0x6f,0x5e,
0x5b,0x7d,0x77,0x79,
0x4f,0x07,0x7c,0x71};

## unsigned char LCD_CODE[16] = {'0','4','8','C',

'1','5','9','D',
'2','6','A','E',
'3','7','B','F' };

## idata unsigned char temp,temp4,temp3,res1,flag,result=0x3F;

void main ()
{
lcd_init();
// configure P2 lower as input to read from rows
//P2=0x0f;

while(1)
{
get_key();

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 37

PU Microcontroller Lab Manual 2017 -18

display();
P3 = 0xFF;
}
} //end of main()

## // get_key() function will make columns high one by one

// and calls scan() function
// on sensing a key from scan() function it
// will compare the received scan code with
// scan code lookup table and returns ASCII code
// rows are read from Port P0 is scan() function
// this function is in an eternal loop

void get_key(void)
{
int i;
display();
flag = 0x00;
while(flag == 0x00)
{
for(row=0;row<4;row++)
{
if( row == 0)
temp3=0xFE;
else if(row == 1)
temp3=0xFD;
else if(row == 2)
temp3=0xFB;
else if(row == 3)
temp3=0xF7;
// make coulmn high one by one output to Port P1 and
// invoke scan() function

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 38

PU Microcontroller Lab Manual 2017 -18

P0 = temp3;
scan();
delay_ms(10);
// on sensing a key scan() function will make flag = 0xff
if(flag == 0xff)
break;
} // end of for

if(flag == 0xff)
break;
} // end of while

// in this for for loop scan code received which is in res1 variable is compared with
// the lookup table for array scan code[] and when a match is
// found will return the correspoding led code for the key pressed
P3 = 0x00; // Enable U21
for(i=0;i<16;i++)
{
if(scan_code[i] == res1)
{
temp1 = 0x87;
lcd_comm();
temp2 = LCD_CODE[i];
lcd_data();
result = LED_CODE[i];
break;
}

}
}// end of get_key();

## // columns are made high one by one in the get_key() function

// will read the rows one by one and if key is sensed

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 39

PU Microcontroller Lab Manual 2017 -18

## // will return the scan code for the key pressed

// row lines are connected to P2.0 - P2.3 lines
void scan(void)
{
unsigned char t;

temp4 = P0;

## if(temp4 != 0xF0)// Means a key is sensed

{
delay_ms(30);
delay_ms(30); // give some delay for debouncing

## temp4 = P0; // read the port again

temp4 = temp4 & 0xF0;

## if(temp4 != 0xF0) // debounce

{
flag = 0xff; // set the flag denoting a key is received
res1 = temp4;
t = temp3 & 0x0F; // take the row input from P2
res1 = res1 | t; // and OR it with column value
} // to get the scan code of the key
pressed
else
{
flag = 0x00;
}

}
} // end of scan()

void display(void)

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 40

PU Microcontroller Lab Manual 2017 -18

{
// P2 = 0x0f is done beacuse P2 upper 4 bits are used
// as address of 7 segment display
// data is output on to P0 port
// result contians
// P3=0x00;
P1 = result;
}

void delay_ms(int i)
{
int j;
for(j=0;j<i;j++);
}

Result:
Thus the program was executed successfully and the output is seen.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 41

PU Microcontroller Lab Manual 2017 -18

## 8. External ADC and Temperature control interface to 8051

Aim: To interface External ADC and temperature control to 8051
12V -12V 5V GND

8051
& relay control
P0 for heater

P3 LCD Interface

P2

Interfacing Diagram
Program:

## /* This program displays the ADC output of the ADC0809 IC. */

//#include "D:\Keil\C51\INC\Atmel\reg51f.h"

#include<at89c51xd2.h>
#include<stdio.h>
// LCD FUNCTION PROTOTYPE
void lcd_init(void);
void lcd_comm(void);
void lcd_data(void);

void delay(int);

## unsigned char temp1;

unsigned char temp2,buf[8];

sbit EOC = P0^4;

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 42

PU Microcontroller Lab Manual 2017 -18

## sbit START_ALE = P0^7;

unsigned char xdata arr1[12]={"ADC O/P = "};
unsigned char xdata arr2[12]={"ADC I/P = "};
unsigned char i,a,temp_hi,temp_low;
unsigned char temp_msg[]={" "};

void main ()
{
START_ALE = 0;

lcd_init();

temp1 = 0x80; // Display the data from first position of first line
lcd_comm(); // Command Writing

for(i=0;i<10;i++)
{
temp2 = arr1[i];
lcd_data(); // Data Writing
}

## P1 = 0xff; // Configure P1 as input to read the ADC o/p

delay(200);

while(1)
{
P0 &= 0xF0; // Select the as input channel

delay(5);
START_ALE = 0;

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 43

PU Microcontroller Lab Manual 2017 -18

## do // Wait until End of conversion

takes place
{
vtemp1=P0;
vtemp1=vtemp1 & 0x10;
} while(vtemp1 == 0x10); // POLL EOC LINE HI TO LOW
do
{
vtemp1=P0;
vtemp1=vtemp1 & 0x10;
} while(vtemp1 == 0x00); // LOW TO HIGH

## adc_val = P1; // display adc result on the data field

temp_hi=temp_hi & 0x0f;

ASCII code
temp_hi = temp_hi + 0x37;
else
temp_hi = temp_hi + '0';

if(temp_low>9)
temp_low = temp_low + 0x37;
else
temp_low = temp_low + '0';

delay(100);

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 44

PU Microcontroller Lab Manual 2017 -18

temp_msg[1] = temp_hi ;
temp_msg[2] = temp_low ;

temp1 = 0x8A;
lcd_comm(); // Command Writing
temp2 = temp_hi;
lcd_data();

temp2 = temp_low;
lcd_data();

i=100;

for(a=0;a<4;a++)
{
buf[a] += '0';
i /= 10;
}

buf[3] = buf[2];
buf[2] = buf[1];
buf[1] = '.';
buf[4] = '\0';

temp1 = 0xC0;
lcd_comm(); // Displaying at 1st line of LCD
for(i=0;i<10;i++)
{
temp2 = arr2[i];

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 45

PU Microcontroller Lab Manual 2017 -18

## lcd_data(); // Data Writing

}

for(i=0;(buf[i]!='\0');i++)
{
temp2 = buf[i];
lcd_data();
}
} // end of while(1)
}

Result:
Thus the program was executed successfully and the output is seen.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 46

PU Microcontroller Lab Manual 2017 -18

9. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC
interface to 8051; change the frequency and amplitude.

Aim: To interface DAC and to write a C program to generate sine, square, triangle and ramp
waveforms

## 12V -12V GND

8051
P0 DAC Interface CRO
Module

## 9.a Sine Wave

Program:
// program to generate sine wave with DAC interface
// Port P0 is use to output the values
// This project includes the following files:
// 1. sine.c the source program
#include <at89c51xd2.h>
// variables used:
// sine_tab: array of sine values in hex for different angles.
// See the attached note for how these values are derived.
// count : index variable for loop count
xdata unsigned char sine_tab[49]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,
0xFF,0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,
0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,
0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80};
idata int count;
void main ()
{

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 47

PU Microcontroller Lab Manual 2017 -18

## // the program is in a endless loop

// ie it executes the code within while(1) for ever
// program ouputs different values correspoding
// different sine angles in hex
// to port P0
// user can observe sine waveform at dac output
while(1)
{
for(count=0;count<49;count++)
{
P0 = sine_tab[count];

}
}

## 9.b. Square Wave

Program:
// program to generate square wave with DAC interface
// Port P0 is use to output the values
// This project includes the following files:
// 1. sq.c the source program

#include<at89c51xd2.h>

// variables used:
// msg : a character array contains the string to output
// on to lcd
// cptr : character pointer used to point to msg array
// required to pass to function lcd_outstr()
void delay(void);
//unsigned char xdata msg[]={"Square"};

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 48

PU Microcontroller Lab Manual 2017 -18

## //unsigned char xdata *cptr;

void main ()
{
// the program is in a endless loop
// ie it executes the code within while(1) for ever
// program ouputs 0 and 0xff alternatively to port P0
// with a delay in between
// user can observe square waveform at dac output

while(1)
{
P0 = 0x0;
delay();
P0 = 0xff;
delay();

## // function to generate delay

// input: none
// returns : none
void delay(void)
{
int i;
for(i=0;i<=300;i++);
}

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 49

PU Microcontroller Lab Manual 2017 -18

## 9.c Triangle Wave

Program:
// program to generate triangular wave with DAC interface
// Port P0 is use to output the values
// This project includes the following files:
// 1. tri.c the source program
#include <at89c51xd2.h>
// variables used:
// count : unsigned char to output the value to P0 port
idata unsigned char count;
void main ()
{

## // the program is in a endless loop

// ie it executes the code within while(1) for ever
// program ouputs 0 and 0x7f to port P0 and
// 0x7f to 0 to port P0
// user can observe triangular waveform at dac output
while(1)
{
// output 0 to 0x7f to P0 port
for(count=0;count!=0xff;count++)
{
P0=count;

}
// output 0x7f to 0 to P0 port

for(count=0xff; count>0;count--)
{
P0=count;
}
}
}

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 50

PU Microcontroller Lab Manual 2017 -18

## 9.d Ramp Wave

Program:
// program to generate ramp wave with DAC interface
// Port P0 is use to output the values
// This project includes the following files:
// 1. ramp.c the source program
// 2. reg51f.h all register and sfr definitions
// 3. lcd.h contains progtotype declarations of lcd functions
// 4. lcdlib.lib contians all lcd functions in library mode
#include <at89c51xd2.h>
// variables used:
// count : character variable used to output count to digital input
idata unsigned char count;
void main ()
{
// initialise count varaible to 0x0
count = 0x0;
// the program is in a endless loop
// ie it executes the code within while(1) for ever
// program ouputs 0 to 0xff to port P0
// it increments the count variable from 0 to 0xff
// user can observe ramp waveform at dac output
while(1)
{
P0 = count; // output count to Port P0
count++; // increment count
}
}

Result:
Thus the program was executed successfully and the output is seen.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 51

PU Microcontroller Lab Manual 2017 -18

## Aim: To interface stepper and DC motor control interface to 8051

12. a) Stepper motor

5V GND

8051
Stepper Motor
P0 Interface Stepper
Module Motor

## Object : To demonstrate the Stepper motor interface.

Connection : Connect the stepper motor interface to 26 pin FRC connector J7
of ESAMCB51.
Output: whenever you run the program the motor rotates in clockwise.
If you want to change the direction press the interrupt button
( P3.2/INTO*). when you press the button, INT0 interrupts the main program
and changes the direction of the motor .

**************************************************************************/
Program:

#include "at89c51xd2.h"
static bit Dir=0;
sbit buzzer = P0^5;

## void ChangeDir(void) interrupt 0 /* Int Vector at 000BH, Reg Bank 1 */

{
Dir = ~Dir; /* Complement the Direction flag */

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 52

PU Microcontroller Lab Manual 2017 -18

}
void delay(unsigned int x) /* Delay Routine */
{
for(;x>0;x--);
}
main()
{
unsigned char Val,i;
EA=0x1; /* Enable Interrupt flag and Interrupt 0 & Serial Interrupt */
EX0=0x1;
ES=1; /*since the monitor is using the serial interrupt it has to
be enabled*/

while(1)
{
if(Dir) //* If Dir Clockwise
{

Val = 0x08;
for(i=0;i<4;i++)
{
P0 = Val; //* Write data for clock wise direction
Val = Val>>1;
delay(575);
}
}
else // AntiClockwise Direction
{

Val = 0x01;
for(i=0;i<4;i++)
{
P0 = Val; // Write data for anticlock wise direction
Val = Val<<1;

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 53

PU Microcontroller Lab Manual 2017 -18

delay(575);
}
}
}
}
12. b) DC motor

8051
DC Motor
P0 Interface DC Motor
Module

## Fig: Interfacing Diagram

Program:
// program to test nifc55 dc motor
// by varying off-time and on-time with different values
// user can observe different speed on dc motor
// typically the off values are 30,24,18 & 12
// on values are 10,16,12 & 28

#include "at89c51xd2.h"

## sbit DC1 = P0^6; //Selecting P0.6

sbit DC2 = P0^7; //Selecting P0.7

## idata unsigned char off_time,on_time;

idata unsigned int i;
void main ()
{

off_time = 1000;
on_time = 1000;

while(1)
{

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 54

PU Microcontroller Lab Manual 2017 -18

## DC1 = DC2 = 0 ; // make P0.6 & P0.7 lo

// 0;
for(i=0;i<on_time;i++)
{
TL0 = 0x00 ; //timer count set for delay
TH0 = 0x10 ;
TR0 =1; // Timer-0 ON
while(!TF0); // Wait untill Timer-0 Overflows
TF0 = 0; // Clear Overflow flag
TR0 = 0; // Timer-0 OFF
}
DC1 = 0 ; // make P0.6 high & make P0.7 low
DC2 = 1;
for(i=0;i<off_time;i++)
{
TL0 = 0x00; //timer count set for delay
TH0 = 0x10;
TR0 =1; // Timer-0 ON
while(!TF0); // Wait untill Timer-0 Overflows
TF0 = 0; // Clear Overflow flag
TR0 = 0; // Timer-0 OFF
}
}

Result:
Thus the program was executed successfully and the output is seen.

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 55

PU Microcontroller Lab Manual 2017 -18

## 11. Elevator interface to 8051

Aim: To write a C program to simulate elevator using 8051

8051
P0 Elevator
Interface
P1 Module

## Fig: Interfacing Diagram

Program:
/* 3 LEVEL ELEVATOR
Connection : interconnet CN1 to CN11 and CN2 to CN10 with the help of 10 pin FRC connector. */
#include <at89c51xd2.h>

## unsigned char Flr[9] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};

unsigned char Flr_clr[9] = {0xff,0x0E0,0x0D3,0xff,0x0B6,0xff,0xff,0xff,0x79};
unsigned char ReqFlr,CurFlr,i,j;

void main(void)
{
AUXR = 0x10; /* Accesiing Ful XRAM */

P1 = 0x00;
P1 = 0x0f0;
while(1)
{
P0 = 0x0f; /* Configure Port as i/p */
ReqFlr = P0 | 0x0f0; /* Append '1' at the Higher nibble position */
while(ReqFlr == 0x0ff)
ReqFlr = P0 | 0x0f0; /* Read Request Floor from P0 */
ReqFlr = ~ReqFlr; /* Complement to get the proper input */

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 56

PU Microcontroller Lab Manual 2017 -18

## if(CurFlr == ReqFlr) /* If Request floor is equal to Current Floor */

{
P1 = Flr_clr[CurFlr]; /* Clear Floor Indicator */
continue; /* Go up to read again */
}
else if(CurFlr > ReqFlr) /* If Current floor is > request floor */
{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /* Move the indicator down */
{
P1 = 0x0f0 | j;
j--;
delay(20000); /* Delay to visualize the effect */
}
}
else /* If Current floor is < request floor */
{
i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /* Move the indicator Up */
{
P1 = 0x0f0 | j;
j++;
delay(20000);
}
}
CurFlr = ReqFlr; /* Update Current floor */
P1 = Flr_clr[CurFlr]; /* Clear the indicator */
}
}

## Dept of ECE , PRESIDENCY UNIVERSITY, Bangalore Page 57

PU Microcontroller Lab Manual 2017 -18

## void delay(unsigned int x)

{
for(;x>0;x--);
}

Result: Thus the program was executed successfully and the output is seen.