Microcontroller Lab Manual
Microcontroller Lab Manual
ENGINEERING
Manandvadi Road, Mysuru-08
(Autonomous Institution under VTU)
Laboratory Manual
Microcontroller Laboratory
Department of Electrical and Electronics Engineering
Department Vision
The department will be an internationally recognized centre of excellence imparting
quality education in electrical engineering for the benefit of academia, industry and society at
large.
Department Mission
M1: Impart quality education in electrical and electronics engineering through theory and its
applications by dedicated and competent faculty.
M2: Nurture creative thinking and competence leading to innovation and technological
growth in the overall ambit of electrical engineering
PROGRAM OUTCOMES
5. Modern tool usage: Create, select and apply appropriate techniques, resources and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the need for and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
Program Specific Outcomes
Our Electrical and Electronics Engineering graduates will have the ability to:
• PSO2: Use and apply state-of-the-art tools to solve problems in the field of Electrical
Engineering .
Course Outcomes
List of experiments:
Intel Corporation is first company who presented 8051 microcontroller in market. It is 8 bit
microcontroller. It have on chip 128 bytes of RAM, 4K bytes ROM, two timers, one serial
port and four general purpose input/output ports. Each port has 8 bit register.
Some of the features that have made the 8051 popular are:
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial values.
Pins 10 to 17 − These pins are known as Port 3. This port serves some functions like interrupts, timer
input, control signals, serial communication signals RxD and TxD, etc.
Pins 18 & 19 − These pins are used for interfacing an external crystal to get the system clock.
Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher order address bus signals
are also multiplexed using this port.
Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a signal from the
external program memory.
Pin 30 − This is EA pin which stands for External Access input. It is used to enable/disable the
external memory interfacing.
Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to demultiplex the
address-data signal of port.
Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower order address and data
bus signals are multiplexed using this port.
2. Create New Project menu will appear with file directory. Create a new folder say
test. Choose the path and the folder name where you want your project. In this
case ‘test’ folder is chosen. In the filename box, key in the project name . Here it is
taken as test. Click on Save button. Now the project test will be saved in test folder.
3. After step 2 is completed successfully next screen for choosing the target will
appear. This is to enable the user to select the target CPU. Select Device for
Target ‘Target 1’ menu will appear with Manufacturing company names.
4. Choose Atmel name and under this menu choose AT89C51ED2.
5. Copy Standard 8051 Startup Code to Project and add file to project willappear.
Click No.
6. Place the mouse cursor on Target in the workspace. Right click the mouse.
Options for Target Target 1 will appear.
7. Select the option by clicking the mouse. Options for Target ‘Target1’ menu will
appear. Here enter Xtal frequency as 11.0592 and tick Use on-chip ROM and
Use On-chip XRAM check boxes.
8. Move on to Output tab. Here tick Create hex file as shown below. This is will
make the compiler linker to create the hex file which will be downloaded to
evaluation board for execution.
9. Expand Target by clicking on ‘+’ to see Source Group 1. This is done to include
‘.asm’ source file or/and Assembler source file to project.
10. Create a ‘.c’ source file by clicking on File menu and choosing Newsub menu.
13. Now we can add this ‘.c’ source file to the project by right clicking on source and
choosing Add files to Group ‘Source Group 1’
14. A file browser dialog box will appear. You can choose the drive, directory and
file to add to project. Here we are pointing to test.asmfile to add. Click on Add
button. Now this file is added to the project.
18. Again in the DEBUG menu select START/STOP debug session in the
disassembly window Program Code will be displayed. in the register space,
Contents of registers are visible.
19. To view memory, got to VIEW and select MEMORY WINDOW. Type the address
of memory location in address window and see its content.C:0X00=CODE
MEMORY, I: 0X00=INTERNAL DATA MEMORY, X: 0X00=EXTERNAL
MEMORY. if there is a need to give input to the program that can be done at this
stage.
20. Click RUN/STEP RUN in the DEBUG menu depending on the requirement. And
see the result in the Specified memory location and also in registers.
ASSEMBLY LANGUAGE PROGRAMS
END
END
2. Program to transfer a block of data bytes starting from the Memory location 30h to another block starting from the
Memory location 40h.
END
3. Program to store a constant value (8-bit) in the external RAM location starting from 9000h to 9004h
END
END
7. Program to transfer block of data bytes starting from memory location 9000h to another block starting from the memory
location 9100h in external memory location.
MOV R0,82h Copy the contents of the DPTR i.e DPL and DPH into
R0 and R1.
MOV R1,83h
Loop: MOVX A,@DPTR Copy the 1st data from the source address pointed by
DPTR.
stack.
PUSH 83h
INC DPTR Increment the DPTR to point to the next address in the
destination block.
MOV R0,82h
Copy the contents of the DPTR into R0 and R1
POP 83h Retrieve the source address from the stack to the
DPTR.
POP 82 h
Repeat the above process till all the data bytes are
DJNZ R2, Loop transferred.
END
8. Program to interchange the block of data bytes present in internal memory.
Loop: MOV A,@R0 Get the 1st data byte from block1.
XCH A,@R1 Exchange the 1st data bytes of both the blocks.
END
9. Program to transfer data from the external memory starting from 9040h to 9044h to stack memory from 71h to 75h and
transfer to internal RAM starting from 30h to 34h using PUSH and POP instruction.
DJNZ R2,UP Check whether the count is zero if not back to label
Up
DEC R0 Decrement R0
Arithmetic Programming:
1. Program to add ‘n’ 8 bit binary numbers present in the internal memory location.
MOV 41h,#00
MOV A,@R0 Get the data from the addr pointed by R0.
DJNZ R1, Back Continue the above operation till the count
becomes zero and halt.
Loop: SJMP Loop
END
2. Program to add two 8 bit binary numbers present in internal memory location store the Sum in next consecutive
memory location.
ADD A,@R0 Add the 1st and the 2nd data byte.
Next: MOV @R0,A Copy the sum to the addrs pointed by R0.
SJMP Loop
Loop: END
MOV DPTR,#RSLT ADDR Use DPTR to point to the addr where result is
stored.
DJNZ R2, Loop Check whether the count is zero if not loop back
else check for no carry. If carry, increment R3.
JNC Next
Get the carry in A register.
INC R3
Save the carry.
Next: MOV A,R3
MOVX @DPTR,A
END
MOV R0,# SRC ADDR Use the register to point to the source location.
ADD A,@R0 Add the data bytes present in A and the address
pointed R0.
END
ADD A,@R0 Add the contents in A with the data present in the
address pointed by R0,
END
6. Program to perform 8 bit binary subtraction.
MOV R0, # SRC ADDR Get the source address into R0.
END
ADD A,@ R0 Add the content of A with the data present in the
addrs pointed by R0.
END
ADD A,@R0 Add the content in A with the data at the addr
pointed by R0.
END
MOV A, @R0 Get the data into A from the addr pointed by R0.
END
MOV DPTR,# 9000h Use DPTR to point to the location where output isstored.
MOVX @DPTR, A Copy the lower byte of the result in the destaddr pointed by
DPTR.
MOVX DPTR,A Copy the data in A reg to the addr pointed by DPTR.
END
MOV A,@ R0 Get the data to the A register from the source.
END
CJNE A, 40h, Next Compare the content of A and addrloc 40. if not
equal jump.
INC 41h
SJMP Back
SQR: MOV A, @R0 Get the data into A reg from the location pointed
by R0.
MOV 0F0h, @R0 Copy the data from the addrloc pointed by R0 to
the B reg.
END
13. Program to check whether the given byte is positive or negative.
MOV A,@R0 Get the data to the A reg from the addr pointed
by R0.
Next : INC R0
END
Stop : MOV 43h,41h Get the data from 41h loc to 43h.
SJMP Back
MOV A,@R0 Get the data from the addr pointed by R0.
END
Programs to illustrate the use of Boolean and Logical Instructions (Bit manipulations).
1. Program to count the number of 1’s and 0’s in a given byte.
MOV R2,#00
MOV A,@R0 Get the data from the addr pointed R0.
Next1: DJNZ R3, Loop Decrement the R3 register and check whether it
is zero.
INC R0
Increment the contents of R3 reg.
MOV 41h,R2
Copy the contents of register R1 and R2 to the
MOV 42h,R1 location 41 and 42h.
END
MOV A,@R0 Get the data into A register from the addr pointed
by R0.
ANL A,#0E0h And the contents of A reg with the imm value.
JNZ Invalid If the result is not zero then the given byte is not
a 2 out of 5 code.
MOV A,@R0 Get the data into A reg from addr pointed by R0.
Next: DJNZ R2, Loop Decrement the count and check for zero.
CJNE R1,#02, Invalid Compare the content of R1 with 02. ifits not
equal then its not a valid code.
MOV 41h,#00 If the code is valid move 00 to the location 41.
LCALL 03
END
MOV A,@R0 Get the data from the location pointed by R0.
MOV @R0,#0FFh
MOV A,@R0 Get the data from the addr pointed by R0.
CJNE A,50h, Next Compare the 1st and 2nd data. If they are not equal
jump to next.
SJMP Next1
Check whether there is a carry. If no, jump to next1
Next: JNC Next1
DJNZ R1,Loop Decrement the count and loop back till it goes to
zero.
MOV 50h,A
Loop1: Store the largest number in the location 50h.
SJMP Loop1
END
MOV A, @R0 Get the data from the addr pointed by R0.
Compare the 1st and 2nd data. If they are not equal
CJNE A,50h, Next jump to next.
Next1: DJNZ R1, Loop Decrement the count and loop back
Loop: MOV A,@R0 Get the data into A from the addr pointed by R0.
MOV @R0,A Copy the data from the reg A to the location
pointed by R0.
MOV @R0,50h
Loop: MOV A,@R0 Get the data into A from the addr pointed by R0.
MOV @R0,A Copy the data from the reg A to the location
pointed by R0.
DEC R0
Decrement the addr pointed by R0, and copy the
MOV @R0,50h data at loc 50h to it.
NCHNG: DJNZ R3, Loop Repeat the above process till 3 goes to zero.
SJMP Loop
Loop:
CJNE A,#39h,Next Compare the data with 39. If not equal jump to
next.
MOV A,@R0 Get the data from the addr pointed by R0.
MOV A,B
SWAP A
Swap the higher and lower nibbles of the A reg.
MOV A,@R0 Get the data from the addr pointed by R0,
SUBB A,#30h Subtract with borrow the data in A reg from 30h.
MOV @R0,A Copy the data in A reg to the addr pointed by R0.
DIV AB Divide A by B
INC R0 Increment R0
DIV AB Divide A by B
INC R0 Increment R0
END
6. Program to convert 8 bit BCD to Binary number
END
Counter Programming
1. Program to implement 8 bit binary up counter.
RET
2.Program to implement 8 bit binary down counter.
Back: DJNZ R3,Back Decrement the content of R3, if not zero go back
and load R2
Here: RET
Back: DJNZ R3,Back Decrement the content of R3, if not zero go back
and load R2
Here: RET
5.Program to implement 8 bit Hex UP counter.
RET
END
CLR C
DELAY: CJNE A,#0FFH,UP Compare and jump to label Up, if A not equal to 0
RET
END
Instruction set of 8051(nptel.ac.in)
8051 Instructions
8051 has 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.
A: Accumulator
B: "B" register
C: Carry bit
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).
@Ri: 8-bit external or internal RAM address available in register R0 or R1. This is used for indirect addressing mode.
Addr11: 11-bit destination address for short absolute jump. Used by instructions AJMP & ACALL. Jump range is 2 kbyte (one page).
Rel: 2's complement 8-bit offset (one - byte) used for short jump (SJMP) and all conditional jumps.
CPL C C 1 1
CPL bit bit 2 1
ANL C, /bit C C. 2 1
ORL C, /bit C C+ 2 1