microcontroller lab manual

31
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

Upload: ssmtejani2

Post on 05-Aug-2015

243 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Microcontroller Lab Manual

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

Page 2: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

1. 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. Go to the command prompt and type a51<filename.asm>2. Go to the command prompt and type s513. Type L for Load4. Type P for previous machine 5. Then type book.bss6. Again type L for load7. 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

2

Page 3: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

Stop free run

Screen 1: (ALT-F1) The main screenThe 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 ScreenInternal 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 screenExternal 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

3

Page 4: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

Higher level language Assembler Linker Cross compiler Interpreter Simulator

2. Data Transfer Instruction

Objectives:-

1) To understand the data transfer operations in the 8051.2) To understand how 8051 executes a program-using

simulator.

Sample program:

mov a,#10hmov b,#10hmov r0,amov r1,#10hmov r1,r2mov 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.

4

Page 5: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

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. 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.

5

Page 6: Microcontroller Lab Manual

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,#20hmov 40h,#30hmov a,3fhand a,40hmov 41h,amov 42h,#00hmov a,#00haddc a,42hmov 42h,a

Modifications:-

1) Identify the function of the program.2) Reduce the size of the program

6

Page 7: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

3) Length wise4) Machine cycle wise5) State the function of addc function.

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.

7

Page 8: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

4. Logic 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 a,#21hmov r0,amovc a,@a+dptrmov r1,amov a,r0swap amov r0,amovc a,@a+dptranl a,#0fhanl 01h,#0fhmovx @ro,amov a,r0swap aanl dph,#00hmov dpl,amov a,r1

8

Page 9: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

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.

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.

9

Page 10: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

5. Bit Level Instructions

Objective:

1). To compare the effects of bit level and byte level operations on various elements of the 8051.

Sample Program:-

mov a,30hclr cmov psw.4,cmov psw.3,cmov r0,#0chmov 20h,#0ehmov c,acc.0cpl cmov 01h,cmov a,20horl a,r0mov r0,a

10

Page 11: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

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

Y2 = ABC + ABC +ABC

6. Jumps &

Calls

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

again: inc dptrmovx a,@dptrmov r7,a

11

Page 12: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

lcall check bcdjnb 00h,nextinc r1

next: djnz ro,againinc dptrmov a,r1movx @dptr,a

same: sjmp same

checkbcd: clr 00hmov a,r7anl a,#ofhcjne a,#09h,lowhighsjmp limit

lowhigh: jnc backlimit: mov a,r7

anl a,#0f0h swap a

cjne a,#09h,finalSetone: setb 00hBack: retFinal: 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.

12

Page 13: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

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.

7. Interrupts

Objectives:

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,#00hmov tl0,#3chmov th0,#5dhmov tmod,#01hmov ie,#82hsetb tr0

here: cjne r0,#20h,herecpl p1.0

13

Page 14: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

mov r0,#00hsjmp here

.org 000bhinc r0mov tl0,#3chmov th0,#5dhreti

.end

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

Page 15: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

8. Keyboard And Seven Segment

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:

.org 0000hrepeat: mov p0,#0ffh

mov p1,#0ffhmov p3,#00h ; first bit is used to provide the

ground 15

Page 16: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

mov 20h,#00h ; to common cathode seven segment

mov p2,#00000001b

clr p1.0setb 20hlcall routine setb p1.0

clr p1.1clr 20hsetb 21hlcall routinesetb p1.1

clr p1.2clr 21hsetb 22hlcall routinesetb p1.2

clr p1.3 clr 22hsetb 23h

lcall routinesetb p1.3

clr p1.4clr 23hsetb 24hlcall routinesetb p1.4ljmp repeat

routine: mov a,p0mov r0,acjne a,#0ffh,check

last: ret

16

Page 17: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

check: jb acc.0,col1jnb 20h,row01mov p2,#00001100b ; display pattern for 1ljmp last

row01: jnb 21h,row02mov p2,#11111010b ; display pattern for 6ljmp last

row02: jnb 22h,row03mov p2,#11011110b ; display pattern for Aljmp last

row03: mov p2,#11111000b ; display pattern for bljmp last

col1: jb acc.1,col2jnb 20h,row11mov p2,#10110110b ; display pattern for 2ljmp last

row11: jnb 21h,row12mov p2,#00001110b ; display pattern for 7ljmp last

row12: jnb 22h,row13mov p2,#10111100b ; display pattern for dljmp last

row13: mov p2,#11110010b ; display pattern for Eljmp last

col2: jb acc.2,col3jnb 20h,row21mov p2,#00111110b ; display pattern for 3ljmp last

row21: jnb 21h,row22mov p2,#11111110b ; display pattern for 8ljmp last

17

Page 18: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

row22: jnb 22h,row23mov p2,#11011111b ; display pattern for A.ljmp last

row23: mov p2,#11111001b ; display pattern for b.

ljmp last

col3: jb acc.3,lastjnb 20h,row31mov p2,#11001000b ; display pattern for 4ljmp last

row31: jnb 21h,row32mov p2,#11011110b ; display pattern for 9ljmp last

row32: jnb 22h,row33mov p2,#10111101b ; display pattern for d.ljmp last

row33: mov p2,#11110011b ; display pattern for E.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

Page 19: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

9. LCD Interfacing

Objective:

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:

19

Page 20: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

.org 0000h

clr p1.3lcdisp: jb p3.0,lcdisp ;wait for key

mov a,#3ch ;8bits, 2 rows, 5*10acall command

lcall delayhere0: jb p3.0,here0

mov a,#0f0h ;screen on, cursor on, blinkingacall command

lcall delay;herex: jb p3.0,herex

mov a,#0b0h ;cursor offacall command

lcall delay;here6: jb p3.0,here6

mov a,#070h ;no blinkacall command

lcall delay;here1: jb p3.0,here1

mov a,#0D0h ;screen offacall command

lcall delay;here5: jb p3.0,here5

mov a,#0b0h ;cursor off, screen onacall command

mov a,#60h ;shift cursor rightacall command

20

Page 21: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

lcall delayhere2: jb p3.0,here2

mov a,#0e1h ;position of cursor line1,space7acall command

here8: jb p3.0,here8

mov a,#30h ;cursor off,blink off, screen onacall command

lcall delayhere9: jb p3.0,here9

mov a,#12h ;display 'H'acall display

lcall delayhere3: jb p3.0,here3

mov a,#40h ;home cursor and clear memoryacall command

lcall delayhere4: jb p3.0,here4 ;

mov a,#12h ;display 'H'

acall displaylcall delay

here7: jb p3.0,here7mov a,#80h ;home cursor and clear memoryacall command

here: sjmp here

command:acall readymov p2,aclr p1.2 ;rs

21

Page 22: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

clr p1.1 ;rwsetb p1.0 ;csclr p1.0 ;csret

display:acall readymov p2,asetb p1.2 ;rsclr p1.1 ;rwsetb p1.0 ;csclr p1.0 ;csret

ready:clr p1.0 ;csmov p2,#0ffhclr p1.2 ;rssetb p1.1 ;rw

wait:clr p1.0 ;cssetb p1.0 ;csjb p2.0,wait ;bsyclr p1.0 ;csret

delay: nopdelay1: mov r6,#0ahdelay2: mov r5,#0ffh

delay3: mov r4,#0ffhdelay4: djnz r4, delay4

djnz r5,delay3djnz r6,delay2ret

.end

Sample Program 2:

22

Page 23: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

.org 0000h

clr p1.3lcdisp: jb p3.0,lcdisp ;wait for key

mov a,#3ch ;8bits, 2 rows, 5*10acall command

lcall delayhere0: jb p3.0,here0

mov a,#0f0h ;screen on, cursor on, blinkingacall command

mov a,#60h ;cursor shiftacall command

mov a,#12h ;display 'H'acall display

mov a,#10100010b ;display 'e'acall display

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

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

mov a,#11110010b;display '0'acall display

lcall delayhere1: jb p3.0,here1

mov a,#80h ;clear screenacall command

mov a,#0a0h ;screen shiftacall command

23

Page 24: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

mov a,#12h ;display 'H'acall display

mov a,#10100010b ;display 'e'acall display

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

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

mov a,#11110010b ;display '0'acall display

lcall delayhere4: jb p3.0,here4

mov a,#0e0h ;screen shift(right)acall command

here5: jb p3.0,here5

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

here3: jb p3.0,here3

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

lcall delayhere2: jb p3.0,here2

mov a,#80h ;clear screenacall command

24

Page 25: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

mov a,#30h ;screen on, cursor off, blinking offacall command

mov a,#60h ;cursor shiftacall command

mov a,#12h ;display 'h'acall display

mov a,#10100010b ;display 'e'acall display

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

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

mov a,#11110010b ;display 'o'acall display

here: ljmp here

command: acall readymov p2,aclr p1.2 ;rsclr p1.1 ;rw

setb p1.0 ;csclr p1.0 ;csret

display:acall readymov p2,asetb p1.2 ;rsclr p1.1 ;rwsetb p1.0 ;csclr p1.0 ;csret

25

Page 26: Microcontroller Lab Manual

MICROCONTRELLER LAB MANUAL

ready:clr p1.0 ;csmov p2,#0ffhclr p1.2 ;rssetb p1.1 ;rw

wait:clr p1.0 ;cssetb p1.0 ;csjb p2.0,wait ;bsyclr p1.0 ;csret

delay: nopdelay1: mov r6,#0ahdelay2: mov r5,#0ffhdelay3: mov r4,#0ffhdelay4: djnz r4, delay4

djnz r5,delay3djnz r6,delay2ret

.end

Exercise:

Find out the output of the programs. Now modify the program to display two words in two

screens and swap them.

26