Sie sind auf Seite 1von 16

BASIC MICRO CONTROLLER PROGRAMS (Experiments 9-12)

PROCEDURE TO ENTER AND EXECUTE PROGRAMS ON 8051 MICROCONTROLLER


KIT

• Programs can be entered from 8000H and above up to 0FFFFH

• Separate memory is allocated for program and data storage. (MD 9000
refers to data memory address and MP 9000 refers to program memory
address.)

• TO ENTER PROGRAM IN HEX CODE

Use `M` (Modify/Display/Move) command along with its memory type


and address of memory location.
>MP Starting Address press < Enter Key>
Ex: >MP 8000 press < Enter Key>
Type the hex codes at each location and press the enter key. After typing
last instruction hex code, press <RESET> to exit from the entry process.

• TO ENTER PROGRAM IN MNEMONICS

• Use `A`(Assembly) command followed by the starting address.


>A Starting Address press < Enter Key>
Ex: >A 8000 press < Enter Key>
Type the hex codes at each location and press the enter key. After typing
last instruction hex code, press <RESET> to exit from the entry process.

• For converting hex codes to mnemonics - use Z(Disassembly) command


>Z Starting Address press < Enter Key>
Ex: >Z 8000 press < Enter Key>

• TO EXECUTE THE PROGRAM


1. Use `J` (Jump) command followed by starting address of the program
>J Starting Address press < Enter Key>
>J 8000 press < Enter Key>

2. Use `G` (Go) command followed by starting address of the program


>G Starting Address press < Enter Key>
• TO VIEW THE RESULTS
• `M` command for memory content

• `R` command for register contents

>MD data memory address press < Enter Key>


>R [Register] press < Enter Key>
Note: To view register contents use LJMP 3 as terminating instruction in the program
instead of LJMP 0.

PROGRAM 9.A

QUESTION: Programs using arithmetic, logical and bit manipulation instructions of


8051

AIM: Write an Assembly Language Program to add, subtract, multiply and divide the
bytes available in internal RAM locations 30H & 31H. Verify the results in register R3 for
addition and register R4 for subtraction.

EQUIPMENT REQUIRED:
8051 Microcontroller trainer kit
Power supply and Keyboard
PROGRAM:
ADDRESS HEXCODE MNEMONIC

8000 MOV A, 30
MOV R1, A
MOV R2, 31

ADD A, R2

MOV R3, A

MOV A, R1

SUBB A, R2

MOV R4, A

LJMP 3

THEORETICAL RESULT:

79 H + 5C H = 0D5 H

79 H – 5C H = 1D H

PRACTICAL RESULT:
To enter inputs in internal RAM use the command

MI Internal RAM address

MI 30

30 00 79

31 00 5C

To verify the outputs from external RAM use the command

R register name

R R3

R3 D5

R R4

R4 1D

PROGRAM 9.B

8-BIT MULTIPLICATION

AIM: Write an Assembly Language Program to multiply two data bytes (13H and 49H).
Store the result from 6000 onwards.

EQUIPMENT REQUIRED:
8051 Microcontroller trainer kit
Power supply
Keyboard

PROGRAM:

ADDRESS HEXCODE MNEMONIC

8500 MOV A#13


MOV 0F0, #49
MUL AB
MOV DPTR, #6000
MOVX @DPTR, A
MOV A, 0F0
INC DPTR
MOVX @DPTR, A
LJMP 0
THEORETICAL RESULT:

13 H X 49 H = 056B H

PRACTICAL RESULT:

Data bytes are entered immediately in the main program.

To verify the outputs from external RAM use the command

MD RAM address

MD 6000

6000 6B (Lower Byte of result)


6001 05 (Upper Byte of result)

PROGRAM 9.C

8-BIT DIVISON

AIM: Write an Assembly Language Program to divide data stores at external RAM
locations 2000H and 2001H and verify the result from registers.

EQUIPMENT REQUIRED:
8051 Microcontroller trainer kit
Power supply
Keyboard
PROGRAM:

ADDRESS HEXCODE MNEMONIC

9000 MOV DPTR, #2000


MOVX A, @DPTR
MOV A, 0F0
INC DPTR
MOVX A, @DPTR
DIV AB
LJMP 3

THEORETICAL RESULT:

0AD H / 12 H = 0B (Reminder) and 09 (Quotient) H

PRACTICAL RESULT:

Load data inputs (0ABH and 12H) at external RAM addresses 2000H and
2001H.

MD RAM address

MD 2000

2000 0AD
2001 12
Verify the outputs from registers A and B, use the command R. A holds
quotient and B with reminder.

VIVA QUESTIONS FOR PROGRAM 9

• What is a micro controller?

• What are the advantages of built in peripherals in microcontrollers?

• What is the purpose for which 8051 is designed?

• Write the features of 8051?

• What is the length of address and data bus in 8051 Microcontroller?

• What are the timers in 8051 microcontroller?

• Write the operations of TMOD register?

• Which mode of timer/counter is called 13-bit counter?


• What is Interrupt Service Routine?

• What are the interrupts in 8051?

PROGRAM 10

8051 TIMER PROGRAMMING

AIM: Write an assembly language program to set timer 0 to provide 5 Seconds time
delay while displaying decimal numbers 0 to 10 on LCD Screen.

EQUIPMENT REQUIRED:

8051 Microcontroller trainer kit


Power supply
Keyboard
Note: For this program timer 0 is used to produce time delay of 5 seconds.
.
Given fosc = 11.0592 MHz

fin = fosc/ 12 = 0.9216 MHz Tin = 1.085 s

n = Delay / Tin = 5 X 10-3 / 1.085 X 10-6 x 65536

= 5 X 10-3 / 71.11 X 10-3 70

If timer 0 is used in mode 1 it will work as a 16-bit timer and count 65536
internal clock pulses by incrementing TH0 & TL0 register values from 0000H to FFFFH

Hence the time delay produced by one timer action is

1.085 X 10-6 Sec x 65536 = 71.11msec

to get 5 Seconds time delay the timer has to run for 70 times.

TH0 8C 00 TL08A 00

TMOD 89 01H Gate C/T’ M1 M0 Gate C/T’ M1 M0


0 0 0 0 0 0 0 1

TCON 88 10H TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
0 0 0 1 0 0 0 0
03B1H is the address of a subroutine that clears LCD monitor
139EH is the starting address of subroutine that displays a number on the LCD.
The number should be loaded in RAM location 71H
9600H is the starting address of delay subroutine

PROGRAM:

ADDRESS HEXCODE MNEMONIC

9000 MOV R2, #00


RPT: LCALL 03B1
MOV A, R2
MOV 71, A
LCALL 139E
LCALL 9600
MOV A, R2
ADD A, #01
DA A
MOV R2, A
CJNE R2, #11, RPT
LJMP 0
9600 MOV R7, #46
MOV 8A, #00
MOV 8C, #00
MOV 89, #01
MOV 88, #10
Self: JNB 8D, SELF
RET
THEORETICAL RESULT:

In 8051 MICROCONTROLLER 1 M Cycle = 12 T states


1 T state = 1.085 sec

To get 5 sec time delay count n should be


So the timer1 consumed 65536 clock pulses by incrementing from 0000H to
FFFFH. and the timer repeatedly turned ON by setting its run bit to 1 for 70 times (=
46H loaded in R7) to get the time delay of 5 seconds in between the display of numbers

PRACTICAL RESULT:

Number 0 to 10 got displayed with an in between delay of 5 seconds.

VIVA QUESTIONS FOR PROGRAM 10

• What are the timers in 8051 microcontroller?

• Write the operations of TMOD register?

• Write the modes of operations of timers

• Which mode of timer/counter is called 13-bit counter?

• Explain the mode 2 in timer?

• Which mode of operation establishes TL and TH as two separate counters?

• Write the two operations of TCON register?

• Indicate the effect of following in 8051 instructions: MOV TMOD, #00010000B.

• Perform the following operation using bit addressable instructions: start Timer1

• How to use 8051 timers as counters

PROGRAM 11

8051 INTERRUPT PROGRAMMING

AIM: Write an assembly language program to transfer data bytes from register to
memory in a continuous manner. Program timer1 interrupt to stop the controller from
data transfer after 100 uSec time delay. Find how many data bytes transferred from
register to memory with in 100 uSec.

EQUIPMENT REQUIRED:

8051 Microcontroller trainer kit


Power supply
Keyboard
THEORY:

For this program timer1 is used to produce an interrupt offer 100sec.

fosc = 11.0592 MHz


fin = fosc/ 12 = 0.9216 MHz
Tin = 1.085 s
n = Delay / Tin = 100 X 10-6 / 1.085 X 10-6 = 92.16 92(10) = 52H

To count 92 clocks TH1 & TL1 should be loaded with TH1 :TL1 = FFFF – 52H= FFADH

TH1 8D FF TL18B AD

TMOD 89 10H Gate C/T’ M1 M0 Gate C/T’ M1 M0


0 0 0 1 0 0 0 0

TCON 88 40H TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
0 1 0 0 0 0 0 0

IE 0A8 88H EA - - ES ET1 EX1 ET0 EX0


0 0 0 0 1 0 0 0

IP 0B8 08H - - - PS PT1 PX1 PT0 PX0


0 0 0 0 1 0 0 0

Interrupt vector address for timer 1 is 001BH.

At 001B address, binary code for LJMP FFF6 is already loaded by manufacturer.

So write the ISR program at an address FFF6.

PROGRAM:

ADDRESS HEXCODE MNEMONIC

8500 MOV 8B, #0AD


MOV 8D,#0FF
MOV 0A8,#88
MOV 0B8,#08
MOV 89,#10
MOV DPTR,#9000
MOV A,#00
MOV 88, #40
BACK: MOVX @DPTR, A
INC A
INC DPTR
SJMP BACK
• ISR Program

FFF6 LJMP 3
FFF9 RET I

THEORETICAL RESULT:

In 8051 Microcontroller 1 M Cycle = 12 T states 1 T state = 1.085 sec

To get 100 sec time delay count n should be n= 100 sec / 1.085 sec =92

The below four instructions take 7 MC in one time loop execution

BACK: MOVX @DPTR, A – 2MC

INC A 1 MC

INC DPTR - 2 MC

SJMP BACK – 2 MC

Microcontroller consume 92 T- STATES (Clock cycles) during 100 sec time delay
in the form of incrementing from 0 to B to repeat the loop for 12 times
[12 (2+1+2+2)+3]+ 4 = 87 + 4 =91.

PRACTICAL RESULT:

Input 00 is entered in the main program immediately into A

To verify the outputs from external RAM use the command MD RAM Address

MD 9000

9000 00
9001 01
9002 02
9003 03
9004 04
9005 05
9006 06
9007 07
9008 08
9009 09
900A 0A
900B 0B

To Verify the register output use the command R

• R A

A-0B

• R DPTR

DPTR- 900B
VIVA QUESTIONS FOR PROGRAM 11

• Write about the interrupts in 8051 serviced?

• What is the advantage of interrupt over polling method?

• What is Interrupt Service Routine?

• What are the interrupts in 8051?

• How can the interrupts in 8051 be enabled or disabled?

• Write about nested interrupts?

• What is the interrupt priority in 8051

• Write the vector addresses of 8051 interrupts

• Draw and explain the format of IP register

• Draw and explain the format of IE register


PROGRAM 12

IMPLEMENTATION OF SERIAL COMMUNICATION BETWEEN PC & 8051

PROCEDURE TO EXECUTE PROGRAMS IN SERIAL COMMUNICATION:


• Connect the 8051 Micro controller to the Personal computer, serially using RS-
232C Serial interfacing cable.
• Keep the DIP switch 4 in ON position.
• Switch on the PC and select MS_DOS mode (or change to DOS prompt).
• Select the relevant directories.
• Enter the program and save the file with the extension .ASM
• Assemble the program by using the command X8051 file name.asm.
• In this process the assembly language errors are checked, and if there is no
errors .ASM file is converted to .OBJ file.
• OBJ file consists of the machine equivalent for the assembly language program.
• Machine equivalent is converted to Hexadecimal in order to make compatible
with micro controller. For this conversion a command LINK51 filename. obj is
used.
• In this .OBJ file is converted to .HEX file. At the same time a .LST is also created
which is a combination of ASM and HEX files.
• To change the command prompt form MS-DOS to ESA51 use XT51 filename.exe
command.
• After getting the command prompt of ESA51 (>) the hex program i.e. HEX file has
to be down loaded by pressing ctrl+D.
• Execute the program by entering J Program Starting Address and G program
Starting Address.
• Verify the result using MD Resultant Address command.

PROGRAM 12.A

DECIMAL TO ASCII CONVERSION


AIM: Write an Assembly Language Program to convert a decimal number 57 to ASCII
form 3537 using logical instructions. Store the ASCII result from 8000H.
EQUIPMENT REQUIRED:
IBM PC with X-8051 software
8051 Microcontroller trainer kit
Power supply
PROGRAM:

ADDRESS HEXCODE MNEMONIC

8100 MOV R1, #57


MOV A, R1
ANL A, #0F
ORL A, #30
MOV DPTR, #8000
MOVX @DPTR, A
MOV A, R1
MOV R2, #F0
ANL A, R2
RL A
RL A
RL A
RL A
ORL A, #30
INC DPTR
MOVX @DPTR, A
LJMP 0
THEORETICAL RESULT:

The given input 57 translated to 3537H

PRACTICAL RESULT:

Input is entered immediately in the main program.

To verify the outputs from external RAM use the command

MD RAM address

MD 8000
8500 37
8501 35

PROGRAM 12.B
SWAPPING

AIM: Write an Assembly Language Program to swap the nibbles of R0 & Accumulator so
that the Lower nibble of R0 swaps with Upper nibble of Accumulator and vice - versa.
Store the results from 6000H onwards.
EQUIPMENT REQUIRED:
IBM PC with X-8051 software
8051 Microcontroller trainer kit
Power supply

PROGRAM:

ADDRESS HEXCODE MNEMONIC

9500 MOV R0, #49

MOV A, #26
SWAP A
XCHD A,@ R0

MOV DPTR, #6000


MOVX @DPTR, A
MOV A,@R0
RL A
RL A
RL A
RL A
INC DPTR
MOVX @DPTR, A
LJMP 0

MI 49

49 00 49

THEORETICAL RESULT:

The given inputs 49H & 26H are swapped to 42H & 96H

PRACTICAL RESULT:
Inputs are entered immediately in the main program.

To verify the outputs from external RAM use the command

MD RAM address MD 5600

5600 42
5601 96

PROGRAM 12.C

COUNTING 1’S IN A GIVEN NUMBER


AIM: Write an Assembly Language Program to count the number of 1’s in a number
available in R4 and check the count value at register R2.
EQUIPMENT REQUIRED:
IBM PC with X-8051 software
8051 Microcontroller trainer kit
Power supply
PROGRAM:
ADDRESS HEXCODE MNEMONIC

8400 MOV R0, #08


MOV R4, #79
MOV R2, #00
MOV A, R4
Back: RLC A
JNC SKIP
INC R2
Skip: DJNZ R0, Back
MOV A, R2
LJMP 3
THEORETICAL RESULT:

In the given input 79H the number of 1’s is 5

PRACTICAL RESULT:

Inputs are entered in the main program immediately into R4.

To verify the outputs from registers use the command R R2


VIVA QUESTIONS FOR PROGRAM 12
• Which registers of 8051 are used to hold data and control data communication?
• Write the baud rate of serial port in mode0 and mode2?
• In which mode of serial port the baud rate is variable?
• Write the two ways in which baud rate can be doubled?
• Draw the format of SCON register and explain its functions?
• Draw the format of PCON register and explain its functions?
• What is the priority of serial port interrupt.
• Can 8051 microcontroller communicate directly with PC without using 8251?
• Write any two differences between synchronous and asynchronous
communication
• Write an instruction to load 75H to SBUF register.

Das könnte Ihnen auch gefallen