You are on page 1of 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.

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

Section 2.5:-

29.Solved below, (Data) E A R T H 9 8 7 6 5

(Locations) 200 201 202 203 204 205 206 207 208 209 20A

Section 2.6:31.8 bit 32.And 33. D7 D6 CY AC

D5 F0

D4 RS1

D3 RS0

D2 OV

D1 --

D0 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

Microcontroller Solutions
From Ali Akbar Siddiqui. 36.CLR C ;CY=0 CPL C; CY 37.G a. CY=1 b. CY=0 c. CY=0
Sir Syed University of Eng& Tech

38.ORG 0000H MOV A,#55H ADD A,#55H ADD A,#55H ADD A,#55H ADD A,#55H END 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

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

INSTRUCTIONS

PUSH 0 PUSH3 PUSH 7 POP 3 POP 7 POP 0

STACK Pointer before execution. 07H 08H 09H 0AH 09H 08H

Stack pointer after execution. 08H 09H 0AH 09H 08H 07H

STACK

66H 7FH 5DH

49.NO. POP 7 POP 3 POP 0 REST 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 Push 5 Push 2 Push 7 Pop 7 Pop 2 Pop 5 STACK Pointer before execution. 70H 71H 72H 73H 72H 71H Stack pointer after execution. 71H 72H 73H 72H 71H 70H STACK 66H 7FH 5DH

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

Chapter 3:Section 3.1:12.MOV R1,#100 HERE1:MOV R2,#10 HERE:DJNZ R2,HERE DJNZ R1,HERE1 13.MOV R1,#100 HERE2MOV R2,#100 HERE1:MOV R3,#10 HERE:DJNZ R3,HERE DJNZ R2,HERE1 DJNZ R1,HERE2 14.Multiplication is taking place so, 200*100=20,000 (times). 15. 16. -128 Bytes 127 Bytes

Section 3.2:17. 18. 19. 20. 21. 22. 3 byte 2 byte 2 bytes 64Kb 2 bytes 1 byte

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

23.Thats 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 F = 18MHz F =18MHZ/12 =1.5MHz T = 1/F =1/1.5MHz T = 0.666usec F = 12MHz F =12MHZ/12 =1MHz T = 1/F =1/1.5MHz T = 1usec F = 25MHz F =25MHZ/12 = 2.08MHz T = 1/F =1/2.08MHz T = 0.48usec F = 11.0592MHz F =11.0592MHZ/12 = 921.6KHz T = 1/F = 1/921.6KHZ T = 1.085usec

28.

29.

30.

32.

Microcontroller Solutions
From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech DELAY:MOV R3,#150 1 machine cycle HERE:NOP 1 mc NOP 1 NOP 1 DJNZ R3,HERE 2 mc RET 2 The time delay of the HERE loop is [150(2+1+1+1)]*1.085usec=0.813msec Now for the instruction outside the loop (mov and ret), (2+1)*1.085Usec = 3.25usec Now 0.813ms + 3.25usec = 0.8162msec 33. F = 16MHz F =16MHZ/12 = 1.33333MHz T = 1/F = 1/1.33333MHZ T = 0.75usec DELAY:MOV R3,#200 1 HERE:NOP 1 NOP 1 NOP 1 DJNZ R3,HERE 2 RET 2 The time delay of the HERE loop is [200(2+1+1+1)]*0.75usec=0.75msec Now for the instruction outside the loop (mov and ret), (2+1)*0.75usec = 2.25usec Now 0.75ms + 2.25usec = 0.75225msec 34. F = 11.0592MHz F =11.0592MHZ/12 = 921.6KHz T = 1/F = 1/921.6KHZ T = 1.085usec

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

DELAY:MOV R5,#100 1 BACK: MOV R2,#200 1 AGAIN:MOV R3,#250 1 HERE:NOP 1 NOP 1 DJNZ R3,HERE 2 DJNZ R3,AGAIN 2 DJNZ R3,BACK 2 RET 2 The time delay of the HERE loop is [250(2+1)]*1.085usec=1.085msec The time delay of the AGAIN loop it repeats 200 times so , 1.085msec*200 = 0.217 + (3*200*1.085usec) = 0.2176s The 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 its 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.

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

Chapter 4:Section 4.1:1. 40 2. VCC 40TH PIN And GND9TH PIN 3. 32 Pins 4. 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 000H MOV P1,#0FFH; MAKE IT AN INPUT PORT MOV A,P1 MOV P2,A MOV P0,A MOV P3,A END 12.ORG 000H MOV P2,#0FFH; MAKE IT AN INPUT PORT MOV A,P2 MOV P1,A MOV P0,A END

Microcontroller Solutions
From Ali Akbar Siddiqui. 13.P3.0 AND P3.1 14.0000H is the address upon reset. 15. (A) ORG 0000H BACK:MOV A,#0AAH MOV P1,A MOV P2,A CALL DELAY MOV A,#55H MOV P1,A MOV P2,A SJMP BACK (B) ORG 0000H MOV A,#0AAH BACK:MOV P1,A MOV P2,A CALL DELAY CPL A MOV P1,A MOV P2,A SJMP BACK
Sir Syed University of Eng& Tech

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.

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 0000H SETB P1.2 SETB P1.5 BACK:CALL DELAY CPL P1.2 CPL P1.5 SJMP BACK END 21.ORG 0000H SETB P2.5 SETB P1.7 SETB P1.3 BACK:CALL DELAY CPL P1.3 CPL P1.7 CPL P2.5 SJMP BACK END 22.ORG 0000H SETB P1.3 BACK:JB P1.3,HERE SJMP BACK HERE:MOV A,#55H

Microcontroller Solutions
From Ali Akbar Siddiqui. MOV P2,A END
Sir Syed University of Eng& Tech

23.ORG 0000H SETB P2.7 BACK:JNB P2.7,HERE SJMP BACK HERE:MOVA,#55H MOV P0,A CALL DELAY MOC A,#0AAH MOV P0,A SJMP HERE END 24.ORG 0000H SETB P2.0 JNB P2.0,HERE MOV A,#99H MOV PI,A SJMP BACK HERE:MOV A,#66H MOV P1,A BACK: END

Microcontroller Solutions
From Ali Akbar Siddiqui. 25.ORG 0000H SETB P1.5 AGAIN:JB P1.5,HERE SJMP AGAIN HERE:CLR PI.3 CALL DELAY SETB P1.3 CALL DELAY CLR P1.3 END 26.ORG 0000H BACK:MOV C,P1.3 ;C IS FOR CARRY FLAG. MOV P1,4,C SJMP BACK END 27. 5TH Bit
Sir Syed University of Eng& Tech

28.ORG 0000H BACK:MOV C,P1.7 MOV P1.0,C MOV C,P1.6 MOV P1.7,C SJMP BACK

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 0000H MOV R0,#50H MOV R1,#40H MOV R3,#30H PUSH 00H PUSH 01H PUSH 03H POP 1DH POP 1EH POP 1FH END 9. Registers R0 and R1. 10.ORG 0000H MOV A,#0FFH MOV R7,#32 MOV R0,#50H NAME:MOV @R0,A

Microcontroller Solutions
From Ali Akbar Siddiqui. INC R0 CALL DELAY DJNZ R7,NAME 11.ORG 0000H MOV DPTR,#400H MOV R7,#10 MOV R0,#30H HERE:CLR A MOVC A,@A+DPTR CALL DELAY INC DPTR MOV @R0,A INC R0 DJNZ R7,HERE BACK:SJMP BACK END 12.ORG 0000 MOV P1,#0FFH MOV A,P1 MOV R0,A ; R0=x MOV B,R0 ; B=x MUL AB ; MULTIPLY A WITH B ANSWER STORE IN A=x*x DA A MOV R1,A ;R1=x*x Or x^2 MOV A,R0 ;A=x MOV B,#2 ; B=2 MUL AB ; A=2*x DA A MOV R7,#5
Sir Syed University of Eng& Tech

Microcontroller Solutions
From Ali Akbar Siddiqui. ADD A,R1 ; A=x^2 + 2*x DA A ADD A,R7 ;A=x^2 + 2*x + 5 END 13.ORG 0000H LJMP MAIN ORG 20H MYDATA: DB 06,09,02,05,07 ORG 300H Main:MOV R0,#30H MOV DPTR,#MYDATA MOV R7,#5 HERE:CLR A MOVC A,@A+DPTR CALL DELAY INC DPTR PUSH 0E0H; Push the Accumulator into stack DJNZ R7,HERE POP 01 POP 02 POP 03 POP 04 POP 0E0H ADD A,R1 ADD A R2 ADD A,R3 ADD A,R4; A Holds the added data.
Sir Syed University of Eng& Tech

Microcontroller Solutions
From Ali Akbar Siddiqui. MOV @R0,A END
Sir Syed University of Eng& Tech

Section 5.3:14.INVALID 15.VALID 16.VALID 17.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 0000H AGAIN:SETB P1.5 CALL DELAY CALL DELAY CALL DELAY CLR P1.5 CALL DELAY SJMP AGAIN DELAY:MOV R1,#240 HERE:DHNZ R1,HERE RET END 21.ORG 0000H AGAIN:SETB P2.7 CALL DELAY CALL DELAY CALL DELAY

Microcontroller Solutions
From Ali Akbar Siddiqui. CALL DELAY CLR P2.7 CALL DELAY SJMP AGAIN DELAY:MOV R1,#240 HERE:DJNZ R1,HERE RET END 22.ORG 0000H SETB P1.4 HERE:JNB P1.4,HERE CMD:SETB P2,7 CALL DELAY CLR P2.7 CALL DELAY SJMP CMD DELAY:MOV R1,#240 HERE:DJNZ R1,HERE RET END 23.ORG 0000H SETB P2.1 HERE:JB P1.4,HERE MOV P0,#55H SJMP $ END
Sir Syed University of Eng& Tech

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

24.80H TO 87H 25.90H TO 97H 26.A0H TO A7H 27.B0H TO B7H 28.Not bit addressable register. 29.88H TO 8FH 30.E0H TO E7H 31.F0H TO F7H 32.D0H TO D7H 33.(a) P0 (b)87H (c)TCON (d)TCON (e)P1H (f)P2 (g)P2 (h)P3 (i)PSW (j)PSW (K)B 34.ORG 0000H SETB RS1; FOR SELECTING REGISTER BANK 2 SETB RS0;FOR SELECTING REGISTER BANK 2 MOV R3,A MOV R5,B END 35.CLR 0D7H 37.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 P0D0H AC0D6H OV0D2H 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

Microcontroller Solutions
From Ali Akbar Siddiqui. 50.MOV 04,C 51.MOV 16H,0D6H ; Auxiliary carry 52.MOV 12H,0D0H 53.ORG 0000H JB ACC.0,HERE SJMP AGAIN HERE :JB ACC.1,HERE1 SJMP AGAIN HERE1:MOV B,#4 DIV AB AGAIN: END 54.ORG 0000H JB ACC.7,LCD_DISPLAY SJMP NACK LCD_DISPLAY: NACK: END 55.ORG 0000H JB 0F7H,HERE SJMP NACK LCD_DISPLAY: NACK: END
Sir Syed University of Eng& Tech

Microcontroller Solutions
From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech 56. (A)ORG 0000H MOV 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 0000H SETB 20H SETB 21H SETB 22H SETB 23H SETB 24H SETB 25H SETB 26H SETB 27H SETB 28H SETB 29H SETB 2AH SETB 2BH SETB 2CH SETB 2DH SETB 2EH SETB 2FH END

Microcontroller Solutions
From Ali Akbar Siddiqui. 57.ORG 0000H MOV B,#8 DIV AB CJNE B,#00,HERE SJMP FIN HERE:MOV R0,A FIN: END
Sir Syed University of Eng& Tech

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

Section 5.4:67.ORG 0000G MOV A,#55H MOV R0,#0C0H MOV R7,#16 HERE:MOV @RO,A INC R0 CALL DELAY DJNZ R7,HERE END

Microcontroller Solutions
From Ali Akbar Siddiqui. 68.ORG 0000H MOV R7,#16 MOV R1,#60H MOV R0,#0D0H HERE:MOV A,@R1 INC R1 MOV @R0,A INC R0 CALL DELAY DJNZ R7,HERE END
Sir Syed University of Eng& Tech

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=1 Cy= 0 CY=0 CY=1 CY=1 2. Already been done in the lab, see your lab files. 3. ORG 0000H MOV DPTR,#MYDATA MOV R7,#9 MOV R2,#00H MOV R3,#00H BACK:CLR A MOVC A,@A+DPTR MOV R3,A PUSH 03 INC DPTR DJNZ R7,BACK MOV R7,#9 CLR A AGAIN:POP 00 ADD A,R0 JNC HERE1 INC R2 HERE1:DJNZ R7,AGAIN MOV R3,A SJMP $ ORG 250H

Microcontroller Solutions
From Ali Akbar Siddiqui. MYDATA: DB 53,94,56,92,74,65,43,23,83 END
Sir Syed University of Eng& Tech

4. Just use DA( Decimal Adjust instruction in question 3) 5. (a) AND (b) ORG 0000H MOV R0,#40H MOV R7,#16 MOV A,#55H HERE:MOV @R0,A INC R0 DJNZ R7,HERE MOV R7,#16 MOV R1,#60H MOV R0,#40H CLR A BACK:ADD A,@R0 JNC HERE1 INC @R1 HERE1:INC R0 DJNZ R7,BACK MOV R0,#61H MOV @R0,A SJMP $ END 9. ORG 0000H MOV R4,#00H

Microcontroller Solutions
From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech MOV A,#48H MOV R0,#9AH ADD A,R0 MOV R7,A MOV A,#0BCH MOV R0,#7FH ADDC 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,#89H ADDC A,R0 MOV R5,A JNC HERE INC R4 HERE:MOV R0,#40H MOV A,R4 MOV @R0,A INC R0 MOV A,R5 MOV @R0,A INC R0 MOV A,R6 MOV @R0,A INC R0 MOV A,R7 MOV @R0,A END Here R5=BE,R6=3BH,R7=E2.The and is BE3BE2H.

Microcontroller Solutions
From Ali Akbar Siddiqui. 12.ORG 0000H mov a,#77 mov b,#34 mulab end 13.ORG 0000H mov a,#77 mov b,#3 divab end 14.No, Only on A and B. 15.ORG 0000H MOV DPTR,#MYDATA MOV R0,#30H CALL TRANSFER CALL ADDITION CALL AVERAGE LJMP FIN TRANSFER: MOV R7,#9 HERE: CLR A MOVC A,@A+DPTR MOV @R0,A INC DPTR INC R0 DJNZ R7,HERE RET
Sir Syed University of Eng& Tech

Microcontroller Solutions
From Ali Akbar Siddiqui. ADDITION: MOV R7,#9 MOV R0,#30H CLR A HERE1: ADD A,@R0 INC R0 DJNZ R7,HERE1 RET AVERAGE: MOV B,#9 DIV AB MOV R7,A RET ORG 250H MYDATA: DB 3,9,6,9,7,6,4,2,8 FIN: END
Sir Syed University of Eng& Tech

Section 6.3:23.(a) A=40h (b)A=F6H (c)A=86H Rest 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.

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 0000H BACK:MOV A,#25H ;Here the carry flag will go high.Always remember CJNE 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 0000H back:mov a,#0ffh ;Here carry flag will not go high as the value of the cjne 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,#56H SWAP A; What swap do is swap the upper and lower nibble now A becomes ; A=65H RR A RR A

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

(C)CLR C MOV A,#4DH ; A=0100 1101B SWAP A; A=D4 OR A= 1101 0100B RRC 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 0101b RRC A ; A= 1 0001 1010b 32.ORG 0000H MOV P1,#0FFH MOV R7,#8 MOV A,P1 AGAIN:RRC A JC HERE INC R0 HERE:DJNZ R7,AGAIN END 33.ORG 0000H MOV R7,#8 MOV A,#68H AGAIN:RRC A JC HERE INC R0 HERE:DJNZ R7,AGAIN END 34.ORG 0000H MOV R7,#8 MOV A,#68H

Microcontroller Solutions
From Ali Akbar Siddiqui. AGAIN:RLC A JC HERE INC R0 HERE:DJNZ R7,AGAIN END 40.ORG 0000H MOV R7,#9 MOV P1,#0FFH AGAIN1:MOV A,P1 ANL A,#0FH ORL A,#30H MOV R1,A MOV R4,#34H HERE:CJNE A,#30H,HERE1 SJMP BACK HERE1:CJNE A,#31H,HERE2 SJMP BACK HERE2:CJNE A,#32H,HERE3 SJMP BACK HERE3:CJNE A,#33H,HERE4 SJMP BACK HERE4:CJNE A,#34H,HERE5 SJMP BACK HERE5:CJNE A,#35H,HERE6 SJMP BACK HERE6:CJNE A,#36H,HERE7 SJMP BACK HERE7:CJNE A,#37H,HERE8 SJMP BACK
Sir Syed University of Eng& Tech

Microcontroller Solutions
From Ali Akbar Siddiqui. HERE8:CJNE A,#38H,HERE9 SJMP BACK HERE9:CJNE A,#39H,HERE10 SJMP BACK HERE10:ANL A,#0FH ADD A,#37H CJNE A,#41H,HERE11 SJMP BACK HERE11:CJNE A,#42H,HERE12 SJMP BACK HERE12:CJNE A,#43H,HERE13 SJMP BACK HERE13:CJNE A,#44H,HERE14 SJMP BACK HERE14:CJNE A,#45H,HERE15 SJMP BACK HERE15:CJNE A,#46H,AGAIN1 BACK: MOV P2,A SJMP $ END
Sir Syed University of Eng& Tech

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 400H COUNT EQU 31 ; Nos of characters in the whole sentence. RAM_ADDR EQU 20H ORG 0000H CALL COPY_DATA CALL CAL_CHKSUM CALL TEST_CHKSUM COPY_DATA: --- ;THERE SUBROUTINES ARE PRESENTIS THE BOOK Pg 172. --------------------------------------RET CAL_CHKSUM:------------------------------------------RET TEST_CHKSUM:--------------------------------------------RET ORG 400H MYBYTE: DB Hello, my fellow World citizens END

Microcontroller Solutions
From Ali Akbar Siddiqui. 46.ORG 0000H MOV R7,#9 MOV P1,#0FFH AGAIN1:MOV A,P1 ANL A,#0FH ORL A,#30H MOV R1,A MOV R4,#34H HERE:CJNE A,#30H,HERE1 SJMP BACK HERE1:CJNE A,#31H,HERE2 SJMP BACK HERE2:CJNE A,#32H,HERE3 SJMP BACK HERE3:CJNE A,#33H,HERE4 SJMP BACK HERE4:CJNE A,#34H,HERE5 SJMP BACK HERE5:CJNE A,#35H,HERE6 SJMP BACK HERE6:CJNE A,#36H,HERE7 SJMP BACK HERE7:CJNE A,#37H,HERE8 SJMP BACK HERE8:CJNE A,#38H,HERE9 SJMP BACK HERE9:CJNE A,#39H,AGAIN1 BACK: MOV P2,A
Sir Syed University of Eng& Tech

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

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

Chapter 9:1. 2. 3. 4. 5. 6. 7. 2 timers 16 bit, Timer 0 and Timer 1. TH0 AND TL0. TH1 AND TL1. NO,These register are not bit addressable. 8 bit 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. No 9. Use the Figure 9-3 of TMOD register. Gate C/T M1 M0 Gate C/T M1 M0 0 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 bit 12.(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.

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.0592Mhz TH=1CH and TL=12H so by combining both it becomes 1C12H A 16 BIT VALUE. Frequency = 11.0592Mhz/12 = 921.6Khz Time = 1/F = 1/921.6Khz = 1.085usec 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=58350value is in decimal convert it in HEX to get 58350=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=16Mhz TH=1CH and TL=12H so by combining both it becomes 1C12H A 16 BIT VALUE. Frequency = 16Mhz/12 = 1.33333Mhz Time = 1/F = 1/1.33333Mhz = 0.75usec

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=58350value is in decimal convert it in HEX to get 58350=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.0592Mhz TH=0F2H and TL=10H so by combining both it becomes F210H a 16 BIT VALUE. Frequency = 11.0592Mhz/12 = 921.6Khz Time = 1/F = 1/921.6Khz = 1.085usec Now 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 get 3568=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.

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.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec For 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 0000H MOV TMOD,#10H MOV TL1,#0CDH MOV TH1,#0F8H SETB TR1;To start the Timer TR0 must be set high. JNB TF1,$ CLR TR1;To stop the timer. CLR TF1 END 21.Frequency = 16Mhz/12 = 1.33333Mhz Time Period (Machine Cycle)= 1/T = 1/1.33333Mhz = 0.75usec Time Period for 2msec = 2msec/0.75usec = 2667 in decimal. 65536-2667 = 62869 for HEX F595H ORG 0000H MOV TMOD,#10H MOV TL1,#95H MOV TH1,#0F5H SETB TR1;To start the Timer TR0 must be set high.

Microcontroller Solutions
From Ali Akbar Siddiqui. JNB TF0,$ CLR TR1 CLR TF1 END
Sir Syed University of Eng& Tech

22.Frequency = 11.0592Mhz/12 = 921.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec Time Period for 2msec = 2.5msec/1.085usec =2304 in decimal. 65536-2304 = 63232 for HEX F700H ORG 0000H MOV TMOD,#01H ;For Timer 0. MOV TL0,#00H MOV TH0,#0F7H SETB TR0 ;To start the Timer TR0 must be set high. JNB TF0,$ CLR TR0 CLR TR0 END 23.Frequency = 11.0592Mhz/12 = 921.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec Time Period for 2msec = 0.2msec/1.085usec = 184in decimal. 65536-184 = 65352 for HEX FF48H ORG 0000H MOV TMOD,#10H ;For Timer 1. MOV TL1,#48H MOV TH1,#0FFH SETB TR1;To start the Timer TR0 must be set high. JNB TF1,$

Microcontroller Solutions
From Ali Akbar Siddiqui. CLR TF1 CLR TR1 END
Sir Syed University of Eng& Tech

24.Frequency = 20Mhz/12 = 1.66666Mhz Time Period (Machine Cycle)= 1/T = 1/1.66666Khz = 0.6usec Time 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 0000H MOV TMOD,#10H ;For Timer 1. MOV R0,#5 HERE:MOV TL1,#0CBH ;You must load TH and TL again as it is not an MOV TH1,#7DH ;auto-reload mode. SETB TR1 ;To start the Timer TR0 must be set high. HERE1:JNZTF1,HERE1 CLR TF1 CLR TR1 DJNZ R0,HERE END

Microcontroller Solutions
From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech 25.Frequency = 11.0592Mhz/12 = 921.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec Time 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 0000H SETB P1.2 MOV TMOD,#10H ;For Timer 1. HERE:MOV TL1,#0FFH MOV TH1,#7FH SETB TR1;To start the Timer TR0 must be set high. JNB TF1,$ CLR TF1 CLR TR1 CPL P1.2 SJMP HERE END 26.Frequency = 11.0592Mhz/12 = 921.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec Time 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 0000H SETB P1.2 MOV TMOD,#10H ;For Timer 1. HERE:MOV TL1,#00H MOV TH1,#0FFH SETB TR1;To start the Timer TR0 must be set high.

Microcontroller Solutions
From Ali Akbar Siddiqui. JNb TF1,$ CPL P1.2 CLR TR1 CLR TF1 SJMP HERE END
Sir Syed University of Eng& Tech

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.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec For 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 0000H MOV TMOD,#10H

;For Timer 1.

Microcontroller Solutions
From Ali Akbar Siddiqui. Sir Syed University of Eng& Tech HERE:MOV TL1,#33H MOV TH1,#0FEH SETB TR1 ;To start the Timer TR0 must be set high. JNB TF1,$ CLR TR1 CLR TF1 SJMP HERE END 31.Frequency = 11.0592Mhz/12 = 921.6Khz Time Period (Machine Cycle)= 1/T = 1/921.6Khz = 1.085usec For 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 0000H MOV TMOD,#01H ;For Timer 1. HERE:MOV TL0,#66H MOV TH0,#0FFH SETB TR0;To start the Timer TR0 must be set high. JNB TF0,$ CLR TR0

Microcontroller Solutions
From Ali Akbar Siddiqui. CLR TF0 SJMP HERE END
Sir Syed University of Eng& Tech

32.Frequency = 20Mhz/12 = 1.66667Mhz Time Period (Machine Cycle)= 1/T = 1/1.66667Mhz = 0.6usec For 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 0000H MOV TMOD,#01H ;For Timer 1. HERE:MOV TL0,#7DH MOV TH0,#0F9H SETB TR0 ;To start the Timer TR0 must be set high. JNB TF0,$ CLR TR0 CLR TF0 SJMP HERE END

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

33.Frequency = 20Mhz/12 = 1.66667Mhz Time Period (Machine Cycle)= 1/T = 1/1.66667Mhz = 0.6usec For 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 0000H HERE:MOV TMOD,#01H HERE:MOV TL0,#D6H MOV TH0,#0FFH SETB TR0 JNB TF0,$ CLR TR0 CLR TF0 SJMP HERE END

;For Timer 1.

;To start the Timer TR0 must be set high.

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

34.Frequency = 11.0592Mhz/12 = 921.6Khz Time 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 0000H MOV R0,#17 ;Value that we divided 921659 by. MOV TMOD,#01H ;For Timer 1. HERE:MOV TL0,#39H MOV TH0,#2CH SETB TR0 ;To start the Timer TR0 must be set high. JNB TF0,$ CLR TR0 CLR TF0 DJNZ R0,HERE END

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

35.Frequency = 16Mhz/12 = 1.33333Mhz Time 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 0000H MOV R0,#33 ;Value that we divided 333333 by. MOV TMOD,#01H ;For Timer 1. HERE:MOV TL0,#8BH MOV TH0,#0D8H SETB TR0 ;To start the Timer TR0 must be set high. JNB TF0,$ CLR TR0;To stop the Timer. CLR TF0 DJNZ R0,HERE END

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

36.Frequency = 11.0592Mhz/12 = 921.6Khz Time 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 0000H MOV TMOD,#20H SETB P1.3 MOV TH1,#0FFH AGAIN:SETB TR1 JNB TF1,$ CLR TR1 CLR TF1 CPL P1.3 SJMP AGAIN END 37.Frequency = 11.0592Mhz/12 = 921.6Khz Time 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.

Microcontroller Solutions
From Ali Akbar Siddiqui. ORG 0000H MOV TMOD,#20H SETB P1.3 MOV TH1,#01 AGAIN:SETB TR1 JNB TF1,$ CLR TR1 CLR TF1 CPL P1.3 SJMP AGAIN END 38.And 39. Are the same, the only difference is instead of XTAL = 11.0596Mhz its 16Mhz.
Sir Syed University of Eng& Tech

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

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

You might also like