You are on page 1of 11

ES Unit 3 Solutions

Question 1: For 8051 system of 11.0592 MHz, find how long it takes to execute each instruction.(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target (d) LJMP (e) SJMP (f) NOP (g) MUL AB (Solution: Machine cycles (a) 1 (b) 1 (c) 2 (d) 2 (e) 2 (f) 1 (g) 4 Time to execute (a) 1x1.085s = 1.085s (b 1x1.085s = 1.085s (c) 2x1.085s = 2.17s (d) 2x1.085s = 2.17s (e) 2x1.085s = 2.17s (f) 1x1.085s = 1.085s (g) 4x1.085s = 4.34s

Question2 : Write code to send out alternative values of 55H and AAH to port 0. Solution : The following code will continuously send out to port 0 the alternating value 55H and AAH BACK: MOV A,#55H MOV P0,A ACALL DELAY MOV A,#0AAH MOV P0,A ACALL DELAY SJMP BACK

Question 3A: - treat r6-r7 and r4-r5 as two 16 bit registers. Perform subtraction between them. Store the result in 20h (lower byte) and 21h (higher byte). Solution: - first we shall clear the carry. Then subtract the lower bytes afterward then subtract higher bytes. Clr c ; clear carry Mov a, r4 ; get first lower byte Subb a, r6 ; subtract it with other Mov 20h, a ; store the result Mov a, r5 ; get the first higher byte Subb a, r7 ; subtract from other Mov 21h, a ; store the higher byte Question 3B: - divide the content of r0 by r1. Store the result in r2 (answer) and r3 (reminder). Then restore the original content of r0. Solution:-after getting answer to restore original content we have to multiply answer with divider and then add reminder in that. Mov a, r0 ; get the content of r0 and r1 Mov b, r1 ; in register A and B Div ab ; divide A by B Mov r2, a ; store result in r2 Mov r3, b ; and reminder in r3 Mov b, r1 ; again get content of r1 in B Mul ab ; multiply it by answer Add a, r3 ; add reminder in new answer Mov r0, a ; finally restore the content of r0 Question 4A: - transfer the block of data from 20h to 30h to external location 1020h to 1030h. Solution: - here we have to transfer 10 data bytes from internal to external RAM. So first, we need one counter. Then we need two pointers one for source second for destination. Mov r7, #0Ah ; initialize counter by 10d Mov r0, #20h ; get initial source location Mov dptr, #1020h ; get initial destination location Nxt: Mov a, @r0 ; get first content in acc Movx @dptr, a ; move it to external location Inc r0 ; increment source location Inc dptr ; increase destination location

Djnz r7, nxt

; decrease r7. if zero then over otherwise move next

Question 4B: - find out how many equal bytes between two memory blocks 10h to 20h and 20h to 30h. Solution: - here we shall compare each byte one by one from both blocks. Increase the count every time when equal bytes are found Mov r7, #0Ah ; initialize counter by 10d Mov r0, #10h ; get initial location of block1 Mov r1, #20h ; get initial location of block2 Mov r6, #00h ; equal byte counter. Starts from zero Nxt: Mov a, @r0 ; get content of block 1 in acc Mov b, a ; move it to B Mov a, @r1 ; get content of block 2 in acc Cjne a, b, nomatch ; compare both if equal Inc r6 ; increment the counter Nomatch: inc r0 ; otherwise go for second number Inc r1 djnz r7, nxt ; decrease r7. if zero then over otherwise move next Question 5: - given block of 100h to 200h. Find out how many bytes from this block are greater then the number in r2 and less then number in r3. Store the count in r4. Solution: - in this program, we shall take each byte one by one from given block. Now here two limits are given higher limit in r3 and lower limit in r2. So we check first higher limit and then lower limit if the byte is in between these limits then count will be incremented. Mov dptr, #0100h ; get initial location Mov r7, #0FFh ; counter Mov r4, #00h ; number counter Mov 20h, r2 ; get the upper and lower limits in Mov 21h, r3 ; 20h and 21h Nxt: Movx a, @dptr ; get the content in acc Cjne a, 21h, lower ; check the upper limit first Sjmp out ; if number is larger Lower: jnc out ; jump out Cjne a, 20h, limit ; check lower limit Sjmp out ; if number is lower Limit: jc out ; jump out Inc r4 ; if number within limit increment count Out: inc dptr ; get next location

Djnz r7, nxt ; repeat until block completes Question 6:- the crystal frequency is given as 12 MHz. Make a subroutine that will generate delay of exact 1 ms. Use this delay to generate square wave of 50 Hz on pin P2.0 Solution: - 50 Hz means 20 ms. And because of square wave 10 ms ontime and 10 ms offtime. So for 10 ms we shall send 1 to port pin and for another 10 ms send 0 in continuous loop. <_x0021_xml:namespace prefix="st1" ns="urn:schemas-microsoftcom:office:smarttags"/>Loop: Setb p2.0 ; send 1 to port pin Mov r6, #0Ah ; load 10d in r6 Acall delay ; call 1 ms delay 10 = 10 ms Clr p2.0 ; send 0 to port pin Mov r6, #0Ah ; load 10d in r6 Acall delay ; call 1 ms delay 10 = 10 ms Sjmp loop ; continuous loop Delay: ; load count 250d Lp2: Mov r7, #0FAh Lp1: Nop ; 1 cycle Nop ; 1+1=2 cycles Djnz r7, lp1 ; 1+1+2 = 4 cycles Djnz r6, lp2 ; 4250 = 1000 cycles = 1000 s = 1 ms ret Question 7:-count number of interrupts arriving on external interrupt pin INT1. Stop whencounter overflows and disable the interrupt. Give the indication on pinP0.0 Solution: -as we know whenever interrupt occurs the PC jumps to one particular location where its ISR is written. So we have to just write one ISR that will do the job Movr2, #00h ; initialize the counter Movie, #84h ; enable external interrupt 1 Here: Sjmp here ; continuous loop Org 0013h Incr2 Cjner2, #00h, out Movie, #00h Clr p0.0 Out : reti ; interrupt 1location ; increment the count ; check whether it overflows ; if yes then disable interrupt ; and give indication ; otherwise keep counting

Answer 8: Generating Square Wave on the Port


A number is assigned to port 1 which simulates input operation. The port 1 contents are complemented and sent to the port 1. The square wave can be observed on port 1 by using CRO.

Program Source Code


************************************************ #include<reg51.h> void delay(int); void delay(int k) { int i,j; for (j=0;j<k+1;j++){ for (i=0;i<100;i++); }} void main(void) { int N1=0; number. while(1) { N1 =N1; delay(10); P1 = N1; delay (10); } // End of main. ************************************************

Answer 9: Sending Data to the Ports by Assigning Values to a Character


This program illustrates assigning values to the character and then sending it to the port. Here the characters a, b, c are assigned with the Hex numbers and these values are sent to ports.

Program Source Code


*************************************************** #include<REG51.H> void main(void) { { }} char a,b,c; while(1) // Define the characters a, b and c. / Assign 01H to a / / Assign 02H to b / / Assign 03H to c / / Send value assigned to a to port 0/ / Send value assigned to b to port 1/ / Send value assigned to c to port 2/ a=0x01; b=0x02; c=0x03; P0=a; P1=b; P2=c; ***************************************************

Answer 10: Configuring the Ports as Input


By default all the ports act as output port. They can be configures as input by writing FFH to them. The programmer need not bother about the actual SFR address. Thanks to the header file included at the top.

The actual input operation is simulated by assigning values to the variables. The actual DIP switch interfaced as input device is covered further in the text. Program below illustrates the simplest way to define the ports as an input port and how to read the input from the ports and the result is observed in the simulation window.

Program Source Code


********************************************************* #include<REG52.H> void main(void) { { }} char N1,N2,N3; P0=0xFF; P1=0xFF; while(1) // Defining the characters. // set P0 as input // set P1 as input /Input value of P0 in N1/ ;/Input value of P1 in N2/ // Addition of the two inputs /Send value of N3 to port 2/ N1=P0; N2=P1 N3=N1+N2; P2=N3;

Answer 11: Software Delay Loops Combined with I/O Ports


The program demonstrates generation of software delay using two loops operating on a single count. An integer x is declared and assigned a count equal to 10,000. While(1) loop keeps the program in continues loop. The other two loops are conditional with the first loop executes when while(x > 1) is valid, i.e., when x is greater than 1. The other loop executes when x is less than 10,000. Note the usage of decrement () and increment (++) operators. Both these loops are embedded in the main loop. To observe the output of this connect the port 1 to LEDs. The output can also be verified in the simulator window.

Program Source Code


********************************************************* **** #include <reg51.h> void main (void) 44
Importance of Ports

{ int x = 10000; while(1) { while(x > 1) { P1 = 0x0f; x--; } // end of first loop while(x < 10000) { P1 = 0xf0; x++; } // end of second loop. / 10000 count delay / / set leds on PORT1 to 00001111 /

/ decrement x / / 10000 count delay / / set leds on PORT1 to 11110000 / / increment x / } // End of open loop. } // End of main. *********************************************************

Answer 12: Alternate Method of Software Delay Generation with Port Programming
This program illustrates the use of FOR construct for delay generation which toggles only one port pin (P1.1). Note the use of ! operator for complementing the variable.

Program Source Code


********************************************************* #include<reg52.h> sbit vin = P1 0 ; void main(void) { int i; vin = 1; while(1) { for(i=0;i<=3000;i++); for(i=0;i<=3000;i++); vin = !vin; //complement port pin P1.0 after delay } } *********************************************************

Answer 13: Scrolling LED


This program illustrates the interfacing of LED with scrolling pattern. The display appears as if the LED is shifting. The LED connected to the port 0 glows one by one. A software delay is used to define scrolling interval.

Program Source Code


******************************************************* #include <REG52.H> void main(void) { { int i; char N1; while(1) // define the integer number //define the character to assign the hex number N1=0x01; while(N1!=0x00) { // routine for scrolling interval for(i=1;i<10000;i++); } }} P0=N1; N1=N1>>1; // display the LED corresponding to N1 //shift right by one *******************************************************

Answer 14: Exploring Bit Capabilities of the Microcontroller


This program makes use of the microcontrollers bit programming capability. Here the single port pin is used as an output. The program toggles the single port pin to give a continuous on/off pattern on pin P1.1. Input from P1.0 is simulated.

Program Source Code


********************************************************* #include<reg52.h> sbit van= P10; sbit new = P11; void main(void) { / Pin declaration vinod = port 1 pin 0 / / Pin declaration new = port 1 pin 1 / // Define the int vinod / Infinite loop (i.e.,countinuos loop) / / Simulating input from port pin P1.0 and assigning the same to character van / / send output to port1 pin1 / //compliment vinod // end of loop int vinod; vinod = 0x00; while(1) { van = vinod; new = vinod; vinod=!vinod; } } // End of main *********************************************************

You might also like