Sei sulla pagina 1di 95

DEPT OF EC

ATRIA IT, BLORE MICROCONTROLLERS LAB


( Common to EC/TC/EE/IT/BM/ML )

Sub Code Hrs / Week Total Hrs.

: : :

06ESL47 03 42

IA Marks : Exam Hours : Exam Marks :

25 03 50

I.

PROGRAMMING

1. Data Transfer Block move, Exchange, Sorting, Finding Largest element in an array 2. Arithmetic Instructiuons Addition/subtraction, multiplication and division, square, cube ( 16 bits arithmetic operations - bit addressable ) 3. Counters 4. Boolean and logical instructions ( bit manipulations ) 5. Conditional CALL and RETURN 6. Code conversions : BCD ASCII; ASCII Decimal; Decimal ASCII; HEXDecimal and Decimal HEX 7. Programs to generate delay, programs using serial port and on-chip timer/counter

INTERFACING

Write C programs to interface 8051 chip to interfacing modules to develop single chip solutions 8. Simple calculator using 6 digit seven segment display and HEX keyboard interface to 8051 9. Alphanumeric LCD panel and HEX keypad input interface to 8051 10. External ADC and temperature control interface to 8051 11. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface to 8051: change the frequency and amplitude 12. Stepper and DC motor control interface to 8051 13. Elevator interface to 8051

Microcontroller lab Manual, 2011

Page 1 of 95

DEPT OF EC

ATRIA IT, BLORE

LIST OF PROGRAMS
I CYCLE PROGRAMS
1) WRITE AN ALP TO TRANSFER A BLOCK OF DATA FROM SOURCE AND DESTINATION USING INTERNAL MEMORY. 2) WRITE AN ALP TO TRANSFER A BLOCK OF DATA FROM SOURCE TO DESTINATION USING EXTERNAL DATA MEMORY. 3) WRITE AN ALP TO EXCHANGE BLOCK OF DATA BETWEEN SOURCE AND DESTINATION USING INTERNAL MEMORY. 4) WRITE AN ALP TO EXCHANGE A BLOCK OF DATA FROM SOURCE TO DESTINATION USING EXTERNAL DATA MEMORY. 5.a) WRITE AN ALP TO SORT A BLOCK OF DATA IN ASCENDING ORDER. 5.b) WRITE AN ALP TO SORT A BLOCK OF DATA IN DECENDING ORDER. 6) WRITE AN ALP TO FIND LARGEST NUMBER IN AN ARRAY. 7.a) WRITE AN ALP TO ADD TWO 16 BIT NUMBERS. 7.b) ADDITION OF TWO SIXTEEN BIT NUMBERS USING INTERNAL MEMORY. 8.a) SUBTRACTION OF TWO SIXTEEN BIT NUMBERS USING INTERNAL MEMORY. 8.b)WRITE AN ALP TO SUBTRACT TWO 16 BIT NUMBERS 9) PROGRAM TO IMPLEMENT A DECIMAL UP COUNTER 10) PROGRAM TO IMPLEMENT A DECIMAL DOWN COUNTER.

Microcontroller lab Manual, 2011

Page 2 of 95

DEPT OF EC

ATRIA IT, BLORE

II CYCLE PROGRAMS
11) WRITE A PROGRAM TO CONVERT BCD TO ASCII CODE. 12) WRITE A PROGRAM TO CONVERT ASCII TO PACKED BCD CODE. 13) WRITE A PROGRAM TO CONVERT HEX TO DECIMAL. 14) WRITE A PROGRAM TO RITE A PROGRAM TO CONVERT DECIMAL TO HEX. 15) WRITE A PROGRAM TO REALIZE THE FOLLOWING BOOLEAN EXPRESSIONS. 16) WRITE AN ALP TO FIND CUBE OF A NUMBER. 17) PROGRAM TO PERFORM 16X8 BIT MULTIPLICATION. 18) PROGRAM TO FIND THE SQUARE OF AN 8-BIT NUMBER: 19) WRITE AN ALP T0 MULTIPLY TWO 16-BIT NUMBERS ORG 0000H 20) WRITE AN ALP TO PERFORM THE FOLLOWING OPERATION. 21) WRITE AN ALP TO TOGGLE P1.2 CONTINOUSLY WIH MAXIMUM DELAY USING TIMER 0, MODE 1 AND MODE 2. 22) WRITE AN ALP TO DISPLAY THE COUNT OF PULSE AT P3.4 USING COUNTER 0 BOTH IN MODE1 AND MODE 2. 23) WRITE A PROGRAM TO SEND TEXT STRING WELCOME TO SERIAL PORT. SET THE BAUD RATE AT 9600,8 BIT DATA, AND 1 STOP BIT (MODE 1).

Microcontroller lab Manual, 2011

Page 3 of 95

DEPT OF EC

ATRIA IT, BLORE

III CYCLE PROGRAMS


WRITE A C PROGRAM FOR:
1) INTERFACING LCD TO MICROCONTROLLER. 2) INTERFACING LSSD TO MICROCONTROLLER. 3) INTERFACING LSSD AND 4X4 HEXKEYPAD TO MICROCONTROLLER. 4) INTERFACING LCD AND 4X4 HEXKEYPAD TO MICROCONTROLLER. 5) INTERFACING DAC TO MICROCONTROLLER. 6) INTERFACING ELEVATOR TO MICROCONTROLLER. 7) INTERFACING STEPPER MOTOR TO MICROCONTROLLER. 8) INTERFACING DC MOTOR TO MICROCONTROLLER

Microcontroller lab Manual, 2011

Page 4 of 95

DEPT OF EC

ATRIA IT, BLORE

INTRODUCTION

Microcontroller lab Manual, 2011

Page 5 of 95

DEPT OF EC

ATRIA IT, BLORE

INTRODUCTION
Microprocessors:
CPU for Computers No RAM, ROM, I/O on CPU chip itself ExampleIntels x86, Motorolas 680x0

Microcontrollers :
A smaller computer On-chip RAM, ROM, I/O ports... ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X

Microcontroller lab Manual, 2011

Page 6 of 95

DEPT OF EC

ATRIA IT, BLORE

Microprocessor Vs. Microcontroller


Microprocessor
CPU is stand-alone. Designer can decide on the amount of ROM, RAM and I/O ports. Expensive General-purpose

Microcontroller
CPU, RAM, ROM, I/O and timer are all on a single chip Fixed amount of on-chip ROM, RAM, I/O ports For applications in which cost, power and space are critical Single-purpose

The Prime use of a Microcontroller is to control the operation of a machine using a fixed program that is stored in ROM.

Applications
1. Home Appliances

2. 3. 4. 5. 6. 7. 8.

Computers Remote Controls Video games Musical Instruments Toys Printer Automobiles Engine Control Air Bag Transmission Control Entertainment Keyless Entry

Microcontroller lab Manual, 2011

Page 7 of 95

DEPT OF EC

ATRIA IT, BLORE

PROCEDURE

Microcontroller lab Manual, 2011

Page 8 of 95

DEPT OF EC

ATRIA IT, BLORE

Procedure for executing a project in Keil Software:

1) Select the Keil vision3 software on the desktop.

Microcontroller lab Manual, 2011

Page 9 of 95

DEPT OF EC

ATRIA IT, BLORE

2)

Select Project New vision Project on the Menu bar.

3)

Create New Project dialog box will be opened. Create a folder in the drive where Keil software is loaded. Save the project (with project name reflecting the program) in the

Microcontroller lab Manual, 2011

Page 10 of 95

DEPT OF EC

ATRIA IT, BLORE

respective folder.

4)

Select device for Target Target1 dialog box will be opened. Go to NXP (founded by Phillips), expand it and

Microcontroller lab Manual, 2011

Page 11 of 95

DEPT OF EC

ATRIA IT, BLORE

select the device P89V51RD2 &click OK.

Microcontroller lab Manual, 2011

Page 12 of 95

DEPT OF EC

ATRIA IT, BLORE

5) Click NO to copy 8051 start up code to project folder and add File to project. A project with Target1 (available in project workspace) will be created.

6)

To create an editor to write the program, go to File New in Menu bar

Microcontroller lab Manual, 2011

Page 13 of 95

DEPT OF EC

ATRIA IT, BLORE

7)

To save the editor, go to FileSave. The file name should reflect the program (can be same as project name). Use the extension .asm for assembly language program and .c for C language program.

8)

To link project and editor, expand Target1 and double click Source Group 1 (or right click on Source Group 1 and go to Add Files to Group Source Group 1). Select

Microcontroller lab Manual, 2011

Page 14 of 95

DEPT OF EC

ATRIA IT, BLORE

file type as ASM Source File. Select the respective file and Add then Close.

9) Write the program in the editor.

Microcontroller lab Manual, 2011

Page 15 of 95

DEPT OF EC

ATRIA IT, BLORE

10) To compile the program, go to Project Translate on the Menu bar

Microcontroller lab Manual, 2011

Page 16 of 95

DEPT OF EC
11)

ATRIA IT, BLORE

Go to Project Build target

12)

Go to Debug Start/Stop Debug Session. Click OK to Evaluation Version.

Microcontroller lab Manual, 2011

Page 17 of 95

DEPT OF EC

ATRIA IT, BLORE

13) The input data has to be given in the memory location if necessary.

Microcontroller lab Manual, 2011

Page 18 of 95

DEPT OF EC

ATRIA IT, BLORE

14) To execute the program, go to debug Run.

Microcontroller lab Manual, 2011

Page 19 of 95

DEPT OF EC

ATRIA IT, BLORE

ASSEMBLY LANGUAGE PROGRAMS

Microcontroller lab Manual, 2011

Page 20 of 95

DEPT OF EC

ATRIA IT, BLORE

1) WRITE AN ALP TO TRANSFER A BLOCK OF DATA FROM SOURCE TO DESTINATION USING INTERNAL MEMORY. org 00h sjmp 30h org 30h mov r0,#40h mov r1,#60h mov r2,#10 back: mov a,@r0 mov @r1,a inc r0 inc r1 djnz r2,back sjmp $ end

;set the source pointer ;set the destination pointer ;set the count ;move the contents of mem.loc.in r0 to accumulator ;move from a to detination address ;increment the source pointer ;increment the destination pointer ;decrement count if 0 stop else continue ;block transfer ;stay in this loop

; INPUT:

0x40

01 02 03 04 05 06 07 08 09 10 01 02 03 04 05 06 07 08 09 10

; OUTPUT: 0x60

Algorithm 1. Initialize registers to hold count data & also the source & destination addresses. 2. Get data from source location into accumulator and transfer to the destination location. 3. Decrement the count register and repeat step 2 till count is zero.

Microcontroller lab Manual, 2011

Page 21 of 95

DEPT OF EC

ATRIA IT, BLORE

2) WRITE AN ALP TO TRANSFER A BLOCK OF DATA FROM SOURCE TO DESTINATION USING EXTERNAL DATA MEMORY org 0000h sjmp 30h org 30h mov dptr,#8100h mov r1,#82h mov r0,#00h mov r2,#10 back: movx a,@dptr push 83h push 82h mov dph,r1 mov dpl,r0 movx @dptr,a inc dptr mov r1,dph mov r0,dpl pop 82h pop 83h inc dptr djnz r2,back1 sjmp $ end ; INPUT: ; OUTPUT:

; assign dptr with source address ; store destination address in reg. r1 &r0 ; set a counter ; contents of mem. loc. Pointed to by source ; pointer moved to ACC. ; contents of source ptr stored in stack ; by pushing value of dph &dpl to stack ; dptr made to point to destination address ; stored in r1 ;&r0 ; contents of ACC transferred to destination ; increment the destination pointer ; contents of dest ptr store in register r1 &r0 ; source pointer value poped back to dptr ; increment the source pointer ; check for the count ; remain in this loop ; end of program 01 02 03 04 05 06 07 08 09 10 01 02 03 04 05 06 07 08 09 10

x: 0x08100 x: 0x08200

Microcontroller lab Manual, 2011

Page 22 of 95

DEPT OF EC

ATRIA IT, BLORE

3) WRITE AN ALP TO EXCHANGE BLOCK OF DATA BETWEEN SOURCE AND DESTINATION USING INTERNAL MEMORY org 0000h sjmp 30h org 30h mov r0,#40h mov r1,#60h mov r2,#10 mov a,@r0

; set the source pointer ; set the destination pointer ; set the count back: ; move the contents of mem.loc.in r0 to ; accumulator xch a,@r1 ; exchange the contents of A and contents of ; memory location in r1 mov @r0,a ; move from A to source location address inc r0 ; increment the source pointer inc r1 ; increment the destination pointer djnz r2,back ; decrement count if 0 stop else continue block ; exchange sjmp $ end ; INPUT: ; OUTPUT: 0x40 0x60 0x40 0x60 01 11 11 01 02 12 12 02 03 13 13 03 04 14 14 04 05 15 15 05 06 16 16 06 07 17 17 07 08 18 18 08 09 19 19 09 10 20 20 10

Algorithm 1. Initialize registers to hold count data (array size) & also the source & destination addresses. 2. Get data from source location into accumulator and save in a register. 3. Get data from the destination location into accumulator. 4. Exchange the data at the two memory locations. 5. Decrement the count register and repeat from step 2 to 4 till count is zero.

Microcontroller lab Manual, 2011

Page 23 of 95

DEPT OF EC

ATRIA IT, BLORE

4) WRITE AN ALP TO EXCHANGE A BLOCK OF DATA FROM SOURCE TO DESTINATION USING EXTERNAL DATA MEMORY org 0000h sjmp 30h org 30h mov dptr,#8100h mov r1,#82h mov r0,#00h mov r2,#10 back: movx a,@dptr mov r3,a push 83h push 82h mov dph,r1 mov dpl,r0 movx a,@dptr xch a,r3 movx @dptr,a mov a,r3 inc dptr mov r1,dph mov r0,dpl pop 82h pop 83h movx @dptr,a inc dptr djnz r2,back sjmp $ end
Microcontroller lab Manual, 2011 Page 24 of 95

DEPT OF EC

ATRIA IT, BLORE

; INPUT: ; OUTPUT:

x:0x08100 x:0x08200 x:0x08100 x:0x08200

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20 01 02 03 04 05 06 07 08 09 10

Microcontroller lab Manual, 2011

Page 25 of 95

DEPT OF EC

ATRIA IT, BLORE

5.a) WRITE AN ALP TO SORT A BLOCK OF DATA IN ASCENDING ORDER org 0000h sjmp 30h org 30h mov dptr, #8100h mov r1,#04h up2: movx a,@dptr mov b,a mov r0,dpl mov 02h,01h inc dpl movx a,@dptr cjne a,b,down1

up1:

sjmp down2 down1: jnc down2 xch a,b movx @dptr,a down2: djnz r2,up1 mov dpl,r0 mov a,b movx @dptr,a inc dpl djnz r1,up2 sjmp $ end ; INPUT: ; OUTPUT:

; Initialize source pointer ; set the count, count should be one less ; than total bytes ; get the first number ; store it in b ; move a copy of dpl for later use ; set the second counter, same as first (r1=r2) ; get the next number by incrementing dpl ; transfer to a ; compare both the numbers,if not equal jump ; to down1 ; if equal jump to down2 ; if a>=b jump to down2 without exchange of ; num.,if a<bexchange the numbers(use jc for ; desending order) ; exchange contents of a and b ; reflect the same change in memory loc.,b will ; store the lowest num ; decrement the second pointer repeat the ; comparision till it is zero ; get the initial value of dptr ,b contains the ; lowest no. ; store b in appropriate mem. loc. ; comparing second num with others by ; incrementing dpl ; decrement the first counter and repeat ;untill ; zero ; be in this loop ; end 06 05 00 01 04 00 01 04 05 06

x: 0x08100 x: 0x08100

Microcontroller lab Manual, 2011

Page 26 of 95

DEPT OF EC

ATRIA IT, BLORE

5.b) WRITE AN ALP TO SORT A BLOCK OF DATA IN DESCENDING ORDER org 0000h sjmp 30h org 30h mov r0,#04 again: mov dptr,#8100h mov r1,#04 back: mov r2,dpl movx a,@dptr mov b,a inc dptr movx a,@dptr cjne a,b,down sjmp skip down: jnc skip mov dpl,r2 movx @dptr,a inc dptr mov a,b movx @dptr,a skip: djnz r1,back djnz r0,again sjmp $ end ; INPUT: x: 0x08100 06 05 00 01 04 ; OUTPUT: x: 0x08100 00 01 04 05 06 Algorithm 1. Store the elements of the array from the address 9000h 2. Initialize a pass counter with array size-1 count (for number of passes). 3. Load compare counter with pass counter contents & initialize DPTR to point to the start address of the array (here 9000h). 4. Store the current and the next array elements pointed by DPTR in registers B and r2 respectively. 5. Subtract the next element from the current element. 6. If the carry flag is set (for ascending order) then exchange the 2 numbers in the array. 7. Decrement the compare counter and repeat through step 4 until the counter becomes 0. 8. Decrement the pass counter and repeat through step 3 until the counter becomes 0.
Microcontroller lab Manual, 2011 Page 27 of 95

DEPT OF EC

ATRIA IT, BLORE

6) WRITE AN ALP TO FIND LARGEST NUMBER IN AN ARRAY org 0000h sjmp 30h org 30h mov dptr,#8100h mov r2,#04h mov b,#00h movx a,@dptr cjne a,b,down1 ; set the pointer to source address ; set the count ; assign b to 0, ; get the first num. ; compare it with b ,if not equal go to ; down1 ; if a<b go to skip ; ifa>b mov contents of a to b ,such that b ; stores the largest number ; increment the pointer ; decrement counter & repeat till it is zero ; b stores the largest no. , it is transferred ; to loc. 60h

back:

down1: jc skip mov b,a skip: inc dptr djnz r2,back mov 60h,b sjmp $ end

;INPUT: x:8100h : 01 00 04 03 ;OUTPUT: d:60h : 04 Algorithm 1. Store the elements of the array from the address 2. Store the length of the array in r3 and set it as counter. 3. DPTR is loaded with starting address of the array. 4. Store the first number of the array in r1 (r1 is assigned to hold the largest number). 5. Increment DPTR. 6. Subtract the number pointed by DPTR from the contents of r1 (to compare whether the next array element is larger than the one in r1). 7. If the element pointed by DPTR is larger then load the larger number into r1. 8. Decrement the counter and repeat steps through 5 until the counter becomes 0. 9. Store the largest number in r1 in address 4062h

Microcontroller lab Manual, 2011

Page 28 of 95

DEPT OF EC

ATRIA IT, BLORE

7.a) WRITE AN ALP TO ADD TWO 16 BIT NUMBERS USING EXTERNAL MEMORY org 0000h sjmp 30h org 30h mov dptr,#8100h mov r0,#82h mov r3,#00h movx a,@dptr mov b,a push dph mov dph,r0 movx a,@dptr add a,b mov r1,a inc dpl pop dph movx a,@dptr mov b,a mov dph,r0 movx a,@dptr addc a,b jnc next mov r3,#01h next: mov r2,a mov dptr,#8300h mov a,r3 movx @dptr,a mov a,r2 inc dpl movx @dptr,a inc dpl mov a,r1 movx @dptr,a sjmp $ end

; lower byte of operand 1

; lower byte of operand 2 ; r1 stores the lower byte of the result ; higher byte of operand 1 ; higher byte of operand 2 ; accumulator stores the higher byte of result ; r2 stores higher byte of result ; stores the carry ; stores HB of result

; stores LB of result

;INPUT1 : 8100: 12 (LB) 8200:45(LB) 8101: 34 (HB) 8201:67(HB)

;OUTPUT: 8300:00 (HB) 8301:9b 8302:57 (LB)

Microcontroller lab Manual, 2011

Page 29 of 95

DEPT OF EC

ATRIA IT, BLORE

7.b) WRITE AN ALP TO ADD TWO SIXTEEN BIT NUMBERS USING INTERNAL MEMORY org 0000h mov r1,#00h mov a,40h mov b,50h add a,b mov 62h,a mov a,41h mov b,51h addc a,b mov 61h,a jnc skip mov r1,#01h mov 60h,r1 sjmp $ d:40h d:50h d:60h

; LB of operand 1 ; LB of operand 2 ; LB result ; HB of operand 1 ; HB of operand 2 ; HB result ; carry

skip:

;INPUT: ;OUTPUT:

12(LB) 56 (LB) 00(HB)

34(HB) 78 (HB) AC 68(LB)

Microcontroller lab Manual, 2011

Page 30 of 95

DEPT OF EC

ATRIA IT, BLORE

8.a) WRITE AN A LP TO SUBTRACT TWO SIXTEEN BIT NUMBERS USING INTERNAL MEMORY org 0000h mov r1,#00h clr c mov a,50h mov b,40h subb a,b mov 62h,a mov a,51h mov b,41h subb a,b mov 61h,a jnc skip mov a,62h cpl a add a,#01h mov 62h,a mov a,61h cpl a addc a,#00h mov 61h,a mov r1,#01h mov 60h,r1 sjmp $ end d:40h d:50h d:60h

;LB of operand 2 ;LB of operand 1 ;HB of operand 2 ;HB of operand1

skip:

;INPUT: ;OUTPUT:

12(LB) 56 (LB) 00(HB)

34(HB) 78 (HB) 44 44(LB)

Microcontroller lab Manual, 2011

Page 31 of 95

DEPT OF EC

ATRIA IT, BLORE

8.b) WRITE AN ALP TO SUBTRACT TWO 16 BIT NUMBERS USING EXTERNAL MEMORY org 0000h mov dptr,#8100h mov r0,#82h clr c mov r3,#00h movx a,@dptr mov b,a push dph mov dph,r0 movx a,@dptr xch a,b subb a,b mov r1,a inc dpl pop dph movx a,@dptr mov b,a mov dph,r0 movx a,@dptr xch a,b subb a,b jnc next mov r3,#01h push 0e0h mov a,r1 cpl a add a,#01h mov r1,a pop 0e0h cpl a ;stores the result in 8300 location next: mov r2,a mov dptr,#8300h mov a,r3 ; r3=00,result is positive ,r3=01 result is ; negative
Page 32 of 95

; lower byte of operand 1

; lower byte of operand 2 ; to subtract second num from first ; r1 stores the lower byte of the result ; higher byte of operand 1 ; higher byte of operand 2 ; accumulator stores the higher byte of result ; if no carry result is correct and if carry ; result is negative and is in 2's complement ; form ; these below steps adjusts the result by ; taking 2's compliment of the result

Microcontroller lab Manual, 2011

DEPT OF EC

ATRIA IT, BLORE

movx @dptr,a mov a,r2 inc dpl movx @dptr,a inc dpl mov a,r1 movx @dptr,a sjmp $ end

;INPUT1 :

X : 8100h: 96 (LB) 8101h: 12 (HB)

8200h:62(LB) 8201h:27(HB)

data1 1296 data2-2762 result-14CC

;OUTPUT:

8300:01 (negative) 8301:14 8302:CC (LB)

Microcontroller lab Manual, 2011

Page 33 of 95

DEPT OF EC

ATRIA IT, BLORE

9) WRITE AN ALP TO IMPLEMENT A DECIMAL UP COUNTER

org 0000h mov a, #00h again: mov r1, #100 back: mov 60h, a add a, #01h da a acall delay djnz back sjmp again

; delay routine for both up/down counter delay : mov r1, #50h back3 : mov r2, #255 back2 : mov r3, #255 back1 : djnz r3, back1 djnz r2, back2 djnz r1, back3 ret end

;OUTPUT: 60H location (d:60h) counts from 00 to 99.

Microcontroller lab Manual, 2011

Page 34 of 95

DEPT OF EC

ATRIA IT, BLORE

10) WRITE AN ALP TO IMPLEMENT A DECIMAL DOWN COUNTER

org 0000h mov a,#99h back: mov 60h, a add a, #99h da a acall delay jnz back mov 60h,a sjmp $ ; delay routine for both up and down counter delay : back3 : back2 : back1 : mov r1, #50h mov r2, #255 mov r3, #255 djnz r3, back1 djnz r2, back2 djnz r1, back3 ret end

;OUTPUT: 60H location (d:60h) counts from 99 to 00.

Microcontroller lab Manual, 2011

Page 35 of 95

DEPT OF EC

ATRIA IT, BLORE

11) WRITE AN ALP TO CONVERT BCD TO ASCII CODE.

org 00h mov dptr,#8100h movx a,@dptr mov r2,a anl a,#0fh orl a,#30h mov r6,a mov a,r2 anl a,#0f0h swap a orl a,#30h mov 61h,a mov 60h,r6 here: sjmp here end

;get the number ; make a copy of num in r2 ;mask the upper nibble ;add 30h to get it's ascii equivalent ;store the result in r6 ;get the original num ;mask the lower nibble ;exchange the upper and lower ;nibbles ;add 30h to get it's ascii equivalent ;store the result in 61h loc. ;store the lower nibble result in 60h . ;remain in this loop

; INPUT: ;OUTPUT:

X:

8100h: 60h: 61h:

39 33 39

Microcontroller lab Manual, 2011

Page 36 of 95

DEPT OF EC

ATRIA IT, BLORE

12) WRITE AN ALP TO CONVERT ASCII TO PACKED BCD CODE org 0000h mov a,#'4' anl a,#0fh swap a mov b,a mov a,# 7 anl a,#0fh orl a,b mov 60h, a sjmp $ end ;INPUT: ;OUTPUT:

; ASCII value of 4(34) is stored in acc. ; mask the upper nibble ; shift the num to left 4 times ; store it in b ; get the next number,a contains 37 ; mask the upper nibble ; join both numbers to get it's BCD ;equivalent ; store the result in loc.60h

4 , 7 60h: 47

Microcontroller lab Manual, 2011

Page 37 of 95

DEPT OF EC

ATRIA IT, BLORE

13) WRITE AN ALP TO CONVERT HEX TO DECIMAL. org 00h mov a,60h mov b,#0ah div ab mov 63h,b mov b,#0ah div ab mov 62h,b mov 61h,a

;input given in loc. 60h ;10 is moved to b ;number divided by 10 ;lsb of result stored in 63h loc. ;10 moved to b ; number divided by 10 ;next digit in result stored in 62h loc. ;msb of result stored in 61h loc.

;INPUT: ;OUTPUT:

60h: 61h: 62h: 63h:

FF 02 05 05

Microcontroller lab Manual, 2011

Page 38 of 95

DEPT OF EC

ATRIA IT, BLORE

14) WRITE AN ALP TO CONVERT DECIMAL TO HEX. org 00h mov a,#255 mov b,#10h div ab mov 61h,b mov b,#10h div ab mov 62h,b sjmp $ end ;INPUT: ;OUTPUT:

;divide number by 16(10h)

60h : 61h :

255 0F 0F

Microcontroller lab Manual, 2011

Page 39 of 95

DEPT OF EC

ATRIA IT, BLORE

15) WRITE A PROGRAM TO REALIZE THE FOLLOWING BOOLEAN EXPRESSION, S = XYZ +XYZ +XYZ org 00h setb 00h clr 01h setb 02h mov c, 01h anl c,02h anlc,/00h mov 03h,c mov c, 00h anl c,/01h anl c, 02h mov 04h,c mov c, 00h anl c,01h anl c,/02h orl c,04h orl c,03h mov 08h,c end ;INPUT: ;OUTPUT: 20h: 21h: 05h 01h

Microcontroller lab Manual, 2011

Page 40 of 95

DEPT OF EC

ATRIA IT, BLORE

16) WRITE AN ALP TO FIND CUBE OF A NUMBER


org 0000h cube equ 60h mov a,cube mov b,a mul ab mov r4,b mov b,cube mul ab mov r1,a mov r2,b mov b,cube mov a,r4 mul ab add a,r2 mov r2,a mov a,b addc a,#00h mov r3,a sjmp $ end

Inputs 1)d:60h FF 2) d:60h 85 3) d:60h 02

Output(answers are in registers r3(HB),r2&r1 d:00h FD 02 FF d:00h 23 E5 FD d:00h 00 00 08

Microcontroller lab Manual, 2011

Page 41 of 95

DEPT OF EC

ATRIA IT, BLORE

17) WRITE AN ALP TO PERFORM 16X8 BIT MULTIPLICATION

org 00h mov r0, #60h mov a, @r0 mov b, 62h mul ab mov 65h, a mov a ,b mov r1 ,a inc r0 mov a ,@r0 mov b, 62h mul ab add a, r1 mov 64h, a mov a,b add a, #00h mov 63h, a sjmp $ end

;stores multiplier ;stores the LB result

;stores the HB result

Inputs 1) d: 60h FF FF FF 2) d: 60h 03 02 26

outputs d:63h FE (HB) FF d:63h 00 72

01(LB) 4C

Microcontroller lab Manual, 2011

Page 42 of 95

DEPT OF EC

ATRIA IT, BLORE

18) WRITE AN ALP TO FIND THE SQUARE OF AN 8-BIT NUMBER

org 0000h mov a, 60h mov b, a mul ab mov 61h, a mov 62h,b

; lsb of result in 61h ; msb of result in 62h

Inputs 1) d:60h FF 2) d:60h 85 3) d:60h 10

outputs d:61h FE01 d:61h 4519 d:61h 0100

Microcontroller lab Manual, 2011

Page 43 of 95

DEPT OF EC

ATRIA IT, BLORE

19) WRITE AN ALP TO MULTIPLY TWO 16 BIT NUMBERS

org 0000h mul_lb equ 60h mul_hb equ 61h mul_lbm equ 62h mul_hbm equ 63h acall mul_lblbm acall mul_hblbm acall mul_lbhbm acall mul_hbhbm acall store sjmp $ mul_lblbm: mov a,mul_lb mov b,mul_lbm mul ab mov r1,a mov r2,b ret mul_hblbm: mov a,mul_hb mov b,mul_lbm mul ab add a,r2 mov r2,a mov a,b addc a,#00h mov r3,a mov a,#00h addc a,#00h mov r4,a ret
Microcontroller lab Manual, 2011 Page 44 of 95

DEPT OF EC

ATRIA IT, BLORE

mul_lbhbm : mov a,mul_lb mov b,mul_hbm mul ab add a,r2 mov r2,a mov a,b addc a,r3 mov r3,a mov a,#00h addc a,#00h mov r4,a ret mul_hbhbm: mov a,mul_hb mov b,mul_hbm mul ab add a,r3 mov r3,a mov a,b addc a,r4 mov r4,a ret store: mov 70h,r4 mov 71h,r3 mov 72h,r2 mov 73h,r1 ret end ; stores HB of the result ; stores LB of the result

INPUT d:60h FF FF FF FF

OUTPUT d:70h FF FE 00 01

Microcontroller lab Manual, 2011

Page 45 of 95

DEPT OF EC

ATRIA IT, BLORE

20) WRITE AN ALP TO PERFORM THE FOLLOWING OPERATION R1=00 add two 8 bit numbers R1=01 subtract two 8 bit numbers R1=02 multiply two 8 bit numbers R1=03 divide two 8 bit numbers org 00h sjmp 30h org 30h mov r1,60h mov 63h,#00h cjne r1,#00h,skip1 mov a,61h mov b,62h add a,b jnc res mov 63h,#01h res: mov 64h,a sjmp down skip1: cjne r1,#01h,skip2 clr c mov a,61h mov b,62h subb a,b jnc res1 cpl a add a,#01h mov 63h,#01h ;indicates negative result res1: mov 64h,a sjmp down skip2: cjne r1,#02h,skip3 mov a,61h mov b,62h mul ab mov 64h,a ;stores lower byte mov 63h,b ;stores upper byte sjmp down skip3: cjne r1,#03h,down
Page 46 of 95

Microcontroller lab Manual, 2011

DEPT OF EC

ATRIA IT, BLORE

mov a,61h mov b,62h div ab mov 63h,a ;stores the quotient mov 64h,b ;stores reminder down:sjmp down end ;Addition ;Input: d:60h=00 61h=01 62h=03 ;Output:d:63h=00 64h=04 ;Input: d:60h=00 61h=ff 62h=ff ;Output:d:63h=01 64h=fe ;Subtraction ; Input: d:60h=01 61h=03 62h=01 ;Output:d:63h=00 64h=02 ;Input: d:60h=01 61h=01 62h=02 ;Output:d:63h=01 64h=02

;Multiplication ;Input: d:60h=02 61h=02 62h=03 ;Output:d:63h=00 64h=06 ;Input: d:60h=02 61h=ff 62h=ff ;Output:d:63h=fe 64h=01

;Division ;Input: d:60h=03h 61h=06h 62h=03 ;Output:d:63h=00h 64h=02h

Microcontroller lab Manual, 2011

Page 47 of 95

DEPT OF EC

ATRIA IT, BLORE

21) WRITE AN ALP TO TOGGLE P1.2 CONTINOUSLY WITH MAXIMUM DELAY USING TIMER 0, MODE1 AND MODE2
MODE 1:

org 0000h back: mov tmod, #01h mov th0, #00h mov tl0, #00h setb tro again: jnb tf0, again clr tr0 clr tf0 cpl p1.2 sjmp back end
MODE 2:

org 0000h mov tmod, #02h mov th0, #00h setb tro again: jnb tf0, again clr tf0 cpl p1.2 sjmp again end

Output to be seen in peripherals (timer 0 and port P1)

Microcontroller lab Manual, 2011

Page 48 of 95

DEPT OF EC

ATRIA IT, BLORE

22) WRITE AN ALP TO DISPLAY THE COUNT OF PULSE AT P3.4 USING COUNTER0 BOTH IN MODE1 AND MODE2
MODE 1:

org 0000h setb P3.4 mov tmod, #05h back: mov th0, #0F0h mov tl0, #0F0h setb tr0 again: mov P0, tl0 mov P0, th0 jnb tf0, again clr tr0 clr tf0 sjmp back end

MODE 2:
org 0000h setb p3.4 mov tmod, #06h mov th0, #00h setb tr0 again: mov p0, tl0 jnb tf0, again clr tr0 clr tf0 sjmp $ end

Microcontroller lab Manual, 2011

Page 49 of 95

DEPT OF EC

ATRIA IT, BLORE

23) WRITE A PROGRAM TO SEND TEXT STRING WELCOME TO SERIAL PORT .SET THE BAUD RATE AT 9600,8 BIT DATA,AND 1 STOP BIT(MODE 1). org 00h sjmp 30h org 30h mov tmod ,#20h mov th1,#-3 mov scon,#50h setb tr1 mov dptr ,#msg back: clr a movc a,@a+dptr jz finish acall transmit inc dptr sjmp back finish: sjmp finish transmit: mov sbuf,a here: jnb ti,here clr ti ret msg: db "WELCOME",0 end

Microcontroller lab Manual, 2011

Page 50 of 95

DEPT OF EC

ATRIA IT, BLORE

INTERFACING PROGRAMS

Microcontroller lab Manual, 2011

Page 51 of 95

DEPT OF EC

ATRIA IT, BLORE

1)Interfacing LCD to Microcontroller 14 pin LCD 2 x 16 LCD

M I C R O C O N T R O L L E R

P2.0 : P2.7

D0 : D7

+5V
L C D

P3.5 P3.6 P3.7

E R/W RS

NOTE : E Enable R / W= 1 To read from LCD = 0 To write on LCD RS Register Select = 1 Data Register = 0 Command Register

Microcontroller lab Manual, 2011

Page 52 of 95

DEPT OF EC

ATRIA IT, BLORE

/* 8051 c program to interface LCD */

#include<reg51.h> #define Idata P2 sbit enable = P3^5; sbit rw=P3^6 ; sbit rs=P3^7 ; sbit busy=P2^7; //DECLARATION OF FUNCTIONS void lcdcmd(unsigned char) ; void lcddata(unsigned char) ; void lcdready(); void Msdelay(unsigned int); //MAIN PROGRAM void main() { //LCD INITIALIZATION lcdcmd(0x38); lcdcmd(0x0E); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80); lcddata('W'); lcddata('E'); lcddata('L'); lcddata('C'); lcddata('O'); lcddata('M'); lcddata('E'); while(1); } //sending a command to use two lines and 5n 7 matrix condition //display on cursor blinking //clear display screen //increament cursor towards right or shift cursor towards right //force the cursor to begin the first line

Microcontroller lab Manual, 2011

Page 53 of 95

DEPT OF EC

ATRIA IT, BLORE

//FUNCTION TO SEND COMMANDS TO LCD void lcdcmd(unsigned char value) { lcdready(); //check the lcd busy flag Idata=value; //put the command value on the lcd pins thru port 2(Idata) rs=0; //reg select=0, to select command reg rw=0; //read-write = 0,to write to lcd enable=1; //strobe the enable pin, send high to low pulse for writing Msdelay(1); //give some time for lcd to react enable=0; return; } // FUNCTION TO SEND DATA TO LCD void lcddata(unsigned char value) { lcdready(); Idata=value; rs=1; //reg select =1,to select the data reg rw=0; //read-write=0, to select the command reg enable=1; //strobe the enable pin,send high to low pulse for writing Msdelay(1); //give some time for lcd to react enable=0; return; } //function to check the busy flag void lcdready() { busy=1;//make the busy pin an input rs=0;//command reg rw=1;//reading from lcd while(busy==1)//wait here for busy flage till it become "0" { enable=0; Msdelay(1); enable=1; } return; } //DELAY ROUTINE void Msdelay(unsigned int itime) { unsigned int i,j; for(i=0;i<itime;i++) for(j=0;j<1275;j++); } Microcontroller lab Manual, 2011 Page 54 of 95

DEPT OF EC

ATRIA IT, BLORE

2) INTERFACING LCD AND 4X4 HEXKEYPAD TO

MICROCONTROLLER

C0 C1 C2 C3 H E X K E Y P A D

P1.0 P1.1 P1.2 P1.3

R0 R1 R2 R3

P0.0 P0.1 P0.2 P0.3

M I C R O C O N T R O L L E R

P2.0 : P2.7

D0 : D7

+5V

L C D P3.5 P3.6 P3.7 E R/W RS

/* 8051 c program to interface LCD & 4x4 HexKeypad to the microcontroller*/


#include<reg51.h> #include<intrins.h> #define COL P1 #define ROW P0 #define DATA_BUS P2 sbit rs = P3^7 ; sbit rw = P3^6 ; sbit enable = P3^5 ; sbit busy = P2^7 ;

Microcontroller lab Manual, 2011

Page 55 of 95

DEPT OF EC

ATRIA IT, BLORE

//function prototype declaration void delay_msec(unsigned int count); void lcd_init(void); void lcdready(void); void lcdcmd(unsigned char ); void lcddata(unsigned char ); unsigned char keyboard(); // look up table for keyboard unsigned char keypad[4][4] ={'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'}; void main( ) { unsigned char key; lcd_init(); while(1) { key=keyboard(); lcddata(key); } } //END OF MAIN PROGRAM

//KEYBOARD FUNCTION unsigned char keyboard() { unsigned char colloc, rowlloc,k,l,i,value; COL = 0xFF; ROW = 0XFE; k=0; l=0; while(1) { ROW=_crol_(ROW,k); colloc = COL; colloc &= 0x0F; if(colloc != 0x0F) { rowlloc = l; break; } k=1; l=l+1; } Microcontroller lab Manual, 2011 Page 56 of 95

DEPT OF EC

ATRIA IT, BLORE

// CHECK COLUMN AND SEND RESULT TO THE LCD lcdcmd(0x80); value=0xFE; colloc=colloc|0xf0; for(i=0;i<4;i++) { if(colloc == value) return(keypad[rowlloc][i]); value=_crol_(value,1); } }

//DELAY ROUTINE void delay_msec(unsigned int count) { unsigned int i,j; for(i=0;i<count;i++) for(j=0;j<1275;j++); }

//LCD READY ROUTINE void lcdready(void) { busy=1; // make it as input pin rs = 0; //select lcd command register to read busy flag rw = 1;// select read operation to read busy flag from lcd while(busy==1) { enable=0; delay_msec(1); enable=1; } return; }

//LCD COMMAND ROUTINE void lcdcmd(unsigned char lcd_cmd) { lcdready(); DATA_BUS = lcd_cmd; Microcontroller lab Manual, 2011 Page 57 of 95

DEPT OF EC
rs = 0; rw = 0; enable=1; delay_msec(1); enable=0; return; }

ATRIA IT, BLORE

//LCD INITIALIZATION ROUTINE void lcd_init(void) { lcdcmd(0x38); //Function mode = interface datalength=8,display //lines=2,character font=5*7 dots. lcdcmd(0x0C); //display on cursor off lcdcmd(0x06); //Cursor increment lcdcmd(0x01);//clear display screen lcdcmd(0x02); }

//LCD DATA ROUTINE void lcddata(unsigned char lcd_data) { lcdready(); // wait until lcd is busy DATA_BUS = lcd_data; // place data value on data bus which is to be written //into lcd. rs = 1; // select lcd data register to store data value rw = 0; // select write operation mode for writing data value //into lcd data register enable=1; // enable lcd to read data value available on the data bus delay_msec(1); enable=0; return; }

Microcontroller lab Manual, 2011

Page 58 of 95

DEPT OF EC

ATRIA IT, BLORE

3) Interfacing LSSD to Microcontroller

M I C R O C O N T R O L L E R

P0.0 : P0.7

Seg A : Seg P
L S S D 6 L S S D 5 L S S D 4 L S S D 3 L S S D 2 L S S D 1

P2.0 : P2.5

LSSD 1 : LSSD 6

L 6

L 5

L 4

L 3

L 2

L 1

LATCHES

LSSDs are common anode segment display Latches are for selection of LSSDs

Microcontroller lab Manual, 2011

Page 59 of 95

DEPT OF EC

ATRIA IT, BLORE

/*8051 c program to interface LSSD to microcontroller*/


#include"reg51.h" /*********************************************************************** Module Name: LSSD Following MC I/O LINES ARE USED TO INTERFACE WITH LSSD LSSD_DATA = P0 LSSD_ADD = P2 LSSD1_ADD = P2.0 LSSD2_ADD = P2.1 LSSD3_ADD = P2.2 LSSD4_ADD = P2.3 LSSD5_ADD = P2.4 LSSD6_ADD = P2.5 *************************************************************/ #define LSSD_ADD P2 #define LSSD1_ADD 0X01 #define LSSD2_ADD 0X02 #define LSSD3_ADD 0X04 #define LSSD4_ADD 0X08 #define LSSD5_ADD 0X10 #define LSSD6_ADD 0X20 #define LSSD_DATA P0 void main(void) { void lssd(unsigned char lssd_add, unsigned char symbol); unsigned int i,j,k; while(1) { for(i = 0; i < 16; i++) { lssd(LSSD1_ADD,i); for(j = 0; j < 35000; j++)//delay { } } } }

Microcontroller lab Manual, 2011

Page 60 of 95

DEPT OF EC

ATRIA IT, BLORE

/*--------------------------------------------------------------------------------------------Module Name: lssd(LED Seven Segment Display) Type : Function File Description: In this module, LED Seven Segment Display Address and ASCII value of a symbol is accepted as formal arguments. The programmer has a provision to display symbol on any one of the 6 digits LED Seven Segment Display by using this module. if the programmer wants to display more than one symbols on different LED Seven Segment Display, then call the same module with different LED Seven Segment Display Address as formal argument. The ASCII value recieved as formal argument, is compared with the first element of an two dimensional "ASCII_symbol" Array, and if exists, then displayed on the selected LED Seven Segement Display, else simply discarded LED Seven Segment Address List LSSD Name Addresss LSSD0 0X01 LSSD1 0X02 LSSD2 0X04 LSSD3 0X08 LSSD4 0X10 LSSD5 0X20 Note : Common Anode LED Seven Segment Display is used in The 80C51 EDK V1.2 ---------------------------------------------------------------------------------------------*/ void lssd(unsigned char lssd_add,unsigned char symbol) { unsigned char i; unsigned char ascii_symbol[17][2] = { 0,0xC0,1,0xf9,2,0xa4,3,0xb0, 4,0x99,5,0x92,6,0x82,7,0xf8, 8,0x80,9,0x98,10,0x88,11,0x83, 12,0xc6,13,0xa1,14,0x86,15,0x8e, ' ',0xff }; //lssd decode value lookup table */

Microcontroller lab Manual, 2011

Page 61 of 95

DEPT OF EC

ATRIA IT, BLORE

for(i = 0; i < 17; i++) { if(ascii_symbol[i][0] == symbol)//check for symbol in lssd decode lookup //table { LSSD_ADD = lssd_add;//Place corresponding Lssd address on //address bus to display symbol LSSD_DATA = ascii_symbol[i][1]; break; } } }

4) Interfacing LSSD and 4x4 HexKeypad to Microcontroller lab Manual, 2011 Page 62 of 95

DEPT OF EC

ATRIA IT, BLORE

Microcontroller

C0 C1 C2 C3 H E X K E Y P A D

P1.0 P1.1 P1.2 P1.3

R0 R1 R2 R3

P0.0 P0.1 P0.2 P0.3

M I C R O C O N T R O L L E R

P0.0 : P0.7

Seg A : Seg P L S S D C A R D LSSD 1 : LSSD 6

P2.0 : P2.5

Microcontroller lab Manual, 2011

Page 63 of 95

DEPT OF EC

ATRIA IT, BLORE

/*8051 C program to interface keyboard &LSSD to microcontroller*/


/*-------------------------------------------------------------------------------*/ #include"reg51.h" #include<intrins.h> #define ROW P3 #define COL P1 /************************************************************************** Module Name: LSSD Following MC I/O LINES ARE USED TO INTERFACE WITH LSSD LSSD_DATA = P0 LSSD_ADD = P2 LSSD1_ADD = P2.0 LSSD2_ADD = P2.1 LSSD3_ADD = P2.2 LSSD4_ADD = P2.3 LSSD5_ADD = P2.4 LSSD6_ADD = P2.5 **************************************************************************/ #define LSSD_ADD P2 #define LSSD1_ADD 0X01 #define LSSD2_ADD 0X02 #define LSSD3_ADD 0X04 #define LSSD4_ADD 0X08 #define LSSD5_ADD 0X10 #define LSSD6_ADD 0X20 void lssd(unsigned char ,unsigned char); unsigned char keyboard(); #define LSSD_DATA P0 //LOOK UP TABLE FOR KEYBOARD unsigned char keypad[4][4] ={'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'}; //LOOK UP TABLE FOR LSSD unsigned char ascii_symbol[17][2] = {'0',0xC0,'1',0xf9,'2',0xa4,'3',0xb0, '4',0x99,'5',0x92,'6',0x82,'7',0xf8, '8',0x80,'9',0x98,'A',0x88,'B',0x83, 'C',0xc6,'D',0xa1,'E',0x86,'F',0x8e, ' ',0xff }; //MAIN PROGRAM void main(void) { unsigned char key;

Microcontroller lab Manual, 2011

Page 64 of 95

DEPT OF EC

ATRIA IT, BLORE

while(1) { key=keyboard(); lssd(LSSD1_ADD,key); } } //END OF MAIN PROGRAM //FUNCTION TO ACCESS PRESSED KEY LOCATION FROM KEYBOARD unsigned char keyboard() { unsigned char colloc, rowlloc,k,l,i,value; COL = 0xFF; //MAKING COLUMN AS INPUT PORT while(1) { ROW = 0XFE; k=0; l=0; while(1) { ROW=_crol_(ROW,k); colloc = COL; colloc &= 0x0F; if(colloc != 0x0F) { rowlloc = l; break; } k=1; l=l+1; } // CHECK COLUMN AND SEND RESULT TO THE LSSD value=0xFE; colloc=colloc|0xf0; for(i=0;i<4;i++) { if(colloc == value) { return(keypad[rowlloc][i]); break; } value=_crol_(value,1); } } } Microcontroller lab Manual, 2011 Page 65 of 95 //GROUNDING ROW ONE //BY ONE BY ROTATING //THE VALUE IN ROW

DEPT OF EC

ATRIA IT, BLORE

// FUNCTION TO DISPLAY THE KEYBOARD VALUE USING LSSD /*--------------------------------------------------------------------------------------------Module Name : lssd(LED Seven Segment Display) Type :Function File Describition :In this module, LED Seven Segment Display Address and ASCII value of a symbol is accepted as formal arguments.The programmer has a provision todisplay symbol on any one of the 6 digits LED Seven Segment Displayby using this module. if the programmer wants to display more than one symbols on different LED Seven Segment Display, then call the same module with different LED Seven Segment Display Address as formal argument. The ASCII value recieved as formal argument, is compared with the first element of an two dimensional "ASCII_symbol" Array, and if exists, then displayed on the selected LED Seven Segement Display, else simply discarded LED Seven Segment Address List LSSD Name Addresss LSSD0 0X01 LSSD1 0X02 LSSD2 0X04 LSSD3 0X08 LSSD4 0X10 LSSD5 0X20 Note : Common Anode LED Seven Segment Display is used in The 80C51 EDK V1.2 ---------------------------------------------------------------------------------------------*/ void lssd(unsigned char lssd_add,unsigned char symbol) { unsigned char i; for(i = 0; i < 17; i++) { if(ascii_symbol[i][0] == symbol) //check for symbol in lssd decode //lookup table. { LSSD_ADD = lssd_add; //Place corresponding Lssd //address on address bus to display //symbol

LSSD_DATA = ascii_symbol[i][1]; break; } } }

Microcontroller lab Manual, 2011

Page 66 of 95

DEPT OF EC

ATRIA IT, BLORE

5) INTERFACING DAC TO MICROCONTROLLER

+12V 0V -12V

M I C R O C O N T R O L L E R

P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7

D7 D6 D5 D4 D3 D2 D1 D0

D A C I N T E R F A C E C A R D +5V

Analog O/P + CRO -

Microcontroller lab Manual, 2011

Page 67 of 95

DEPT OF EC

ATRIA IT, BLORE

/*INTERFACING DAC TO MICROCONTROLLER*/ include"reg51.h" /*----------------------------------------------------------------------------Module Name : Square Wave Generator Describition : P1 is connected to Digital Data lines of DAC P1.0 = DAC_D7 P1.1 = DAC_D6 P1.2 = DAC_D5 P1.3 = DAC_D4 P1.4 = DAC_D3 P1.5 = DAC_D2 P1.6 = DAC_D1 P1.7 = DAC_D0

V0=Vref(DAC_D7/256+DAC_D6/128+DAC_D5/64+DAC_D4/32+DAC_D3/16+DAC_D2/ 8+DAC_D1/4+DAC_D0/2) V0 is equal to Vref only when 0xff is applied to Digital Data lines of DAC. By varying the delay frequency can be altered ,and by varying the value sent to P1 amplitude can be varied. Timers can also be used as delay generators,and they can be designed for particular frequency -------------------------------------------------------------------------------*/ void main(void) { unsigned int i; while(1) { P1 = 0x00; for(i = 0; i < 2500; i++); P1 = 0xff; for(i = 0; i < 2500; i++); } }

//Assign 00h value to DAC //Data lines via P1 //Delay loop //Assign 0ffh value to DAC //Data lines via P1 //Delay loop

Microcontroller lab Manual, 2011

Page 68 of 95

DEPT OF EC

ATRIA IT, BLORE

#include"reg51.h" /*----------------------------------------------------------------------------Module Name : Sine_wave Generator Describition : P1 is connected to Digital Data lines of DAC P1.0 = DAC_D7 P1.1 = DAC_D6 P1.2 = DAC_D5 P1.3 = DAC_D4 P1.4 = DAC_D3 P1.5 = DAC_D2 P1.6 = DAC_D1 P1.7 = DAC_D0 V0=Vref(DAC_D7/256+DAC_D6/128+DAC_D5/64+DAC_D4/32+DAC_D3/16+DAC_D2/ 8+DAC_D1/4+DAC_D0/2) V0 is equal to Vref only when 0xff is applied to Digital Data lines of DAC. -------------------------------------------------------------------------------*/ void main(void) { unsigned int sine_lk_tble[255] = { 128,131,134,137,140,143,146,149,150,156, 159,162,165,168,171,174,176,179,182,185, 188,191,193,196,199,201,204,206,209,211, 213,216,218,220,222,224,226,228,230,232, 234,236,237,239,240,242,243,245,246,247, 248,249,250,251,252,252,253,254,254,255, 255,255,255,255,255,255,255,255,255,255, 254,254,253,252,252,251,250,249,248,247, 246,245,243,242,240,239,237,236,234,232, 230,228,226,224,222,220,218,216,213,211, 209,206,204,201,199,196,193,191,188,185, 182,179,176,174,171,168,165,162,159,156, 152,149,146,143,140,137,134,128,124,121, 118,115,112,109,106,103,99,96,93,90, 84,81,79,76,73,70,67,64,62,59,56,54,51, 49,46,40,42,39,37,35,33,31,29,27,25,23,21,19,18, 16,15,13,12,10,9,8,7,6,5,4,3,3,2,1,1,0,0,0,0, 0,0,0,0,0,0,0,1,1,2,3,3,4,5,6,7,8,9,10,12, 13,15,16,18,19,21,23,25,27,29,31,33,35,37,39,42,44,46,49,51, 54,56,59,62,64,67,70,73,76,79,81,84,87,90,93,96,99,103,106,109, 112,115,118,121,124 }; int i; while(1) { for(i = 0; i < 255; i++) { P1 = sine_lk_tble[i]; } } } Microcontroller lab Manual, 2011 Page 69 of 95

DEPT OF EC

ATRIA IT, BLORE

#include"reg51.h" /*----------------------------------------------------------------------------Module Name : Triangular Wave Generator P1 is connected to Digital Data lines of DAC P1.0 = DAC_D7 P1.1 = DAC_D6 P1.2 = DAC_D5 P1.3 = DAC_D4 P1.4 = DAC_D3 P1.5 = DAC_D2 P1.6 = DAC_D1 P1.7 = DAC_D0 V0=Vref(DAC_D7/256+DAC_D6/128+DAC_D5/64+DAC_D4/32+DAC_D3/16+DAC_D2/ 8+DAC_D1/4+DAC_D0/2) V0 is equal to Vref only when 0xff is applied to Digital Data lines of DAC. -------------------------------------------------------------------------------*/ void main(void) { unsigned char i,j; while(1) { while(i < 255) { i++; //increment i for triangular_Wave P3 = i; //Assign i value to DAC Data //lines via P1 for(j = 0; j < 10; j++);//Delay loop } while(i > 0) { i--; //decrement i for triangular_Wave P3 = i; //Assign i value to DAC Data lines via P1 for(j = 0; j < 10; j++);//Delay loop } } } Describition :

Microcontroller lab Manual, 2011

Page 70 of 95

DEPT OF EC

ATRIA IT, BLORE

#include"reg51.h" /*----------------------------------------------------------------------------Module Name : UP_Sawtooth Generator Describition : P1 is connected to Digital Data lines of DAC P1.0 = DAC_D7 P1.1 = DAC_D6 P1.2 = DAC_D5 P1.3 = DAC_D4 P1.4 = DAC_D3 P1.5 = DAC_D2 P1.6 = DAC_D1 P1.7 = DAC_D0

V0=Vref(DAC_D7/256+DAC_D6/128+DAC_D5/64+DAC_D4/32+DAC_D3/16+DAC_D2/ 8+DAC_D1/4+DAC_D0/2) V0 is equal to Vref only when 0xff is applied to Digital Data lines of DAC. -------------------------------------------------------------------------------*/ void main(void) { unsigned char i,j; while(1) { i = 0; while(i < 255) { i++; //increment i for UP_Saw_Tooth_Wave P1 = i; //Assign i value to DAC Data lines via P1 for(j = 0; j < 50; j++); //Delay loop } } }

Microcontroller lab Manual, 2011

Page 71 of 95

DEPT OF EC

ATRIA IT, BLORE

6) INTERFACING ELEVATOR TO MICROCONTROLLER

Led 7 7 6 5 4 3 2 1 E L E V A T O R C A R D L E D S 0 P1.0 : P1.7 M I C R O C O N T R O L L E R

Led 0 F L O O R S

P0.0 : P0.7

0 Floors

Microcontroller lab Manual, 2011

Page 72 of 95

DEPT OF EC

ATRIA IT, BLORE

/*INTERFACING ELEVATOR CARD TO MICROCONTROLLER*/ #include"reg51.h" #include"intrins.h" /*----------------------------------------------------------------------------Module Name : Elevator Describition :P1 is connected to LEDS(COMMON ANODE) as Elevtor Output P0 is connected to FLOORS as Elevtor Input **************************************************************************** **/ void main(void) { unsigned char pres_elev_pos; unsigned char next_elev_pos; void delay_msec(unsigned int count); P0 = 0xff; pres_elev_pos = 0xfe; P1 = pres_elev_pos; while(1) { next_elev_pos = P0; if(pres_elev_pos != next_elev_pos && next_elev_pos != 0xff) { while(pres_elev_pos != next_elev_pos) { if(pres_elev_pos > next_elev_pos) { pres_elev_pos = _crol_(pres_elev_pos,1); } else { pres_elev_pos = _cror_(pres_elev_pos,1); } P1 = pres_elev_pos; delay_msec(500); } } } }

Microcontroller lab Manual, 2011

Page 73 of 95

DEPT OF EC

ATRIA IT, BLORE

/*--------------------------------------------------------------------------------------FUNCTION NAME : TIMER0 AS DELAY GENERATOR DESCRIBITION : IN THIS FUNCTION, TIMER/COUNTER0 IS CONFIGURED AS DELAY GENERATOR WITH RESOLUTION OF 1 msec. NOTE : CLOCK/CYCLE=6 TH0TL0 = 65536 - ((11.0592 X 10^6) X DELAY RESOLUTION)/(CLOCK/CYCLE) IF DELAY RESOLUTION = 1 msec. AND CLOCK/CYCLE=6 THEN TH0 TL0 = 65536 - (11.0592 X 10^6) X 1X10^3)/6= F8CCH ----------------------------------------------------------------------------------------*/ void delay_msec(unsigned int count) { unsigned int i; TMOD = 0X01; //CONFIGURE TIMER/COUNTER0 AS TIMER //FOR MODE1(16-BIT COUNTER) TR0 = 1; //START TIMER0 for(i = 0; i < count; i++) // LOOP AS LONG AS REQUIRED DELAY IS //ATTAINED { TH0 = 0XF8; //ASSIGN VALUE TO TIMER0 ` //REGISTER TO //GENERATE 1 mSEC DELAY TL0 = 0XCC; while(!TF0); //LOOP HERE UNTIL TIMER0 OVERFLOW //FLAG GETS SET TF0 = 0; //CLEAR TIMER0 OVERFLOW FLAG TO //CHECK NEXT OVERFLOW } TR0 = 0; //STOP TIMER0 }

Microcontroller lab Manual, 2011

Page 74 of 95

DEPT OF EC

ATRIA IT, BLORE

7) INTERFACING STEPPER MOTOR TO MICROCONTROLLER

STEPPER/DC MOTOR CARD Direction P0.0 M I C R O C O N T R O L L E R

motor connection

SM0 SM1 SM2 SM3

P1.0 P1.1 P1.2 P1.3

+5v Gnd +12v

Microcontroller lab Manual, 2011

Page 75 of 95

DEPT OF EC

ATRIA IT, BLORE

/*8051 C PROGRAM TO INTERFACE STEPPERMOTOR*/ #include"reg51.h" / **************************************************************************** * Module Name : Stepping Motor Descripition : Following MC I/O LINES ARE USED TO INTERFACE WITH STEPPER MOTOR INTERFACING CARD STEPPER MOTOR = P1.0-P1.3 P0.0=1 for change in direction **************************************************************************** **/ sbit direction=P0^0; void main() { char sm_pulsea[4] = {0x0a,0x09,0x05,0x06},i; char sm_pulsec[4]={0x06,0x05,0x09,0x0a}; unsigned int j; direction=1; //making it as an input pin while(1) { if(direction == 1) { for(i = 0; i < 4; i++) { P1 = sm_pulsea[i]; for(j = 0; j < 3000; j++);//speed control //delay routine } } else { for(i = 0; i < 4; i++) { P1 = sm_pulsec[i]; for(j = 0; j <3000; j++);//speed control //delay routine } } } }

Microcontroller lab Manual, 2011

Page 76 of 95

DEPT OF EC

ATRIA IT, BLORE

8) INTERFACING DC MOTOR TO MICROCONTROLLER

STEPPER/DC MOTOR CARD

motor connection

dcm1 dcm2

M I C R O C P1.0 O P1.1 N T R O L L E R

+5v gnd +12v

Microcontroller lab Manual, 2011

Page 77 of 95

DEPT OF EC

ATRIA IT, BLORE

/*8051 C PROGRAM TO INTERFACE DC MOTOR*/ #include"reg51.h" / **************************************************************************** * Module Name : DC Motor Describition : Following MC I/O LINES ARE USED TO INTERFACE WITH DC MOTOR INTERFACING CARD DC MOTOR = P1.0-P1.1 **************************************************************************** **/ void main(void) { unsigned int i; while(1) { P1 = 0x01; //P1=0x02 to change direction for(i = 0; i < 30000; i++); //positive pulse //width P1 = 0x00; for(i = 0; i < 30000; i++); //width } }

//negative pulse

Microcontroller lab Manual, 2011

Page 78 of 95

DEPT OF EC

ATRIA IT, BLORE

VIVA QUESTIONS

Microcontroller lab Manual, 2011

Page 79 of 95

DEPT OF EC

ATRIA IT, BLORE

VIVA QUESTIONS
1. Define Microprocessors and Microcontrollers? 2. State the differences between a Microprocessor and a Microcontroller? 3. Define Von Newman and Harvard Architecture and state their differences? 4. Differentiate between RISC and CISC systems? 5. State some features of 8051 Microcontroller? 6. How many General Purpose Registers are there in 8051? Name them? 7. What do you mean by Register Banks and how do you switch to different Register Banks? 8. State the capacity of the internal RAM and ROM of 8051? 9. Explain the structure of RAM? 10. State the capacity of external RAM/ROM that can be connected to 8051? 11. State the differences between 8051, 8031 and 8052? 12. How many interrupts are there in 8051? 13. How many Timers are there in 8051? Explain the different modes of Timer? 14. State the difference between Timer and Counter? 15. What do you mean by SFRs? Name a few. 16. State the operating frequency of 8051 microcontroller? 17. Define PSW? 18. What do you mean by Flags and how many flags are there in 8051? 19. Explain Stack operation? 20. State the functions of Program Counter and Data Pointer? 21. What is Serial Communication? State its advantages over Parallel Communication. 22. How many ports are there in 8051? Explain them. 23. Differentiate between Latch and Port? 24. Explain the purposes of TCON, TMOD, SCON, and PCON? 25. Explain the Interrupt Priority? 26. Explain IP and IE Registers? 27. What is the purpose of using SJMP at the end of each program? 28. What is the significance of EA pin, ALE pin and PSEN pin? 29. What is the Reset value of Stack Pointer? 30. What do you mean by Assembler Directives? Name a few. 31. State different Addressing Modes? 32. What is the difference between MOVC and MOVX? 33. Differentiate between mov A, #01H and mov A, 01H. 34. State the difference between mov A, 01H and mov C, 01H. 35. State unconditional jump instructions and explain their working ranges.
Microcontroller lab Manual, 2011 Page 80 of 95

DEPT OF EC

ATRIA IT, BLORE

36. What is an Interrupt? Explain what happens when a system is

interrupted? 37. What is Subroutine? 38. What is the difference between ACALL and CCALL? 39. What is the difference between RET and RETI? 40. Name a few conditional jumps (both bit and byte). 41. Explain the following instructions: a) CJNE b) DAA c) MOLAB d) ADDC.
42. Are these instructions valid?

a) b) c) d) e) f)

mov R1, R0 XCH A, R1 XCH R1, A SUBB A, B DA A XCH A,@dptr

43. State Logic data types and their ranges. 44. Explain SFR data types. 45. Calculate the delay in the following programs: a) mov R1, #255 BACK: mov R2, #20 HERE: DJNZ R2, HERE DJNZ R1, BACK 46. Explain modes 0, 1, 2, 3 of Timers. 47. What is framing? 48. What is half duplex and full duplex communication? 49. What is RS232? 50. What is the significance of MAX232? 51. What is synchronous and asynchronous communication? 52. What are UART and USART? 53. What are the different modes of serial communication? 54. What is the significance of TI and RI? 55. What is the significance of PCON and TCON SFR in serial communication? 56. What do you mean by Interrupt Vector table? 57. What is the difference between Interrupt and Polling? 58. What is the difference between Edge triggered and Level triggered interrupts?
Microcontroller lab Manual, 2011 Page 81 of 95

DEPT OF EC

ATRIA IT, BLORE

MODEL QUESTION BANK

Microcontroller lab Manual, 2011

Page 82 of 95

DEPT OF EC

ATRIA IT, BLORE


IA MARKS: 25 EXAM HOURS: 03 EXAM MARKS: 50

QUESTION BANK LAB: 8051 MICROCONTROLLER CODE: 06ESL47

1.a) Write an ALP to move a block of 20 bytes from H to H using internal and external memory. b) Write a 8051 C Program to display ATRIA COLLEGE on LCD. 2.a) Write an ALP to move a block of 10 bytes from H to H using internal and external memory. b) Write a 8051 C Program to interface a dc motor. Show speed change and rotate it in both clockwise and anticlockwise direction. 3.a) Write an ALP to exchange a block of 10 bytes between source location( H) and destination location( H) using internal memory. b) Write a 8051 C program to interface 4 X 4 Hex Keypad and display the key pressed on LSSD. 4.a) Write an ALP to sort an array in ascending order. b) Write an ALP to convert BCD to ASCII and ASCII to BCD. 5.a) Write an ALP to find cube of 8-bit data. b) Write an ALP to convert ASCII to BCD and BCD to ASCII. 6.a) Write an ALP to multiply 16-bit data X 8-bit data. b) Write an ALP to find largest number in an array. 7.a) Write an ALP to implement up counter and down counter. b) Write a 8051 C Program to interface keyboard and LSSD to 8051. 8.a) Write an ALP to convert Hex to Decimal and Decimal to ASCII. b) Write a 8051 C Program to interface keyboard and LSSD to 8051. 9.a) Write an ALP to sort a n bytes of data in descending order. b) Write an ALP to find largest number in an array. 10.a) Write an ALP to toggle P1.2 continuously with some delay. Use mode2, Timer 0, Write an ALP to implement an Up counter. b) Write a 8051 C Program to generate triangular wave, Saw tooth wave and Square wave using DAC interface. 11.a) Write an ALP to add two 16-bit numbers. b) Write an 8051 C Program to interface a stepper/DC motor. 12.a) Write an ALP to subtract two 16-bit numbers. b) Write an ALP to implement a down counter.

Microcontroller lab Manual, 2011

Page 83 of 95

DEPT OF EC

ATRIA IT, BLORE

13.a) Write an ALP to exchange 10H block of data between location 8100H and 8200H. b) Write a 8051 C Program to interface LSSD with microcontrollers. Display numbers from 0 to F on LSSD. 14 (a) Write an ALP to exchange 10 blocks of data between location 0040H and 0060H both using external memory (b) Write a 8051 C Program to interface an elevator card to c 15 (a) Write an ALP to count the pulses at P3. 4 and display the counter value on port P0 using mode 2 and timer 0.Use simulator. (b) Write a 8051 C Program to interface elevator card. 16 (a) Write an ALP to sort N bytes of data in ascending order. (b) Write a 8051 C Program to interface dc motor to 8051. 17.(a) Write an ALP to add 2-16 bit numbers and store result in location 60H, 61H, 62H. (b) Write a 8051 C Program to display WELCOME on LCD. 18(a) Write an ALP to multiply 16-bit data and 8 bit data. (b) Write an ALP to find smallest number in an array. 19(a) Write an ALP to find cube of a 8 bit number. (b) Write an 8051 C program to interface stepper motor to c . 20 (a) Write an ALP to convert Hex to decimal and decimal to ASCII. b) Write a 8051 C program to display numbers from 0 to F using LSSD. 21(a) Write an ALP implement UP and down counter. (b) Write an 8051 C Program to Interface Elevator to 8051. 22a) Write an ALP to realize the following expression: b) Write an 8051 C program to display numbers 0 to 9 on LCD.

Microcontroller lab Manual, 2011

Page 84 of 95

DEPT OF EC

ATRIA IT, BLORE

APPENDIX I 8051 ARCHITECTURE

Microcontroller lab Manual, 2011

Page 85 of 95

DEPT OF EC

ATRIA IT, BLORE

8051 ARCHITECTURE

Block Diagram :

Microcontroller lab Manual, 2011

Page 86 of 95

DEPT OF EC

ATRIA IT, BLORE

Internal Architecture:

Microcontroller lab Manual, 2011

Page 87 of 95

DEPT OF EC

ATRIA IT, BLORE

Pin Details of 8051:

Microcontroller lab Manual, 2011

Page 88 of 95

DEPT OF EC

ATRIA IT, BLORE

Accumulator (A) :
It is most versatile register and used for many operations like adition, subtraction, interger multiplication, division and Boolean bit manipulations. It is also used for all data transfers between 8051 and External memory.

B Register :
Used with A register for multiplication and division operation. It can be used as data storage register.

Program Status Word :

Microcontroller lab Manual, 2011

Page 89 of 95

DEPT OF EC

ATRIA IT, BLORE

RAM memory space allocation in the 8051 :

Program Memory ( ROM ) :

Microcontroller lab Manual, 2011

Page 90 of 95

DEPT OF EC

ATRIA IT, BLORE

Data Memory ( RAM ):

8051 TIMERS:
It has 2 Timers(16 bit). Timer 0 Timer 1 TCON and TMOD are the Special Function Registers (SFR ) associated with timers. Timers are used in 4 different modes. Mode 0 : 13 bit timer mode Mode 1 : 16 bit timer mode Mode 2 : 8 bit auto reload Mode 3 : Split timer mode
Microcontroller lab Manual, 2011 Page 91 of 95

DEPT OF EC

ATRIA IT, BLORE

8051 Serial Port :


It has one serial port. SCON, PCON and SBUF are the SFRs associated with serial port. Modes of operation, Mode 0 : Shift Register Mode, Baud = f/12 Mode 1 : 8 bit UART, Baud variable Mode 2 : 9 bit UART, Baud = f/32 or f/64 Mode 3 : 9 bit UART, Baud variable Where, f oscillator frequency UART Universal Asynchronous Receiver Transmitter

Microcontroller lab Manual, 2011

Page 92 of 95

DEPT OF EC

ATRIA IT, BLORE

8051 Interrupts
It has 6 interrupts. IP, IE and TCON are the SFRs associated with interrupts. Reset External Hardware Interrupt 0(int 0) External Hardware Interrupt 1(int 1) Timer 0 Interrupt (TF 0) Timer 1 Interrupt (TF 1)

Serial Port Interrupt ( TI or RI )

Addressing modes
The 5 addressing modes are :

1) Immediate Addressing Mode


In this addressing mode the data is available as part of the instruction. The immediate data must be preceded by the Pound (#) Sign . Ex :MOV A,#25H ; load 25H into accumulator MOV R4,#62 ; load decimal value 62 into R4 MOV DPTR,#4521h ; load DPTR with value 4521H
2)

Register Addressing Mode


It involves the use of registers to hold the data to be manipulated. Ex :MOV A,R0 MOV R2,A ; copy contents of R0 to Accumulator ; copy contents of Accumulator into R2

NOTE :- Data can be moved between accumulator and registers ( Rn, where n = 0 to 7 ) but not between Rn registers ie., register to register transfer is not allowed. Ex :- MOV R0,R1 is invalid.

Microcontroller lab Manual, 2011

Page 93 of 95

DEPT OF EC
3)

ATRIA IT, BLORE

Direct Addressing Mode


In this addressing mode all the 128 bytes of internal RAM & the memory lacation allocated for SFRs may be addressed and used for data manipulation. Ex :MOV R0,40H ; Copy the contents of memory loc. 40H to R0 MOV 80H,A ; Copy the contents of A to mem. loc. 80H

4) Indirect Addressing Mode


In this , a register is used as a pointer to the data .The pointer register must be preceded by the symbol @. Ex: MOV A,@R0 ; move contents of RAM loc. whose ; address is held by R0 to A. MOV @R1,A ; move contents of Accumulator into ; RAM loc. whose address is held by R1 NOTE :- Only R0 & R1 registers can be used as pointers

5)

Indexed Addressing Mode


This is widely used in accessing data elements of look up tables located in Program ROM space . Ex: MOVC A,@A+DPTR ; the contents of the mem. loc. ; (A+DPTR) is moved to A MOVC A,@A+PC ; the contents of the mem. loc. ; (A+PC) is moved to A NOTE: DPTR Data Pointer PC Program Counter MOVC indicates code memory is accessed

Types of Instructions
1) 2) 3) 4) Data Transfer Instructions ( Internal and External ) Arithmetic Instructions Logical and Boolean Instructions Jump and Call Instructions

Microcontroller lab Manual, 2011

Page 94 of 95

DEPT OF EC

ATRIA IT, BLORE

APPENDIX-II INSTRUCTION SET

Microcontroller lab Manual, 2011

Page 95 of 95

Potrebbero piacerti anche