0% found this document useful (0 votes)
472 views26 pages

Microcontroller Lab Manual

This document contains an introduction and table of contents for a microcontroller lab manual. The table of contents lists topics that will be covered, including data move instructions, arithmetic instructions, logical instructions, jump and call instructions, interrupts, seven segment and keyboard interfacing, and LCD interfacing. Sample programs and exercises are provided for several of the topics to demonstrate how to program the microcontroller and utilize different instructions.

Uploaded by

ssmtejani2
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
472 views26 pages

Microcontroller Lab Manual

This document contains an introduction and table of contents for a microcontroller lab manual. The table of contents lists topics that will be covered, including data move instructions, arithmetic instructions, logical instructions, jump and call instructions, interrupts, seven segment and keyboard interfacing, and LCD interfacing. Sample programs and exercises are provided for several of the topics to demonstrate how to program the microcontroller and utilize different instructions.

Uploaded by

ssmtejani2
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 26

MICROCONTRELLER LAB MANUAL

Table of Contents
1. Introduction to Simulator 2. Data Move Instructions MOV, MOVX, MOVC 3. Introduction to Kit / Arithmetic Instructions 4. Bit level Programming 5. Logical instruction
6.

Jump and Call Instructions----- Short and Long jumps

7. Interrupts 8. Seven Segment and Keyboard interfacing 9. LCD interfacing

MICROCONTRELLER LAB MANUAL

Introduction To

Simulator
Simulator usually shows the internal register and memory location on the screen of the personal computer and allows the programmer to perform all of the operations. One can also watch the data change as the program operates. This saves considerable time because the register and memory contents do not have to be displayed using separate monitor commands. The visual representation also gives the programmer better feel for what is taking place in the program. The 8051 simulator runs on IBM PC and compatible computers and the requirements for the PC are : 512 K RAM DOS version 2.1 or newer IBM mono, CGA, EGA compatible monitor Two disk drives How to Simulate a Program: 1. 2. 3. 4. 5. 6. Go to the command prompt and type a51<filename.asm> Go to the command prompt and type s51 Type L for Load Type P for previous machine Then type book.bss Again type L for load 7. Then type O for object file <filename.obj> 8. Now press R to run To run a simulation the screen set file is loaded into the simulator first followed by programming the object code format. The program is then run using this simulator commands: Reset the program counter(PC) to 0000H Single step the program Free run the program Free run until breaking is reached Stop free run
2

MICROCONTRELLER LAB MANUAL

Screen 1: (ALT-F1) The main screen The viewer can observe the operations of the PC,SP,IE and A registers and ports P1 and P3 in individual windows. Special function registers DPL,DPH,PCON,TMOD,TL0. TL1,TH0 and TH1 can be found in the internal RAM window 2 which displays a portion of the SFR area. Internal RAM window 1 shows registers banks 0 and 1. An instruction execution window will display program mnemonics as the program is operated. Screen 2: (ALT-F2) The internal RAM Screen Internal RAM windows 3 to 5 display internal RAM from 10h to 3fh. Window 6 shows the SFR area, which includes SCON and SBUF. The SP, DPTR, A and PC are also shown. Screen 3: (ALT-F3) The code ROM Screen Program code addresses from 0000h to 00BFh are displayed in code memory windows1 to 3. The PC,A,DPTR and instruction execution windows are also part of this screen. Screen 4: (ALT-F4) The external RAM screen External RAM from addresses 0000h to 00BFh are displayed in external data memory windows 1 to 3. The PC, A, DPTR and instruction execution windows are also part of this screen. Exercise:1. Try to assemble any set of instructions (detailed idea of the instructions not expected) and also simulate the program. Also observe the .lst, .obj and .exe file. 2. Define Machine language Assembly language Lower level language Higher level language Assembler Linker Cross compiler Interpreter Simulator
3

MICROCONTRELLER LAB MANUAL

2. Transfer ction
Objectives:-

Data Instru

1) To understand the data transfer operations in the 8051. 2) To understand how 8051 executes a program-using simulator. Sample program: mov mov mov mov mov mov a,#10h b,#10h r0,a r1,#10h r1,r2 10h,#FFh

Modifications: 1) Check all the instruction whether they are valid or not. 2) Rewrite the instructions, which are invalid so as to provide the same function. 3) Find out the total length of the program, also write the addresses of the individual instruction assuming the initial address in number of bytes of the program after correction. 4) Write the function of each instruction used in the program. 5) Modify the above program to reduce its size. Exercise: Write down the hex code for the instructions and differentiate opcodes and operands. What is size of registers in program? What will be the size of the date bus accordingly? What will be the contents of the PC at the instruction mov r0,a ? Mention addressing mode of each instruction.
4

MICROCONTRELLER LAB MANUAL

Write down the sequence of steps for the first instructions from fetching it from memory to its execution in the CPU. Write instructions to transfer the value 10H to internal RAM location 8H,external ram location 0008H and 0100H. Write an instruction or a set of instructions to transfer a byte from internal RAM location 7H to external RAM location 0007H, 0010H and 0100H. Do inverse of program 8. Write a program to transfer byte from code memory to internal RAM and external RAM. Write a program to transfer byte from external RAM to external ROM. Write a program to transfer byte from internal RAM to internal ROM.

MICROCONTRELLER LAB MANUAL

3. Arithmetic Instructions
Objectives: 1) To understand arithmetic operation of the 8051 and their limitation. 2) To follow the instruction execution at machine cycle level. Sample Program:mov 3Fh,#20h mov 40h,#30h mov a,3fh and a,40h mov 41h,a mov 42h,#00h mov a,#00h addc a,42h mov 42h,a Modifications:1) 2) 3) 4) 5) Identify the function of the program. Reduce the size of the program Length wise Machine cycle wise State the function of addc function.

MICROCONTRELLER LAB MANUAL

Exercise: Write a program to perform addition/subtraction of two 32-bit numbers. Store the result with carry/borrow. Write a program to reverse the elements of an array. Write a program to count number of positive negative and zero elements of an array of eight bit signed elements. Do signed additions. -1d + 27d, 100d + 50d, 45d +75d, -30d + (-50d), -70d + (-70d). Write a program to Mul, Div and DAA (BCD addition) of two numbers.

MICROCONTRELLER LAB MANUAL

4. ctions
Objectives:

Logic Instru

1) To understand arithmetic operation of the 8051 and their limitation. 2) To follow the instruction execution at machine cycle level Sample program; mov a,#21h mov r0,a movc a,@a+dptr mov r1,a mov a,r0 swap a mov r0,a movc a,@a+dptr anl a,#0fh anl 01h,#0fh movx @ro,a mov a,r0 swap a anl dph,#00h mov dpl,a mov a,r1 movx @dptr,a Modifications: 1) What is the function of above program ? 2) Modify the above program to complement the lower nibble and set the higher nibble to 1111 in the bytes of the code memory. Store the resulting bytes in same external RAM location.

MICROCONTRELLER LAB MANUAL

Exercise: Write a program to find parity of a 16 bit number. Write a program to transfer the LSB of 4 memory locations starting from 0f00H in the code memory in lower nibble of R0. Mask of the upper nibble of R0 by ones.

MICROCONTRELLER LAB MANUAL

5. Level Instructions
Objective:

Bit

1). To compare the effects of bit level and byte level operations on various elements of the 8051. Sample Program:mov a,30h clr c mov psw.4,c mov psw.3,c mov r0,#0ch mov 20h,#0eh mov c,acc.0 cpl c mov 01h,c mov a,20h orl a,r0 mov r0,a

Modifications:1) Reduce the length of the above program. 2) Rewrite the above program so as to repeat the above operation on 10 eight-bit numbers starting from 30h. Exercise: Implement function Y1 = ABC + ABC +ABC
10

MICROCONTRELLER LAB MANUAL

Y2 = ABC + ABC +ABC

Jumps & Cal ls


Objectives: 1) To develop the ability of transferring control within a program. 2) To identify the situation where a sub program can be used for helping the main program. 3) To understand the importance of stack with reference to branching. Sample program: mov r1,#00h mov dptr,#4000h movx a,@dptr mov r0,a inc dptr movx a,@dptr mov r7,a lcall check bcd jnb 00h,next inc r1 djnz ro,again inc dptr mov a,r1 movx @dptr,a sjmp same

again:

next:

same:

checkbcd: clr 00h mov a,r7 anl a,#ofh cjne a,#09h,lowhigh sjmp limit lowhigh: jnc back limit: mov a,r7
11

MICROCONTRELLER LAB MANUAL

anl a,#0f0h swap a cjne a,#09h,final Setone: setb 00h Back: ret Final: jnc back sjmp setone .end Modifications: 1. Identify the function of the program. 2. Reduce the size of the program Length wise Machine cycle wise Exercise: Rewrite the subroutine using jc instruction. Rewrite the main program using jb instruction An array of BCD numbers starts from 4001h in memory. End of the array is detected by a non-BCD number. Count the number of elements in array. Write a program to transfer 10 bytes from external RAM location 30H onwards to 300H onwards. Write a program to transfer 10 bytes from internal RAM 30H onwards to external RAM 30H onwards. Write a program using procedure to find the number of one in dptr.

12

MICROCONTRELLER LAB MANUAL

7.
Objectives:

Interrupts

1) To understand the concept of interrupts and distinguish normal subroutine and service routine. 2) To realize hardware requirements for using external interrupt in place of internal interrupt. Sample program: .org 0000h mov r0,#00h mov tl0,#3ch mov th0,#5dh mov tmod,#01h mov ie,#82h setb tr0 here: cjne r0,#20h,here cpl p1.0 mov r0,#00h sjmp here .org 000bh inc r0 mov tl0,#3ch mov th0,#5dh reti .end

13

MICROCONTRELLER LAB MANUAL

Modification: 1) Modify the above program such that you write a delay routine of 0.5ms, which is called 100 times which in turn is called 20 times. Exercise: Write the program to generate a pure software delay of one second demonstrate on an oscilloscope. Also mention the setting of various SFRs. Interface a push button switch at one of the external interrupt pins. Write an assembly program such that every time you push the button your accumulator is incremented by one and the content is transferred to an external memory location. Also see what all problems you are facing. Now interface the push button with keydebouncing circuit and see the difference between the two. Note:- Crystal frequency = 10 Mhz.

14

MICROCONTRELLER LAB MANUAL

8. And Segment

Keyboard Seven Interfacing

Objective: 1) To develop a program based on available hardware to utilize keyboard and seven segment display devices. 2) To understand the operation of keyboard matrix. Sample Program: repeat: .org 0000h mov p0,#0ffh mov p1,#0ffh mov p3,#00h ; first bit is used to provide the ground mov 20h,#00h ; to common cathode seven segment mov p2,#00000001b clr p1.0 setb 20h lcall routine setb p1.0 clr p1.1 clr 20h setb 21h lcall routine setb p1.1 clr p1.2 clr 21h setb 22h lcall routine setb p1.2
15

MICROCONTRELLER LAB MANUAL

clr p1.3 clr 22h setb 23h lcall routine setb p1.3 clr p1.4 clr 23h setb 24h lcall routine setb p1.4 ljmp repeat routine: mov a,p0 mov r0,a cjne a,#0ffh,check last: ret check: jb acc.0,col1 jnb 20h,row01 mov p2,#00001100b ljmp last row01: jnb 21h,row02 mov p2,#11111010b ljmp last row02: jnb 22h,row03 mov p2,#11011110b ljmp last

; display pattern for 1

; display pattern for 6

; display pattern for A ; display pattern for b

row03: mov p2,#11111000b ljmp last col1: jb acc.1,col2 jnb 20h,row11 mov p2,#10110110b ljmp last

; display pattern for 2

16

MICROCONTRELLER LAB MANUAL

row11: jnb 21h,row12 mov p2,#00001110b ljmp last row12: jnb 22h,row13 mov p2,#10111100b ljmp last

; display pattern for 7

; display pattern for d ; display pattern for E

row13: mov p2,#11110010b ljmp last col2: jb acc.2,col3 jnb 20h,row21 mov p2,#00111110b ljmp last row21: jnb 21h,row22 mov p2,#11111110b ljmp last row22: jnb 22h,row23 mov p2,#11011111b ljmp last

; display pattern for 3

; display pattern for 8

; display pattern for A. ; display pattern for b.

row23: mov p2,#11111001b ljmp last col3: jb acc.3,last jnb 20h,row31 mov p2,#11001000b ljmp last row31: jnb 21h,row32 mov p2,#11011110b ljmp last row32: jnb 22h,row33 mov p2,#10111101b ljmp last row33:

; display pattern for 4

; display pattern for 9

; display pattern for d. ; display pattern for E.


17

mov p2,#11110011b

MICROCONTRELLER LAB MANUAL

ljmp last .end Exercise: 1) Find out the output of the program. 2) Now modify the program and interface two seven segments and continuously scroll GOD over it.

18

MICROCONTRELLER LAB MANUAL

9. erfacing
Objective:

LCD Int

1) To study the LCD interfacing in the available hardware using various commands. 2) To write a program for displaying desired information on the LCD Sample Program 1: .org 0000h clr p1.3 lcdisp: jb p3.0,lcdisp ;wait for key

mov a,#3ch ;8bits, 2 rows, 5*10 acall command lcall delay here0: jb p3.0,here0 mov a,#0f0h ;screen on, cursor on, blinking acall command lcall delay ; herex: jb p3.0,herex mov a,#0b0h ;cursor off acall command lcall delay ; here6: jb p3.0,here6 mov a,#070h ;no blink
19

MICROCONTRELLER LAB MANUAL

acall command lcall delay ; here1: jb p3.0,here1 mov a,#0D0h acall command lcall delay ; here5: jb p3.0,here5 mov a,#0b0h ;cursor off, screen on acall command mov a,#60h ;shift cursor right acall command lcall delay here2: jb p3.0,here2 mov a,#0e1h ;position of cursor line1,space7 acall command here8: jb p3.0,here8 ;screen off

mov a,#30h ;cursor off,blink off, screen on acall command lcall delay here9: jb p3.0,here9 mov a,#12h acall display ;display 'H'

lcall delay here3: jb p3.0,here3 mov a,#40h ;home cursor and clear memory acall command
20

MICROCONTRELLER LAB MANUAL

lcall delay here4: jb p3.0,here4 mov a,#12h

;display 'H'

acall display lcall delay here7: jb p3.0,here7 mov a,#80h ;home cursor and clear memory acall command here:sjmp here command: acall ready mov p2,a clr p1.2 ;rs clr p1.1 ;rw setb p1.0 ;cs clr p1.0 ;cs ret display: acall ready mov p2,a setb p1.2 ;rs clr p1.1 ;rw setb p1.0 ;cs clr p1.0 ;cs ret ready: clr p1.0 ;cs mov p2,#0ffh clr p1.2 ;rs setb p1.1 ;rw wait: clr p1.0 ;cs setb p1.0 ;cs jb p2.0,wait clr p1.0 ;cs ret

;bsy

21

MICROCONTRELLER LAB MANUAL

delay: delay1: delay2: delay3: delay4: djnz djnz ret .end

nop mov r6,#0ah mov r5,#0ffh mov r4,#0ffh djnz r4, delay4 r5,delay3 r6,delay2

Sample Program 2: .org 0000h clr p1.3 lcdisp: jb p3.0,lcdisp ;wait for key

mov a,#3ch ;8bits, 2 rows, 5*10 acall command lcall delay here0: jb p3.0,here0 mov a,#0f0h ;screen on, cursor on, blinking acall command mov a,#60h ;cursor shift acall command mov a,#12h acall display mov a,#10100010b acall display mov a,#00110010b ;display 'H' ;display 'e' ;display 'l'
22

MICROCONTRELLER LAB MANUAL

acall display mov a,#00110010b acall display ;display 'l'

mov a,#11110010b acall display lcall delay here1: jb p3.0,here1

;display '0'

mov a,#80h ;clear screen acall command mov a,#0a0h ;screen shift acall command mov a,#12h acall display mov a,#10100010b acall display mov a,#00110010b acall display mov a,#00110010b acall display mov a,#11110010b acall display lcall delay here4: jb p3.0,here4 mov a,#0e0h ;screen shift(right) acall command here5: jb p3.0,here5
23

;display 'H' ;display 'e' ;display 'l' ;display 'l' ;display '0'

MICROCONTRELLER LAB MANUAL

mov a,#0a0h ;screen shift(left) acall command

here3:

jb p3.0,here3

mov a,#0a0h ;screen shift(left) acall command lcall delay here2: jb p3.0,here2 mov a,#80h ;clear screen acall command mov a,#30h ;screen on, cursor off, blinking off acall command mov a,#60h ;cursor shift acall command mov a,#12h acall display mov a,#10100010b acall display mov a,#00110010b acall display mov a,#00110010b acall display mov a,#11110010b acall display here: ljmp here ;display 'h' ;display 'e' ;display 'l' ;display 'l' ;display 'o'

24

MICROCONTRELLER LAB MANUAL

command: acall ready mov p2,a clr p1.2 ;rs clr p1.1 ;rw setb p1.0 ;cs clr p1.0 ;cs ret display: acall ready mov p2,a setb p1.2 ;rs clr p1.1 ;rw setb p1.0 ;cs clr p1.0 ;cs ret ready: clr p1.0 ;cs mov p2,#0ffh clr p1.2 ;rs setb p1.1 ;rw wait: clr p1.0 ;cs setb p1.0 ;cs jb p2.0,wait clr p1.0 ;cs ret delay: delay1: delay2: delay3: delay4: djnz djnz ret .end

;bsy

nop mov r6,#0ah mov r5,#0ffh mov r4,#0ffh djnz r4, delay4 r5,delay3 r6,delay2

25

MICROCONTRELLER LAB MANUAL

Exercise: Find out the output of the programs. Now modify the program to display two words in two screens and swap them.

26

You might also like