You are on page 1of 62

MICROCONTROLLER PROGRAMMING LAB

PROGRAM I(A&B)
FAMILIARIZATION WITH MICROCONTROLLER KIT
AND SIMULATOR
AIM: To familiarization with microcontroller kit and simulator.
SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil software/EDSIM 1
2 Window Xp operating system 1

THEORY:

Earlier to Microcontrollers, Microprocessors were greatly used for each and every
purpose. Microprocessors were containing ALU, general purpose register, stack pointer, program
counter, clock counter and so many other features which the today’s Microcontroller also
possesses. But the difference between them exists with respect to the number of instructions,
access times, size, reliability, PCB size and so on.
Microprocessor contains large instruction set called as CISC processor whereas
Microcontroller contains less number of instructions and is called as RISC processor. The access
time is less in case of microcontrollers compared to microprocessors and the PCB size reduces in
case of microcontrollers. There are many versions of microcontrollers 8051, 80528751, AT8951
from Atmel Corporation and many more. In this manual we will study about the 8051
architecture, its features, programming and interfacing. 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:
1. Operates with single Power Supply +5V.
2. 8-bit CPU optimized for control applications.
3. 16-bit program counter (PC) and 16-bit data pointer (DPTR).
4. 8-bit program status word (PSW).
5. 8-bit stack pointer (SP).
6. 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).

IIET Page 1
MICROCONTROLLER PROGRAMMING LAB

7. 128 bytes of On-Chip Data Memory (Internal RAM):


8. Four Register Banks, each containing 8 registers (R0 to R7) [Total 32 reg]
9. 16-bytes of bit addressable memory.
10. 80 bytes of general-purpose data memory (Scratch Pad Area).
11. Special Function Registers (SFR) to configure/operate microcontroller.
12. 32 bit bi-directional I/O Lines (4 ports P0 to P3).
13. Two 16-bit timers/counters (T0 and T1).
14. Full duplex UART (Universal Asynchronous Receiver/Transmitter).
15. On-Chip oscillator and clock circuitry.

STEPS TO CREATE AND COMPILE Keil μVision-3/4 PROJECT:


1. Double Click on the  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- AT89C51ED2 or AT89C51RD2 as per the board).On clicking
‘OK’, the following option is displayed. Choose ‘No’.

4. Create a source file (using File->New), type in the assembly or C program and

IIET Page 2
MICROCONTROLLER PROGRAMMING LAB

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.

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.

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.

IIET Page 3
MICROCONTROLLER PROGRAMMING LAB

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.

RESULT: To familiarization with microcontroller kit and simulator has been studied.
APPLICATIONS:
Microcontrollers are mostly used in following electronic equipments :
 Mobile Phones
 Auto Mobiles
 CD/DVD Players
 Washing Machines
 Cameras
 In Computers-> Modems and Keyboard Controllers
 Security Alarms
 Electronic Measurement Instruments.
 Microwave Oven.

IIET Page 4
MICROCONTROLLER PROGRAMMING LAB

ADVANTAGES OF MICROCONTROLLER:
Microcontroller's use increased rapidly. Now these are used in almost every electronic
equipment like Washing Machines, Mobile Phones and Microwave Oven. Following are the
most important facts about Microcontrollers, which causes rapid growth of their use:
You may also like: Difference Between Microcontroller & Microprocessor
 Microcontrollers are cheap and very small in size, therefore they can be embedded on any
device.
 Programming of Microcontrollers is simple to learn. Its not much complicated.
 We can use simulators on Computers to see the practical results of our program. Thus we
can work on a Embedded project without even buying the required Components and
Chips. Thus we can virtually see the working of our project or program.

EXTENSION :
By this we can execute an program.

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?

IIET Page 5
MICROCONTROLLER PROGRAMMING LAB

IIET Page 6
MICROCONTROLLER PROGRAMMING LAB

PROGRAM I(C)
REGISTER ADDRESSING TECHNIQUES
AIM: Write an ALP for addition using register addressing modes.
SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAMS:

IMMEDIATE ADDRESSING MODE


org 0000H
LJMP MAIN
MAIN:MOV A,#50H
MOV R1,#30H
ADD A,R1
END

INDIRECT ADDRESSING MODE


org 0000H
LJMP MAIN
MAIN:MOV 40H,#50H
MOV 41H,#30H
MOV R0,#40H
MOV R1,#41H
MOV A,@R0
MOV B,@R1
ADD A,B
END

IIET Page 7
MICROCONTROLLER PROGRAMMING LAB

DIRECT ADDRESSING MODE


org 0000H
LJMP MAIN
MAIN:MOV 40H,#50H
MOV A,#30H
ADD A,40H
END

REGISTER ADDESSING MODE


ORG 0000H
LJMP MAIN
MAIN:
MOV A,#03H
CPL A
ADD A,#01H
END

RESULT:
An ALP for addition for 8 bit using register addressing modes is studied.

IIET Page 8
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 1
8 BIT ADDITION, SUBTRACTION,MULTIPLICATION &
DIVISON

AIM: Write an ALP for addition, subtraction multiplication & division


of two 8 bit numbers.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAMS

ADDITION 8-BIT

org 0000H
LJMP MAIN
MAIN:MOV A,#50H
ADD A,#30H
END

SUBTRACTION

org 0000H
LJMP MAIN
MAIN:MOV A,#50H
SUBB A,#30H
END

IIET Page 9
MICROCONTROLLER PROGRAMMING LAB

MULTIPLICATION

org 0000H
LJMP MAIN
MAIN:MOV A,#50H
MOV B,#30H
MUL AB
END

DIVISION

org 0000H
LJMP MAIN
MAIN:MOV A,#50H
MOV B,#30H
MUL AB
END

RESULT:
An ALP for addition, subtraction,multiplication & divison for 8 bit is studied.

APPLICATIONS:
By this student will be knowing how to do the addition and subtraction by binary number.

EXTENSION:
By simple modification we can executed many programs like multiplication,division,etc

VIVA QUESTIONS :
1. List out the differences between Microcontroller and Microprocessor.
2. How are Microcontrollers more suitable than Microprocessor for Real Time Applications?
3. What are the General Features of Microcontroller?
4. Explain briefly the classification of Microcontroller.
5. Explain briefly the Embedded Tools.

IIET Page 10
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 2

16 BIT ADDITION AND SUBTRACTION

AIM: To write a assembly language program to 16 BIT addition and subtraction.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 keil software 1
2 Window Xp operating system 1

ALGORTHIM:
1. Start.
2. Write the program in EDSIM.
3. Executed the program.
4. If no errors and warnings.
5. Note the address and opcode.
6. Stop

PROGRAMS

ADDITION :

ORG 0000H
LJMP MAIN
MAIN:
MOV A,#34H
MOV B, #12H
ADD A, B
MOV R0,A
MOV A, #B4H
MOV B,#C2H
ADDC A, B
MOV R1,A
END

IIET Page 11
MICROCONTROLLER PROGRAMMING LAB

SUBSTRACTION:

ORG 0000H
LJMP MAIN
MAIN:
MOV A,#34H
MOV B, #12H
SUBB A, B
MOV R0,A
MOV A, #B4H
MOV B,#C2H
SUBB A, B
MOV R1,A
END

RESULT: Addition and subtraction of 16 bit is performed.

APPLICATIONS:
By this student will be knowing how to do addition and subtraction the by binary number.

EXTENSION:
By simple modification we can executed many programs like multiplication and,division
,etc
VIVA QUESTION:

1. What is the PSW? And briefly describe the function of its fields.
2. What is the difference between PC and DPTR?
3. What is the difference between PC and SP?
4. What is ALE? Explain the functions of the ALE in 8051.
5. Describe the 8051 oscillator and clock.

IIET Page 12
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 3
SQUARE AND CUBE OF DECIMAL NUMBERS

AIM: To write a assembly language program to find square and cube of a given
binary number.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

FLOWCHART:

SQUARE OF A BINARY NUMBER:

start

Load 05H in A

Load A with B

MUL AB

End

IIET Page 13
MICROCONTROLLER PROGRAMMING LAB

CUBE OF A BINARY NUMBER:

start

Load 03H into A

Load A with R1

Load A with B

MUL AB

Load A with R2

Load B with R1

MUL AB

end

IIET Page 14
MICROCONTROLLER PROGRAMMING LAB

PROGRAM:

SQUARE OF A BINARY NUMBER:

ORG 0000H
LJMP MAIN
MAIN: Mov A, #05H
Mov R0,A
Mov B,R0
MUL AB
END

CUBE OF A BINARY NUMBER:

ORG 0000H
LJMP MAIN
MAIN: MovA, #03H
Mov R0,A
Mov B,R0
MUL AB
Mov B,R0
MUL AB
END

Result:
Square and cube of a given binary number is performed and executed.
APPLICATIONS:
By this student will be knowing how to do Square and cube the by binary number.
EXTENSION:
By simple modification we can executed many programs like multiplication and, division
,etc

IIET Page 15
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 4(A)
LCM OF TWO BINARY NUMBERS.

AIM: To write a assembly language program to find LCM of a given binary numbers.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM

ORG 0000H
LJMP MAIN
MAIN:MOV 40H,#0EH
MOV 41H,#04H
MOV A,40H
MOV B,41H
MUL AB
MOV R1,A
MOV A,40H
MOV B,41H
LCALL HCF
MOV B,R5
MOV A,R1
DIV AB
MOV R4,A
LJMP EXIT
HCF:CJNE A,B,BIG
BIG:JNC DIVI

IIET Page 16
MICROCONTROLLER PROGRAMMING LAB

XCH A,B
DIVI:MOV R5,B
DIV AB
MOV A,B
JNZ AGAIN
AGAIN:MOV A,R5
LJMP DIVI
EXIT:
END

RESULT: LCM of given binary numbers are studied.

APPLICATIONS:
By this student will be knowing how to do LCM the by binary number.

EXTENSION:
By simple modification we can executed many programs like multiplication and division
,etc

VIVA QUESTION:
1. Explain briefly the difference between the Von Neumann and the Harvard Architecture.
2. Describe in detail how the register banks are organized.
3. What are the bit addressable registers and what is the need?
4. What is the need for the general purpose RAM area?
5. Write a note on the Stack and the Stack Pointer.

IIET Page 17
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 4(B)
HCF OF THREE BINARY NUMBERS

AIM: To write a assembly language program to find HCF of a given binary numbers.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:
ORG 0000H
LJMP MAIN
MAIN:MOV 40H,#0EH
MOV 41H,#04H
MOV 42H,#07H
CLR C
MOV A,40H
MOV B,41H
LCALL HCF
LOOP:MOV B,R5
MOV A,42H
LCALL HCF
LJMP EXIT
HCF:CJNE A,B,BIG
BIG:JNC DIVI
XCH A,B
DIVI:MOV R5,B
DIV AB

IIET Page 18
MICROCONTROLLER PROGRAMMING LAB

MOV A,B
JNZ AGAIN
AGAIN:MOV A,R5
LJMP DIVI
EXIT:
END

RESULT: HCF of given binary numbers are studied.

APPLICATIONS:
By this student will be knowing how to do HCF the by binary number.

EXTENSION:
By simple modification we can executed many programs like multiplication and division
,etc

VIVA QUESTION:
6. Explain briefly the difference between the Von Neumann and the Harvard Architecture.
7. Describe in detail how the register banks are organized.
8. What are the bit addressable registers and what is the need?
9. What is the need for the general purpose RAM area?
10. Write a note on the Stack and the Stack Pointer.

IIET Page 19
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 5
TO BLOCK MOVE 10 BYTES OF DATA (WITHOUT OVERLAP)

AIM: To write a assembly language program to block move 10 bytes of data from
0x30/0x39 to 0x40/0x49 without overlap.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

ALGORTHIM:

1. Start.
2. Load 00H to 09H into 30H to 39H location.
3. Move the data 00H to 09H into 40H to 49H location.
4. stop

PROGRAM :

ORG 0000H
LJMP MAIN
MAIN:
MOV 30H,#00H
MOV 31H,#01H
MOV 32H,#02H
MOV 33H,#03H
MOV 34H,#04H
MOV 35H,#05H
MOV 36H,#06H
MOV 37H,#07H
IIET Page 20
MICROCONTROLLER PROGRAMMING LAB

MOV 38H,#08H
MOV 39H,#09H
MOV R0,#30H
MOV R1,#40H
MOV R2,#0AH
BACK:
MOV A,@R0
MOV @R1,A
DEC R1
DEC R0
DJNZ R2, BACK
END

RESULT:
An ALP for block transfer of data without overlap has been studied.

APPLICATIONS:

By this student will be knowing how to do transfer and exchange the by binary number

VIVA QUESTIONS:
1. Explain the Direct Addressing Mode. Explain the Indirect Addressing Mode.
2. Explain the Code Addressing Mode.
3. Explain in detail the Functional Classification of 8051 Instruction set
4. What are the instructions used to operate stack?
5. What are Accumulator specific transfer instructions?

IIET Page 21
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 6
TO BLOCK MOVE 10 BYTES OF DATA (WITH OVERLAP)

AIM: To write a assembly language program to block move 10 bytes of data from
0x30/0x39 to 0x35/0x39 with overlap.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

ALGORTHIM:

1. Start.
2. Load 00H to 09H into 30H to 39H location.
3. Move the data 00H to 09H into 35H to 39H and so on location.
4. stop

PROGRAM :

ORG 0000H
LJMP MAIN
MAIN:
MOV 30H,#10H
MOV 31H,#11H
MOV 32H,#12H
MOV 33H,#13H
MOV 34H,#14H
MOV 35H,#15H
MOV 36H,#16H
MOV 37H,#17H
MOV 38H,#18H
MOV 39H,#19H
IIET Page 22
MICROCONTROLLER PROGRAMMING LAB

MOV R0,#30H
MOV R1,#35H
MOV R2,#0AH
MOV A,R2
ADD A,R1
MOV R1,A
MOV A,R2
ADD A,R0
MOV R0,A
DEC R0
DEC R1
BACK:
MOV A,@R0
MOV @R1,A
DEC R0
DEC R1
DJNZ R2, BACK
END

RESULT:
An ALP for block transfer of data with overlap has been studied.

APPLICATIONS:

By this student will be knowing how to do transfer and exchange the by binary number

VIVA QUESTIONS:
1. Explain the Direct Addressing Mode. Explain the Indirect Addressing Mode.
2. Explain the Code Addressing Mode.
3. Explain in detail the Functional Classification of 8051 Instruction set
4. What are the instructions used to operate stack?
5. What are Accumulator specific transfer instructions?

IIET Page 23
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 7
TO BLOCK EXCHANGE 10 BYTES OF DATA
AIM: To write a assembly language program to block exchange 10 bytes of data
between 0x30/0x39 to 0x40/0x49.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

ALGORTHIM:
1. Start.
2. Load 0H to 09H into 30H to 39H location.
3. Load 10H to 19H into 40H to 49H location.
4. stop

FLOW CHART:
start

Move 10H to 19H into 30H to 39H

Move 20H to 29H into 40H to 49H


And exchange the data into 30h to 39h

Stop

IIET Page 24
MICROCONTROLLER PROGRAMMING LAB

PROGRAM:
ORG 0000H
LJMP MAIN
MAIN:
MOV 30H,#10H
MOV 31H,#11H
MOV 32H,#12H
MOV 33H,#13H
MOV 34H,#14H
MOV 35H,#15H
MOV 36H,#16H
MOV 37H,#17H
MOV 38H,#18H
MOV 39H,#19H
MOV 40H,#20H
MOV 41H,#21H
MOV 42H,#22H
MOV 43H,#23H
MOV 44H,#24H
MOV 45H,#25H
MOV 46H,#26H
MOV 47H,#27H
MOV 48H,#28H
MOV 49H,#29H
MOV R0,#30H
MOV R1,#40H
MOV R2,#0AH
BACK:
MOV A,@R0
MOV B,@R1

IIET Page 25
MICROCONTROLLER PROGRAMMING LAB

XCH A,B
MOV @R0,A
MOV @R1,B
INC R0
INC R1
DJNZ R2, BACK
END

RESULT: To block exchange 10 bytes of data between 0x30/0x39 to 0x401/0x49 is studied

APPLICATIONS:
By this student will be knowing how to do Data transfer from peripheral to memory
through DMA the by binary number.

EXTENSION:
By simple modification we can executed many programs like Data transfer from
peripheral to memory through any data.

VIVA QUESTIONS:
1. What is the difference between INC and ADD instructions?

2. What is the difference between DEC and SUBB instructions?

3. What is the use of OV flag in MUL and DIV instructions?

4. What are single and two operand instructions?

5. Explain Unconditional and Conditional JMP and CALL instructions

IIET Page 26
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 8
TO BLOCK 10 BYTES OF DATA EXTERNAL TO INTERNAL MEMORY

AIM: To write a assembly language program to block exchange 10 bytes of data from
0x30/0x39 to 0x1000x1009(external to internal memory or vice versa).

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ORG 0000H
LJMP MAIN
MAIN:
MOV DPTR,#1000H
MOV R0,#30H
MOV R2,#0AH
BACK:
MOVC A,@A+DPTR
MOV @R0,A
INC R0
DJNZ R2,BACK
ORG 1000H
DB 01H,02H,03H,04H,05H,06H,07H,08H,09H,0AH
END

IIET Page 27
MICROCONTROLLER PROGRAMMING LAB

RESULT: To block move 10 bytes of data between 0x1000x1009 to 0x30/0x39


is studied.
APPLICATIONS:
By this student will be knowing how to do Data transfer from peripheral to memory
through DMA the by binary number.

EXTENSION:
By simple modification we can executed many programs like Data transfer from
peripheral to memory through any data.

VIVA QUESTIONS:
1. What is the difference between INC and ADD instructions?
2. What is the difference between DEC and SUBB instructions?
3. What is the use of OV flag in MUL and DIV instructions?

IIET Page 28
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 9(A)

LARGEST/SMALLEST NUMBER IN A GIVEN ARRAY

AIM: To write a assembly language program to find smallest/largest number in 10 bytes of


data from 0x30/0x39 (R3 should store smallest or largest no.) and (R4 –should store address of
that no.)

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:
ORG 0000H
LJMP MAIN
MAIN:MOV 30H,#06H
MOV 31H,#03H
MOV 32H,#09H
MOV 33H,#05H
MOV 34H,#07H
MOV 35H,#0AH
MOV 36H,#02H
MOV 37H,#04H
MOV 38H,#01H
MOV 39H,#08H
MOV R0,#30H
MOV R2,#0AH
MOV A,@R0
MOV B,R0
MOV R4,B
DEC R2

IIET Page 29
MICROCONTROLLER PROGRAMMING LAB

LOOP1:INC R0
MOV B,@R0
CJNE A,B,LOOP2
LOOP2:JC LOOP3
MOV A,B
MOV R3,A
MOV B,R0
MOV R4,B
DJNZ R2,LOOP1
LJMP EXIT
LOOP3:MOV R3,A
DJNZ R2,LOOP1
EXIT:
END

NOTE:
Change the instruction JC with JNC in loop2 for largest no.

EXPECTED OUTPUT:

Smallest number in an given array is:


R3=01h(smallest no.)
R4=38h(address of smallest no.)

RESULT:
An ALP for smallest number in an given array is studied.

VIVA QUESTIONS:
1. Explain the need for Hardware Timers and Counters?
2. Give a brief introduction on Timers/Counter.
3. What is the difference between Timer and Counter operation?
4. How many Timers are there in 8051?
5. What are the three functions of Timers?

IIET Page 30
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 9(B)
SEARCH OF AN ELEMENT IN 10 BYTES OF DATA

AIM: Searching for an element in the 10 bytes of data from 0x30-0X39(R3=1, if element is
found else R3=0)

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ORG 0000H
LJMP MAIN
MAIN: MOV 30H,#2AH
MOV 31H,#3CH
MOV 32H,#1AH
MOV 33H,#09H
MOV 34H,#10H
MOV 35H,#6EH
MOV 36H,#8AH
MOV 37H,#1EH
MOV 38H,#7AH
MOV 39H,#3BH
MOV R0,#30H
MOV R1,#0AH
MOV R3,#00H
MOV A,#10H
LOOP1: MOV B, @R0
CJNE A,B ,LOOP2

IIET Page 31
MICROCONTROLLER PROGRAMMING LAB

INC R3
LJMP EXIT
LOOP2: INC R0
DJNZ R2, LOOP2
EXIT:
END

EXPECTED RESULT:
R3=1, because the number 10H has found

RESULT:
Searching for an element in the 10 bytes of data from 0x30-0X39 is studied.

IIET Page 32
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 10(a)
ASCENDING ORDER

AIM: To write and execute an ALP to sort 10 bytes from 0x30-0x39 in an


ascending order

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ORG 0000H
LJMP MAIN
MAIN:MOV 30H,#09H
MOV 31H,#06H
MOV 32H,#2AH
MOV 33H,#1CH
MOV 34H,#0AH
MOV 35H,#1FH
MOV 36H,#04H
MOV 37H,#01H
MOV 38H,#2DH
MOV 39H,#0CH
AGAIN:MOV R0,#30H
MOV R2,#09
MOV R3,#09
CLR C
UP:MOV A,@R0
MOV B,A
INC R0
MOV A,@R0

IIET Page 33
MICROCONTROLLER PROGRAMMING LAB

CJNE A,B,LOOP
LOOP:JC SKIP
MOV A,@R0
DEC R0
MOV @R0,A
MOV A,B
INC R0
MOV @R0,A
SKIP:DJNZ R2,UP
DJNZ R3,AGAIN
END

RESULT:
Sorting the 10 bytes of data from 0x30-0X39 in ascending order is studied.

IIET Page 34
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 10(a)
DESCENDING ORDER

AIM: To write and execute an ALP to sort 10 bytes from 0x30-0x39 in an


descending order

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ORG 0000H
LJMP MAIN
MAIN:MOV 30H,#09H
MOV 31H,#06H
MOV 32H,#2AH
MOV 33H,#1CH
MOV 34H,#0AH
MOV 35H,#1FH
MOV 36H,#04H
MOV 37H,#01H
MOV 38H,#2DH
MOV 39H,#0CH
AGAIN:MOV R0,#30H
MOV R2,#09
MOV R3,#09
CLR C
UP:MOV A,@R0
MOV B,A
INC R0
MOV A,@R0

IIET Page 35
MICROCONTROLLER PROGRAMMING LAB

CJNE A,B,LOOP
LOOP:JNC SKIP
MOV A,@R0
DEC R0
MOV @R0,A
MOV A,B
INC R0
MOV @R0,A
SKIP:DJNZ R2,UP
DJNZ R3,AGAIN
END

RESULT:
Sorting the 10 bytes of data from 0x30-0X39 in descending order is studied.

IIET Page 36
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 11(A)
2’S COMPLEMENT
AIM: To write a assembly language program to 2’S COMPLEMENT using CPL
instruction

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ORG 0000H
LJMP MAIN
MAIN:MOV A,#04H
CMP A
ADD A,#01H
END

RESULT: 2’S complement is performed.

IIET Page 37
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 11(B)
PACKED TO UNPACKED CONVERSION

AIM: To write a assembly language program to packed to unpacked using ANL


instruction

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN: LDA 3000H
MOV B,A
MVI C,04
ANI F0
L1: RRC
DCR C
JNZ L1
STA 3001
MOV A,B
ANI 0F
STA 3002
HLT
END DB 98H

RESULT: Packed to unpacked is performed.

IIET Page 38
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 12
ASCII TO BCD CONVERSION

AIM: To write and execute an ALP for convert ASCII TO BCD Conversion using XRL
instruction
.
SOFTWARE REQURIED:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ASCII TO PACKED BCD

ORG 0000H
LJMP MAIN
MAIN:MOV A,#'4'
MOV B,#'9'
XRL A,#30H
SWAP A
XRL B,#30H
ORL A,B
END

ASCII TO UNPACKED BCD

ORG 0000H
LJMP MAIN
MAIN:MOV A,#'4'
XRL A,#30H
MOV B,#'9'

IIET Page 39
MICROCONTROLLER PROGRAMMING LAB

XRL B,#30H
END

RESULT:
An ALP for convert ASCII to BCD Conversion is studied.

IIET Page 40
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 13(a)
UP/DOWN HEX-DECIMAL COUNTER

AIM: To Write an assembly language program to implement UP/DOWN hexadecimal counter-


(program should check value @R0=0x30,if 0x30=0 then up counter else down counter ).

APPARATUS:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:
ORG 0000H
LJMP MAIN
MAIN:MOV R0,#30H
MOV @R0,#00H
CJNE @R0,#00H,DOWN
UP:MOV A,#00H
L1:INC A
CJNE A,#0FH,L1
LJMP MAIN
DOWN:MOV A,#0FH
L2:DEC A
CJNE A,#00H,L2
LJMP MAIN
END

IIET Page 41
MICROCONTROLLER PROGRAMMING LAB

EXPECTED OUTPUT :

R0=00h if UP-COUNTER,
Else (change R0 with #01h) for down counter

RESULT:
An ALP for UP/DOWN hexadecimal counter is studied.

IIET Page 42
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 13(B)
UP/DOWN BCD COUNTER

AIM: To Write an assembly language program to implement UP/DOWN BCD counter should
be check @R0=0x30. If 0x30=0 then up count else down count.
APPARATUS:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:
ORG 0000H
LJMP MAIN
MAIN:MOV R0,#30H
MOV @R0,#01H
CJNE @R0,#00H,DOWN
UP:MOV A,#00H
L1:MOV R5,A
ADD A,#01H
DA A
LJMP L1
DOWN:MOV A,#99H
L2:MOV R5,A
ADD A,#99H
DA A
LJMP L2
LJMP MAIN

IIET Page 43
MICROCONTROLLER PROGRAMMING LAB

END

EXPECTED OUTPUT :

R0=00h if UP-BCD COUNTER, Else (change R0 with #01h) for down BCD counter
Compare R0- down counter start from 99H TO 00H.
Else up counter start from 00H TO 99H.

RESULT:
An ALP for UP/DOWN BCD counter is studied.

IIET Page 44
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 14(A)
DELAY USING INSTRUCTIONS

AIM: To write and execute an ALP for delay using instructions.


APPARATUS:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

ALGORTHIM:
1. Start
2. Load A as 00H.
3. Long call to back.
4. Move 0FH to R0.
5. Compare R0 with 00h.
6. Loop continue until R0 is 00h.
7. Accumulator value start from 1 to 6.
8. End.

PROGRAM:

ORG 0000H
LJMP MAIN
MAIN:
MOV A,#00H
BACK: LCALL DELAY
INC A
CJNE A,#06H BACK
MOV R0,#0FH
HERE: DJNZ R0,HERE
RET: LCALL

IIET Page 45
MICROCONTROLLER PROGRAMMING LAB

END

RESULT:
An ALP for delay using instructions is studied.

APPLICATIONS:
By this student will be knowing how to Boolean and logical instruction.
EXTENSION:
By simple modification we can executed many programs UP/DOWN decimal counter

VIVA QUESTIONS:
1. Why do we require a reset pin?
2. How can you enable/disable some or all the interrupts?
3. Explain how interrupt priorities are set? And how interrupts that occur simultaneously are
handled.
4. What Events can trigger interrupts, and where do they go after getting triggered?
5. What are the actions taken when an Interrupt Occurs?

IIET Page 46
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 14(B)
DELAY USING TIMERS

AIM: To write and execute an ALP for delay using timers


.
APPARATUS:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:

ORG 0000H

BACK:ACALL DELAY

LJMP BACK

DELAY:MOV TMOD,#01H

MOV R0,#0FH

LOOP:MOV TH0,#0FFH

MOV TL0,#0FEH

SETB TR0

HERE:JNB TF0,HERE

CLR TR0

CLR TF0

DJNZ R0,LOOP

IIET Page 47
MICROCONTROLLER PROGRAMMING LAB

RET

END

EXECPTED INPUTS AND OUTPUTS:

R0=0FH, DEC=R0 TO 00H


WHEN TF0=1, DECREMENT R0 UPTO 00H
TIME DELAY IS 16

RESULT:
An ALP for implement a delay using timers is studied.

IIET Page 48
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 15

LOGICAL OR DELAY LOOP USING CALL & RETURN INSTRUCTIONS

AIM: To write and execute an ALP for delay using CALL & RETURN instructions
.
APPARATUS:

SNO NAME QUANTITY


1 Keil/EDSIM software 1
2 Window Xp operating system 1

PROGRAM:
ORG 0000H
BACK:
MOV A,#0CH
MOV P2,A
MOV A,#00H
MOV P2,A
LCALL PORTCHANGE
SJMP BACK
PORTCHANGE:
MOV A,#0FH
MOV P1,A
MOV A,#00H
MOV P1,A

IIET Page 49
MICROCONTROLLER PROGRAMMING LAB

RET
END
RESULT:
An ALP for implement a delay using call & return instructions is studied

PROGRAM 16
TO GENERATE A SQUARE WAVE.

AIM: Write a C program for generating a square wave of 50 Hz on pin 1.2.


Assume crystal frequency is 11.0592 using timer.

SOFTWARE REQURIED:
SNO NAME QUANTITY
1 keil software 1

ALGORTHIM:
1. Start.
2. Write the program in keil .
3. Compile and debug
4. Satisfy the output.
5. Create the exectable file.
6. By using unipro software emulate the burn into AT89C51.
7. Test the output on target,
8. Stop.

IIET Page 50
MICROCONTROLLER PROGRAMMING LAB

FLOW CHART:

start

Write the program in keil

Errors-0
Warnings-0

Desire
d
output

Create exectable HEX file

Using unipro burn into the chip

Test the ouput on target

stop

IIET Page 51
MICROCONTROLLER PROGRAMMING LAB

CIRCUIT DIAGRAM:

PROGRAM

#include<regx51.h>
delay(viod);

int main(void)
{

P1_5=0;
while(1)
{
TMOD=0x01;
TH0=0xFF;
TL0=0xF2;
delay(1000);
}

IIET Page 52
MICROCONTROLLER PROGRAMMING LAB

delay(void)
{
TR0=1;
while(TF0!=1);
P1_5=~P1_5;
TF0=0;
TR0=0;
}

OUTPUT

IIET Page 53
MICROCONTROLLER PROGRAMMING LAB

RESULT:

Generating a square wave is studied.

APPLICATIONS:
By this student will be knowing how to to generate square wave.

EXTENSION:
By simple modification we can executed many programs like interface LCD display to
generate square wave.
VIVA QUESTIONS:
1. What is the significance of DA instruction?
2. What is packed and unpacked BCD?
3. What is the difference between CY and OV flag?
4. When will the OV flag be set?
5. What is an ASCII code?

IIET Page 54
MICROCONTROLLER PROGRAMMING LAB

BEYOND THE SYLLABUS


PROGRAM 17
TO TOGGLE THE PORTS WITH SPECIFIED TIME INTERVAL

AIM: To interface the toggle the ports with the specified time interval.
SOFTWARE REQURIED:
SNO NAME QUANTITY
1 keil software 1
2 8051 microcontroller kit for interfacing 1
3 Window Xp operating system 1
4 Target board 1
5 Unipro programmer kit 1
6 Unipro software 1
ALGORTHIM:
1. Start.
2. Write the program in keil .
3. Compile and debug
4. Satisfy the output.
5. Create the exectable file.
6. By using unipro software emulate the burn into AT89C51.
7. Test the output on target,
8. Stop.

IIET Page 55
MICROCONTROLLER PROGRAMMING LAB

FLOW CHART:

start

Write the program in keil

Errors-0
Warnings-0

Desire
d
output

Create exectable HEX file

Using unipro burn into the chip

Test the ouput on target

stop

PROGRAM:

#include<reg51.h>
#include<stdio.h>
void delay(unsigned int count);
int main(void)
{
while(1)

IIET Page 56
MICROCONTROLLER PROGRAMMING LAB

{
P0=0XAA;
delay(1000);
P0=0x55;
delay(1000);

P3=0x44;
delay(1000);
P3=0x88;
delay(1000);
}
}
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<109;j++);
}

IIET Page 57
MICROCONTROLLER PROGRAMMING LAB

OUTPUT

RESULT:
Toggle the ports with specified time interval has been studied.

APPLICATIONS:
By this student will be knowing how to toggle the ports with specified time interval
EXTENSION:
By simple modification we can executed many programs like interface LCD display to
generate square wave.

VIVA QUESTIONS:
1. Describe the Interrupt Enable (IE) special function register and its various bits.
2. Describe the Interrupt Priority (IP) special function register and its need.
3. Explain in detail how the Timer Flag interrupts are generated.

IIET Page 58
MICROCONTROLLER PROGRAMMING LAB

PROGRAM 18
TO DISPLAY MESSAGE

AIM: To display message on given address, in memory window.


SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 8051 microcontroller kit for interfacing 1
3 Window Xp operating system 1

PROGRAM:

#include<reg51.h>
#include<stdio.h>
void delay(unsigned int count);
data unsigned char xdata *name=0x1600;
int main(void)
{
while(1)
{
int i=0;
char a[30]="INDUR INISTITUTE OF ENGG &TECH";
for(i=0;i<30;i++)
{
*name=a[i];
name++;
delay(1000);
}
for(i=30;i>0;i--)
{
*name='*';
name--;
delay(1000);
}
}
}
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<109;j++);
}

IIET Page 59
MICROCONTROLLER PROGRAMMING LAB

OUT PUT:

RESULT:
Displaying the message has been studied.
APPLICATIONS:
By this student will be knowing how for Displaying the message.
EXTENSION:
By simple modification we can executed many programs like interface LCD display to
generate square wave.

VIVA QUESTIONS:
IIET Page 60
MICROCONTROLLER PROGRAMMING LAB

1. Why the Reset interrupt is called a non-maskable interrupt?


2. Why do we require a reset pin?
3. How can you enable/disable some or all the interrupts?
4. Explain how interrupt priorities are set? And how interrupts that occur simultaneously are
handled.
5. What Events can trigger interrupts, and where do they go after getting

IIET Page 61
MICROCONTROLLER PROGRAMMING LAB

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
2. “MSP430 Microcontroller Basics”, John Davies, Elsevier, 2010
3. “The 8051 Microcontroller Architecture, Programming & Applications”, 2e Kenneth J.
Ayala ;Penram International, 1996 / Thomson Learning 2005.
4. “The 8051 Microcontroller”, V.Udayashankar and MalikarjunaSwamy, TMH, 2009
5. “Microcontrollers: Architecture, Programming, Interfacing and System Design”,Raj Kamal,
“PearsonEducation,2005.

IIET Page 62

You might also like