 |
SOLVED PROBLEMS
ADDRESSING MODES
Q. 1: Write program to send 44H to ports P1 and P2, using (a) their addresses (b) their
names.
Solution:
(a) MOV A, #44H
MOV PI, A
MOV P2, A
(b) MOV A, #44H
MOV 80,A
MOV OAOH,A
Note: Moving data to a port changes the port latch; moving data from a port gets data from the port pins.
Q. 2: Write program to copy a block of 8 bytes of data to RAM locations starting at 50H from RAM locations 30H.
Solution:
MOV RO, #30H ; source pointer
MOV RI, #50H ; destination pointer
MOV R3, #8 ; counter
RETURN: MOV A, @R0 ; get a byte from source
MOV @RI, A ; copy it to destination
INC R0 ; increment source pointer
INC RI ; increment destination pointer
DJNZ R3, RETURN; keep doing it for all eight bytes
Q. 3: Write program to transfer the bytes into RAM locations starting at 50H assuming that ROM space starting at 240H contains "INDIA"
Solution:; (a) By using a counter
ORG 0000
MOV DPTR,#MY_DATA;Load ROM pointer
MOV R0,#50H ;Load RAM pointer
MOV R2,#5;Load counter
RETURN: CLR A;A=O
MOVC A,@A+DPTR ;Move data from code space
MOV @R0,A ;Save it in RAM
INC DPTR ;Increment ROM pointer
INC R0 ;Increment RAM pointer
DJNZ R2,RETURN ;Loop untilcounter=0
HERE: SJMP HERE
;ON-CHIP CODE SPACE USED FOR STORING DATA
ORG
240H
MY_DATA: DB "INDIA"
END
;(b) By using null char for end of sting
ORG
0000
MOV DPTR,#MY_DATA
;Load ROM pointer
MOV R0,#50H
;Load RAM pointer
RETURN: CLR A
;A=0
MOVC A,@A+DPTR
;Move data from code space
JZ HERE
;Exit if null char
MOV @R0, A
;Save it in RAM
INC DPTR
; Increment ROM pointer
R0
;Increment RAM pointer
SJMP RETURN
;Loop
HERE
: SJMP HERE
;ON-CHIP CODE SPACE USED FOR STORING DATA
ORG 240H
MYDATA: DB "INDIA" ,0 ;null char for end of string
END
Q-4: Write program to get the x value from P1 and send x2 to P2, continuously.
Solution:
ORG 0
MOV DPTR,#300H ;Load lock-up table address
MOV A,#0FFH
;A=FF
MOV P1,A
;Configure P1 as input port
RETURN MOV A,P1
;Get X
MOVC A,@A+DPTR ;Get X square from table
MOV P2,A
;issue IT to P2
SJMP RETURN
;Keep doing it
ORG 300H
XSQR_TABLE:
DB 0,1,4,9,16,25,36,49,64,1
END
Note: this program uses loo-up table instead of calculation.
ARITHMETIC/LOGIC OPERATIONS
Q-1: Assume that RAM locations 50-55 have the following values. Write a program to find the sum of the values. At the end of the program, register A should contain the low byte and R7 the high byte. All values are in hex.
50=(2E)
51=(5B)
52=(A2)
53=(30)
54=(C5)
55=(EB)
Solution:
MOV R0,#50H ;load pointer
MOV R2,#6 ;load counter
CLR A
;A=0
MOV R7,A ;clear R7
ADD A,@R0 ;add the byte pointer to by
R0
JNC NEXT ;if
CY=0 don't accumulate carry
INC R7
;keep track of carries
INC
R0
;increment pointer
DJNZ R2,REPEAT ;repeat until R2 is zero
Q. 2: Write program to add two 16-bit numbers. The numbers are 2E5Fh and
A3B4h.Store the sum in R7 and R6;R6 should have the lower byte.
Solution:
CLR C
;make CY=0
MOV A,#05FH ;load the low byte now A=5FH
ADD A,#B4H ;add the low byte now
MOV R6,A ;save the low byte of the sum in R6
MOV A,#2EH ;load the high byte
ADDC A,#A3H ;add with the carry
MOV R7,A ;save the high byte of the sum
Q. 3: Assume that RAMl locations 50-55 have the following 6 BCD data. Write a program
to find the sum of the values. The result must be in BCD.
50=(43)
51=(54)
52=(65)
53=(27)
54=(78)
55=(91)
Solution:
MOV R0,#40H ;load pointer
MOV R2,#5
;load counter
CLR A
;A=0
MOV R7,A
;clear R7
REPEAT: ADD
A,@R0 ;add the byte pointer to by
R0
DA A
;adjust for BCD
JNC NEXT
;if CY=0 don't accumulate carry
INC R7
;keep track of carries
NEXT: INC
R0
;increment pointer
DJNZ R2,REPEAT ;repeat until R2 is zero
Q. 4: Write a program to get hex data in the range of
00-FFh from the port 0 and convert it to decimal. Save the digits in R7,R6 and R5,where the least significant digit is in R7.
Solution:
MOV A,#OFFH
MOV PO,A ;make P0 an
input port
MOV A,PO ;read data from
P0
MOV B,#10 ;B=0A hex (10 dec)
DIV AB
;divide by 10
MOV R7,B ;save lower digit
MOV B,#10
DIV
AB ;divide by 10 once more
MOV R6,B
;save the next digit
MOV R5,A ;save the last digit
Q. 5: Write a program to convert packed BCD to two ASCII numbers and place them in
R2 and R6.it to decimal. Assuming A has packed BCD.
Solution:
MOV A,#29H ;A=29H, packed
BCD
MOV R2,A
;keep a copy of BCD data in
R2
ANL A,#OFH ;mask the upper
nibble(A=09)
ORL A,#30H ;make it an ASCII,
A=39H('9)
MOV R6,A
;save it (R6=39H ASCII
char)
MOV A,R2
;A=29H;get the original
data
ANL A,#OFOH ;mask the lower nibble(A=20)
RR A
;rotate
right
RR A
;rotate
right
RR A
;rotate
right
RR A
;rotate right, (A=02)
ORL A,#30H ;A=32H, ASCII
char. '2'
MOV R2,A
;save ASCII char in R2
TIMER/COUNTER
Q. 1: Find the value of timer's registers to generate a time delay of 5 ms. Assuming crystal frequency 11.0592 MHz. Show the program for timer 0 to create a pulse width of 5 ms on p2.3.
Solution:
Since crystal freq=11.0592 MHz, the counter counts up every 1.085
microseconds. To get 5 ms we need (5 ms/1.085 us) 4608 clocks. to
achieve that we need to load TH and TL the value
65536-4608=60928=EE00h i.e. TH=EE and TL=00.
CLR P2.3
;clear P2.3
MOV TMOD,#01 ;Timer0O,mode 1(16-bit mode)
START MOV
TL0,#0 ;TL0=0, the low byte
MOV TH0,#0EEH ;TH0=EE(hex), the high byte
SETB P2.3
;SET high P2.3
SETB
TR0
;start timer 0
REPEAT JNB TF0,REPEAT ;monitor timer Flag 0 until it rolls
over
CLR
TR0
;stop timer 0
CLR
TF0
;clear timer 0 flag
Q. 2: Write a program to generate a square wave of2 KHz frequency on pin P1.5.
Assuming crystal frequency 11.0592 MHz.
Solution:
MOV TMOD,#10H ;timer 1, mode 1(16-bit)
REPEAT: MOV TL1,#1AH
;TL1=1A,low byte of timer
MOV TH1,#0FFH
;TH1=FF,high byte
SETB
TR1
;start the timer 1
RETURN: JNB TFl,RETURN
;stay until timer rolls over
CLR
TR1
;stop timer 1
CPL P1.5
;comp. p1.5 to get high, low
CLR
TF1
;clear timer flag 1
SJMP REPEAT
;reload timer since mode 1 is not auto-reload
Q. 3: Write a program to generate a square wave of 50 Hz frequency on pin Pl.5.
Assuming crystal frequency 11.0592 MHz.
Solution:
MOV TMOD,#10H
;timer 1, mode 1
(16-bit)
REPEAT: MOV TLl,#00 ;TL1=00,low byte of timer
MOV TH1,#0DCH
;T1=DC,Hi byte
SETB
TR1
;start the timer 1
JNB
TF1,RETURN
;stay until timer rolls over
CLR
TR1
;stop timer 1
CPL P2.3
;comp. p2.3 to get hi, 10
CLR
TF1
;clear timer flag 1
SJMP REPEAT
;reload timer since mode 1is not auto-reload
BACK |
 |