8051 Mazidi Solution

51
Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech Chapter 2 Section 2.1:- 1. 8 bit 2. 8 bit 3. 8 bit 4. PSW (Program Status Word) is of 16 bit. 5. Necessary (for literal value). 6. 28H and it is kept in accumulator. 7. (a),(d),(g) are illegal and for f only 0 is required before F5H 8. (c),(d) are illegal. 9. 44H and kept in Accumulator (A). 10.1EH and kept in Accumulator (A). Section 2.4 21.0000H 22.Program counter will look for the location 0000H and if the program is not starting for that address, it will consider that there is no program written so program has to start from the location 0000H. 26.Lowest Memory is 0000H and the Highest memory is FFFFH.

Transcript of 8051 Mazidi Solution

Page 1: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Chapter 2Section 2.1:-

1. 8 bit2. 8 bit3. 8 bit4. PSW (Program Status Word) is of 16 bit.5. Necessary (for literal value).6. 28H and it is kept in accumulator.7. (a),(d),(g) are illegal and for f only 0 is required before F5H8. (c),(d) are illegal.9. 44H and kept in Accumulator (A).10.1EH and kept in Accumulator (A).

Section 2.4

21.0000H22.Program counter will look for the location 0000H and if the program is not

starting for that address, it will consider that there is no program written so program has to start from the location 0000H.

26.Lowest Memory is 0000H and the Highest memory is FFFFH.

Page 2: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Section 2.5:-

29.Solved below,(Data) (Locations)

E 200A 201R 202T 203H 204

9 2058 2067 207- 2086 2095 20A

Section 2.6:-

31.8 bit32.And 33.

D7 D6 D5 D4 D3 D2 D1 D0CY AC F0 RS1 RS0 OV -- P

34.We know that in 8051 registers are of 8 bits CY Flag is raised when the carry is generated beyond past the max value that a register can store like FFH + 1.

35.AC is raised when a carry is generated from D3 to D4. Like

Mov a,#0FH

Add a,#1

Page 3: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

36.CLR C ;CY=0CPL C; CY

37.Ga. CY=1b. CY=0c. CY=0

38.ORG 0000HMOV A,#55HADD A,#55HADD A,#55HADD A,#55HADD A,#55HEND

39.RS0 and RS1.40.On Startup Stack Location is 07H.42.24 Bytes.43.Register Bank 0

44.Register Bank 0 from 00H to 07H.Register Bank 1 from 08H to 0FH.Register Bank 2 from 10H to 17H.Register Bank 3 from 18H to 1FH.

45.(a) 04H (b) 00H (c) 07H (d) 05H

Page 4: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

48.

INSTRUCTIONS STACK Pointer before

execution.

Stack pointer after

execution.

STACK

PUSH 0 07H 08H 66HPUSH3 08H 09H 7FHPUSH 7 09H 0AH 5DHPOP 3 0AH 09HPOP 7 09H 08HPOP 0 08H 07H

49.NO.POP 7POP 3POP 0REST OF THE PROGRAM WILL REMAIN THE SAME.

50.After execution of ( Mov SP,#70H ), Stack Pointer location has now become 70H instead of 07H.

INSTRUCTIONS STACK Pointer before

execution.

Stack pointer after execution.

STACK

Push 5 70H 71H 66HPush 2 71H 72H 7FHPush 7 72H 73H 5DHPop 7 73H 72HPop 2 72H 71HPop 5 71H 70H

Page 5: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Chapter 3:-Section 3.1:-

12.MOV R1,#100 HERE1:MOV R2,#10HERE:DJNZ R2,HEREDJNZ R1,HERE1

13.MOV R1,#100 HERE2MOV R2,#100HERE1:MOV R3,#10HERE:DJNZ R3,HEREDJNZ R2,HERE1DJNZ R1,HERE2

14.Multiplication is taking place so,200*100=20,000 (times).

15. -128 Bytes16. 127 Bytes

Section 3.2:-

17. 3 byte18. 2 byte19. 2 bytes20. 64Kb21. 2 bytes22. 1 byte

Page 6: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

23.That’s because Stack works on the concept of LIFO so if the push is implied 2 times for instance, then pop must be used 2 times exactly.

Section 3.3:-

27. T = 1.2usec F = 1/T = 833.333KHz System frequency = 833.333KHz*12 = 10MHz

28. F = 18MHz F =18MHZ/12 =1.5MHz T = 1/F =1/1.5MHz T = 0.666usec

29. F = 12MHz F =12MHZ/12 =1MHz T = 1/F =1/1.5MHz T = 1usec

30. F = 25MHz F =25MHZ/12 = 2.08MHz T = 1/F =1/2.08MHz T = 0.48usec

32. F = 11.0592MHz F =11.0592MHZ/12 = 921.6KHz T = 1/F = 1/921.6KHZ T = 1.085usec

Page 7: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

DELAY:MOV R3,#150 1 machine cycleHERE:NOP 1 mc NOP 1NOP 1DJNZ R3,HERE 2 mc RET 2The time delay of the HERE loop is [150(2+1+1+1)]*1.085usec=0.813msecNow for the instruction outside the loop (mov and ret), (2+1)*1.085Usec = 3.25usecNow 0.813ms + 3.25usec = 0.8162msec

33. F = 16MHz F =16MHZ/12 = 1.33333MHz T = 1/F = 1/1.33333MHZT = 0.75usec

DELAY:MOV R3,#200 1HERE:NOP 1 NOP 1 NOP 1 DJNZ R3,HERE 2 RET 2The time delay of the HERE loop is [200(2+1+1+1)]*0.75usec=0.75msecNow for the instruction outside the loop (mov and ret), (2+1)*0.75usec = 2.25usecNow 0.75ms + 2.25usec = 0.75225msec

34. F = 11.0592MHz F =11.0592MHZ/12 = 921.6KHz T = 1/F = 1/921.6KHZT = 1.085usec

Page 8: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

DELAY:MOV R5,#100 1BACK: MOV R2,#200 1AGAIN:MOV R3,#250 1HERE:NOP 1 NOP 1 DJNZ R3,HERE 2 DJNZ R3,AGAIN 2DJNZ R3,BACK 2 RET 2The time delay of the HERE loop is [250(2+1)]*1.085usec=1.085msecThe time delay of the AGAIN loop it repeats 200 times so , 1.085msec*200 = 0.217 + (3*200*1.085usec) = 0.2176sThe time delay of the BACK loop, it is repeated 100 times so 0.2176*100=21.76sec

Time Delay = 21.76sec

35.Try it yourself it’s just like 34 with only 2 loops instead of 3 loope.

36. To 39.For the problems from 36 to 39 everything remains the same except the time delay that is changed due to the change of Microcontroller now you must take Clock of DS89C420/30 Microcontroller i.e equal to 1 rather than 12 that was for 8051.

40.Yes it is 12 times faster because it only have 1 clock and on the other hand 8051 have 12 clocks so if we decrease the clocks our microcontroller becomes faster.

Page 9: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Chapter 4:-

Section 4.1:-

1. 402. VCC 40TH PIN And GND9TH PIN3. 32 Pins4. 8 Pins and from 32 to 39.5. 8 Pins and from 1 to 8.6. 8 Pins and from 21 to 28.7. 8 Pins and from 10 to 17.8. Input 9. P0 (Port 0)10.P1 (Port 1)

11.ORG 000HMOV P1,#0FFH; MAKE IT AN INPUT PORTMOV A,P1MOV P2,AMOV P0,AMOV P3,AEND

12.ORG 000HMOV P2,#0FFH; MAKE IT AN INPUT PORTMOV A,P2MOV P1,AMOV P0,AEND

Page 10: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

13.P3.0 AND P3.114.0000H is the address upon reset.15.

(A) ORG 0000H

BACK:MOV A,#0AAHMOV P1,AMOV P2,ACALL DELAYMOV A,#55HMOV P1,AMOV P2,ASJMP BACK

(B) ORG 0000HMOV A,#0AAH

BACK:MOV P1,AMOV P2,ACALL DELAYCPL AMOV P1,AMOV P2,ASJMP BACK

Section 4.2:-

16.All ports are bit addressable.17.The advantages for Bit addressable mode is that u con manipulate aa single

bit without disturbing and other bits of the port by using Setb and Clr.18.Setb P1.X OrClr P1.X where X can vary from 0 to 7.

Page 11: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

19.No , a whole port cannot be complemented at a time.

20.ORG 0000HSETB P1.2 SETB P1.5

BACK:CALL DELAYCPL P1.2CPL P1.5SJMP BACKEND

21.ORG 0000HSETB P2.5SETB P1.7SETB P1.3

BACK:CALL DELAYCPL P1.3CPL P1.7CPL P2.5SJMP BACKEND

22.ORG 0000HSETB P1.3

BACK:JB P1.3,HERESJMP BACK

HERE:MOV A,#55H

Page 12: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

MOV P2,AEND

23.ORG 0000HSETB P2.7

BACK:JNB P2.7,HERESJMP BACK

HERE:MOVA,#55HMOV P0,ACALL DELAY MOC A,#0AAHMOV P0,ASJMP HEREEND

24.ORG 0000HSETB P2.0

JNB P2.0,HEREMOV A,#99HMOV PI,ASJMP BACK

HERE:MOV A,#66HMOV P1,ABACK:END

Page 13: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

25.ORG 0000HSETB P1.5

AGAIN:JB P1.5,HERESJMP AGAIN

HERE:CLR PI.3CALL DELAYSETB P1.3CALL DELAYCLR P1.3END

26.ORG 0000HBACK:MOV C,P1.3 ;C IS FOR CARRY FLAG.MOV P1,4,CSJMP BACKEND

27. 5TH Bit

28.ORG 0000HBACK:MOV C,P1.7MOV P1.0,CMOV C,P1.6MOV P1.7,CSJMP BACK

Page 14: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Chapter 5:-Section 5.1 and 5,2:-

3. See on page 123 Figure 5-1, and on page 124 figure 5-2.4. Register bank 1 , 2 , 3 share the space with stack because by default stack

starts from 07H and after increment data is stored in 08H on the other hand Register bank 1 address starts from 08H as well see page 123 fig 5-1.

6. It copies the contents of the location 0F0H into the accumulator rather than the value 0F0H.

7. Same as question nos 6.

8. ORG 0000HMOV R0,#50HMOV R1,#40HMOV R3,#30HPUSH 00HPUSH 01HPUSH 03HPOP 1DHPOP 1EHPOP 1FHEND

9. Registers R0 and R1.

10.ORG 0000HMOV A,#0FFHMOV R7,#32MOV R0,#50HNAME:MOV @R0,A

Page 15: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

INC R0CALL DELAYDJNZ R7,NAME

11.ORG 0000HMOV DPTR,#400HMOV R7,#10MOV R0,#30HHERE:CLR AMOVC A,@A+DPTRCALL DELAYINC DPTRMOV @R0,AINC R0DJNZ R7,HEREBACK:SJMP BACKEND

12.ORG 0000MOV P1,#0FFHMOV A,P1MOV R0,A ; R0=xMOV B,R0 ; B=xMUL AB ; MULTIPLY A WITH B ANSWER STORE IN A=x*xDA AMOV R1,A ;R1=x*x Or x^2MOV A,R0 ;A=xMOV B,#2 ; B=2MUL AB ; A=2*xDA AMOV R7,#5

Page 16: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

ADD A,R1 ; A=x^2 + 2*xDA AADD A,R7 ;A=x^2 + 2*x + 5END

13.ORG 0000HLJMP MAINORG 20HMYDATA: DB 06,09,02,05,07

ORG 300HMain:MOV R0,#30HMOV DPTR,#MYDATAMOV R7,#5

HERE:CLR AMOVC A,@A+DPTRCALL DELAY INC DPTRPUSH 0E0H; Push the Accumulator into stackDJNZ R7,HERE

POP 01POP 02POP 03POP 04POP 0E0HADD A,R1ADD A R2ADD A,R3ADD A,R4; A Holds the added data.

Page 17: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

MOV @R0,AEND

Section 5.3:-

14.INVALID15.VALID16.VALID17.All ports are bit addressable.18.See for answer on page124 figure 5-2.19. (b),(c),(d),((f),(g),(h) are valid.

20.ORG 0000HAGAIN:SETB P1.5CALL DELAYCALL DELAYCALL DELAYCLR P1.5CALL DELAYSJMP AGAIN

DELAY:MOV R1,#240HERE:DHNZ R1,HERERETEND

21.ORG 0000H

AGAIN:SETB P2.7CALL DELAYCALL DELAYCALL DELAY

Page 18: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

CALL DELAYCLR P2.7CALL DELAYSJMP AGAIN

DELAY:MOV R1,#240HERE:DJNZ R1,HERERETEND

22.ORG 0000HSETB P1.4HERE:JNB P1.4,HERE

CMD:SETB P2,7CALL DELAYCLR P2.7CALL DELAYSJMP CMD

DELAY:MOV R1,#240HERE:DJNZ R1,HERERETEND

23.ORG 0000HSETB P2.1HERE:JB P1.4,HEREMOV P0,#55HSJMP $END

Page 19: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

24.80H TO 87H25.90H TO 97H26.A0H TO A7H27.B0H TO B7H28.Not bit addressable register.29.88H TO 8FH30.E0H TO E7H31.F0H TO F7H32.D0H TO D7H33.(a) P0 (b)87H (c)TCON (d)TCON (e)P1H (f)P2 (g)P2 (h)P3 (i)PSW (j)PSW (K)B

34.ORG 0000HSETB RS1; FOR SELECTING REGISTER BANK 2SETB RS0;FOR SELECTING REGISTER BANK 2MOV R3,AMOV R5,BEND

35.CLR 0D7H37.See example 5-14.38.To check the carry flag there are instructions namely JC and JNC.39. And 40 see example 5-14. 41.CY0D7H

P0D0HAC0D6HOV0D2H

42.For this question see page 124 fig 5-2.46.For this question see page 123 fir 5-1.47.(a)20H (b)28H (c)18H (d)2DH (e)53H (f)15H (g)2CH (h)2AH (i)14H (j)37H

(k)7FH

Page 20: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

50.MOV 04,C51.MOV 16H,0D6H ; Auxiliary carry 52.MOV 12H,0D0H

53.ORG 0000HJB ACC.0,HERESJMP AGAINHERE :JB ACC.1,HERE1SJMP AGAINHERE1:MOV B,#4DIV ABAGAIN:END

54.ORG 0000HJB ACC.7,LCD_DISPLAYSJMP NACKLCD_DISPLAY:

NACK:END

55.ORG 0000HJB 0F7H,HERESJMP NACKLCD_DISPLAY:

NACK:END

Page 21: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

56.(A)ORG 0000HMOV R0,#24H; PROGRAM IS DONE WITH THE HELP OF FIG 5-1. MOV A,#0FFH MOV @R0,A MOV R0,#25H MOV A,#0FFH MOV @R0,A END

(B)ORG 0000HSETB 20HSETB 21HSETB 22HSETB 23HSETB 24HSETB 25HSETB 26HSETB 27HSETB 28HSETB 29HSETB 2AHSETB 2BHSETB 2CHSETB 2DHSETB 2EHSETB 2FHEND

Page 22: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

57.ORG 0000HMOV B,#8DIV ABCJNE B,#00,HERESJMP FINHERE:MOV R0,AFIN:END

58.ORG 0000HMOV R1,#8BACK:MOV A,R2RRC A; Rotate Right through carry means instead of 8 bit rotation it JC HERE ;include carry flag as an MSB(most significant bit).INC R0HERE:DJNZ R1,BACKEND

Section 5.4:-

67.ORG 0000GMOV A,#55HMOV R0,#0C0HMOV R7,#16

HERE:MOV @RO,AINC R0CALL DELAYDJNZ R7,HEREEND

Page 23: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

68.ORG 0000HMOV R7,#16MOV R1,#60HMOV R0,#0D0H

HERE:MOV A,@R1INC R1MOV @R0,AINC R0CALL DELAYDJNZ R7,HEREEND

Page 24: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Chapter 6:-Section 6.1:-

1. (a) AC=1 (b)AC=1 (c)AC=1 (e)AC=1Cy= 0 CY=0 CY=1 CY=1

2. Already been done in the lab, see your lab files.

3. ORG 0000HMOV DPTR,#MYDATAMOV R7,#9MOV R2,#00HMOV R3,#00HBACK:CLR AMOVC A,@A+DPTRMOV R3,APUSH 03INC DPTRDJNZ R7,BACK

MOV R7,#9CLR AAGAIN:POP 00ADD A,R0JNC HERE1INC R2HERE1:DJNZ R7,AGAINMOV R3,ASJMP $ORG 250H

Page 25: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

MYDATA: DB 53,94,56,92,74,65,43,23,83END

4. Just use DA( Decimal Adjust instruction in question 3)

5. (a) AND (b)

ORG 0000HMOV R0,#40HMOV R7,#16MOV A,#55HHERE:MOV @R0,AINC R0DJNZ R7,HERE

MOV R7,#16MOV R1,#60HMOV R0,#40HCLR ABACK:ADD A,@R0JNC HERE1INC @R1HERE1:INC R0DJNZ R7,BACKMOV R0,#61HMOV @R0,ASJMP $END

9. ORG 0000HMOV R4,#00H

Page 26: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

MOV A,#48HMOV R0,#9AHADD A,R0MOV R7,AMOV A,#0BCHMOV R0,#7FHADDC A,R0 ;ADC is add with carry, if by adding A and R0 MOV R6,A ;Carry generates .What will it do , it will add both A and R0 With MOV A,#34H;the Upside to it, it will also add the carry flag if it generates.MOV R0,#89HADDC A,R0MOV R5,AJNC HEREINC R4

HERE:MOV R0,#40HMOV A,R4MOV @R0,AINC R0MOV A,R5MOV @R0,AINC R0MOV A,R6MOV @R0,AINC R0MOV A,R7MOV @R0,AEND Here R5=BE,R6=3BH,R7=E2.The and is BE3BE2H.

Page 27: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

12.ORG 0000Hmov a,#77mov b,#34mulabend

13.ORG 0000Hmov a,#77mov b,#3divabend

14.No, Only on A and B.

15.ORG 0000HMOV DPTR,#MYDATAMOV R0,#30HCALL TRANSFERCALL ADDITIONCALL AVERAGELJMP FIN

TRANSFER:MOV R7,#9HERE:CLR AMOVC A,@A+DPTRMOV @R0,AINC DPTR INC R0DJNZ R7,HERERET

Page 28: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

ADDITION:MOV R7,#9MOV R0,#30HCLR AHERE1:ADD A,@R0INC R0DJNZ R7,HERE1RET

AVERAGE:MOV B,#9DIV ABMOV R7,ARET

ORG 250HMYDATA: DB 3,9,6,9,7,6,4,2,8

FIN:END

Section 6.3:-

23.(a) A=40h (b)A=F6H (c)A=86HRest do it yourself just use Keil write instruction and see the result in project window.

24.Just as in Question no 23 write those instruction in Kiel and view the result of accumulator in Project window.

Page 29: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

27.There is no such instruction like CJE.28.In this question you must monitor the status of the carry flag after the

execution of CJNE. Write a program below and check the status of the carry flag in the PSW resister.

(a) ORG 0000HBACK:MOV A,#25H ;Here the carry flag will go high.Always rememberCJNE A,#44H,over ; that carry will only go high when the value of source SJMP BACK ; of CJNE instruction is greater than its destination.OVER:END ;

(b)ORG 0000Hback:mov a,#0ffh ;Here carry flag will not go high as the value of thecjne a,#6fh,over ;destination is greater than that of the source.sjmp back

over:end

Rest of the parts of question 28 now you can do them on your own.

30.(a)MOV A,#56HSWAP A; What swap do is swap the upper and lower nibble now A becomes ; A=65H RR A RR A

Page 30: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

(C)CLR CMOV A,#4DH ; A=0100 1101BSWAP A; A=D4 OR A= 1101 0100BRRC A ;9 BIT ROTATION, A= 0 0110 1010B. You can see the zero before ;8-bit that is the carry bit that you included through RRC instructin.RRC A ; A= 0 0011 0101bRRC A ; A= 1 0001 1010b

32.ORG 0000HMOV P1,#0FFHMOV R7,#8MOV A,P1AGAIN:RRC AJC HEREINC R0HERE:DJNZ R7,AGAINEND

33.ORG 0000HMOV R7,#8MOV A,#68HAGAIN:RRC AJC HEREINC R0HERE:DJNZ R7,AGAINEND

34.ORG 0000HMOV R7,#8MOV A,#68H

Page 31: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

AGAIN:RLC AJC HEREINC R0HERE:DJNZ R7,AGAINEND

40.ORG 0000HMOV R7,#9MOV P1,#0FFHAGAIN1:MOV A,P1ANL A,#0FHORL A,#30HMOV R1,A

MOV R4,#34HHERE:CJNE A,#30H,HERE1SJMP BACKHERE1:CJNE A,#31H,HERE2SJMP BACKHERE2:CJNE A,#32H,HERE3SJMP BACKHERE3:CJNE A,#33H,HERE4SJMP BACKHERE4:CJNE A,#34H,HERE5SJMP BACKHERE5:CJNE A,#35H,HERE6SJMP BACKHERE6:CJNE A,#36H,HERE7SJMP BACKHERE7:CJNE A,#37H,HERE8SJMP BACK

Page 32: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

HERE8:CJNE A,#38H,HERE9SJMP BACKHERE9:CJNE A,#39H,HERE10SJMP BACKHERE10:ANL A,#0FHADD A,#37HCJNE A,#41H,HERE11SJMP BACKHERE11:CJNE A,#42H,HERE12SJMP BACKHERE12:CJNE A,#43H,HERE13SJMP BACKHERE13:CJNE A,#44H,HERE14SJMP BACKHERE14:CJNE A,#45H,HERE15SJMP BACKHERE15:CJNE A,#46H,AGAIN1

BACK:MOV P2,ASJMP $ END

Page 33: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

43.This program is same as the check-sum program, right next to 6-36 example. The only difference is here you have to find the checksum byte of a whole sentence and in the program you had to find the check sum of HEX values. I point out the difference that has to make for this program the rest of the program will remain the same,------------------------------------------------------DATA_ADDR EQU 400HCOUNT EQU 31 ; Nos of characters in the whole sentence.RAM_ADDR EQU 20H

ORG 0000HCALL COPY_DATACALL CAL_CHKSUMCALL TEST_CHKSUM

COPY_DATA: --- ;THERE SUBROUTINES ARE PRESENTIS THE BOOK Pg 172.-------------------- --------------------RETCAL_CHKSUM:---------------------------------------------RETTEST_CHKSUM:-----------------------------------------------RETORG 400HMYBYTE: DB ‘Hello, my fellow World citizens’END

Page 34: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

46.ORG 0000HMOV R7,#9MOV P1,#0FFHAGAIN1:MOV A,P1ANL A,#0FHORL A,#30HMOV R1,A

MOV R4,#34HHERE:CJNE A,#30H,HERE1SJMP BACKHERE1:CJNE A,#31H,HERE2SJMP BACKHERE2:CJNE A,#32H,HERE3SJMP BACKHERE3:CJNE A,#33H,HERE4SJMP BACKHERE4:CJNE A,#34H,HERE5SJMP BACKHERE5:CJNE A,#35H,HERE6SJMP BACKHERE6:CJNE A,#36H,HERE7SJMP BACKHERE7:CJNE A,#37H,HERE8SJMP BACKHERE8:CJNE A,#38H,HERE9SJMP BACKHERE9:CJNE A,#39H,AGAIN1BACK:MOV P2,A

Page 35: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

SJMP $ END

Page 36: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Chapter 9:-

1. 2 timers2. 16 bit, Timer 0 and Timer 1.3. TH0 AND TL0.4. TH1 AND TL1.5. NO,These register are not bit addressable.6. 8 bit 7. TMOD is used to initialize the Timer0 or Timer1 and also Mode of timer to

which we have to use.it also let us to select that weather we have to use Timer or Counter.

8. No9. Use the Figure 9-3 of TMOD register.

Gate C/T M1 M0 Gate C/T M1 M00 1 1 0 0 1 1 0

10.Just Divide the XTAL values with 12 for frequencies and for the time period take the inverse of frequence.

11.(a)13 bit (b)16 bit (c)8 bit12.(a)Mode 08192 in decimal you can find out by 2^(13)=8192, that is

because our Mode is of 13 bit and 2000H(b)Mode 165536 in decimal 2^(16)=65536, it is 16 bit and HEX Value is FFFFH.(c)Mode 2256 in decimal 2^(8)-256, it is 8 bit and HEX value is FFH.

Page 37: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

16.TF0 flag is raised when value of TH and TL rolls over, which means after FFFFH in TH and TL is incrimented, TF0 goes high.XTAL=11.0592MhzTH=1CH and TL=12H so by combining both it becomes 1C12H A 16 BIT VALUE.Frequency = 11.0592Mhz/12 = 921.6KhzTime = 1/F = 1/921.6Khz = 1.085usecNow 1C12H is a HEX value, you must subtract it with the max vale of 16 bit in decimal i.e FFFFH=65536 and for 1C12H=7186. Now you must subtract 65536-7186=58350value is in decimal convert it in HEX to get58350=E3EEH. Now Multiply 58350 with 1.085usec,58350*1.085usec = 63.3msec. Which means it generate the time delay of 63.3msec approximately.TF flag will roll over when 1C12H will complete its counts till FFFFH or Simply it count like this 1C12,1C13,1C14,1C15 and onwards till it reach FFFFH and after FFFFH it raises the TF0 flag. You can also test it like this, just add 1C12H+E3EE=10000H, you can see that 10000H, now TL0=00H, TH0=00H and TF0=1,this shows that the value is rolled over.

17.TF0 flag is raised when value of TH and TL rolls over, which means after FFFFH in TH and TL is incremented, TF0 goes high.XTAL=16MhzTH=1CH and TL=12H so by combining both it becomes 1C12H A 16 BIT VALUE.Frequency = 16Mhz/12 = 1.33333MhzTime = 1/F = 1/1.33333Mhz = 0.75usec

Page 38: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

Now 1C12H is a HEX value, you must subtract it with the max vale of 16 bit in decimal i.e FFFFH=65536 and for 1C12H=7186. Now you must subtract 65536-7186=58350�value is in decimal convert it in HEX to get58350=E3EEH. Now Multiply 58350 with 1.085usec,58350*0.75usec = 47.3msec. Which means it generate the time delay of 47.3msec approximately

18.TF0 flag is raised when value of TH and TL rolls over, which means after FFFFH in TH and TL is incrimented, TF0 goes high.XTAL=11.0592MhzTH=0F2H and TL=10H so by combining both it becomes F210H a 16 BIT VALUE.Frequency = 11.0592Mhz/12 = 921.6KhzTime = 1/F = 1/921.6Khz = 1.085usecNow F210H is a HEX value, you must subtract it with the max vale of 16 bit in decimal i.e FFFFH=65536 and for F210H=61968. Now you must subtract 65536-61968=3568 this value is in decimal convert it in HEX to get3568=0DFH. Now Multiply 3568 with 1.085usec,3568*1.085usec = 3.87msec. Which means it generate the time delay of 3.87msec approximately.TF flag will roll over when F210H will complete its counts till FFFFH or Simply it count like this F210,F211,F212,F213 and onwards till it reach FFFFH and after FFFFH it raises the TF0 flag. You can also test it like this, just add F210H+0DF0H=10000H, you can see that 10000H, now TL0=00H, TH0=00H and TF0=1,this shows that the value is rolled over.

19.It can be done by only changing the XTAL value and calculate rest.

Page 39: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

20.XTAL = 11.0592Mhz, and need to find the delay 0f 2msec, Timer 1 is programed in mode 1.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecFor the Time delay of 2msec = 2msec/1.085usec = 1843 in decimal.Now subtract it from the max value that can be stored in TH and TL combined, so 65536-1843 = 63693, now convert it in HEX to get 63693 = F8CDH, this is the value to be loaded in TH and TL, now to write the program.ORG 0000HMOV TMOD,#10HMOV TL1,#0CDHMOV TH1,#0F8HSETB TR1;To start the Timer TR0 must be set high.JNB TF1,$CLR TR1;To stop the timer.CLR TF1END

21.Frequency = 16Mhz/12 = 1.33333MhzTime Period (Machine Cycle)= 1/T = 1/1.33333Mhz = 0.75usecTime Period for 2msec = 2msec/0.75usec = 2667 in decimal.65536-2667 = 62869 for HEX F595H

ORG 0000HMOV TMOD,#10HMOV TL1,#95HMOV TH1,#0F5HSETB TR1;To start the Timer TR0 must be set high.

Page 40: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

JNB TF0,$CLR TR1CLR TF1END

22.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecTime Period for 2msec = 2.5msec/1.085usec =2304 in decimal.65536-2304 = 63232 for HEX F700H

ORG 0000HMOV TMOD,#01H ;For Timer 0.MOV TL0,#00HMOV TH0,#0F7HSETB TR0 ;To start the Timer TR0 must be set high.JNB TF0,$CLR TR0CLR TR0END

23.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecTime Period for 2msec = 0.2msec/1.085usec = 184in decimal.65536-184 = 65352 for HEX FF48H

ORG 0000HMOV TMOD,#10H ;For Timer 1.MOV TL1,#48HMOV TH1,#0FFHSETB TR1;To start the Timer TR0 must be set high.JNB TF1,$

Page 41: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

CLR TF1CLR TR1END

24.Frequency = 20Mhz/12 = 1.66666MhzTime Period (Machine Cycle)= 1/T = 1/1.66666Khz = 0.6usecTime Period for 100msec = 100msec/0.6usec = 166667 in decimal.Now we can see that 166667 is greater than 65536 that is the max value that can be stored in TH and TL.So now divide 166667 with any value that makes it less than 65536, here I divide it by 5,166667/5 = 33333 in decimal now this value is less than 166667, OK now remember the value that you divide 166667 with. Now,65536-33333 = 32203 for HEX 7DCBH

ORG 0000HMOV TMOD,#10H ;For Timer 1.MOV R0,#5HERE:MOV TL1,#0CBH ;You must load TH and TL again as it is not anMOV TH1,#7DH ;auto-reload mode.SETB TR1 ;To start the Timer TR0 must be set high.HERE1:JNZTF1,HERE1CLR TF1CLR TR1DJNZ R0,HEREEND

Page 42: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

25.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecTime Period for the lowest square wave you must put the maximum value in TH and TL i.e FFFEH and it should be divided by 2 because of the square wave to get 7FFFH.

ORG 0000HSETB P1.2MOV TMOD,#10H ;For Timer 1.HERE:MOV TL1,#0FFHMOV TH1,#7FHSETB TR1;To start the Timer TR0 must be set high.JNB TF1,$CLR TF1CLR TR1CPL P1.2SJMP HEREEND

26.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecTime Period for the Highest square wave you must put the minimum value in TH and TL i.e 0000H because it only has to increment all the way from 0000h to FFFFH to roll over mean from 0000H, 0001H, 0002H, till 10000H

ORG 0000HSETB P1.2MOV TMOD,#10H ;For Timer 1.HERE:MOV TL1,#00HMOV TH1,#0FFHSETB TR1;To start the Timer TR0 must be set high.

Page 43: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

JNb TF1,$CPL P1.2CLR TR1CLR TF1SJMP HEREEND

27. AND 28. Are solved just like question 25 and 26, only change that occurs is to change the values of crystal frequency to 16Mhz instead of 12Mhz, and then find the remaining value and write a program.

29.Timer goes through states that are from F1H, F2H, F3H, F4H, F5H, F6H, F7H, F8H, F9H, FAH, FBH, FCH, FDH, FEH, FFH and lastly 100 which makes the Timer in mode 2 to roll over, total of 16 state.

30.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecFor 1Khz frequency,

T = 1/F = 1/1Khz = 1msec

Time of wave must be divided by 2 because of high time and low time.T = 1msec/2 = 0.5msec.

Now time for Delay = 0.5msec/1.085usec = 461 in decimal for HEX 1CDH.65536-461 = 65075 in decimal for HEX FE33H is the value to load in TH and TL.

ORG 0000HMOV TMOD,#10H ;For Timer 1.

Page 44: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

HERE:MOV TL1,#33HMOV TH1,#0FEHSETB TR1 ;To start the Timer TR0 must be set high.JNB TF1,$CLR TR1CLR TF1SJMP HEREEND

31.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usecFor 3Khz frequency,

T = 1/F = 1/3Khz = 0.3333msec

Time of wave must be divided by 2 because of high time and low time.T = 0.33333msec/2 = 0.166666msec.

Now time for Delay = 0.166667msec/1.085usec = 154 in decimal for HEX 09AH.

65536-154 = 65382 in decimal for HEX FF66H is the value to load in TH and TL.

ORG 0000HMOV TMOD,#01H ;For Timer 1.HERE:MOV TL0,#66HMOV TH0,#0FFHSETB TR0;To start the Timer TR0 must be set high.JNB TF0,$CLR TR0

Page 45: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

CLR TF0SJMP HEREEND

32.Frequency = 20Mhz/12 = 1.66667MhzTime Period (Machine Cycle)= 1/T = 1/1.66667Mhz = 0.6usecFor 0.5Khz frequency,

T = 1/F = 1/0.5Khz = 2msec

Time of wave must be divided by 2 because of high time and low time.T = 2msec/2 = 1msec.

Now time for Delay = 1msec/0.6usec = 1667 in decimal for HEX 683H.

65536-1667 = 63869 in decimal for HEX F97DH is the value to load in TH and TL.

ORG 0000HMOV TMOD,#01H ;For Timer 1.HERE:MOV TL0,#7DHMOV TH0,#0F9HSETB TR0 ;To start the Timer TR0 must be set high.JNB TF0,$CLR TR0CLR TF0SJMP HEREEND

Page 46: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

33.Frequency = 20Mhz/12 = 1.66667MhzTime Period (Machine Cycle)= 1/T = 1/1.66667Mhz = 0.6usecFor 10Khz frequency,

T = 1/F = 1/10Khz = 50usec

Time of wave must be divided by 2 because of high time and low time.T = 50usec/2 = 25usec.

Now time for Delay = 25usec/0.6usec = 42 in decimal for HEX 02AH.

65536-42 = 65494in decimal for HEX FFD6H is the value to load in TH and TL.

ORG 0000HHERE:MOV TMOD,#01H ;For Timer 1.HERE:MOV TL0,#D6HMOV TH0,#0FFHSETB TR0 ;To start the Timer TR0 must be set high.JNB TF0,$CLR TR0CLR TF0SJMP HEREEND

Page 47: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

34.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec

Time (T) = 1sec/1.085usec = 921659.

921659 this value is greater than 65536 so 921659/17 = 54215 in decimal and for HEX 0D3C7H.

65536-54215 = 11321 in decimal for HEX 2C39H is the value to load in TH and TL.

ORG 0000HMOV R0,#17 ;Value that we divided 921659 by.MOV TMOD,#01H ;For Timer 1.HERE:MOV TL0,#39HMOV TH0,#2CHSETB TR0 ;To start the Timer TR0 must be set high.JNB TF0,$CLR TR0CLR TF0DJNZ R0,HEREEND

Page 48: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

35.Frequency = 16Mhz/12 = 1.33333MhzTime Period (Machine Cycle)= 1/T = 1/1.33333Mhz = 0.75usec

Time (T) = 0.25sec/0.75usec = 333333.

333333 this value is greater than 65536, so 333333/33 = 10101 in decimal and for HEX 2775H.

65536-10101 = 55435 in decimal for HEX D88BH is the value to load in TH and TL.

ORG 0000HMOV R0,#33 ;Value that we divided 333333 by.MOV TMOD,#01H ;For Timer 1.HERE:MOV TL0,#8BHMOV TH0,#0D8HSETB TR0 ;To start the Timer TR0 must be set high.JNB TF0,$CLR TR0;To stop the Timer.CLR TF0DJNZ R0,HEREEND

Page 49: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

36.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec

Lowest square wave we must take the largest value i.e 256 i.e 0FFH is HEX for TH because this is mode 2 = 2*256*1.085usec = 0.555msec and for Frequence =1/T = 1.8Khz.

ORG 0000HMOV TMOD,#20HSETB P1.3MOV TH1,#0FFHAGAIN:SETB TR1JNB TF1,$CLR TR1CLR TF1CPL P1.3SJMP AGAINEND

37.Frequency = 11.0592Mhz/12 = 921.6KhzTime Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec

Highest square wave we must take the smallest value i.e 00H for TH because this is mode and the Maximum Frequency is 0.45Mhz, T = 2*1*1.085usec = 2.1usec, and for the Frequency = 1/T = 1/2.1usec = 460Khz.

Page 50: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech

ORG 0000HMOV TMOD,#20HSETB P1.3MOV TH1,#01AGAIN:SETB TR1JNB TF1,$CLR TR1CLR TF1CPL P1.3SJMP AGAINEND

38.And 39. Are the same, the only difference is instead of XTAL = 11.0596Mhz its 16Mhz.

Rest of the Questions Inshaa Allah (God Will’s) I will E-mail you after EID.

Page 51: 8051 Mazidi Solution

Microcontroller Solutions From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech