0% found this document useful (0 votes)
68 views

MC Lab Manual-2024

This document appears to be a lab manual for a Microcontrollers lab course at Reva University School of Electrical and Electronics Engineering. It contains instructions for students on the lab protocols and safety procedures. The document outlines the course objectives, which are to understand microcontroller architecture, programming, and interfacing microcontrollers with peripheral devices. It also includes a syllabus detailing the programming and interfacing experiments and assessments to be completed in the lab.

Uploaded by

Mrnotorious24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

MC Lab Manual-2024

This document appears to be a lab manual for a Microcontrollers lab course at Reva University School of Electrical and Electronics Engineering. It contains instructions for students on the lab protocols and safety procedures. The document outlines the course objectives, which are to understand microcontroller architecture, programming, and interfacing microcontrollers with peripheral devices. It also includes a syllabus detailing the programming and interfacing experiments and assessments to be completed in the lab.

Uploaded by

Mrnotorious24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

REVA UNIVERSITY SCHOOL OF EEE

School of Electrical & Electronics Engineering

MICROCONTROLLER
LAB MANUAL
B20EE0407

2023-2024

NAME : _______________________

SRN : _______________________

SEM & SEC : _______________________

SCHOOL OF ELECTRICAL AND ELECTRONICS ENGINEERING


REVA UNIVERSITY
RUKMINI KNOWLEDGE PARK
KATTIGENAHALLI, YELAHANKA, BANGALORE-560064
www.reva.edu.in

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

Electrical & Electronics Engineering

MICROCONTROLLER
LAB MANUAL
B20EE0407

2023-2024

NAME : _______________________

SRN : _______________________

SEM & SEC : _______________________

SCHOOL OF ELECTRICAL AND ELECTRONICS ENGINEERING


REVA UNIVERSITY
RUKMINI KNOWLEDGE PARK
KATTIGENAHALLI, YELAHANKA, BANGALORE-560064
www.reva.edu.in

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

This is to certify that Mr./Ms……………………………


bearing SRN ……………….. has satisfactorily completed
the course of Experiments in MICROCONTROLLER
LAB(B20EE0407)prescribed by REVA University for
B.Tech Course in the Laboratory of this university in the
year 2023-2024

Signature of Faculty in-charge Signature of the Director,


School of EEE

Signature of the Examiner

MICROCONTROLLERS LAB B18EE4080


REVA UNIVERSITY SCHOOL OF EEE

INDEX
Particulars of the Experiment Performed
Marks Staff
Sl N0 Title of Experiment Date
Obtained Signature

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

SYLLABUS

SUBJECT: MICROCONTROLLERS LAB


SUBJECT CODE: B18EE4080
Course Objectives
1. Understand the architecture of microcontroller and various features associated with the
different models of the microcontrollers.
2. Understanding of various computations and accessing methods associated with the
microcontrollers.
3. Gain the knowledge of programming.
4. Develop the ability to program the microcontroller in controlling the different applications
in real time.
5. Develop the capability to program and interface various devices to the microcontroller.

Course Outcomes
After the completion of the course the student shall be able to:
1. Write the codes using Assembly Level Program and C language for the 8051
microcontrollers.
2. Debug the given code using KEIL software.
3. Interface the microcontroller with peripheral devices for simple electrical and electronic
circuits.
4. Apply the logic to implement the real time applications.

PART-A
I. PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array.
2. i) Arithmetic Instructions -Addition/subtraction, multiplication and division (16 bits Arithmetic
operations – bit addressable).
ii) Boolean & Logical Instructions (Bit manipulations).
3. i) Code conversion: BCD – ASCII ; Decimal - HEX.
ii) Counters.
4. Programs on Serial Communication.(Transfer data serially).
5. Stack Operation.

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

II. INTERFACING
Write C programs to interface 8051 chip to Interfacing modules to develop single chip
solutions.

1. To Generate Sine, Square, Triangular, Ramp, Rectangular, Sawtooth waveform


using DAC interface to 8051 (With modifications).

2. Stepper motor control interface to 8051.

3. DC motor control interface to 8051.

4. Alphanumeric LCD panel interface to 8051.

5. LED interfacing to 8051

GENERAL INSTRUCTIONS TO STUDENTS


1. Students should come with thorough preparation for the program to be executed.

2. Students should take prior permission from the concerned faculty before availing the Leave.

3. Students should come with formals with apron and must be present on time in the laboratory.

4. Students will not be permitted to attend the laboratory unless they bring the practical Record
fully completed in all respects pertaining to the experiment conducted in the previous lab.

5. Students should obtain the signature of the staff-in-charge in the observation book after
completing each experiment.

6. Practical record and observation book should be maintained neatly.

7. All the students must enter their system numbers in the system log book.

8. Once the lab is over, students should shut down the systems and keep the system chairs properly
in order.

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

CONTENTS
Sl.No Name of the Experiment Page No.

Introduction to Microcontroller 1
Steps to create and compile Keil µvision -3/4 project 4
Execution steps using Keil µvision 6
PART A- PROGRAMMING (Assembly Language Program)
1a To copy a block of 10 Numbers from one location to other location 13
1b To Exchange a block of 10 numbers from one location to other location 15
1c To sort the given set of number in ascending order starting from location X 17
1d To find the largest number from array starting from location X & store the 19
result in location Y
2a Addition of two 8 bit hexadecimal numbers 21
2b Subtraction of two 16 bit hexadecimal numbers 23
2c To perform the following operation on given byte 25
i). Set the bits 0 & 1, Reset bits 2 & 3,Complement bits 6 & 4
2d To find the 2’s complement of a given 16 bit number. 26
2e Write an assembly language program to perform the following operations on 27
3 bytes of data stored from location X onwards
(A) OR the lower nibble of location X with upper nibble of location X+1
(B) XOR the result of (A) with contents of location X+2
3a To convert BCD to ASCII 29
3b To convert Decimal to HEX 31
3c To display the decimal up/ down counter 32
4 To transfer the data “ELECTRICAL ROCKS” Serially 34
5 Stack Operation 36
PART B – INTERFACING (C Program)
6 To Generate Sine, Square, Triangular, Ramp, Rectangular, Sawtooth 38
waveform using DAC interface to 8051 (With modifications).
7 To control the speed of stepper motor with interfacing to 8051 44
8 To control the speed of DC motor with interfacing to 8051 45
9 To display a given message(REVA University) on LCD Panel using 8051 46
Microcontroller LCD panel details
10 LED Interfacing to 8051 47
Challenging / Critical thinking Questions 49
Sample VIVA Questions 50
Microcontroller Question Bank 53
Annexure – I (8051 Instruction Sets) 55
Annexure – II (Special function registers of 8051) 63
Additional programs 66

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

INTRODUCTION TO MICROCONTROLLERS
A microcontroller can be compared to a small standalone 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.

Features Of 8051
MCS 8051 is an 8-bit single chip microcontroller with many built-in functions and is the core for
all MCS-51 devices.

The main features of the 8051 core are:


➢ Operates with single Power Supply +5V.
➢ 8-bit CPU optimized for control applications.
➢ 16-bit program counter (PC) and 16-bit data pointer (DPTR).
➢ 8-bit program status word (PSW).
➢ 8-bit stack pointer (SP).
➢ 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
➢ 128 bytes of On-Chip Data Memory (Internal RAM):
• Four Register Banks, each containing 8 registers (R0 to R7)(Total 32
registers).
• 16 bytes of bit addressable memory.
• 80 bytes of general-purpose data memory (Scratch Pad Area).
➢ Special Function Registers (SFR) to configure/operate microcontroller.
➢ 32 bit bi-directional I/O Lines (4 ports P0 to P3).
➢ Two 16-bit timers/counters (T0 and T1).
➢ Full duplex UART (Universal Asynchronous Receiver/Transmitter).
➢ 6-source/5-vector interrupts (2 external and 3 internal) with two priority levels.
➢ On-Chip oscillator and clock circuitry.

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

Pin Diagram For 8051 Microcontroller

MICROCONTROLLERS LAB B20EE0407 2


REVA UNIVERSITY SCHOOL OF EEE

General Block Diagram of 8051 Microcontroller Architecture

MICROCONTROLLERS LAB B20EE0407 3


REVA UNIVERSITY SCHOOL OF EEE

STEPS TO CREATE AND COMPILE Keil µVision- 3/4


PROJECT:

1. Double Click on the µVision-3/4 icon on the desktop.

2. Close any previous projects that were opened using – Project -> Close

3. Start Project – New Project, and select the CPU from the device database (Database-
Atmel- AT89S592as per the board).On clicking ‘OK’, the following option is displayed.
Choose ‘Yes’.

4. Click on file in the menu bar and select new to get text window (to type the code). Add
this file to the project by Right clicking on the Source Group in the Project Window and
the “Add Files to Group” option.

5. Set the Target options using -> Project – Options for Target opens the μ Vision2
Options for Target – Target configuration dialog. Set the XTAL (Crystal frequency)
frequency as 11.0592 MHz, and also the Options for Target – Debug – use Simulator.

6. If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the
COM Port to which the board is connected and select the baud rate as 19200 or 9600
(recommended). Enable Serial Interrupt option if the user application is not using on-
chip UART, to stop program execution.

MICROCONTROLLERS LAB B20EE0407 4


REVA UNIVERSITY SCHOOL OF EEE

7. Build the project; using Project -> Build Project. µVision translates all the user application
and links. Any errors in the code are indicated by – “Target not created” in the Build
window, along with the error line. Debug the errors. After an error free, to build go to Debug
mode.

8. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog. Or
by clicking in the icon .

9. The program is run using the Debug-Run command & halted using Debug-Stop Running.

Also the (reset, run, halt) icons can be used. Additional icons are

(step, step over, and step into, run till cursor)


.
10. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status,
etc. If it is a part-A program, the appropriate memory window is opened using View ->
memory window (for data RAM & XRAM locations), Watch window (for timer program),
serial window, etc.

Note: To access data RAM area type address as D: 0020h. Similarly to access the DPTR region
(XRAM-present on chip in AT89C51ED2) say 9000h location type in X: 09000H.

MICROCONTROLLERS LAB B20EE0407 5


REVA UNIVERSITY SCHOOL OF EEE

EXECUTION STEPS using KEIL μ vision:

MICROCONTROLLERS LAB B20EE0407 6


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 7


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 8


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 9


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 10


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 11


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 12


REVA UNIVERSITY SCHOOL OF EEE

Program -1(a)
Write an assembly language program to copy a block of ten numbers from one location to
other location.

Code Comment
ORG 00H

MOV R2,#0AH

MOV R0,#20H

MOV R1,#50H

NEXT: MOV A,@R0

MOV @R1,A

INC R0

INC R1

DJNZ R2,NEXT

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 13


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions

1. Brief the algorithm used to carry out block move operation.

2. Indicate source address and the destination address of the data.

3. Change the source address and destination address and carry out the programming.
Indicate the address locations.

MICROCONTROLLERS LAB B20EE0407 14


REVA UNIVERSITY SCHOOL OF EEE

Program -1(b)
Write an assembly language program to exchange a block of 10 numbers from one location
to other location.

Code Comment
ORG 00H

MOV R2,#0AH

MOV R0,#20H

MOV R1,#50H

NEXT:MOV A,@R0

XCH A,@R1

MOV @R0,A

INC R0

INC R1

DJNZ R2,NEXT

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 15


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions

1. Brief the algorithm used to carry out exchange operation.

2. Indicate addresses of the data used to exchange.

3. Change the addresses of the data and carry out the programming. Indicate the address
locations.

MICROCONTROLLERS LAB B20EE0407 16


REVA UNIVERSITY SCHOOL OF EEE

Program -1(c)
Write an assembly language program to sort the given set of numbers in ascending order,
starting from location X.

Code Comment
ORG 00H

nxtpass : MOV R3, #04h

MOV R0, #50h

MOV R2, #04h

nxtcomp : MOV A,@R0

MOV B, A

INC R0

MOV A,@R0

CJNE A, B, notequal

SJMP SKIP

notequal: JNC SKIP

DEC R0

MOV @R0, A

INC R0

MOV @R0, B

SKIP : DJNZ R2, nxtcomp

DJNZ R3, nxtpass

END

Observations:

MICROCONTROLLERS LAB B20EE0407 17


REVA UNIVERSITY SCHOOL OF EEE

Results & Inference

Answer the following Questions

1. Brief the algorithm used to sort the numbers in ascending order.

2. Indicate addresses of the data used for sorting.

3. Change the addresses of the data and carry out the programming. Indicate the address
locations.

4. What does the instruction @R0, @R1 mean?

MICROCONTROLLERS LAB B20EE0407 18


REVA UNIVERSITY SCHOOL OF EEE

Program -1(d)
Write an assembly language program to find the largest number from array starting from
location X and store the result in location Y.

Code Comment
ORG 0000h

MOV R0, #40h

MOV R2, #05h

MOV 50h, #00h

rpt: MOV A, @R0

CJNE A, 50h, here

SJMP less

here: JC less

MOV 50h, A

less: INC R0

DJNZ R2, rpt

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 19


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions


1. Brief the algorithm used to find the largest element of the array.

2. Indicate addresses of the data used in finding largest element.

3. Modify the program to find smallest element from the array of 5 numbers.

MICROCONTROLLERS LAB B20EE0407 20


REVA UNIVERSITY SCHOOL OF EEE

Program -2(a)
Write a program to Add two 8 bit hexadecimal numbers

Code Comments
ORG 00H

MOV A,#04H

MOV B,#02H

ADD A,B

MOV R1,A

MOV 30H,A

END

Observations:

Results & Inference

Answer the following Questions

1. Where the final result is stored?

MICROCONTROLLERS LAB B20EE0407 21


REVA UNIVERSITY SCHOOL OF EEE

2. Change the internal register location and store the result. Indicate the location.

3. Change the internal memory location and store the result. Indicate the location.

MICROCONTROLLERS LAB B20EE0407 22


REVA UNIVERSITY SCHOOL OF EEE

Program -2(b)
Write a program to Subtract of two 16 bit hexadecimal numbers

Code Comment
ORG 00H

CLR C

MOV A,#50H

SUBB A,#30H

MOV R0,A

MOV A,#55H

SUBB A,#35H

MOV R1,A

CLR A

SUBB A,#00H

MOV R2,A

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 23


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions

1. Brief the operation of the instruction SUBB.

2. Draw PSW register and indicate various flags.

3. What is the function of ‘C’ flag in case of SUBB operation?

4. Change the internal register location and store the result. Indicate the location.

MICROCONTROLLERS LAB B20EE0407 24


REVA UNIVERSITY SCHOOL OF EEE

Program -2(c)
Write an assembly language program to perform the following operations on a given byte of
data.

Set the bits 0 & 1, Reset bits 2&3, Compliment bits 6&7.

Code Comment
ORG 00H

MOV A, 40h

MOV P2,#00H

ORL A, #03h

ANL A, #0F3h

XRL A, #0C0h

MOV 50h, A

END

Observations:

Results & Inference

Answer the following Questions

1. Give examples of other bit manipulation instructions.

MICROCONTROLLERS LAB – B18EE4080 25


REVA UNIVERSITY SCHOOL OF EEE

Program -2(d)

Write an assembly language program to find the 2’s compliment of a given 16 bit number.

Code Comment
ORG 0000h

MOV A, 41h
CPL A
ADD A,#01H
MOV 51h, A
MOV R1,A
MOV A, 40h
CPL A
ADDC A,#00H
MOV 50h, A
MOV R2,A
END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 26


REVA UNIVERSITY SCHOOL OF EEE

Program -2(e)
Write an assembly language program to perform the following operations on 3 bytes of data
stored from location X onwards
(A) OR the lower nibble of location X with upper nibble of location X+1
(B)XOR the result of (A) with contents of location X+2.

Code Comment
ORG 0000h

MOV A, 40h

ANL A, #0Fh

MOV 50h, A

MOV A, 41h

SWAP A

ANL A, #0Fh

ORL A, 50h

XRL A, 42h

MOV 50h, A

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 27


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions

1. Explain the logical instructions AND, OR and XOR.

2. How are the logical instructions used for bit manipulation? Give examples.

MICROCONTROLLERS LAB B20EE0407 28


REVA UNIVERSITY SCHOOL OF EEE

Program -3(a)
Write an assembly language program to convert Binary to ASCII.

Code Comment
MOV DPTR,#9000H

MOVX A,@DPTR

MOV R0,A

SWAP A

MOV DPTR,#900DH

ACALL ASCII

MOV A,R0

ACALL ASCII

SJMP $

ASCII: ANL A,#0FH

ADD A,#30H

MOVX @DPTR,A

INC DPTR

RET

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 29


REVA UNIVERSITY SCHOOL OF EEE

LOGIC:
Binary ASCII
If binary < 0Ah Add #30h
If binary >=0Ah Add #37h

Answer the following Questions


1. Take different binary number and convert it to ASCII.

2. Explain the LCALL instruction and its use in the program..

MICROCONTROLLERS LAB B20EE0407 30


REVA UNIVERSITY SCHOOL OF EEE

Program -3(b)
Write an assembly language program to convert Decimal to HEX.

Code Comment
MOV DPTR,#9000H

MOVX A,@DPTR

MOV R0,A

ANL A,#0F0H

SWAP A

MOV B,#0AH

MUL AB

MOV R1,A

MOV A,R0

ANL A ,#0FH

ADD A,R1

MOVX @DPTR,A

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 31


REVA UNIVERSITY SCHOOL OF EEE

Program -3(c)

Write an assembly language program to display the decimal up/down counter

Code Comment
ORG 00H
CLR A
UP:ADD A,#01H
MOV 30H,A
DA A
MOV P0,A
MOV A,30H
CJNE A,#99H,UP
DOWN:SUBB A,#01H
MOV 40H ,A
DA A
MOV P0,A
MOV A,40H
CJNE A,#00,DOWN
END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 32


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions

1. Mention the counting range of the program (up counter and down counter)

2. What does the instruction DA A do?

3. Write an assembly language program to display the decimal down counter in PORT 0

MICROCONTROLLERS LAB B20EE0407 33


REVA UNIVERSITY SCHOOL OF EEE

Program - 4

Write an assembly language program to transfer the data “ELECTRICAL ROCKS”


serially.

Code Comment
ORG 00H
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON ,#50H
SETB TR1
AGAIN: MOV A,#'E'
ACALL TRANS
MOV A,#'L'
ACALL TRANS
MOV A,#'E'
ACALL TRANS
MOV A,#'C'
ACALL TRANS
MOV A,#'T'
ACALL TRANS
MOV A,#'R'
ACALL TRANS
MOV A,#'I'
ACALL TRANS
MOV A,#'C'
ACALL TRANS
MOV A,#'A'
ACALL TRANS
MOV A,#'L'
ACALL TRANS
MOV A,#20H
ACALL TRANS
MOV A,#'R'
ACALL TRANS
MOV A,#'O'
ACALL TRANS
MOV A,#'C'
ACALL TRANS
MOV A,#'K'
ACALL TRANS
MOV A,#'S'
ACALL TRANS
MOV A,#9
ACALL TRANS
AGAIN1: SJMP AGAIN1
TRANS: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET
END

MICROCONTROLLERS LAB B20EE0407 34


REVA UNIVERSITY SCHOOL OF EEE

Observations:

Results & Inference

Answer the following Questions


1. What is SBUF register?

2. What is SCON register?

MICROCONTROLLERS LAB B20EE0407 35


REVA UNIVERSITY SCHOOL OF EEE

Program - 5
Write an assembly language program to transfer the data on to the stack and Retrieve the
data from stack

Code Comment
ORG 0000h

MOV R1,#24H

MOV R2,#11H

MOV R3,#0F2H

PUSH 1

PUSH 2

PUSH 3

POP 2

POP 1

POP 3

END

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 36


REVA UNIVERSITY SCHOOL OF EEE

Answer the following Questions

1. Find the contents of stack before and after execution of the given instructions?
MOV R2,#30H
MOV R3,#40H
MOV R4,#50H
PUSH 2
PUSH 3
PUSH 4

2. Where does the stack pointer points upon reset?

MICROCONTROLLERS LAB B20EE0407 37


REVA UNIVERSITY SCHOOL OF EEE

Part B
Interfacing

MICROCONTROLLERS LAB B20EE0407


REVA UNIVERSITY SCHOOL OF EEE

Program -6(i)
Write a C program to generate the sinusoidal waveform and display the same on CRO

Vout= 5V+ (5V*Sin θ)

#include <reg52.h>
void main( )
{
unsigned char i;
unsigned char idata table[180]=
{128,132,136,141,154,150,154,158,163,167,171,175,180,184,188,192,195,199,203,206,210,213
,216,220,223,226,228,231,234,236,238,241,243,244,246,247,248,249,250,251,252,253,254,255,
255,255,255,255,254,254,253,252,251,249,246,244,243,241,238,236,234,231,228,226,223,220,
216,213,210,206,203,199,195,192,188,184,180,175,171,167,163,158,154,150,145,141,136,132,
128,123,119,114,110,105,101,97,92,88,84,80,75,71,67,64,60,56,52,49,45,42,39,35,32,29,27,24,
21,19,17,14,12,11,9,7,6,4,3,2,1,1,0,0,0,0,0,0,0,0,1,1,2,3,4,6,7,9,11,12,14,17,19,21,24,27,29,32,3
5,39,42,45,49,52,56,60,64,67,71,75,80,84,88,92,97,101,105,110,114,119,123,128};
P0=0X00;
while(1)
{
for(i=0;i<180;i++)
P0=table[i];
}
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 38


REVA UNIVERSITY SCHOOL OF EEE

Program – 6(ii)
Write a C program to generate the Square waveform and display the same on CRO
#include<reg52.h>
Void MSDelay ( unsigned int );
Void main ( )
{
P0 = 0 X 00;
While (1)
{
P0 = 0 X 00;
MSDelay(50);
P0 = 0 X ff;
MSDelay(50);
}
}
Void MSDelay ( unsigned int value)
{unsigned int y;
for ( y = 0 ; y < value ; y++ );
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 39


REVA UNIVERSITY SCHOOL OF EEE

Program – 6 (iii)
Write a C program to generate the triangular waveform and display the same on CRO

#include<reg52.h>
void main ( )
{
Unsigned int i;
P0 = 0 X 00;
While (1)
{
for (i = 0; i<255; i++)
{
P0 = i;
}

for (i = 255; i>0; i--)


{
P0 = i;
}
}
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 40


REVA UNIVERSITY SCHOOL OF EEE

Program – 6(iv)
Write a C program to generate the Ramp waveform and display the same on CRO

#include<reg52.h>
void main ( )
{
Unsigned int i;
P0 = 0 X 00; set P0 as i/p port
While (1)
{
for (i = 0; i<255; i++)
{
P0 = i;
}
}
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 41


REVA UNIVERSITY SCHOOL OF EEE

Program – 6(v)
Write a C program to generate the Rectangular waveform and display the same on CRO
#include<reg52.h>
Void MSDelay ( unsigned int );
void main ( )
{
P0 = 0 X 00;
While (1)
{
P0 = 0 X 00;
MSDelay(50);
P0 = 0 X ff;
MSDelay(100);
}
}
Void MSDelay (unsigned int value)
{Unsigned int y;
for ( y = 0 ; y < value ; y++ );
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 42


REVA UNIVERSITY SCHOOL OF EEE

Program -6 (vi)
Write a C program to generate the Saw tooth waveform and display the same on CRO
#include<reg52.h>
void main ( )
{
Unsigned int i;
P0 = 0 X 00;
While (1)
{
for (i = 0; i<255; i = i+5)
{
P0 = i;
}

for (i = 255; i>1; i--)


{
P0 = i;
}
}
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 43


REVA UNIVERSITY SCHOOL OF EEE

Program -
44
Write a C program for Stepper motor speed control interface with 8051 microcontroller

7a.To rotate Stepper motor in clockwise 7b.To rotate Stepper motor in anticlockwise
direction direction

#include<reg52.h> #include<reg52.h>
Void MSDelay(unsigned char value); Void MS Delay(unsigned char value);
void main( ) void main( )
{ {
While (1) While (1)
{ {
P0=0X66; P0=0X33;
MSDelay(1); MSDelay(1);
P0=0Xcc; P0=0X99;
MSDelay(1); MSDelay(1);
P0=0X99; P0=0Xcc;
MSDelay(1); MSDelay(1);
P0=0X33; P0=0X66;
MSDelay(1); MSDelay(1);
} }
} }
Void MSDelay(unsigned char value) Void MSDelay(unsigned char value)
{ {
Unsigned int y,x; Unsigned int y,x;
for(x=0;x<500;x++); for(x=0;x<500;x++)
for(y=0;y<value;y++); for(y=0;y<value;y++);
} }

Observations:

Results & Inference

MICROCONTROLLERS LAB- B18EE4080 44


REVA UNIVERSITY SCHOOL OF EEE

Program -8
Write a C program for DC motor speed control interface with 8051 microcontroller

8a. To rotate DC motor in clockwise 8b. To rotate DC motor in anticlockwise


direction direction

#include<reg52.h>
Void MSDelay (unsigned char value); #include<reg52.h>
sbit enablr=P0^6; Void MSDelay (unsigned char value);
sbit mtr_1=P0^7; sbit enablr=P0^6;
sbit mtr_2=P0^4; sbit mtr_1=P0^7;
void main( ) sbit mtr_2=P0^4;
{ void main( )
while(1) {
{ while(1)
mtr_1=1; {
mtr_2=0; mtr_1=0;
MSDelay(50); mtr_2=1;
mtr_1=0; MSDelay(50);
} mtr_1=0;
} }
Void MSDelay(unsigned char value) }
{ Void MSDelay(unsigned char value)
Unsigned int y,x; {
for(x=0;x<40;x++) Unsigned int y,x;
for(y=0;y<value;y++); for(x=0;x<40;x++)
} for(y=0;y<value;y++);
}

Observations:

Results & Inference

MICROCONTROLLERS LAB B20EE0407 45


REVA UNIVERSITY SCHOOL OF EEE

Program - 9
Write a C Program to display a given message on LCD Panel using 8051 microcontroller
LCD Panel Details:
• The size of LCD is 16 characters and 2 lines.
• The address of the first line is 80H and second line is C0H.

MICROCONTROLLERS LAB B20EE0407 46


REVA UNIVERSITY SCHOOL OF EEE

#include<reg52.h>
Sfr ldata=0x80;
Sbit rs=P3^7;
Sbit rw=P3^6;
Sbit en=P3^5;
sbit busy=P0^7;
void MSDelay(unsigned char);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdready( );
unsigned char i;
unsigned char idatamsg[15]={"REVA University"} ;
void main( )
{
lcdcmd(0x38);
lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x83);
for(i=0;i<12;i++)
lcddata(msg[i]);
while(1)
{
lcdcmd(0xc3);
}
}
Void MSDelay(unsigned char value)
{
Unsigned int x,y;
for(x=0;x<900;x++)
for(y=0;y<value;y++);
}
Void lcdcmd(unsigned char value)
{
lcdready( );
ldata=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}
Void lcddata(unsigned char value)
{
lcdready( );
ldata=value;
rs=1;
rw=0 ;
en=1 ;
MICROCONTROLLERS LAB B20EE0407 47
REVA UNIVERSITY SCHOOL OF EEE

MSDelay(1);
en=0;
return;
}
Void lcdready( )
{
busy=1;
rs=0;
rw=1;
while(busy==1)
{
en=0;
MSDelay(1);
en=1;
}
return;
}

Observations:

Results & Inference

Challenge / Critical thinking Questions:


1. Write code to add #1 to the contents of accumulator in 5 different ways

MICROCONTROLLERS LAB B20EE0407 48


REVA UNIVERSITY SCHOOL OF EEE

Challenging/Critical thinking questions


1. Write code to add #1 to the contents of accumulator in 5 different ways.
2. Consider the following code segment
MOV A,#47H
MOV B,#24H
ADD A,B
DA A
What would be the contents of the accumulator after the ADD? After DA A Instruction
3. Write ALP to multiply the numbers in R2 and Port 2and store in external RAM location
20H(LSB) and 21H(MSB)
4. Write a program to find the 2’s complement for a given number and store the result in 47H
5. Write an ALP to mask the MSB of the given number
6. Consider that a switch SW is connected to pin P2.3. Monitor the SW status and if SW =0,
send “HELLO” and if SW=1,send “WORLD” serially. Assume baud rate is 9600,8 bit data and
1 stop bit.
7. Write an ALP to exchange the data between two registers using stack instructions.
8. Find the delay in the subroutine, if the crystal frequency is 12MHz
DELAY:MOV R1,#0F3H
MOV R0,#00H
LOOP:DJNZ R0, LOOP
DJNZ R1,LOOP
RET
9. What are the ways to create a time delay? Discuss the factors affecting accuracy of the time
delay.
10. Write a program to read the status of switch shown in the figure. If S is ON ,turn on the LED
else turn off the LED.

MICROCONTROLLERS LAB B20EE0407 49


REVA UNIVERSITY SCHOOL OF EEE

Sample Viva Questions


1. What do you mean by Embedded System? Give examples.
2. Why are embedded Systems useful?
3. What are the segments of Embedded System?
4. What is Embedded Controller?
5. What is Microcontroller?
6. List out the differences between Microcontroller and Microprocessor.
7. How are Microcontrollers more suitable than Microprocessor for Real Time Applications?
8. What are the General Features of Microcontroller?
9. Explain briefly the classification of Microcontroller.
10. Explain briefly the Embedded Tools.
11. Explain the general features of 8051 Microcontroller.
12. How many pin the 8051 has?
13. Differentiate between Program Memory and Data Memory.
14. What is the size of the Program and Data memory?
15. Write a note on internal RAM. What is the necessity of register banks? Explain.
16. How many address lines are required to address 4K of memory? Show the necessary
calculations.
17. What is the function of accumulator?
18. What are SFR’s? Explain briefly.
19. What is the program counter? What is its use?
20. What is the size of the PC?
21. What is a stack pointer (SP)?
22. What is the size of SP?
23. What is the PSW? And briefly describe the function of its fields.
24. What is the difference between PC and DPTR?
25. What is the difference between PC and SP?
26. What is ALE? Explain the functions of the ALE in 8051.
27. Describe the 8051 oscillator and clock.
28. What are the disadvantages of the ceramic resonator?
29. What is the function of the capacitors in the oscillator circuit?
30. Show with an example, how the time taken to execute an instruction can be calculated.
31. What is the Data Pointer register? What is its use in the 8051?
32. Explain how the 8051 implement the Harvard Architecture?
33. Explain briefly the difference between the Von Neumann and the Harvard Architecture.
34. Describe in detail how the register banks are organized.
35. What are the bit addressable registers and what is the need?
36. What is the need for the general purpose RAM area?
37. Write a note on the Stack and the Stack Pointer.
38. Why should the stack be placed high in internal RAM?
39. Explain briefly how internal and external ROM gets accessed.
40. What are the different addressing modes supported by 8051 Microcontroller ?
41. Explain the Immediate Addressing Mode.
42. Explain the Register Addressing Mode.
43. Explain the Direct Addressing Mode.
44. Explain the Indirect Addressing Mode.
45. Explain the Code Addressing Mode.
46. Explain in detail the Functional Classification of 8051 Instruction set
47. What are the instructions used to operate stack?

MICROCONTROLLERS LAB B20EE0407 50


REVA UNIVERSITY SCHOOL OF EEE

48. What are Accumulator specific transfer instructions?


49. What is the difference between INC and ADD instructions?
50. What is the difference between DEC and SUBB instructions?
51. What is the use of OV flag in MUL and DIV instructions?
52. What are single and two operand instructions?
53. Explain Unconditional and Conditional JMP and CALL instructions.
54. Explain the different types of RETURN instructions.
55. What is a software delay?
56. What are the factors to be considered while deciding a software delay?
57. What is a Machine cycle?
58. What is a State?
59. Explain the need for Hardware Timers and Counters?
60. Give a brief introduction on Timers/Counter.
61. What is the difference between Timer and Counter operation?
62. How many Timers are there in 8051?
63. What are the three functions of Timers?
64. What are the different modes of operation of timer/counter?
65. Give a brief introduction on the various Modes.
66. What is the count rate of timer operation?
67. What is the difference between mode 0 and mode 1?
68. What is the difference Modes 0,1,2 and 3?
69. How do you differentiate between Timers and Counters?
70. Explain the function of the TMOD register and its various fields?
71. How do you control the timer/counter operation?
72. What is the function of TF0/TF1 bit
73. Explain the function of the TCON register and its various fields?
74. Explain how the Timer/Counter Interrupts work.
75. Explain how the 8051 counts using Timers and Counters.
76. Explain Counting operation in detail in the 8051.
77. Explain why there is limit to the maximum external frequency that can be counted.
78. What’s the benefit of the auto-reload mode?
79. Write a short note on Serial and Parallel communication and highlight their advantages and
disadvantages.
80. Explain Synchronous Serial Data Communication.
81. Explain Asynchronous Serial Data Communication.
82. Explain Simplex data transmission with examples.
83. Explain Half Duplex data transmission with examples.
84. Explain Full Duplex data transmission with examples.
85. What is Baud rate?
86. What is a Modem?
87. What are the various registers and pins in the 8051 required for Serial communication? Explain
briefly.
88. Explain SCON register and the various fields.
89. Explain serial communication in general (synchronous and asynchronous). Also explain the
use of the parity bit.
90. Explain the function of the PCON register during serial data communication.
91. How the Serial data interrupts are generated?
92. How is data transmitted serially in the 8051? Explain briefly.
93. How is data received serially in the 8051? Explain briefly.
94. What are the various modes of Serial Data Transmission? Explain each mode briefly.
95. Explain with a timing diagram the shift register mode in the 8051.
MICROCONTROLLERS LAB B20EE0407 51
REVA UNIVERSITY SCHOOL OF EEE

96. What is the use of the serial communication mode 0 in the 8051?
97. Explain in detail the Serial Data Mode 1 in the 8051.
98. Explain how the Baud rate is calculated for the Serial Data Mode 1.
99. How is the Baud rate for the Multiprocessor communication Mode calculated?
100. Explain in detail the Multiprocessor communication Mode in the 8051.
101. Explain the significance of the 9th bit in the Multiprocessor communication Mode.
102. Explain the Serial data mode 3 in the 8051.
103. What are interrupts and how are they useful in Real Time Programming?
104. Briefly describe the Interrupt structure in the 8051.
105. Explain about vectored and non-vectored interrupts in general.
106. What are the five interrupts provided in the 8051?
107. What are the three registers that control and operate the interrupts in 8051?
108. Describe the Interrupt Enable (IE) special function register and its various bits.
109. Describe the Interrupt Priority (IP) special function register and its need.
110. Explain in detail how the Timer Flag interrupts are generated.
111. Explain in detail how the Serial Flag interrupt is generated.
112. Explain in detail how the External Flag interrupts are generated.
113. What happens when a high logic is applied on the Reset pin?
114. Why the Reset interrupt is called a non-maskable interrupt?
115. Why do we require a reset pin?
116. How can you enable/disable some or all the interrupts?
117. Explain how interrupt priorities are set? And how interrupts that occur simultaneously are
handled.
118. What Events can trigger interrupts, and where do they go after getting triggered?
119. What are the actions taken when an Interrupt Occurs?
110. What are Software generated interrupts and how are they generated?
111. What is RS232 and MAX232?
112. What is the function of RS and E pins in an LCD?
113. What is the use of R/W pin in an LCD?
114. What is the significance of DA instruction?
115. What is packed and unpacked BCD?
116. What is the difference between CY and OV flag?
117. When will the OV flag be set?
118. What is an ASCII code?

MICROCONTROLLERS LAB B20EE0407 52


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLER - LAB QUESTION BANK

1. a) Write an ALP to move a Block of N-data starting at location X to


location Y using 8051/MSP430
b) Write a C program to interface stepper motor to 8051.

2. a) Write an ALP to find cube of given 8-bit data using 8051 /MSP430.
b) Write a C program to interface stepper motor to 8051.

3. a) Write an ALP to implement a binary/decimal up/down counter using 8051


/MSP430.
b) Write a C program to interface stepper motor to 8051.

4. a) Write an ALP to find the largest / smallest element in an array using 8051.
b) Write a C program to interface stepper motor to 8051.

5. a) Write an ALP to exchange two blocks of data present at location X and Y


respectively using 8051/MSP430
b) Write a C program to generate Sine waveform using DAC. Display the
waveform on CRO.

6. a) Write an ALP to arrange a set of N 8-bit numbers starting at location X in


ascending/descending order using 8051 /MSP430.
b) Write a C program to generate triangular wave of amp = (1V-5V)
using DAC. Display the waveform on CRO.

7. a) Write an ALP to perform 16-bit multiplication using 8051 /MSP430.


b) Write a C program to generate Ramp wave of amp = (1V-5V) using
DAC. Display the waveform on CRO.

8. a) Write an ALP to convert two digit BCD number to its equivalent ASCII
value using 8051 /MSP430.
b) Write a C program to generate square wave of amp = (1V-5V)
using DAC. Display the waveform on CRO.

9. a) Write an ALP to find whether the given number is palindrome or not using
8051.

MICROCONTROLLERS LAB B20EE0407 53


REVA UNIVERSITY SCHOOL OF EEE

b) Write a C program to generate Sine waveform using DAC. Display the


waveform on CRO.

10. a) Write an ALP to convert given Hexadecimal number to its equivalent


Decimal number using 8051 /MSP430.
b) Write a C program to interface DC motor to 8051.

11. a) Write an ALP to convert given Hexadecimal number to its equivalent


Decimal number using 8051 /MSP430.
b) Write a C program to interface DC motor to 8051.

12. a) Write an ALP to convert given Decimal number to its equivalent


Hexadecimal using 8051 /MSP430.

MICROCONTROLLERS LAB B20EE0407 54


REVA UNIVERSITY SCHOOL OF EEE

Annexure- I
8051 Instruction Sets
The 8051 about 111 instructions. These can be grouped into the following categories:

1. Arithmetic Instructions
2. Logical Instructions
3. Data Transfer instructions
4. Boolean Variable Instructions
5. Program Branching Instructions

The following nomenclatures for register, data, address and variables are used while write
instructions

1. A: Accumulator
2. B: "B" register
3. C: Carry bit
4. Rn: Register R0 - R7 of the currently selected register bank
5. Direct: 8-bit internal direct address for data. The data could be in lower 128bytes of RAM
(00 - 7FH) or it could be in the special function register (80 - FFH).
6. @Ri: 8-bit external or internal RAM address available in register R0 or R1. This is used
for indirect addressing mode.
7. #data8: Immediate 8-bit data available in the instruction.
8. #data16: Immediate 16-bit data available in the instruction.
9. Addr11: 11-bit destination address for short absolute jump. Used by instructions AJMP &
ACALL. Jump range is 2 Kbyte (one page).
10. Addr16: 16-bit destination address for long call or long jump.
11. Rel: 2's complement 8-bit offset (one - byte) used for short jump (SJMP) and all conditional
jumps.
12. bit: Directly addressed bit in internal RAM or SFR

Arithmetic Instructions

Mnemonics Description Bytes Instruction Cycles


ADD A, Rn A A + Rn 1 1
ADD A, direct A A + (direct) 2 1
ADD A, @Ri A A + @Ri 1 1
ADD A, #data A A + data 2 1
ADDC A, Rn A A + Rn + C 1 1
ADDC A, direct A A + (direct) + C 2 1
ADDC A, @Ri A A + @Ri + C 1 1
ADDC A, #data A A + data + C 2 1
DA A Decimal adjust accumulator 1 1

MICROCONTROLLERS LAB B20EE0407 55


REVA UNIVERSITY SCHOOL OF EEE

DIV AB Divide A by B
A quotient 1 4
B remainder
DEC A A A -1 1 1
DEC Rn Rn Rn - 1 1 1
DEC direct (direct) (direct) - 1 2 1
DEC @Ri @Ri @Ri - 1 1 1
INC A A A+1 1 1
INC Rn Rn Rn + 1 1 1
INC direct (direct) (direct) + 1 2 1
INC @Ri @Ri @Ri +1 1 1
INC DPTR DPTR DPTR +1 1 2
MUL AB Multiply A by B
A low byte (A*B) 1 4
B high byte (A* B)

SUBB A, Rn A A - Rn - C 1 1
SUBB A, direct A A - (direct) - C 2 1
SUBB A, @Ri A A - @Ri - C 1 1
SUBB A, #data A A - data - C 2 1

Logical Instructions

Mnemonics Description Bytes Instruction Cycles


ANL A, Rn A A AND Rn 1 1
ANL A, direct A A AND (direct) 2 1
ANL A, @Ri A A AND @Ri 1 1
ANL A, #data A A AND data 2 1
ANL direct, A (direct) (direct) AND A 2 1
ANL direct, #data (direct) (direct) AND data 3 2
CLR A A 00H 1 1
CPL A A A 1 1
ORL A, Rn A A OR Rn 1 1
ORL A, direct A A OR (direct) 1 1
ORL A, @Ri A A OR @Ri 2 1
ORL A, #data A A OR data 1 1
ORL direct, A (direct) (direct) OR A 2 1
ORL direct, #data (direct) (direct) OR data 3 2
RL A Rotate accumulator left 1 1
RLC A Rotate accumulator left through 1 1
carry
RR A Rotate accumulator right 1 1
RRC A Rotate accumulator right through 1 1
carry
SWAP A Swap nibbles within Acumulator 1 1
MICROCONTROLLERS LAB B20EE0407 56
REVA UNIVERSITY SCHOOL OF EEE

XRL A, Rn A A EXOR Rn 1 1
XRL A, direct A A EXOR (direct) 1 1
XRL A, @Ri A A EXOR @Ri 2 1
XRL A, #data A A EXOR data 1 1
XRL direct, A (direct) (direct) EXOR A 2 1
XRL direct, #data (direct) (direct) EXOR data 3 2
Data Transfer Instructions

Mnemonics Description Bytes Instruction


Cycles
MOV A, Rn A Rn 1 1
MOV A, direct A (direct) 2 1
MOV A, @Ri A @Ri 1 1
MOV A, #data A data 2 1
MOV Rn, A Rn A 1 1
MOV Rn, direct Rn (direct) 2 2
MOV Rn, #data Rn data 2 1
MOV direct, A (direct) A 2 1
MOV direct, Rn (direct) Rn 2 2
MOV direct1, (direct1) (direct2) 3 2
direct2
MOV direct, @Ri (direct) @Ri 2 2
MOV direct, #data (direct) #data 3 2
MOV @Ri, A @Ri A 1 1
MOV @Ri, direct @Ri (direct) 2 2
MOV @Ri, #data @Ri data 2 1
MOV DPTR, DPTR data16 3 2
#data16
MOVC A, A Code byte pointed by A + DPTR 1 2
@A+DPTR
MOVC A, @A+PC A Code byte pointed by A + PC 1 2

MOVC A, @Ri A Code byte pointed by Ri 8-bit address) 1 2


MOVX A, @DPTR A External data pointed by DPTR 1 2

MOVX @Ri, A @Ri A (External data - 8bit address) 1 2


MOVX @DPTR, A @DPTR A(External data - 16bit address) 1 2

PUSH direct (SP) (direct) 2 2


POP direct (direct) (SP) 2 2
XCH Rn Exchange A with Rn 1 1
XCH direct Exchange A with direct byte 2 1
XCH @Ri Exchange A with indirect RAM 1 1

MICROCONTROLLERS LAB B20EE0407 57


REVA UNIVERSITY SCHOOL OF EEE

XCHD A, @Ri Exchange least significant nibble of A with that 1 1


of indirect RAM

Boolean Variable Instructions

Mnemonics Description Bytes Instruction Cycles


CLR C C-bit 0 1 1
CLR bit bit 0 2 1
SET C C 1 1 1
SET bit bit 1 2 1
CPL C C 1 1
CPL bit bit 2 1
ANL C, /bit C C. 2 1
ANL C, bit C C. bit 2 1
ORL C, /bit C C+ 2 1
ORL C, bit C C + bit 2 1
MOV C, bit C bit 2 1
MOV bit, C bit C 2 2

Program Branching Instructions

Mnemonics Description Bytes Instruction Cycles


ACALL addr11 PC + 2 (SP) ; addr 11 PC 2 2
AJMP addr11 Addr11 PC 2 2
CJNE A, direct, rel Compare with A, jump (PC + rel) if not 3 2
equal
CJNE A, #data, rel Compare with A, jump (PC + rel) if not 3 2
equal
CJNE Rn, #data, rel Compare with Rn, jump (PC + rel) if not 3 2
equal
CJNE @Ri, #data, rel Compare with @Ri A, jump (PC + rel) if 3 2
not equal
DJNZ Rn, rel Decrement Rn, jump if not zero 2 2
DJNZ direct, rel Decrement (direct), jump if not zero 3 2
JC rel Jump (PC + rel) if C bit = 1 2 2
JNC rel Jump (PC + rel) if C bit = 0 2 2
JB bit, rel Jump (PC + rel) if bit = 1 3 2
JNB bit, rel Jump (PC + rel) if bit = 0 3 2
JBC bit, rel Jump (PC + rel) if bit = 1 3 2
JMP @A+DPTR A+DPTR PC 1 2
JZ rel If A=0, jump to PC + rel 2 2
MICROCONTROLLERS LAB B20EE0407 58
REVA UNIVERSITY SCHOOL OF EEE

JNZ rel If A ≠ 0 , jump to PC + rel 2 2


LCALL addr16 PC + 3 (SP), addr16 PC 3 2
LJMP addr 16 Addr16 PC 3 2
NOP No operation 1 1
RET (SP) PC 1 2
RETI (SP) PC, Enable Interrupt 1 2
SJMP rel PC + 2 + rel PC 2 2
JMP @A+DPTR A+DPTR PC 1 2
JZ rel If A = 0. jump PC+ rel 2 2
JNZ rel If A ≠ 0, jump PC + rel 2 2
NOP No operation 1 1

8051 Microcontroller Special Function Registers

All the 21 8051 Microcontroller Special Function Registers (SFRs) along with their functions and Internal
RAM Address is given in the following table

MICROCONTROLLERS LAB B20EE0407 59


REVA UNIVERSITY SCHOOL OF EEE

Instruction Sets

MICROCONTROLLERS LAB B20EE0407 60


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 61


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 62


REVA UNIVERSITY SCHOOL OF EEE

Annexure - II
8051 Special Function Registers
1. Timer Mode Control Register (TMOD):
TMOD can be considered to be two duplicate 4-bit registers, each of which
controls the action of one of the timers. The “Timer” or “Counter” function is selected
by control bits C/T, and in different operating modes, which are selected by bit-pairs
(M1, M0) in TMOD.
MSB LSB

GATE C/T M1 M0 GATE C/T M1 M0

Timer 1 Timer 0

Gating control when set. Counter “x” is enabled only while “INTx” pin is
GATE high and “TRx” control pin is set. When cleared Timer “x” is enabled
whenever “TRx” control bit is set.
Timer or Counter Selector cleared for Timer operation (input from internal
C/T
system clock.) Set for Counter operation (input from “Tx” input pin).
M1 M0 OPERATI0N
0 0 13-bit Timer/Counter 5-bits of “TLx” and 8-bits of “THx” are used.
0 1 16-bit Timer/Counter 8-bits of “TLx” and 8-bits of “THx” are cascaded.
8-bit auto-reload Timer/Counter “THx” holds a value which is to be
1 0
reloaded into “TLx” each time it overflows.
(Timer 0) TL0 is an 8-bit Timer/Counter controlled by the standard Timer
1 1 0 control bits. TH0 is an 8-bit timer only controlled by Timer 1 control bits.
Timer/Counter 1 stopped.

2. Interrupt Enable (IE) Register :

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


EA x x ES ET1 EX1 ET0 EX0

Symbol Name and Function


Enable All. If 0, Disables all interrupts and no interrupt is acknowledged. If
EA 1, each interrupt can be individually enabled or disabled by programming
appropriate bit.
x Reserved
x -
ES Enable Serial Interrupt. If 1, enables TI or RI to generate interrupt.

ET1 Enable Timer 1 interrupt. If 1, Enables the TF1 to generate the interrupt.

MICROCONTROLLERS LAB B20EE0407 63


REVA UNIVERSITY SCHOOL OF EEE

EX1 Enable External interrupt 1. If 1, Enables the INT1 to generate the interrupt.
ET0 Enable Timer 0 interrupt. If 1, Enables the TF0 to generate the interrupt.
EX0 Enable External interrupt 0. If 1, Enables the INT0 to generate the interrupt.

3. Timer Control Register (TCON):


TCON has control bits and flags for the timers in the upper nibble, and control
bits and flags for the external interrupts in lower nibble.
MSB LSB

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Bit Symbol Function


Timer 1 overflow flag. Set by hardware on Timer/Counter
TCON.7 TF1 overflow. Cleared by hardware when processor vectors to
interrupt routine, or clearing the bit in software.
Timer 1 Run control bit. Set/cleared by software to turn
TCON.6 TR1
Timer/Counter on/off.
Timer 0 overflow flag. Set by hardware on Timer/Counter
TCON.5 TF0 overflow. Cleared by hardware when processor vectors to
interrupt routine, or by clearing the bit in software.
Timer 0 Run control bit. Set/cleared by software to turn
TCON.4 TR0
Timer/Counter on/off.
Interrupt 1 Edge flag. Set by hardware when external interrupts
TCON.3 IE1
edge detected. Cleared when interrupt processed.
Interrupt 1 type control bit. Set/cleared by software to specify
TCON.2 IT1
falling edge/low level triggered external interrupts.
Interrupt 0 Edge flag. Set by hardware when external interrupts
TCON.1 IE0
edge detected. Cleared when interrupt processed.
Interrupt 0 Type control bit. Set/cleared by software to specify
TCON.0 IT0
falling edge/low Level triggered external interrupts.

4. Interrupt Priority (IP) Register:

Each source of the interrupt can be individually programmed to be in either of


the two priority levels. The priorities can be assigned to each interrupt by programming
appropriate bits in the SFR Interrupt Priority Register.

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


x x x PS PT1 PX1 PT0 PX0

Symbol Name and Function


x Reserved
PS Priority of Serial Interrupt. If 1, Priority of Serial Interrupt is higher

MICROCONTROLLERS LAB B20EE0407 64


REVA UNIVERSITY SCHOOL OF EEE

PT1 Priority of Timer 1 interrupt. If 1, Priority of Timer 1 interrupt is higher


PX1 Priority of External interrupt 1. If 1, Priority of the INT1 is higher
PT0 Priority of Timer 0 interrupt. If 1, Priority of Timer 0 Interrupt is higher
PX0 Priority of External interrupt 0. If 1, Priority of the INT0 is higher

Serial Port Control Register (SCON):


The serial port control and status register is the Special Function Register SCON.
This register contains not only the mode selection bits, but also the 9th data bit for
transmit and receive (TB8 and RB8) and the serial port interrupt bits (TI and RI).
MSB LSB

SM0 SM1 SM2 REN TB8 RB8 TI RI

Where SM0, SM1 specify the serial port mode, as follows:

SM0 SM1 Mode Description Baud Rate


0 0 0 Shift register f osc / 12
0 1 1 8-bit UART Variable
1 0 2 9-bit UART f osc / 64 or fosc /32
1 1 3 9-bit UART variable

Enables the multiprocessor communication feature in Modes 2 and 3. In Mode 2 or


3, if SM2 is set to 1, then Rl will not be activated if the received 9th data bit (RB8)
SM2
is 0. In Mode 1, if SM2=1 then RI will not be activated if a valid stop bit was not
received. In Mode 0, SM2 should be 0.
Enables serial reception. Set by software to enable reception. Clear by software to
REN
disable reception.
The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software
TB8
as desired.
In Modes 2 and 3, is the 9th data bit that was received. In Mode 1, it SM2=0, RB8 is
RB8 the stop bit that was received. In Mode 0, RB8 is not used.
Transmit interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or
TI at the beginning of the stop bit in the other modes, in any serial transmission. Must
be cleared by software only.
Receive interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or
RI halfway through the stop bit time in the other modes, in any serial reception (except
see SM2). Must be cleared by software only.

MICROCONTROLLERS LAB B20EE0407 65


REVA UNIVERSITY SCHOOL OF EEE

Additional Programs for Fast Learners

1. Program to generate arithmetic progression. //Tn=a+(n-1)d


clr c
mov r1,#03h //d
mov r2,#01h //a
mov r3,#08h
mov r0,#30h
mov 30h,r2
mov r4,#01h
next: mov a,r1
mov b,r4
mul ab
addc a,r2
inc r0
mov
@r0,a inc
r4
djnz r3,next
end

2. Program to find the 2 out of 5 code.

mov r0,#30h ; r0 as pointer


mov a,@r0 ; no. to the Acc.
ANL a,#0e0h
jnz notvalid ; checks the 1st 3bits
mov a,@r0
mov r2,#5 ; r2 as counter
mov r1,#00h ; r0 stores no. of ones
clr c
check: rrc a
jnc skip
inc r1 ; if carry increment r1
skip: djnz r2,check
mov A,r1
clr c
subb A,#02h
jnz notvalid ; (a)!= 0 ===> (r2)!= 2 no. is not valid.
inc r0
mov @r0,#0ffh ; valid 2 out of 5 code
sjmp exit
notvalid: inc r0
mov @r0,#11h ; in valid 2 out of 5 code
exit: sjmp exit
end

MICROCONTROLLERS LAB B20EE0407 66


REVA UNIVERSITY SCHOOL OF EEE

3. Program to generate first ten Fibonacci numbers.

Mov dptr, #9000h


Mov r3, #08h
Movx a, @dptr
Mov r0,a
Inc dptr
Movx a, @dptr
Back: xch a, r0
Add a,r0
Inc dptr
Movx @dptr,a
Djnz r3,back
Lcall 0003h
.
4. Program to add multibyte numbers.

Mov dptr,#9000h
Mov r1,#04h
Mov r2,#90h
Mov r3,#91h
Mov r4,#92h
clr c
Mov dph,r2
Back: Movx a, @dptr Mov r5,a
Mov dph,r3
Movx a,@dptr
Addc a,r5 //Note: For multibyte subtraction
subb a,r5
Mov dph,r4
Movx @dptr,a
Inc dptr
Djnz r1,back
Jnc end1
Mov a,#01h
Movx @dptr, a
End1: lcall 0003h
End

MICROCONTROLLERS LAB B20EE0407 67


REVA UNIVERSITY SCHOOL OF EEE

MICROCONTROLLERS LAB B20EE0407 68

You might also like