8051 Lab Pgms
8051 Lab Pgms
1
EXTERNAL
INTERRUPTS
4K
ROM TIMER 1
INTERRUPT 128 Byte COUNTER
CONTROL RAM INPUTS
TIMER 0
CPU
P0 P2 P1 P3 TXD RXD
X1 X2
ADDRESS /
DATA
Fig.1. General Block Diagram of 8051 Microcontroller Architecture
4. Create a source file (using File->New), type in the assembly or C program and
save this (filename.asm/filename.c) and add this source file to the project using
either one of the following two methods. (i) Project->Manage->Components,
Environment Books->addfiles-> browse to the required file -> OK
“OR” ii) right click on the Source Group in the Project Window and the Add
Files to Group option.
2
5. Set the Target options using -> Project – Options for Target opens the
µ Options for Target – Target configuration dialog. Set the Xtal
(Crystal frequency) frequency as 11.0592 MHz, and also the Options for Target
– Debug – use either Simulator / Keil Monitor- 51 driver.
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.
7. Build the project; using Project -> Build Project.
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
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.
3
11. 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.
4
Experiment no. 1 : Data Transfer Programming
a. Write an assembly language program to transfer N = bytes of data
from location A: h to location B: h.
Aim:
Procedure:
Out put:
Before Execution:
After Execution:
5
Result:
6
b. Write an assembly language program to exchange N = h bytes of data
at location A: h and at location B: h.
After Execution:
7
c. Write an assembly language program to find the largest element in a
given array of N = h bytes at location 4000h. Store the largest element
at location 4062h.
Let N = 06h
mov r3,#6 //length of the array
mov dptr,#4000H //starting address of array
movx a,@dptr
mov r1,a
Result:
Before Execution:
After Execution:
8
d. Write an assembly language program to sort an array of N = h bytes
of data in ascending/descending order stored from location 9000h.
(Using bubble sort algorithm)
Let N = 06h
mov r0,#05H //count (N-1) array size =
N loop1: mov dptr, #9000h //array stored from address 9000h
mov r1,#05h //initialize exchange counter
loop2: movx a, @dptr //get number from array and store in B
register mov b, a
inc dptr
movx a, @dptr //next number in the array
clr c //reset borrow flag
mov r2, a //store in R2
subb a, b //2nd-1st No, since no compare instruction in 8051
jnc noexchg // JC - FOR DESCENDING ORDER
mov a,b //exchange the 2 nos in the array
movx @dptr,a
dec dpl //DEC DPTR - instruction not
present mov a,r2
movx @dptr,a
inc dptr
Result:
Before Execution:
Note:
Analyze the bubble sort algorithm for the given data. Also try with different sorting
algorithms.
9
Experiment no. 2: Arithmetic Instruction Programming
Result:
Input: Output:
10
b. Write an assembly language program to perform the subtraction of
two 16-bit numbers.
Result:
Note: Try with different data. Ex: (Smaller number) – (larger number).
11
c. Write an assembly language program to perform the multiplication of
two 16-bit numbers.
Result:
Note: Write the logic of the program. Try with some other logic.
12
d. Write an assembly language program to find the square of a given
number N.
Let N = 05
Result:
Input: Output:
Result:
Before Execution: D: 21H = 22H =
Result:
Input: Output:
14
c. i Write an assembly language program to convert a binary (hex) number
into decimal.
Result:
Input: Output:
Result:
Input: Output:
15
Experiment no. 9: LCD and keypad interfacing
Block Diagram:
16
Program to interface LCD and KEYPAD :
#include <REG51xD2.H>
#include "lcd.h"
unsigned char getkey();
void delay(unsigned int);
main()
{
unsigned char key,tmp;
InitLcd(); //Initialise LCD
WriteString("Key Pressed="); // Display msg on LCD
while(1)
{
GotoXY(12,0); //Set Cursor Position
key = getkey(); //Call Getkey method
}
}
17
STEPS TO CREATE AND COMPILE MSP430 PROJECT IN IAR-
EMBEDDED WORKBENCH:
15. Create a source file (using File->New), type in the assembly or C program and
save this (filename.asm/filename.c) and add this source file to the project using
right click on the Source Group in the Project Window and the Add Files to
Group option.
16. Compile the project; using this button. Then click make . Any errors in the
code are indicated by the window shown in figure below, along with the error line.
Debug the errors. After an error free, go to Download and Debug mode by using
this icon.
18
17. After clearing errors and clicking download & debug button below window will
appear.
19
13. Data Transfer and Arithmetic Instruction Programming
a. Write an assembly language program to transfer N = bytes of data from
location A: h to location B: h (without overlap).
MOV.W #0x8000, R5
MOV.W #0x9000,R6
MOV.B #5,R7
again: MOV.W @R5+,0(R6)
INCD.W R6
DEC R7
JNZ again
JMP $
END
Result:
Input:
Output:
20
b. Write an assembly language program to exchange N = h bytes of data at
location A : h and at location B : h.
MOV.W #0x8000, R5
MOV.W #0x9000,R6
MOV.B #5,R7
Result:
Input: Output:
21
c. Write an assembly language program to sort an array of N = h bytes of
data in ascending/descending order stored from location 9000h.(use bubble
sort algorithm)
Note: For smallest number take the first element in the ascending order sorted array
and for largest number take the first element in the descending order sorted array
22
d. Write an assembly language program to perform the addition of two 32-bit
numbers.
Result:
Input: Output:
23
e. Write an assembly language program to perform the subtraction of two
32-bit numbers.
MOV.W #0X9000,R4
MOV.W #0XFFFF,R7
SUB.W R4,R7
MOV.W #0XFFFF,R5
MOV.W #0XFFFF,R6
SUBC R5,R6
JMP $
END
Result:
Input: Output:
24
f. Write an assembly language program to perform the multiplication of two
16-bit numbers.
Result:
Input: Output:
Note: For square of a number give both the numbers same value.
25
14 .Boolean & Logical Instruction Programming
Results:
26
b. Write an assembly language program to find weather the given 8 bit
number is even or odd.
Results:
MOV.W #0X1234, R5
MOV.W #0XABCD,R6
MOV.W R6,R7
MOV.W R6,R8
AND.W R5,R6 //R6=R5 AND R6
XOR.W R5,R7 //R7=R5 XOR R7
INV.W R8 //R8=NOT R8
INV.W R5
AND.W R8,R5
INV.W R5 //R5=R8 OR R5
JMP $
27
Results:
28
15.Counter and Code Conversion Programming
a. Write an assembly language program to implement (display) an 16 bit
UP/DOWN binary (hex).
DELAY:
MOV.W #0X50,R6
LOOP1: MOV.W #0X50,R7
LOOP: DEC R7
JNZ LOOP
DEC R6
JNZ LOOP1
RET
END
29
b. Write an assembly language program to implement (display) an 16 bit
UP/DOWN Decimal counter.
#include "msp430.h" ; #define controlled include
file NAME main ; module name
PUBLIC main ; make the main label visible outside this module
ORG 0FFFEh
DC16 init ; set reset vector to 'init' label
RSEG CSTACK ; pre-declaration of
segment
RSEG CODE ; place program in 'CODE'
segment init: MOV #SFE(CSTACK), SP ; set
up stack
main: NOP ; main program
MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer
DELAY:
MOV.W #0X50,R6
LOOP1: MOV.W
#0X50,R7 LOOP: DEC R7
JNZ LOOP
DEC R6
JNZ LOOP1
RET
END
RESULT:
R5 is decremented in BCD from 9999, 9998, ……, 0000, 9999, 9998……
30
c. Write an assembly language program to convert a 8-bit BCD number into
ASCII.
MOV.B #0X12, R5
MOV.B R5,R6
AND.B #0X0F,R6
ADD.B #0X30,R6
AND.B #0XF0,R5
RRA.B R5
RRA.B R5
RRA.B R5
RRA.B R5
ADD.B
#0X30,R5
MOV.B R5,R7
JMP $
END
Result:
Input: Output:
31
d. Write an assembly language program to convert a ASCII number into
Decimal.
MOV.B #0X35, R5
SUB.B #0X30,R5
MOV.B R5,R6
JMP $
END
Result:
Input: Output:
MOV.B #0X05, R5
ADD.B #0X30,R5
MOV.B R5,R6
JMP $
END
Result:
32
Input: Output:
33
f. Write an assembly language program to convert a binary (hex) number
into decimal.
#include "msp430.h" ; #define controlled include
file NAME main ; module name
PUBLIC main ; make the main label visible outside this
module ORG 0FFFEh
DC16 init ; set reset vector to 'init' label
RSEG CSTACK ; pre-declaration of
segment
RSEG CODE ; place program in 'CODE'
segment init: MOV #SFE(CSTACK), SP ; set
up stack
main: NOP ; main program
MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer
MOV.B #0XFE,R5
MOV.B #0X0A,R6
CALL #AA
MOV.B R5,R9
MOV.B R7,R5
CALL #AA
AND.W #0X00FF,R7
SWPB R7
RLA.B R5
RLA.B R5
RLA.B R5
RLA.B R5
ADD.W R5,R7
ADD.W R9,R7
JMP $
AA:
MOV.B #0XFF,R7
LOOP: INC R7
SUB.B R6,R5
JC LOOP
ADD.W #0x0A,R5
RET
END
Result:
Input: Output:
34
g. Write an assembly language program to convert a decimal number
into binary(hex).
#include "msp430.h" ; #define controlled include
file NAME main ; module name
PUBLIC main ; make the main label visible outside this
module ORG 0FFFEh
DC16 init ; set reset vector to 'init' label
RSEG CSTACK ; pre-declaration of
segment
RSEG CODE ; place program in 'CODE'
segment init: MOV #SFE(CSTACK), SP ; set up
stack
main: NOP ; main program
MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer
MOV.B #0X99,R5
MOV.B #0X10,R6
MOV.B #0XFF,R7
LOOP: INC R7
SUB.B R6,R5
JC LOOP
ADD.B #0x10,R5
AND.W #0X00FF,R7
MOV.B #0X00,R8
AGAIN:ADD.B #0X0A,R8
DEC R7
JNZ AGAIN
ADD.B R5,R8
JMP $
END
Result:
Input: Output:
35
Additional Programs
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
36
3. Program to generate first ten Fibonacci 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 put 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
37
REFERENCES
1. “The 8051 Microcontroller and Embedded Systems – using assembly and C ”-,
by Muhammad Ali, Mazidi and Janice Gillespie Mazidi and Rollin D. McKinlay;
PHI, 2006 / Pearson, 2006
38
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.
39
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?
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.
40
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.
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?
41
MICROCONTROLLER - LAB QUESTION BANK
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.
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.
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.
b) Write a C program to generate Sine waveform using DAC. Display the
waveform on CRO.
42
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 Decimal number to its equivalent Hexadecimal
using 8051 /MSP430.
b) Write a C program to interface DC motor to 8051.
13. a) Write an ALP to count number of 1’s and 0’s in the given 8-bit data using 8051
/MSP430.
b) Write a C program to interface Elevator to 8051.
14. a) Write an ALP to convert given ASCII number to its equivalent Decimal
number.
b) Write a C program to interface Elevator to 8051
15. a) Write an ALP to find whether given number is even or odd using 8051
/MSP430.
b) Write a C program to interface LCD panel and Hex keypad to 8051.
16. a) Write an ALP to convert given Decimal number to its equivalent ASCII using
8051 /MSP430.
b) Write a C program to interface LCD panel and Hex keypad to 8051.
43
Annexure-A
MCS-51 Instruction
set
Instruction Set
44
45
46
Annexure -B
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.
ET1 Enable Timer 1 interrupt. If 1, Enables the TF1 to generate the interrupt.
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.
47
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
48
5. 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
49
Annexure C
Architecture of MSP430 microcontroller
All the addressing modes for the source operand and all four addressing modes for the
destination operand can address the complete address space. The bit number show the
content of the As resp. Ad mode bits.
50
Instruction Set of MSP430
51
Microcontrollers Lab- 2015-
10ESL47 16
APPENDIX D
MCU 8051IDE
MCU 8051 IDE is a new modern graphical integrated development environment for
microcontrollers based on 8051. For those who believe 8051 is a great piece of
technology this IDE is a new way how to see and feel these still famous
microcontrollers. MCU 8051 IDE is noncommercial open-source software
primarily for Microsoft Windows and GNU/Linux Supported programming languages
are C language and assembly. It has its own assembler and support for 2 external
assemblers. For C language it uses SDCC compiler. There are packages for various
Linux distributions (.rpm and .deb), and installer for Microsoft Windows. This IDE
contains simulator, source code editor, assembler, HW programmer and much other
tools. Simulator supports over 79 MCU primarily from Atmel. There is also support
for simple hardware simulation (like LEDs, keys, etc.).
MCU 8051 IDE simulator is also equipped with a few simulated simple
hardware devices, which can be connected to the simulated MCU. These
virtual hardware components are intended primarily to offer a better insight
into programs interacting with things like LEDs or keys.