+ All Categories
Home > Documents > Micro Controller Lab Manual1

Micro Controller Lab Manual1

Date post: 08-Apr-2015
Category:
Upload: tejasviv
View: 686 times
Download: 4 times
Share this document with a friend
102
MICROCONTROLLER LAB ( 06ES L47) I.PROGRAMMING 1. Data Transfer – Block move, Exchange , Sorting, Finding largest element in an array 2. Arithmetic Instructions – Addition / Subtraction , Multiplication and Division , square , Cube – ( 16 bits Arithmetic Operations – bit addressable) 3. Counters 4. Boolean & Logical Instructions (Bit manipulations) 5. Conditional CALL & RETURN 6. Code conversion: BCD – ASCII ; ASCII – Decimal; Decimal – ASCII;HEX – Decimal and Decimal- HEX 7. Programs to generate delay , programs using serial port and on- Chip timer counter . II. 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. Alpha numeric LCD panel and Hex keypad input interface to 8051
Transcript
Page 1: Micro Controller Lab Manual1

MICROCONTROLLER LAB ( 06ES L47)

I. PROGRAMMING

1. Data Transfer – Block move, Exchange , Sorting, Finding largest element in an array

2. Arithmetic Instructions – Addition / Subtraction , Multiplication and Division , square , Cube – ( 16 bits Arithmetic Operations – bit addressable)

3. Counters 4. Boolean & Logical Instructions (Bit manipulations)5. Conditional CALL & RETURN6. Code conversion: BCD – ASCII ; ASCII – Decimal; Decimal –

ASCII;HEX – Decimal and Decimal- HEX7. Programs to generate delay , programs using serial port and on-

Chip timer counter .

II. 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. Alpha numeric LCD panel and Hex keypad input interface to 8051

10.External ADC and Temperature control interface to 805111.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 805113.Elevator interface to 8051

Page 2: Micro Controller Lab Manual1

Procedure to start up with Keil Micro Vision 3

a) Starting microvision 3 Click on keil Micro Vision icon on the desktop

b) Loading a project into MicroVision 3 Click on Project menu , Select Close Project if any Projects are Present or Select New Project from the drop – down menu. Enter the file name .UV 2 and Click on OK Double Click on ATMEL from the wizard then select AT89c51 and Press OK Micro vision 3 will load 8051 Microcontroller Projects file and Display as : Target 1 Source group 1 Startup. A 51

c) Editing and Assembling

Type the program in the work space window Now save the file and right click source group 1 .Select Add files to group ‘ ‘source group’ , Let the files be in ASM . Select the corresponding file from the list and click OK. To assemble select build target , if no error(s) are found the output window will display. “ 8051 MICROCONTROLLER PROJECTS” – O error(s) , 0 warning (s) If error(s) are found then select Rebuild Target an then the Programmer will find it easy to correct the error(s).

d) Debugging To debug Click on debug button.

For memory display, select Memory window icon under View option

Enter the bytes(s) at memory window(address)

Now Click on Run button to run the program continuously

After debugging ends the value will be stored in registers memory and will also be displayed in memory window.

Page 3: Micro Controller Lab Manual1

1. Write a program for moving a block of data from one memory location to other location.

ALGORITHM:

1. Initialize source and destination address.2. Set the block length(counter).3. Transfer data bytes from source address to destination address.4. Increment source and destination address.5. Decrement block length.6. If block length = 0 , halt the program else go back to step3.

Page 4: Micro Controller Lab Manual1

ORG 0000h SJMP 30h ORG 30h MOV DPH, #80h MOV R0, #35h MOV R1, #41h MOV R3, #05h

BACK : MOV DPL,R0 MOVX A ,@DPTR MOV DPL,R1

MOVX @DPTR,AINC R0INC R1DJNZ R3,BACK

HERE: SJMP HERE END

OUTPUT:

Before Execution

X: 0X008035 : 01 02 03 04 05 00 00 00X: 0X008041 : 00 00 00 00 00 00 00 00After Execution

X: 0X008035 : 01 02 03 04 05 00 00 00X: 0X008041 : 01 02 03 04 05 00 00 00

Page 5: Micro Controller Lab Manual1

2.Write a program to exchange a block of data bytes from one location to other location.

Algorithm:

1. Initialize source and destination address.2. Set the block length(counter).3. Exchange data bytes present in source address and destination

address.4. Increment source and destination address.5. Decrement the block length.6. If block length = 0 , halt the program else go back to step3.

Page 6: Micro Controller Lab Manual1

ORG 0000hSJMP 30hORG 30hMOV R0, # 27hMOV R1, # 41hMOV R3, # 05h

BACK: MOVX A, @R0MOV R2,AMOVX A, @R1MOVX @R0,AMOV A, R2MOV @R1,AINC R0INC R1DJNZ R3,BACK

HERE: SJMP HEREEND

OUTPUT:Before Execution X: 0027h : 10 20 30 40 50 00 00 00 00 X: 0041h : 60 70 80 90 75 00 00 00 00

After Execution

Address X : 0027h : 60 70 80 90 75 00 00 00 00 X : 0041h : 10 20 30 40 50 00 00 00 00

3. Write a program to sort the given array in ascending or descending order using bubble sort technique

Algorithm

1. Set the block length(counter).2. Set the number of comparisons (passes).

Page 7: Micro Controller Lab Manual1

3. Initialize source address.4. Get the data from source address.5. Increment source address.6. Compare data between 2 source address.7. If 1st data > 2nd data , decrement the number of comparisons else

exchange the 1st and 2nd data and then decrement the number of comparisons.

8. The number of comparisons are zero , then decrement the block length else go to Step 4 .

9. If block length = 0 , halt the program else go BACK to step3.

Descending Order

ORG 0000h SJMP 30h ORG 30h MOV R0 , # 05h DEC R0

BACK: MOV DPTR, #9000h MOV A, R0 MOV R1,A

UP : MOVX A, @ DPTR MOV B,A INC DPTR MOVX A, @ DPTR CLR C MOV R2,A SUBB A,B JC No exchange MOV A,B MOVX @ DPTR , A DEC DPL MOV A,R2 MOVX @ DPTR , A INC DPTR

No exchange: DJNZ R1, UP DJNZ R0, BACK

HERE : SJMP HERE END

Page 8: Micro Controller Lab Manual1

OUTPUT:

Before Execution X: 9000h : 04 02 06 05 08

After Execution X: 9000h : 08 06 05 04 02

Algorithm

1. Set the block length(counter).2. Set the number of comparisons (passes).3. Initialize source address.4. Get the data from source address.5. Increment source address.6. Compare data between 2 source addresses.7. If 1st data < 2nd data , decrement the number of comparisons else

exchange the 1st and 2nd data and then decrement the number of comparisons .

8. The number of comparisons are zero , then decrement the block length else go to Step 4 .

9. If block length = 0 , halt the program else go back to step3.

Page 9: Micro Controller Lab Manual1

Ascending Order

ORG 0000hSJMP 30hORG 30hMOV R0 , # 05hDEC R0

BACK: MOV DPTR, #9000hMOV A, R0MOV R1,A

UP : MOVX A, @ DPTRMOV B,AINC DPTRMOVX A, @ DPTRCLR CMOV R2,ASUBB A,BJNC No exchangeMOV A,BMOVX @ DPTR , A DEC DPLMOV A,R2MOVX @ DPTR , A INC DPTR

No exchange: DJNZ R1, UPDJNZ R0, BACK

Page 10: Micro Controller Lab Manual1

HERE : SJMP HEREEND

OUTPUT:

Before Execution

X: 9000h : 04 02 06 09 08

After Execution

X: 9000h : 02 04 06 08 09

4. WAP to find the largest element in an array

Algorithm

1. Set the block length.2. Initialize the source address.3. Get the data from source address & assume it as reference data.4. Increment the source address.5. Get the data from source address and assume it as input data .6. Compare input data & the reference data.7. If reference data is greater than the input data , increment the source

address .

Page 11: Micro Controller Lab Manual1

8. Else input data will be the reference data & then increment the source address.

9. Decrement the block length.10.If block length =0, then reference data = largest number else go to

step 4.11.Store the largest number.

ORG 0000HSJMP 30HORG 30HMOV R3,#05HMOV DPTR,#4000HMOVX A,@DPTRMOV R1,A

BACK: INC DPTR

Page 12: Micro Controller Lab Manual1

MOVX A,@DPTRMOV R2,ACLR CSUBB A,R1JC SKIPMOV A,R2MOV R1,A

SKIP: DJNZ R3,BACKMOV DPL,#62HMOV A,R1MOVX @DPTR,A

HERE: SJMP HEREEND

RESULT:

BEFORE EXECUTION

X: 4000H: 23 21 24 28 03 18

AFTER EXECUTION

X:4062H: 28

5..WAP TO FIND THE SMALLEST ELEMENT IN AN ARRAY.

Algorithm

1. Set the block length.2. Initialize the source address.3. Get the data from source address & assume it as reference data.

Page 13: Micro Controller Lab Manual1

4. Increment the source address.5. Get the data from source address and assume it as input data.6. Compare input data & the reference data.7. If reference data is less than the input data , increment the source

address.8. Else , input data will be the reference data & then increment the

source address.9. Decrement the block length.10.If block length =0 then reference data = largest number else go to

step 4.11.Store the smallest number.

ORG 0000H SJMP 30H ORG 30H MOV R3,#05H MOV DPTR,#4000H MOVX A,@DPTR MOV R1,A

BACK: INC DPTR MOVX A,@DPTR MOV R2,A CLR C SUBB A,R1

Page 14: Micro Controller Lab Manual1

JNC SKIP MOV A,R2 MOV R1,A

SKIP: DJNZ R3,BACK MOV DPL,#62H MOV A,R1 MOVX @DPTR,A

HERE: SJMP HERE END

RESULT:

BEFORE EXECUTION

X: 4000H: 23 21 24 28 03 18

AFTER EXECUTION

X:4062H: 03

6. Write a Program to add two 16 – bit numbers

Algorithm

1. Store two 16- bit data into R0, R1 & R2 , R3 Register.2. Add lower bytes of two data’s in R1 & R3.3. Store lower byte result in R4 reg.4. Add higher bytes of two data’s in R0 & R2 along with carry.5. Store higher byte result in R5 reg.6. Strore carry in R6 reg.

Page 15: Micro Controller Lab Manual1

ORG 00HSJMP 30HORG 30HMOV R0, # 1DHMOV R1, # 22HMOV R2, # 33H

MOV R3, # 44HCLR C

MOV A, R1ADD A, R3MOV R4, AMOV A, R0

ADDC A, R2MOV R5,AMOV A, #00H

ADDC A, #00HMOV R6, A

HERE : SJMP HERE END

Page 16: Micro Controller Lab Manual1

RESULT:

BEFORE EXECUTION : AFTER EXECUTION R0= 1DH R0 = 1DH

R1 = 22H R1 = 22H R2 =33H R2 =33H R3 = 44H R3 = 44H R4 = 00 R4 = 66 --SUM

R5 = 00 R5 = 50-- SUM R6 = 00 R6 = 00 -- CARRY

7. WAP to add two 32 bit numbers

Algorithm

1. Set the counter ( no of bytes ).2. Initialize the two source address as 8000h & 8020h 3. Initialize the destination address as 8060h.4. Get the data’s from both source addresses.5. Add data’s along with carry.6. Store the result into destination address & increment it.7. Decrement the counter .8. If counter = 0 , store the carry into destination address.9. Else go to step 4.

Page 17: Micro Controller Lab Manual1

ORG 0000H SJMP 30H ORG 30H MOV DPH, #80H MOV R0, #00H MOV R1, #20H MOV R2, #60H MOV R3, #04H CLR C

UP: MOV DPL, R0 MOVX A, @DPTR MOV R4, A MOV DPL, R1 MOVX A, @DPTR ADDC A, R4 MOV DPL, R2 MOVX @DPTR ,A INC R0 INC R1 INC R2 DJNZ R3, UP MOV A, #00H ADDC A, #00H MOV DPL, R2 MOVX @DPTR, A

HERE: SJMP HERE END

RESULT:

BEFORE EXECUTIONX: 8000H: EE 0A 0C 0B

Page 18: Micro Controller Lab Manual1

X: 8020H: 21 22 23 24

AFTER EXECUTIONX: 8060H: 0F 2C 2F 2F

8 .WAP TO SUBTRACT TWO 16 BIT NUMBERS

ALGORITHM

1. Store two 16 bit data into R0, R1 & R2 ,R3 registers.2. Subtract lower bytes of both the data in R1 & R3 register along with the borrow.3. Store lower byte result in r4 register.4. Subtract higher bytes of both data’s in R0 & R2 register along with borrow.5. Store higher byte result in R5 register.6. End of code.

Page 19: Micro Controller Lab Manual1

ORG 00h SJMP 30hORG 30hMOV R0, # 1DhMOV R1, # 22hMOV R2, # 33hMOV R4, #44hCLR CMOV A, R1SUBB A, R3MOV R4, AMOV A, R0SUBB A, R2MOV R5, A

HERE: SJMP HERE End

Page 20: Micro Controller Lab Manual1

9. WRITE A PROGRAM TO PERFORM 32- BIT SUBTRACTION.

ALGORI THM.

1. Initialize the counter using R3 register.2. Initialize the two source addresses as 8000h & 8020h.3. Initialize the destination address as 8060h.4. Get the data from both source addresses.5. Subtract both data’s along with borrow.6. Store the result in to destination address.7. Increment both the source addresses & the destination address.8. Decrement the counter.9. If counter is zero, end the code.10.Else go to step 4.

Page 21: Micro Controller Lab Manual1

ORG 0000H SJMP 30HORG 30HMOV DPH, # 80HMOV R0, # 00HMOV R1, # 20HMOV R2, #60HMOV R3, #04HCLR C

UP: MOV DPL, R0MOVX A, @DPTRMOV R4, AMOV DPL, R1MOVX A , @DPTRSUBB A, R4MOV DPL, R2MOVX @DPTR, AINC R0INC R1INC R2DJNZ R3,UP

HERE: SJMP HEREEND

RESULT:

Page 22: Micro Controller Lab Manual1

BEFORE EXECUTIONX: 8000H: 10 20 30 40 X: 8020H: 50 60 70 80

AFTER EXECUTIONX: 8060H: 40 40 40 40

10, WRITE A PROGRAM TO FIND SQUARE OF THE GIVEN 8 BIT NUMBER

Algorithm

1. Store the 8bit data at the memory location 8000h.2. Multiply with the same data .3. Store the lower byte result from accumulator into 8001h memory

location & the higher byte result from register B into 8002h memory location.

4. End of code.

Page 23: Micro Controller Lab Manual1

ORG 00H SJMP 30HORG 30HMOV DPTR, # 8000HMOVX A, @DPTRMOV 0F0H , AMUL ABINC DPTRMOVX @DPTR, AMOV A, 0F0H

INC DPTR MOVX @DPTR, A

HERE: SJMP HEREEND

RESULT:

BEFORE EXECUTIONX: 8000H: FF 00 00 00

AFTER EXECUTION

Page 24: Micro Controller Lab Manual1

X: 8000H: FF 01 FE 00

11.Write a program to find the cube of a given number.

Algorithm.

1. Store the 8bit data at the memory location 8000h.2. Multiply with the same data .3. Store the lower byte of result from register A into register R2 &

higher byte of result from reg B into reg R3.4. Multiply the lower byte of reult in reg R2 from the input data. 5. Store the lower byte result from reg A int0 memory location 8001h

& higher byte result from reg B into reg R4.6. Get the higher byte result of previous multiplication from reg R3 as

in step 3 into reg A & multiply with the input data .7. Add along with carry the lower byte of result in reg A & the higher

byte result of previous multiplication in reg R4 & store the result at memory location 8002h.

8. Add along with carry the higher byte of result in reg B with the contents of reg A.

Page 25: Micro Controller Lab Manual1

9. Store the result at memory loaction 8003h.10.End of code.

ORG 00H SJMP 30HORG 30HMOV DPTR, # 8000HMOVX A, @DPTRMOV 0F0H , AMOV R1, AMUL ABMOV R2, AMOV R3, 0F0H MOV 0F0H, R1MUL ABINC DPTRMOVX @DPTR, AMOV R4, 0F0H

Page 26: Micro Controller Lab Manual1

MOV A, R3MOV 0F0H , R1MUL ABADD A, R4INC DPTRMOVX @DPTR, AMOV A, #00HADDC A, 0F0HINC DPTRMOVX @DPTR, A

HERE: SJMP HEREEND

RESULT:

BEFORE EXECUTION

X: 0X008000H: FF 00 00 00 00 00

AFTER EXECUTION X: 0X008020H: FF FF 02 FD 00 00

12. WAP to search the key element in a given array and to display the position of the key element in an array & display the message for search failure using linear search technique

Algorithm

1. Set the block length using reg R0.2. Store the element to be searched in reg R1.3. Store the data 01h in reg R2 which acts like a pointer.4. Initialize the source address as 8000h.5. Get the data from source address & compare it with the content of

memory location 01h.6. If it is equal , store the contents of reg R2(position) at mem

location 8050h.

Page 27: Micro Controller Lab Manual1

7. Else increment the source address & content of reg R2 & decrement block length.

8. If block length is not zero go to step 5.9. Else store the result offh for search failure at mem location 8050h.10.End of code.

ORG 0000H SJMP 30HORG 30HMOV R0, # 05HMOV R1, # 23HMOV R2, #01HMOV DPTR, # 8000H

UP : MOVX A, @DPTRCJNE A, 01H, NEXTSJMP SUCCESS

NEXT : INC R2INC DPTRDJNZ R0,UPMOV R2, # 0FFH

Page 28: Micro Controller Lab Manual1

SUCCESS: MOV A, R2 MOV DPTR, # 8050HMOVX @DPTR,A

HERE: SJMP HEREEND

RESULT:

BEFORE EXECUTION

X: 8000H: 21 24 23 03 08

AFTER EXECUTION

X: 8050H: 03H

13. WAP to evaluate the given boolean expression

ALGORITHM

1. Store two 8 bit data at mem loc’s 8000h & 8001h & move data to reg R0 & R1 respectively.

2. Get the data in reg R0 to reg A.3. Complement the first data then AND it with the second data & store

the result in reg R2.4. Get the second data from reg R1 into reg A.5. Complement the second data & AND it with first data in reg R0.

Page 29: Micro Controller Lab Manual1

6. Logically OR the content of reg A with content of reg R2.7. Increment the memory location to 8002h & store the result.8. End of code.

ORG 00H SJMP 30HORG 30HMOV DPTR, # 8000h

MOVX A, @DPTRMOV R0, A

Page 30: Micro Controller Lab Manual1

INC DPTRMOVX A, @DPTRMOV R1, AMOV A, R0CPL AANL A, R1MOV R2, AMOV A, R1CPL A ANL A, R0ORL A, R2INC DPTR

MOVX @DPTR , A HERE: SJMP HERE

End

RESULT:

BEFORE EXECUTION:X: 0X008000H: 22 25 00 00

AFTER EXECUTION:X: 0X008000H: 22 25 07 00

14. WAP to find 2 out of 5 code.

Algorithm

1. Store the 8bit data at mem location 8000h.

Page 31: Micro Controller Lab Manual1

2. Initialise R2 as 00h & reg R3 as counter.3. Mask higher three bits of data with 0E0h.4. If the result is not zero , the number is not a 2 out of 5 code.Display

0AAh at mem location 8001h.5. If the result is zero, get the original 8 bit data into reg A.6. Count the number of 1's in lower five bits by rotating right the

content of reg A & store in reg R2.7. If the content of reg R2 is equal to 2, the number is a palindrome.

Display 0FFh at memory loaction 8001h.8. Else display 0AAh at mem location 8001h which is not a 2 out of 5

code.9. End of code.

Page 32: Micro Controller Lab Manual1

ORG 00H SJMP 30H ORG 30H MOV DPTR, # 8000h

MOVX A, @DPTR MOV R1,A ANL A, # 0E0H JNZ SKIP MOV R2, #00H MOV R3, # 05H CLR C MOV A, R1

UP: RRC A JNC DOWN INC R2

DOWN : DJNZ R3, UP MOV A, R2 CJNE A , # 02H,SKIP MOV A , #0FFH SJMP STOP

SKIP: MOV A, #0AAH STOP: INC DPTR MOVX @DPTR , A HERE: SJMP HERE

End

RESULT:

BEFORE EXECUTION

X: 0X008000H: 0A 00 00 00 AFTER EXECUTION

X: 0X008000H: 0A FF 00 00

Page 33: Micro Controller Lab Manual1

15 . WAP to find a number is positive or negative

Algorithm

1. Store the 8bit data at mem location 8000h.2. Check MSB of the given data. 3. If MSB=1 , the result is negative .Display 0AAH at mem loc

8001h.4. If MSB=0 , the result is positive.Display 0FFH at mem loc 8001h.5. End of code.

Page 34: Micro Controller Lab Manual1

ORG 0000H SJMP 30HORG 30HMOV DPTR, # 8000h

MOVX A, @DPTRCLR C RLC AJNC DOWNMOV A, # 0AAHSJMP EXIT

DOWN: MOV A, #0FFH EXIT: INC DPTR MOVX @DPTR,A HERE: SJMP HERE

End

RESULT:

BEFORE EXECUTION

X: 8000H: 34H

AFTER EXECUTION

X: 8001H: FFH

BEFORE EXECUTION

X: 8000H: A4H

AFTER EXECUTION

X: 8001H: AAH

Page 35: Micro Controller Lab Manual1

16 .WAP to check the given data even or odd

Algorithm

1. Store the 8bit data at mem location 8000h.2. Clear the carry flag.3. Rotate the content of reg A right along with carry.4. Check the LSB of the given data .5. If LSB=1 , the number is odd. Dispaly 0DDh at mem loc 8001h.6. Else the number is even. Display 0EEh at mem loc 8001h.7. End of code.

Page 36: Micro Controller Lab Manual1

ORG 0000H SJMP 30HORG 30HMOV DPTR, # 8000h

MOVX A, @DPTRCLR C RLC AJNC DOWNMOV A, # 0DDHSJMP EXIT

DOWN: MOV A, #0EEHEXIT: INC DPTR MOVX @DPTR,AHERE: SJMP HERE

End

RESULT:

BEFORE EXECUTION:X: 8000H: 5AH

AFTER EXECUTION:X: 8001H: EEH

BEFORE EXECUTION:X: 8000H: FFH

AFTER EXECUTION;X: 8001H: DDH

Page 37: Micro Controller Lab Manual1

17. WAP to count the number of 1's & 0's

Algorithm

1. Store the 8bit data at mem location 8000h.2. Initialize reg R1 & R2 with 00h.3. Initialize reg R3 as counter.4. Clear the carry flag.5. Rotate the content of reg A along with carry.6. If carry flag is 1 increment R1 else increment R2.7. Decrement the counter.8. If counter=0 ,store the contents of regR1 & reg R2 at mem loc at

8001h & 8002h.9. Else go to step 5.10.End of code.

Page 38: Micro Controller Lab Manual1

ORG 0000H SJMP 30HORG 30HMOV DPTR, # 8000h

MOVX A, @DPTRMOV R1, #00HMOV R2, #00HMOV R3, #08HCLR C

UP: RRC A JNC DOWN

INC R1SJMP EXIT

DOWN : INC R2 EXIT: DJNZ R3, UP INC DPTR

MOV A, R1 MOVX @DPTR,A INC DPTR MOV A,R2

MOVX @DPTR,A HERE: SJMP HERE

End

Page 39: Micro Controller Lab Manual1

RESULT:

BEFORE EXECUTION:X: 8000H: 6AH

:

AFTER EXECUTION:X: 8000H: 6AH X: 8001H: 04H

X: 8002H: 04H

18. WAP to check the given data is bitwise palindrome or not.

Algorithm

1. Store the 8bit data at mem location 8000h.2. Store the input data in reg R2.3. Initialize reg R3 with 00h to get reverse data & reg R4 as counter.4. Rotate input data in reg A right through carry and store it in reg

R1.5. Rotate reverse data with carry left side & store it in reg R3.6. Decrement the counter.7. If counter ≠0, go to step 4.8. Else compare the input data with reverse data.9. If input data = reverse data , the 8 bit data is palindrome & displays

011h at mem loc 8001h.10.Else the 8 bit data is not a palindrome & display 0FFh at mem loc

8001h.11.End of code.

Page 40: Micro Controller Lab Manual1

ORG 0000H

SJMP 30H

ORG 30H

MOV DPTR, # 8000hMOVX A, @DPTRMOV R1, AMOV R2, AMOV R3, #00hMOV R4, #08hCLR C

UP: MOV A, R1 RRC A MOV R1, A MOV A, R3 RLC A

MOV R3, ADJNZ R4, UPMOV A, R2

CJNE A ,03H,DOWN MOV A, #11H

SJMP EXITDOWN: MOV A, #0FFHEXIT: INC DPTR

Page 41: Micro Controller Lab Manual1

MOVX @DPTR, AHERE: SJMP HERE

END

RESULT:

Before Execution : X : 8000h : 66hAfter Execution: X : 8001h : 11h

Before Execution : X : 8000h : 68hAfter Execution: X : 8001h : FFh

19. WAP to check the given data is nibble wise palindrome.

Algorithm

1. Store the 8bit data at mem location 8000h & in reg R1.2. Exchange the nibbles of input data in reg A.3. Compare the data in reg A with the data in reg R1.4. If equal , the 8 bit data is a palindrome & display 011h at mem loc

8001h.5. Else the 8 bit data is not a palindrome & display 0FFh at mem loc

8001h.6. End of code.

Page 42: Micro Controller Lab Manual1

ORG 0000HSJMP 30HORG 30HMOV DPTR, #8000hMOVX A, @DPTRMOV R1,ASWAP ACJNE A , 01H,EXITMOV A, #11HSJMP DOWN

EXIT : MOV A, #0FFHDOWN: INC DPTR

MOVX @DPTR ,AHERE: SJMP HERE

END

RESULT:

Page 43: Micro Controller Lab Manual1

Before Execution : X : 8000h : 77hAfter Execution: X : 8001h : 11h

Before Execution : X : 8000h : ABhAfter Execution: X : 8001h : FFh

19. WAP to perform 16 bit multiplication.

Algorithm

1. Store two16 bit data in registers R0,R1 & R2 R3.2. Intialise reg R4 & R5 with 00h.3. Multiply lower bytes of both data i.e in reg R1 & R3.4. Store lower byte of product in reg A at mem loc 8000h & higher

byte of product in reg B at mem loc 16h.5. Multiply lower byte of 1st data i.e. in reg R1 with higher byte of 2nd

data i.e in reg R2 & store the result at meme loc 17h & 18h respectively.

6. Multiply higher byte of 1st data i.e in reg R0 with lower byte of 2nd data i.e in reg R3 & store the result of product in reg A & at mem loc 19h.

Page 44: Micro Controller Lab Manual1

7. Add lower byte of product i.e in reg A with the data in meme loc 17h & 16h & store the result at mem loc 8001h & the carry generated in reg R4.

8. Multiply higher bytes of both data i.e in reg R0 & R2 & store the result in reg A & at mem loc 20h.

9. Add the lower byte & product in reg A with the data in mem loc 19h & 18h along with carry in reg R4 & store the result at mem loc 8002h & the carry generated in reg R5.

10.Add the carry in reg R5with the data at mem loc 20h & store the result at mem loc 8003h.

11.End of code.

ORG 0000HSJMP 30HORG 30HCLR CMOV R0, #1DhMOV R1, #22hMOV R2, #44hMOV R3, #78hMOV R4, #00hMOV R5, #00hMOV A, R1MOV B, R3MUL AB

Page 45: Micro Controller Lab Manual1

MOV DPTR, # 8000hMOVX @DPTR , AMOV 16h, BMOV A, R1MOV B, R2MUL ABMOV 17H, A

MOV 18H, B MOV A, R0 MOV B, R3

MUL ABMOV 19h, BADD A, 17HJNC DOWNINC R4

DOWN: ADD A, 16h JNC DOWN1

INC R4DOWN1: INC DPTR

MOVX @DPTR, A MOV A,R0 MOV B,R2 MUL AB

MOV 20h, B ADD A, 19h

JNC DOWN2 INC R5

DOWN2: ADD A ,18h JNC DOWN3 INC R5

DOWN3: ADD A,R4 JNC DOWN4 INC R5

DOWN4: INC DPTR MOVX @DPTR , A MOV A, 20h ADD A, R5

INC DPTR MOVX @DPTR , A

HERE: SJMP HERE END

RESULT:

Page 46: Micro Controller Lab Manual1

Before Execution: X : 8000h : F0 After Execution: X : 8001h : AF X : 8002h : CA X : 8003h : 07

21. WAP to find square 16 bit numbers.

ALGORITHM:

1. Store 16 bit data in reg R0R12. Initialize the reg’s R3 & R4 with 00h3. Multiply the lower byte of data by itself i.e data in reg R1 & store

the lower byte of Result in reg A mem loc 8000h & higher byte Result in reg B at mem loc 16h

4. Multiply the lower byte of input data in reg R1 with higher byte of input data in reg R0 & store the lower byte Result in reg A at mem loc 17h & higher byte Result in reg B at mem loc 18h.

Page 47: Micro Controller Lab Manual1

5. Again multiply the lower byte of input data in reg R1 with higher byte of input data in reg R0 & store the lower byte Result in reg A at mem loc 19h & higher byte Result in reg B at mem loc 20h

6. Add the data’s at mem loc’s 19h with data at mem loc 17h & 16h & store the Result at mem loc 8001h & carry generated in reg R3.

7. Multiply the higher byte itself & store lower byte the Result in reg A & higher byte Result at mem loc 21h.

8. Add the data in reg A with the data at mem loc 20h & 18h along with carry in reg R3 & store the Result at mem loc 8002h & the carry generated in reg R4.

9. Add the data at mem loc 21h with the carry in reg R4 & store the Result at mem loc 8003h

10.End

ORG 00HSJMP 30HORG 30HMOV R3, #00hMOV R4,#00hMOV R0, #44hMOV R1,#78hMOV A,R1MOV B, AMUL ABMOV dptr, # 8000hMOVX @dptr , AMOV 16h,BMOV A,R1MOV B,R0MUL AB

Page 48: Micro Controller Lab Manual1

MOV 17h, A MOV 18h, B MOV 19h,A

MOV 20h,Badd A, 17hJNC DOWNINC R3

DOWN: add a, 16hJNC down1INC R3

DOWN1: INC DPTR MOVX @dptr, a

MOV A,R0 MOV B,R0

MUL AB MOV 21h , B add A, 20h JNC down2 INC R4

down2: add a, 18h JNC DOWN4

INC R4down 3: add a,R3 JNC down4

INC R4down 4: INC dptr MOVX @dptr,a

MOV A,21h add A,R4 INC dptr

MOVX @dptr,aHERE: Sjmp HERE

end

RESULT:

Before Execution : X : 8000h : 40hAfter Execution: X : 8001h : F8h

Before Execution : X : 8002h : 4FhAfter Execution: X : 8003h: 12h

Page 49: Micro Controller Lab Manual1

21. WAP to perform 16 bit division

1. Store the 16 bit dividend in regs R0R1 and the 8 bit divisor in reg R2

2. Initialize the regs R4,R5 & B with 00h to store the quotient & no of shifts.

3. Incremenet the no of shifts4. Rotate lower byte of divisor to left with carry & store as lower byte

of divisor in reg R2.5. Rotate higher byte of divisor to left with carry & store as higher

byte of divisor in reg R3.6. Check the carry flag7. If cy=0 go to step 3.8. If cy=1 , rotate lower byte of divisor right with carry & store in reg

R3.9. Rotate higher byte of divisor right with carry & store in reg R2.10.Subtract lower byte of dividend with data in reg R3 & store in reg

R0.11.Subtract higher byte of dividend with data in reg R2 & store in reg

R1.12.Check the carry flag.13.If cy=0, compliment carry else get the original 16 bbit dividend &

then compliment carry.14.Rotate lower byte of quotient in reg R4 with carry left & store in

reg R4.15.Rotate higher byte of quotient in reg R5 with carry left & store in

reg R5.16.Decrement the no of shifts in reg B17.If no of shifta =0, store the data in reg’s R0,& R1 as remainder.18.Else go to step 8. 19.end

Page 50: Micro Controller Lab Manual1

ORG 00HSJMP 30HORG 30HMOV R0 #99hMOV R1#99hMOV R2 #15hMOV R3,#00hMOV R4, #00hMOV R5,#00hMOV B #00hDiv 1: INC BMOV A,R2RLC AMUL ABMOV R2,AMOV A,R3RLC AMOV R3,AJNC Div 1Div 2 : MOV A,R3RRC AMOV R3,AMOV A,R2RRC AMOV R2,ACLR CMOV 07h, R1MOV 06h, R0MOV A, R0SUBB A,R2MOV R0,AMOV A,R1SUBB A,R3MOV R1,AJNC Div 3

Page 51: Micro Controller Lab Manual1

MOV R1, 07hMOV R0,06hDiv 3 CPL CMOV A, R4RLC AMOV R4,AMOV A,R5RLC AMOV R5,ADJNZ B, Div 2MOV R3, 05hMOV R2, 04h

HERE: Sjmp HERE end

RESULT: R0 = 09H R4 = 50H

R1 = 00H R5 = 07HR2 = 50H R6 = 09HR3 = 07H R7 = 00H

22) Write a Program to perform ASCII to BCD/DECIMAL Conversion

Algorithm

Store two ASCII numbers at Memory location 8000h & 8001hMask first data at Memory location 8000h with 0FH & Reverse the

nibbles of the Result.Store BCD obtained in reg R1Mask 2nd data at Memory location 8001h with 0FHStore the Result in reg ALogically OR the data in Reg A with data in reg R1Store the Result at Memory location 8002hEnd of code

Page 52: Micro Controller Lab Manual1

22ORG 00HSJMP 30HORG 30HMOV dptr, # 8000hMOVX A,@dptrANL A, #0FHSWAP AMOV R1,AINC dptrMOVX A,@dptr

Page 53: Micro Controller Lab Manual1

ANL A, #0FHORL A,R1MOVX @dptr,A

HERE : SJMP HEREend

RESULT:

Before Execution : X : 8000h : 35h 39hAfter Execution: X : 8002h : 59h

Before Execution : X : 8000h : 31h 35hAfter Execution: X : 8002h: 15h

23. WAP to perform BCD./decimal to ASCII conversion.

1. Store BCD numbers at mem loc 8000h

Page 54: Micro Controller Lab Manual1

2. Mask the lower 4 bits of input data with 0Fh3. Add 30h to get ASCII equivalent & store the Result at mem loc

8001h.4. Mask the higher 4 bits of input data with F0h & swap the data5. Add 30h to get ASCII equivalent & store the Result at mem loc

8002h6. End

Page 55: Micro Controller Lab Manual1

ORG 00HSJMP 30HORG 30HMOV DPTR, # 8000HMOVX A,@DPTRMOV R1,AANL A, #0FHORL A, #30HINC DPTRMOVX @DPTR,AMOV A,R1ANL A, #0F0HSWAP AORL A, #30HINC DPTRMOVX @DPTR,A

HERE : SJMP HEREEND

RESULT:Before Execution : X : 8000h : 09h After Execution: X : 8001h : 39h X : 8002h : 30h

Page 56: Micro Controller Lab Manual1

24. WAP to perform hexadecimal/binary to decimal conversion

1. Store the hexadecimal data as input at mem loc 8000h2. Divide the input data with 0Ah stored in reg B3. Store the quotient in reg R1 & the remainder in reg B.4. Again divide the quotient with 0Ah & store the quotient at mem

loc 8001h5. Exchange the higher & lower nibble of remainder in reg B & OR it

with quotient.6. Store the Result at mem loc 8002h7. End

Page 57: Micro Controller Lab Manual1

24ORG 00HSJMP 30HORG 30HMOV DPTR, # 8000HMOVX A,@DPTRMOV B, # 0AHDIV ABMOV R1,AMOV R2,BMOV B, # 0AHDIV ABINC DPTRMOVX @DPTR,AMOV A,BSWAP AORL A, R2INC DPTRMOVX @DPTR,A

HERE : SJMP HEREEND

Page 58: Micro Controller Lab Manual1

RESULT:Before Execution : X : 8000h : FFh After Execution: X : 8001h : 02h X : 8002h : 55h

25. WAP to perform decimal to hexadecimal/binary conversion

1. Store the decimal data as input at mem loc 8000h2. Mask the lower 4 bits of input data with 0Fh & swap the data &

multiply with 0Ah.3. Store the lower byte of Result in reg R1.4. Mask the higher 4 bits with 0Fh & add it with the data in reg R1.5. Store the Result at mem loc 8001h6. End.

Page 59: Micro Controller Lab Manual1

ORG 00HSJMP 30HORG 30HMOV DPTR, # 8000HMOVX A,@DPTR

Page 60: Micro Controller Lab Manual1

MOV R1,AANL A, #0FHMOV R2,AMOV A, R1ANL A, #0F0HSWAP AMOV B , #0AHMUL ABADD A, R2INC DPTRMOVX @DPTR,A

HERE : SJMP HEREEND

RESULT:Before Execution : X : 8000h : 44h After Execution: X : 8001h : 2Ch

Page 61: Micro Controller Lab Manual1

26. WAP to perform hexadecimal/binary to ASCII conversion

1. Store the binary data as input at mem loc 8000h2. Mask the higher nibble with 0Fh3. Convert lower nibble to ASCII equivalent by subtracting 0Ah with

it along borrow.if the Result is greater than 0Ah add 30h else add 37h & store the Result at mem loc 8001h.

4. Mask the lower nibble with F0h & swap the data.5. Convert the higher nibble to ASCII equivalent by subtracting 0Ah

with it along with borrow.6. If the Result is greater than 0Ah add 30h else add 37h & store the

Result at mem loc 8002h.7. End

Page 62: Micro Controller Lab Manual1

26ORG 00HSJMP 30HORG 30HMOV DPTR, # 8000HMOVX A,@DPTRMOV R1,AANL A, #0FHACALL ASCIIINC DPTRMOV A,03HMOVX @DPTR,AMOV A, R1ANL A, #0F0HSWAP AACALL ASCIIINC DPTRMOV A,03HMOVX @DPTR,A

ASCII: MOV R2,ACLR C SUBB A, # 0AHMOV A, R2JC DOWNADD A, #07HADD A, #30H

DOWN: ADD A, #30HMOV 03H ,ARET

HERE : SJMP HEREEND

Page 63: Micro Controller Lab Manual1

RESULT:Before Execution : X : 8000h : 9Ah After Execution: X : 8001h : 41h

27. Write a Program to Perform ASCII to hexadecimal binary

1. Store the ASCII data at Memory location 8001h2. Subtract 30h with the ASCII data3. If the Result is not equal to 0Ah check if the Result is greater than

0Ah store the Result at number location 8001h4. Else subtract 07h from the Result & Store it at memory location

8002h 5. End

Page 64: Micro Controller Lab Manual1

ORG 00HSJMP 30HORG 30HMOV DPTR, # 8000HMOVX A,@DPTRMOV R1,ACLR C SUBB A, #30HMOV R2, ACJNE A, # 0AH,EXITEXIT: JC LOOPMOV A, R2SUBB A, # 07HLOOP:INC DPTRMOVX @DPTR,A

HERE : SJMP HEREEND

RESULT:

Page 65: Micro Controller Lab Manual1

Before Execution : X : 8000h : 39h After Execution: X : 8001h : 09h

28.Write a Program UP counter using delay subroutine (Hexadecimal)

ALGORITHM1.Initialise the initial count = 00h2. Call delay3. Add 01h to initial count4. Display the count at Port25.End of code

Page 66: Micro Controller Lab Manual1
Page 67: Micro Controller Lab Manual1

ORG 00H SJMP 30H ORG 30H MOV A, #00HL1: A CALL DELAY ADD A, #01H MOV P2,A SJMP L1DELAY: MOV R1,#0FFHUP2: MOV R2,#0FFHUP1: MOV R3,#20HUP: DJNZ R3,UP DJNZ R2,UP1 DJNZ R1,UP2 RET END

RESULT

PORT 2 00H 01H 1FH FFH

Page 68: Micro Controller Lab Manual1

29 Write a Program DOWN counter using delay subroutine (Hexadecimal)

ALGORITHM1.Initialise the initial count = FFH2. Call delay3. Subtract 01h from initial count4. Display the count at Port25.End of code

Page 69: Micro Controller Lab Manual1

ORG 00H SJMP 30H

Page 70: Micro Controller Lab Manual1

ORG 30H MOV A, #FFHL1: A CALL DELAY SUBB A, #01H MOV P2,A SJMP L1DELAY: MOV R1,#0FFHUP2: MOV R2,#0FFHUP1: MOV R3,#20HUP: DJNZ R3,UP DJNZ R2,UP1 DJNZ R1,UP2 RET END

RESULT

PORT 2 FFh

1Fh 00h

Page 71: Micro Controller Lab Manual1

30.Write a Program to Perform Decimal UP counter using delay subroutine

ALGORITHM1.Initialise the initial value to 00h2. Call delay3. Add 01h to the initial value4. Adjust the count value to decimal5.Display the count value at port26.End of code

Page 72: Micro Controller Lab Manual1

ORG 00H SJMP 30H ORG 30H MOV A, #00HL1: A CALL DELAY ADD A, #01H DAA MOV P2,A SJMP L1DELAY: MOV R1,#0FFHUP2: MOV R2,#0FFHUP1: MOV R3,#20HUP: DJNZ R3,UP DJNZ R2,UP1 DJNZ R1,UP2 RET END

Page 73: Micro Controller Lab Manual1

RESULT

PORT 2 00

50 99

31.Write a Program to Perform Decimal DOWN counter using subroutine

ALGORITHM1.Initialise the initial value to 99h2. Call delay

Page 74: Micro Controller Lab Manual1

3. Add 99h to the initial count4. Adjust the count value to decimal5.Display the count value at port26.End of code

Page 75: Micro Controller Lab Manual1

ORG 00H SJMP 30H ORG 30H MOV A, #00HL1: A CALL DELAY ADD A, #99H DAA MOV P2,A SJMP L1DELAY: MOV R1,#0FFHUP2: MOV R2,#0FFHUP1: MOV R3,#20HUP: DJNZ R3,UP DJNZ R2,UP1 DJNZ R1,UP2 RET END

RESULT

PORT 2 99

50 00

Page 76: Micro Controller Lab Manual1

VIVA QUESTIONSMICROCONTROLLER

1. What is microprocessor?Ans. A general-purpose digital central processing unit.

List the difference between MC & MP.Ans. MC: Has on chip memory & ADC. It has single bit manipulation instructions.

List the types of MCs.Ans. 8-bit MC – 8031/8051 16-bit MC – MC68HC12 , 8096 32-bit MC - 80969, M683xx

4. What is CICS & RISC architecture MC?Ans. CISC: When MC has an instruction set that supports many addressing modes for arithmetic & logical operations.RISC: When MC has an instruction set that supports one or two addressing modes for arithmetic & logical operations.

Page 77: Micro Controller Lab Manual1

5. Difference between Harvard & Princeton architecture.Ans. Harvard architecture: Distinct memory address space for program & data memory.Princeton architecture: Separate memory address space for program & data memory.

6. Examples for Harvard & Princeton architecture.Ans. Harvard architecture: 8051 Princeton architecture: 68HC11

Define I/O port.Ans. Input & output circuit that connects MC to external devices.

8. Define Bus.Ans. Conducting lines that carry information within MC & from MC to external devices

Types of BusesAns. Address Bus: Carries address – 16 bits Data bus: Carries data – 8 bits

10.Crystal frequency used in 8051.Ans. 12Mhz

Function of Program counter (PC).Ans. It is a 16-bit register. PC stores the address of the next instruction to be executed.

12.Capacity of Internal ROM & RAMAns. RAM – 128 bytes ROM – 4K bytes

13.Define CPU.Ans. Heart of the processor that process the data.

14.Function of PSEN signal.Ans. Enables the program memory.

15.Function of EA signal.Ans. Enables the external memory.

16.Number of timers & counters available in 8051.

Page 78: Micro Controller Lab Manual1

Ans. Two 16- bit timers/counters are available in 8051.

17.What do you mean by bit addressable instruction?Ans. The instruction that allows change of a bit in a register is called bit addressable instruction.

18.Two 8-bit memory pointers used in 8051.Ans. R0 & R1 are two 8- bit internal memory pointers.

19.Number of banks available in 8051.Ans. 4 banks are available. Each bank has 8 registers. Each register is of 8 – bits.

20.External data memory is accessed by 16 bit register called data pointer (DPTR)

21.Define Addressing modes. Ans. The way in which the instructions are executed is called Addressing modes.

22.Types of addressing modes.Ans. Indirect addressing modes Direct addressing modes Register Addressing modes Indexed Addressing modes

23.What are Indexed Addressing modes?Ans. Used in accessing data elements of look up table entries located in ROM.Registers DPTR & A are used. Instruction MOVC is used.

24.How to find the relative address?Ans. @A + DPTR = relative address.

25.Define Asynchronous serial communication.Ans. Each character is transmitted at a time.

26. Define synchronous serial communicationAns. Each block of data is transmitted & received at a time.

26.What is Data Framing?Ans. Placing the character between a start bit & stop bit.

Page 79: Micro Controller Lab Manual1

27.Registers used in serial communication.Ans. SBUF & SCON registers.

28.Name two bits of SCON register that is used for selecting the serial mode of communication.

Ans. SM0 & SM1

Importance of TI flags.Ans. Monitors SBUF register to assure that it is not overloaded.

30.How to increase the baud rate?Ans. To use a higher frequency crystal. To change a bit SMOD in the PCON register.

RAM location used as Scratch padAns. 30-7FH

Name the instructions that are bit addressability,Ans. Register A, B, PSW, IP, IE, ACC, SCON and TCON

What is a Flag?Ans. One bit register that indicates the result of the operation.

Flags used in 8051. Ans. AC (Auxiliary carry), OV (over flow), P( parity) & CY (carry).

Bits used in PSW register to select register bank.Ans. RS0 & RS1.

Function of the PCON register?Ans. Doubles the baud rate

Function of TCON register.Ans. Indicates the overflow of timer/counters Controls the start/stop of counters/timers.

Function of TMOD register.Ans. Selects the timer mode of operation Selects the timer/counter.

What is polling?Ans. MC checks the devices in the round robin fashion.

Page 80: Micro Controller Lab Manual1

What is an interrupt?Ans. A signal that request the MC to give its attention.

Advantages of Interrupt.Ans. MC can serve many devices. Priority can be assigned to the devices.

What is ISR?Ans. A small program associated with each interrupt is called ISR.

Six interrupts of 8051Ans. Reset Two timer interrupts One serial communications interrupts Two external interrupts.

Priority of interruptsAns. IE0 TF0 IE1 TF1 RI or TI TF2 or EXF2

Register used for enabling & disabling interrupts.Ans. IE registers.

What is level triggered interrupt?Ans. The interrupt is triggered when the signal is low at INT0 & INT1.

What is edged triggered interrupt?Ans. The interrupt is triggered when the signal makes a transition from high to low level at INT0 & INT1.

What is subroutine?Ans. A small program that is separately written and is being called from the main program is called subroutine.

Instruction used in subroutine.Ans. Call & Return instructions.

What are branching instruction?

Page 81: Micro Controller Lab Manual1

Ans. The instruction that enables the execution of the instruction to jump from one memory location to another location is called as branching instructions.

Types of branching instructions.Ans. Conditional & unconditional branching instructions.

What is Conditional branching instruction?Ans. The Pc jumps to a location specified by the address in instruction based on the condition is called as conditional branching instruction. Eg. JNC, JC, JNZ

What is unconditional branching instruction?Ans. The Pc jumps to a location specified by the address in instruction without any condition is called as unconditional branching instruction. Eg JMP.

Difference between LCALL & ACALL.Ans. LCALL: 16-bit address ACALL: 11- bit address

55. Difference between LJMP & SJMPAns. LJMP: within 64k bytes SJMP: within –128 to +128 bytes

Type of communication used in 8051.Ans. Serial communication

What is USART?Ans. Universal synchronous – asynchronous receiver transmitter.

What is interfacing?Ans. Connecting the MC to external devices is called as interfacing.

MC is connected to the external devices through IO ports.

8051 has 4 8-bit ports (P0, P1, P2, P3)

8255 is one of the most widely used I/O chip.

Connecting I/O chip to the CPU is called memory mapped I/O since memory space is used to access I/O device

Page 82: Micro Controller Lab Manual1

In I/O mapped I/O, I/O devices uses different address.


Recommended