0% found this document useful (0 votes)
6 views33 pages

Model Question1

The document is an examination paper from Gujarat Technological University for the subject 'Microcontroller and Interfacing' for BE Semester V, dated November 26, 2014. It includes various questions on microcontroller operations, such as the importance of PUSH and POP instructions, the PSW register, addressing modes, and types of jump instructions. The paper aims to assess students' understanding of microcontroller concepts and programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views33 pages

Model Question1

The document is an examination paper from Gujarat Technological University for the subject 'Microcontroller and Interfacing' for BE Semester V, dated November 26, 2014. It includes various questions on microcontroller operations, such as the importance of PUSH and POP instructions, the PSW register, addressing modes, and types of jump instructions. The paper aims to assess students' understanding of microcontroller concepts and programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

GUJARAT TECHNOLOGICAL UNIVERSITY

BE - SEMESTER–V • EXAMINATION – WINTER • 2014


Subject Code: 151001 Date: 26-11-2014
Subject Name: Microcontroller and Interfacing
Time: 10.30 am - 01.00 pm Total Marks: 70

Instructions:
1. Attempt all questions.
2. Make suitable assumptions wherever necessary.
3. Figures to the right indicate full marks.

Q.1 (a) (1) Give importance of PUSH and POP instruction in RAM content.
PUSH and POP are special instructions that are associated with stack operation. Using these
instructions the data is transferred between stack and specified direct address.
PUSH instruction saves data on to location pointed by SP, while saving a new data byte, the SP
is automatically incremented by 1 and data byte is stored at SP+1 address. POP instruction
retrieves data from location pointed by SP, while retrieving, data will be read from address in SP
and then SP is decremented by 1. PUSH and POP instructions use SP register indirectly and not
specified in an instruction i.e. these instructions assumes that SP is pointing to top of the stack .
These instructions supports only direct addressing mode. The formats of PHSH and POP
instructions are given below. Note that a programmer should define the stack at proper place
before it can be used, it is done by loading suitable internal RAM address in the SP.
PUSH direct // Increment SP, copy data from address direct to address in SP
POP direct // copy data from address in SP to address direct, and then decrement SP
For example,

MOV SP, #50H // initialize SP to point to address 50H


MOV 35H, #10H // load data 10H in to address 35H
PUSH 35H // Increment SP to 51H, copy contents of address 35H to address 51H
// i.e. (51H) = 10H
POP 00H // copy data from address 51H to address 00H, i.e. (00H) =10H and
// decrement SP to 50H.

(2) Make R0 as index register.


Here, the register holds the address that contains the data i.e. the number in the register is treated
as an pointer to address. Indirect addressing mode is the most flexible and useful in array
operations. There are two types of indirect addressing in the 8051
Register indirect addressing mode: The register indirect addressing uses only register R0 or R1
to hold address of the data in internal RAM, these two registers are also referred to as pointer
registers or simply pointers. The symbol @ is used along with R0 or R1 to indicate indirect
addressing. For example,
MOV @Ri, #data // load constant value in to address contained in Ri
MOV @R0, #30H // If R0=40H, → (40H)=30H

MOV @Ri, direct // copy data form address direct to address Ri


MOV @R0, 10H // If (10H)=50H, R0=15H→ (15H)=50H

(3) Explain PSW register of 8051.


Program status word is an 8 bit register. It is also referred as flag register or processor status
word. Flag is a flip flop (1 bit storage element) used to store and indicate the nature of result
produced by execution of certain instructions. The state of flags (0 or 1) are tested by other
instructions (program flow control or branch instructions) to make decisions. PSW structure is
explained in Table.
Table Program status word structure

PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0


CY AC F0 RS1 RS0 OV -- P
MSB LSB

Bit Symbol Flag name and description


7 C (or CY) Carry; Used in arithmetic, logic and Boolean operations
6 AC Auxiliary carry ; useful only for BCD arithmetic
5 F0 Flag 0; general purpose user flag
4 RS1 Register bank selection bit 1
3 RS0 Register bank selection bit 0
RS1 RS0
0 0 Bank 0
0 1 Bank 1
1 0 Bank 2
1 1 Bank 3
2 0V Overflow; used in arithmetic operations
1 -- Reserved; may be used as a general purpose flag
0 P Parity; set to 1 if A has odd number of ones, otherwise reset to 0

CY: Carry flag


It is a carry (or borrow) used in addition and subtraction operations. It is set to 1 when there is
carry out from MSB (D7 bit) after an addition (or a borrow into D7 bit during a subtraction). It is
also used as the ‘Accumulator’ for the Boolean operations. It can be directly modified by bit
level instructions.

AC: Auxiliary carry flag


It is a half carry (carry out from bit D3 to D4) used in conventional BCD arithmetic.

F0: Flag 0
It is a general purpose flag. It can be used as a one bit memory location to record some event.

RS0 and RS1: Register bank select bits


These bits are used to select the register bank.

OV: Overflow flag


It is set to 1 to indicate that result of signed arithmetic is erroneous (out of range).

P: Parity flag
It indicates the parity of the Accumulator; it is set to 1 if the accumulator register has odd
number of ones, otherwise reset to 0 i.e. even parity.

(4) Briefly discuss EQU and DB directive.


Equate- EQU
Equates label to the number.
EQU TEMP, #20 will assign name TEMP to data #20.

Define Byte- DB
This directive defines the byte type variable. When DB is used to define data, the numbers can
be in decimal, binary, hex, or ASCII formats. For the binary numbers B is as a suffix. Similarly,
H is used after hexadecimal numbers. Irrespective of the type of the byte, the assembler will
convert the byte to hex. To indicate ASCII numbers, the characters are placed in quotation marks
(‘character’).The DB is also used to allocate memory in byte sized chunks.
Example Specify the addresses of each of bytes defined by following declarations.
ORG 0100H
DB 10
DB 10H
DB ‘ABCD’
DB 00011111B
Solution:
DB 10 // define byte 10 (decimal) and store at address 0100H
DB 10H // define byte 10 (hex) and store at address 0101H
DB ‘ABCD’ // define string ‘ABCD’ and store at addresses 0102H to 0105H
DB 00011111B // define byte 00011111(binary) = 1FH and store at address 0106H
(5) Represent -68D in signed number.
First represent magnitude in binary
i.e. 68D= 01000100
Find its 2’s complement
It is 10111100
(6) Find the TH1 value (in both decimal and hex ) to set the baud rate 9600
Assume 11.0592 MHz XTAL frequency.
The value of TH1 can be found as,

TH1 = 256 – {(FOSC x 2SMOD) ÷ (12 × 32× FBAUD)}

For 9600 bps rate, assuming SMOD =0

TH1 = 256 – {(11.0592 × 106 × 1) ÷ (384 × 9600)}


= 253 = FDH

(7) How will you alter sequence of interrupt priority?


It can be done by programming corresponding bit to 0 in IP register.

Using IP register each interrupt source can be assigned individually either low or high priority
level by clearing or setting corresponding bit. A low priority interrupt can be interrupted by high
level priority interrupt, but vice versa is not true.
.
Example Assign highest priority to timer0 and lowest priority to INT0
Solution:
MOV IP, #00011110
This instruction will assign priority ‘high’ to timer 0 (TF0), external interrupt 1 (IE 1), timer 1
(TF1), and serial port (TI or RI) interrupts and among these ‘high’ priority level interrupts, timer
0 (TF0) has the highest priority (only for this example!) and then 
INT1 ,TF1 and serial port

interrupt in decreasing order. Since external interrupt 0 (INT0) is assigned ‘low’ priority it will
be given lowest priority.

Refer topic 16. 8 for more details.


.
Q.1 (b) (1) Explain conditional and unconditional jump instruction
Unconditional jumps
The unconditional jump does not test any condition and jump is always taken. The 8051 support
three types of unconditional jumps: short (relative) jump, absolute jump and long (direct) jump.
These jumps differ in range (in terms of bytes) over which the jump can be taken.
Short jump
We know that the PC always contains the address of the next instruction (with respect to
instruction that is being executed). Using a short jump a program may only jump to instructions
within 127 bytes in forward direction (+127) or 128 bytes in reverse direction (-128) with
respect to contents of the PC(next instruction). A short jump is called relative jump because
destination address that is specified in the instruction (and then placed in the program counter) is
relative to the address where the jump instruction is written. The advantage offered by relative
jump is that it allows relocation. The instruction for short jump is SJMP and its format is,

SJMP rel // jump to relative destination address rel

SJMP is two byte instruction, first byte is op-code and second byte is relative address.

Absolute jump
AJMP instruction logically divides entire program memory in to 32 pages of 2K Bytes each.
AJMP instruction can jump within a page of 2K.

The instruction for absolute jump is AJMP and its format is,

AJMP add11 // jump to absolute destination address add11

Long jump
LJMP instruction allows a jump to any location within entire program address space i.e.
anywhere from 0000H to FFFFH. It is a 3 byte instruction, first byte is the op-code for the
instruction, second and third byte represents directly 16 bit destination address. Therefore long
jump is also referred as direct jump. Advantage of LJMP instruction is its address range.

The instruction for long jump is LJMP and its format is,

LJMP add16 // jump to direct destination address add16

Conditional jump
Conditional jump instructions first test the condition specified in an instruction opcode. If the
condition is true, then the jump is taken by modifying value of the PC, otherwise program
execution continues to the next sequential instruction. All conditional jumps are short relative
jumps. The conditional jumps are categorized as bit and byte jumps.
Bit jumps
Bit jump instructions check the status of addressable bit specified as a part of instruction. The
jump is taken to specified relative address if the condition (for specified bit) is satisfied (or true),
otherwise program execution proceeds to the next instruction. The destination address is
calculated by adding the relative address with the PC. These instructions are used to monitor the
status of specified bit and to take the decision based on its value. Bit jump instructions are listed
below.

JC rel // jump to relative address rel if C=1 (Jump if Carry)


JC BACK // jump to (carry on program execution from) label BACK if C=1

JNC rel // jump to relative address rel if C=0 (Jump if No Carry)


JNC NEXT // jump to (continue program execution from) label NEXT if C=0

JB bit, rel // jump to relative address rel if bit =1 (Jump if Bit)


JB 00H, TMP // jump to label TMP if bit 00H is set (content of 00H is 1)

JNB bit, rel // jump to relative address rel if bit =0 (Jump if No Bit)
JNB 05H, MP // jump to label MP if bit 05H is clear (content of bit address 05H is 0)

JBC bit, rel// jump to relative address rel if bit = 1, and then clear bit (Jump if Bit,
// then Clear)
JBC 50H, XY // jump to label XY if bit 50H is set (content of bit address50H is 1), then clear
// bit 50H

Byte jumps
Byte jump instructions check byte of data to make a decision whether to jump to destination
address or continue to the next instruction. The byte jump instructions are listed below.

JZ rel // jump to relative address rel if A is 0 (Jump if Zero)


JNZ rel // jump to relative address rel if A is not 0(Jump if Not Zero)
CJNE A, direct, rel // compare A with contents of address direct and jump to relative
// address rel if they are not equal, if A is less than contents of address
// direct, set carry flag to 1, otherwise clear to 0
// CJNE means Compare and Jump if Not Equal)
CJNE A, #data, rel // compare A with immediate value data and jump to relative address
// rel if they are not equal, if A is less than immediate value data, set
// carry flag to 1, otherwise clear to 0
CJNE Rn, #data, rel // compare Rn with immediate value data and jump to relative address
// rel if they are not equal, if Rn is less than immediate value data, set
// carry flag to 1, otherwise clear to 0
CJNE @Ri,#data, rel // compare contents of address in Ri with immediate value data and
// jump to relative address rel if they are not equal, if contents of
// address in Rn is less than immediate value data, set carry flag to 1,
// otherwise clear to 0
DJNZ Rn, rel // decrement register Rn by 1 and jump to relative address rel if
// content of Rn is not zero after decrement operation, no flags are
// affected
// DJNZ means Decrement and Jump if Not Zero
DJNZ direct, rel // decrement contents of address direct by 1 and jump to relative //
address rel if content of address direct is not zero after decrement
// operation, no flags are affected

(2) List the call instruction and discuss each in brief.


In the 8051 there are two instructions for calling a subroutine, LCALL (long call) and ACALL
(absolute call),. The formats of these instructions are given below:
LCALL add16 // call the subroutine at address add16 located anywhere in the entire
// program memory space of 64KB, also save (push) the address of the
// next instruction following LCALL (return address) on to the stack
LCALL DISPLAY // call a subroutine DISPLAY, save return address on the stack

ACALL add11 // call the subroutine at address add11 located on same page as the next
// instruction, also save the address of the next instruction following
// ACALL (return address) on to the stack
ACALL COMPARE // call a subroutine COMPARE, save return address on the stack

Refer topic 7.3 for more details.

Q.2 (a) What is addressing mode of microcontroller? Enlist it and explain


each with proper example.
The way by which source or destination (usually source) operands are specified in an instructions
is called addressing mode. The instruction may contain one, two or no operand. The data is
accessed from source address, processed in the ALU and stored at destination address. The ways
by which these data (or address of data) are specified are called addressing modes.

Addressing modes
1. Immediate addressing
2. Register addressing
3. Direct addressing
4. Indirect addressing
• Register indirect addressing
• Indexed addressing

1. Immediate addressing mode

The data (constant) is specified as a part of instruction in a program memory. The data is
available immediately as a part of instruction itself, therefore immediate addressing is very fast.
However, since the data is fixed, at run time it is not flexible. The instructions using an
immediate operand have an 8 bit or 16 bit number following the op-code. For example,

MOV A, #data // load 8 bit immediate data in to Accumulator


MOV A, #55H // A= 55H

MOV Rn,#data // load 8 bit immediate data into Rn


MOV R3, #0FFH // R3= FFH

2. Register addressing mode


In register addressing mode, the operands are specified by register names. Register A and R0 to
R7 may be named as a part of the instruction mnemonic. The advantage of register addressing
mode is that it occupies only one byte memory, and is fast because only on-chip registers are
accessed i.e. instruction takes only one machine cycle for execution. For example,

MOV A, Rn // copy contents of register Rn to Accumulator


MOV A, R2 // If R2=10H →A=10H

3. Direct addressing mode

The data is accessed directly from the memory address specified as one of the operand i.e. one of
the operand is an 8-bit address for internal RAM location. Internal RAM includes 128 byte of
RAM from (00H-7FH) and any special function register. It is more flexible compared to
immediate and register addressing because the value to be accessed from address may be
variable. These are 2 byte instructions (3 bytes when source and destination are both direct
addresses). The address refers to either byte location or a specific bit in a bit addressable byte.
For example,

MOV A, direct // copy data from (contents of) address direct in to A


MOV A, 10H // If address 10H contains data 50H i.e (10H)=50H→ A=50H

MOV direct, A // copy data from A to address direct


MOV 10H, A // If A=44H→ (10H)= 44H

MOV Rn, direct // copy data from address direct in to register Rn


MOV R5, 80H // If (80H)= FFH→R5= FFH

MOV direct1, direct2 // copy data from address direct2 to address direct1
MOV 50H, 83H // If (83H)=10H→ (50H)=10H

4. Indirect addressing mode


The data is specified indirectly in an instruction i.e. address of the data (rather than data itself) is
specified as one of the operand. Here, the register holds the address that contains the data i.e. the
number in the register is treated as an pointer to address. Indirect addressing mode is the most
flexible and useful in array operations. There are two types of indirect addressing in the 8051

Register indirect addressing mode: The register indirect addressing uses only register R0 or R1
to hold address of the data in internal RAM, these two registers are also referred to as pointer
registers or simply pointers. The symbol @ is used along with R0 or R1 to indicate indirect
addressing. For example,
MOV @Ri, #data // load constant value in to address contained in Ri
MOV @R0, #30H // If R0=40H, → (40H)=30H

MOV @Ri, direct // copy data form address direct to address Ri


MOV @R1, 10H // If (10H)=50H, R1=15H→ (15H)=50H

MOV direct, @Ri // copy data from address in Ri to address direct


MOV 10H, @R1 // If R1=50H, (50H)=15H→ (10H)=15H

MOV @Ri, A // copy data from A to address in Ri


MOV @R0, A // If A=50H, R0=15H→ (15H)=50H

Indexed addressing mode: Two registers are used to form the address of the data. The contents
of either DPTR or PC are used as a base address and the A is used as index (or offset) address.
The final address is formed by adding these two registers. It results in a forward reference of 0 to
255 bytes from the base address. They are used to access only program memory (internal as well
as external)
Indexed addressing is used to access data tables (lookup tables) from the program memory and
implementing jump tables. They are also suitable for multidimensional array operations.
The instructions are:
MOVC A, @A+PC // copy data (or code) byte from program memory address formed by
//addition of contents of A and PC into A

MOVC A, @A+DPTR// copy data (or code) byte from program memory address formed by
// addition of contents of A and DPTR into A

(b) Why 8051 called embedded microcontroller? Give logical justification


The 8051 microcontroller is complete functional microcomputer i.e. it contains the circuitry of
microprocessor and in addition it has built in memory (ROM, RAM), I/O circuits and peripherals
necessary for an application.

The 8051 microcontroller is an entire computer built into a single semiconductor chip. The term
“micro” as mentioned above means small in size, and the term “controller” means they are
normally used to control the machines or gadgets.
The 8051 microcontroller contains all (or major) components in a single chip to make system
based around them standalone. It includes data and code memory, various on-chip peripherals
like timers/counters, serial port, etc, interface controllers, and general purpose I/O ports which
allow it to directly interface to external environment. It has following onchip components:
• 8 bit CPU with Boolean processing capabilities.
• 4K bytes on-chip *program memory.
• 128 bytes on-chip data memory.(64 Kbytes each program and external data address
space.)
• 32 bidirectional I/O lines organized as four 8-bit I/O ports.
• serial port – Full duplex UART.
• 2 16-bit timers/counters.
• Two-level prioritized interrupt structure.
• Direct byte and bit addressability.
OR
Q.2(b) Explain following instruction set by giving suitable example.

(1) XOR
XRL destination byte, source byte
Function: Bitwise EX-OR operation between source and destination byte.
It performs the bitwise logical EX-OR operation between the source and destination operands and
stores the result in the destination operand. The Accumulator or a direct address in internal RAM
can be destination. The format of the instruction for each addressing mode is given below,
XRL A, #data // A=A EX-OR data
XRL A, Rn // A=A EX-OR Rn
XRL A, @Ri // A=A EX-OR (Ri)
XRL A, direct // A=A EX-OR (direct)
XRL direct, #data // (direct) = (direct) EX-OR data
XRL direct, A // (direct) = (direct) EX-OR A

Example:
MOV A, #0FEH // A=FEH
XRL A, #10H // A= FEH EX-OR 10H= EEH

MOV R0, #4AH // R0=4AH


MOV A, #55H // A=55H
XRL A, R0 // A=55H EX-OR 4AH= 1FH

MOV 10H, #72H // (10H) =72H


MOV A, #0FH // A=FFH
XRL 10H, A // (10H) = 72H EX-OR 0FH= 8CH

(2) CJNE
CJNE destination byte, source byte, rel address
Function: Compare and jump if not equal.
It compares the magnitudes of the destination byte and source byte (first two operands) and jumps
to specified relative address if their values are not equal, otherwise program execution proceeds to
next instruction. The destination address is calculated by adding the relative address with the PC.
The carry flag is set if the destination byte (unsigned) is less than the source byte, else the carry
flag is cleared. Neither operand is affected.
The format of the instruction for each addressing mode is given below,
CJNE A, direct, rel // If A≠(direct), PC=PC+rel
CJNE A, #data, rel // If A≠data, PC=PC+rel
CJNE Rn, #data, rel // If Rn≠data, PC=PC+rel
CJNE @Ri,#data, rel // If (Ri)≠data, PC=PC+rel

Example: The following instructions monitors the status of port 2 continuously until it is 50H,
MOV P2, #0FFH // configure P2 as input
REPEAT: MOV A, P2 // read status of P2 in to A
CJNE A, #50H, REPEAT // repeat until P2 status is 50H

(3) ADDC
ADDC A, source operand
Function: Add source operand byte and Carry flag with Accumulator. (Full addition)
It adds the specified source operand byte as well as Carry flag with the Accumulator, and stores
result in the Accumulator. The C, AC and OV flags are affected exactly similar to ADD
instruction discussed above.
While dealing with multi byte addition we need to consider the propagation of carry from lower
bytes to the higher byte i.e. we need to perform full addition. The instruction ADDC is used for
such operations. The suffix C after ADD indicates carry flag is included in the addition.
ADDC A, #data // A = A + data + C
ADDC A, Rn // A= A+ Rn + C
ADDC A, direct // A= A+ (direct) + C
ADDC A, @Ri // A= A+ (Ri) + C

Example Consider two 16 bit numbers 5292H and 1570H.


Solution: The addition of these two 16 bit numbers will be performed in following manner,
1
52 92 H
+ 15 70 H
68 02 H
The program to perform above addition using ADDC instruction is given below,
CLR C // C=0; initially carry is cleared
MOV A, #092H // A=92H
ADDC A, #70H // A= 92H+ 79H+0=02H, C=1
MOV R2, A // R2=02H, save lower byte result in to R2
MOV A, #52H // A=52H,
ADDC A, #15H // A= 52H+15H+1=68H, C=0
MOV R3, A // R3=68, save higher byte of result in to R3

(4) MOVX
MOVX destination byte, source byte
Function: Move to/from external data memory
It transfers data between the Accumulator and a byte from external data memory. The suffix ‘X’
in the mnemonic indicates the external data memory. The address of data byte can be specified
by 8 bit or 16 bit indirect address. The 8 bit address can be specified either by R0 or R1, using
these instructions we can access only 256 bytes and it is usually used to access I/O ports. The 16
bit address is specified by DPTR register any byte from entire 64K data memory can be
accessed.
Flags affected: None
MOVX A, @DPTR // A= (DPTR)Ext RAM
MOVX @DPTR, A // (DPTR)Ext RAM=A
MOVX A, @Ri // A= (Ri)Ext RAM
MOVX @Ri, A // (Ri)Ext RAM= A

Example: Assume DPTR=0100H, (0100)Ext RAM =20H, R0=30H and (30H) Ext RAM =35H before
execution of following instructions,
MOVX A,@DPTR // A= 20H
MOVX A, @R0 // A=35H

Assume A=55H, DPTR=0100H and R1=80H before execution of following instructions,


MOVX @DPTR, A // (0100)Ext RAM = 55H
MOVX @R1, A // (80H) Ext RAM = 55H

(5) RETI
RETI
Function: Return from interrupt service routine
It is used to return to main program from interrupt service routine (ISR). It pops (retrieves) two
bytes from top of stack (return address) in to PC and SP is decremented by two. It also retrieves
the saved status of interrupt enable bits, thus, enabling the same and lower priority interrupts
again i.e. restores the interrupt logic. The program execution continues at the return address,
which is usually the next instruction after the last instruction executed before entering in to ISR
i.e. instruction at which the interrupt request was detected.
RETI // PCH=(SP); SP=SP-1; PCL=(SP);SP=SP-1
Example: Assume (45H)=10H, (44H)=20H and SP=45H before execution of following
instructions,
RETI // PC = 1020H, SP=43H. Program execution continues at address 1020H and also
// restores the interrupt logic.

(6) MOVC A,@A+DPTR


Function: Move code byte
It copies data (or code) byte from program memory. The address of the byte is formed by
addition of contents of A and 16 bit DPTR.
Modulo 16 addition is performed while addition of A and DPTR. This instruction is used to
access data from lookup tables. Refer topic 8.4for more details.
MOVC A,@A+ DPTR // A= (A+DPTR)code

Example: for MOVC A,@A+ DPTR


ORG 0000H
MOV DPTR, #1000H // load address of lookup table in DPTR
MOV A, #02H // value for which square is to be found
MOVC A, @A+DPTR // access look up table, place result in A

ORG 1000H // define lookup table at ROM address 0100H
DB 0H, 1H, 4H, 9H, 10H,19H,24H,31H,40H,51H,64H

(7) DJNZ
DJNZ byte, rel address
Function: Decrement and jump if not zero
It decrements the specified byte first, and jump to specified relative address if result (after
decrement) is not zero, otherwise program execution proceeds to next instruction. The destination
address is calculated by adding the relative address with the PC. Note that it is relative jump.
Two addressing mode combinations are possible, the destination byte may be register or direct
address. The format of the instruction for each addressing mode is given below,
DJNZ Rn, rel // Rn=Rn-1; If Rn≠0, PC=PC+rel
DJNZ direct, rel // (direct)=(direct)-1; if (direct)≠0, PC=PC+rel

The DJNZ instruction is used to repeat the loop for fixed number of times. The typical structure
of loop using DJNZ instruction is shown below, it reads value of P2 and sends it to P1 10 times.
MOV R4, #10 // load the count (number of times loop to be repeated)
LOOP: MOV A, P2 // A=P2begin loop
MOV P1, A // P1=A
// end loop
DJNZ R4, LOOP // check whether loop is repeated required times, If not
// repeat it, otherwise, exit from loop and continue
// program execution at next instruction
...

Q.3 (a) Discuss the configuration of TMOD and TCON SFR in Timer
operation. Prepare ALP (Assembly language program) to generate 250ms
delay subroutine using Timer 1 mode 2 with 11.0592 quarts clock.
TMOD register

Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
MSB LSB
Bit Symbol Description
7/3 Gate Start/stop control using hardware or software. When Gate=0, start/stop of
timer is controlled only by TR1/0 bits; While Gate=1, it is controlled by
 pin
TR 1/0 as well as signal on INT1/0
6/2 
C/T C/T=0 configures timer as a interval timer (or time delay generator),
C/T=1 will configure timer as event counter
5/1 M1 Mode select bit 1
4/0 M0 Mode select bit 0
M1 M0
0 0 Mode 0;13 bit timer
0 1 Mode 1; 16 bit timer/counter
1 0 Mode 2; 8 bit auto reload
1 1 Mode 3; split timer mode, TL0 as 8 bit timer/counter and
TH0 as 8 bit timer controlled by control bits of timer 0 and
timer 1 respectively. Timer 1 operation timer/counter
stopped.
Refer topic 14.3.1 for detailed discussion of TMOD register.

TCON register

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


MSB LSB

Bit Symbol Description


7 ( TCON.7) TF1 Timer 1 overflow flag; Set to 1(by hardware) when timer 1
overflows; Cleared to 0 automatically when controller vectors to
interrupt service routine at address 001BH.
6 ( TCON.6) TR1 Timer 1 run control bit; set to 1 by a program to start
timer/counter 1; Cleared to 0 to stop timer/counter 1.
5 ( TCON.5) TF0 Timer 0 overflow flag; Set to1 (by hardware) when timer 0
overflows; Cleared to 0 automatically when controller vectors to
interrupt service routine at address 000BH
4 ( TCON.4) TR0 Timer 0 run control bit; Set to 1 by a program to start
timer/counter 0; Cleared to 0 to stop timer/counter 0.
3 ( TCON.3) IE1 External interrupt 1 edge flag; Set by hardware when external
interrupt is detected on 
INT1 pin; Cleared by hardware when
controller vectors to interrupt service routine at address 0013H
only when interrupt is configured as an edge triggered interrupt
(see IT1 bit below)
2 ( TCON.2) IT1 External Interrupt 1 signal type control bit; set to 1 by a program
to configure interrupt 1 as a edge triggered (falling edge); cleared
to 0 to configure it as level triggered (low level)
1 ( TCON.1) IE0 External interrupt 0 edge flag; Set by hardware when external
interrupt is detected on 
INT0 pin; Cleared by hardware when
controller vectors to interrupt service routine at address 0003H
only when interrupt is configured as an edge triggered interrupt
(see IT0 bit below)
0 ( TCON.0) IT0 External Interrupt 0 signal type control bit; set to 1 by a program
to configure interrupt 0 as a edge triggered (falling edge); cleared
to 0 to configure it as level triggered (low level)

Refer topic 14.3.2 for detailed discussion of TCON register.

For timer mode 2: It is an 8 bit mode.


To get the maximum delay we should load minimum count in the timer registers i.e. 00H. So
timer overflow flag will overflow after 256cycles. For 11.0592 MHz crystal frequency
maximum delay that can be generated in mode 2 is 256 x 1.085µs=277.76 µs.
To get delay of 250ms, we will generate the delay of 250 µs and repeat it 1000 (100 x 10) times.

To generate delay of 250 µs, we have to wait for 250/1.085 = 230.4≈230 machine cycles.
Therefore the count to be loaded in TH1 register is 256 - 230= 26D=1AH

DELAY: MOV TMOD, #20H // configure timer 1 in mode 2


MOV TH1, #1Ah // load count value in TH1
SETB TR1 // start timer
MOV R2, #10 // loop counter to repeat 1000 (10 x100) times
AGAIN: MOV R3, #100 //
HERE: JNB TF1, HERE // wait until timer overflows
CLR TF1 // clear timer overflow flag
DJNZ R3, HERE // repeat delay 100 times
DJNZ R2, AGAIN // repeat delay 10 times

(b) Draw and describe how 8051 microcontroller communicates serially with
Max 232 and DB-9 connector?
The following figure shows the connection between RS 232C DB9 connector (PC side) and the
8051 using MAX232 chip.
Figure Interfacing 8051 with RS 232 device
The MAX 232 chip is used as a line driver (voltage converter). It uses +5V power supply for its
operation, which is same as supply voltage of the 8051, therefore single supply is required for
both the 8051 and MAX 232 chip. (Internally MAX 232 contains +5V to +10V voltage doubler
and +10 V to -10V inverter. It requires four capacitors of typical value 22 µF for this
conversion). We know that the 8051 has two pins TXD (P3.1) and RXD (P3.0) for transmission
and reception of serial data, respectively. These pins are TTL compatible, therefore, they require
line drivers to make them RS 232C compatible. It has two sets of line drivers for transmitting
(T1 and T2) and receiving data (R1 and R2). Only one set is required for one serial
communication system. Note that T1in is connected with TXD pin of the 8051, while T1out is
connected with RXD pin of RS232 side connector i.e. TXD signal of 8051 is connected (of
course after voltage conversion) to RXD signal of RS232 device (PC), Similarly, R1in is
connected to TXD pin of RS232 side connector while R1out is connected with RXD pin of the
microcontroller.

OR
Q.3 (a) Design and briefly explain 8031-based microcontroller system to
interface 8K bytes Program memory and 8Kbytes Data memory.
Interfacing of 8Kbytes of SRAM (6264) and 8Kbytes of EEPROM (2864) with the 8051 both
starting at address 0000H is shown below.
Interfacing RAM as well as ROM with 8051

(Refer fig 21.16 in book for correct diagram)


The 8051 has two parallel address spaces for RAM and ROM ranging from 0000H to FFFFH,
therefore we can connect both RAM and ROM at same address range. Different control signals
are used to access data from both memories.

The 8KByte chip requires 13 address lines (A12-A0). RAM signals WE  and 
OE are connected
with WR  of the 8051 respectively and ROM signals 
 and RD OE and 
CE are connected with

PSEN of the 8051as shown in figure.
 and
The 8051 has 64 Kbyte data memory space. It is accessed using ‘MOVX’ instructions. RD
 signals from the 8051 are used to access RAM.
WR

PSEN is generated either when program memory is accessed by ‘MOVC’ instructions or during
 (chip enable) of memory chip if only
program byte fetches. It may also be connected with CE
one ROM chip is present in a system.

Refer example 21.12 for detailed description.

Q.3 (b) Write a C program using interrupts to (1) Generate a 10 KHz


frequency on P2.0 using T1 8-bit auto-reload (2) Use timer 0 as an event
counter to count up a 1-Hz pulse and display it on P0.
Solution:

Assume crystal frequency is 12MHz.


In a given program we have to perform two different tasks simultaneously, so polling of timer
overflow flag cannot be used to generate square wave because it will make microcontroller busy
in monitoring timer overflow flag for majority of the time. Instead of using polling approach we
have to use interrupts to handle such situations more efficiently. We have to write interrupt
service routine for timer to generate square wave.

Finding the count for 10KHz frequency:

Assume Timer 1 is used to generate square wave.


For 10 KHz square wave, ON period = OFF period =50µs
Number of timer clock cycles required to generate 50 µs =50µs/1µs= 50
This 50 cycle time delay can be generated by mode 0, 1, or 2, here timer 1 mode 2 is used.
∴ Count to be loaded in TH1 = 256 – 50 = 206 = CEH

#include <reg51.h>
sbit SWAVE = P2^0;
sbit COUNTER_IN=P3^4; // define P3.4 as COUNTER_IN

void timer1 (void) interrupt 3 // ISR for timer 1


{
SWAVE = ~SWAVE; // toggle pin P2.0 to generate square wave
}
void main (void)
{
TMOD=0x26; // initialize timer 1 in mode 2, T0 as counter
TH1=0xCE; // load count for desired delay
TL0=0x00; // initial count
TH0=0x00; // initial count
IE=0x88; // enable interrupt for timer 1
COUNTER_IN=1; // configure timer 0 input pin as an input
TR1=1; // start timer 1 to generate delay
TR0=1; // start timer 0 to count

while (1) // perform following task forever


{
P0=TL0; // display count on P0
}
}

Q.4 (a) Draw and describe ADC0804 and temperature sensor interfacing
circuit with port1 of 8051 Microcontroller.
(Do not consider LCD interfacing part for this example, it is only given for better understanding
of practical circuit)
Problem statement. Design a temperature monitoring system using the 89C51 microcontroller
which measures and display the temperature on LCD. The temperature range to be measured is
from 100C to 1500C. A buzzer should sound if the temperature goes above 1000C. Discuss the
steps to develop the program and write a program to perform given task.

We will use LM35 as a temperature sensor. It provides output voltage in direct proportion to
temperature. LM35 output voltage increases by 10mV for each 0C increase in its temperature.
We will use LM35 in basic configuration shown in figure 19.12.

This configuration is used for temperature range 2 ºC to 150 ºC only. The corresponding output
voltage is from 20mV (2 x 10mV) to 1500mV (150 x 10mV). Our temperature range is within
this range, therefore this configuration is used. For analog to digital conversion the ADC 0804 is
used. Since LM35 gives 10mV increase in the output voltage per degree centigrade, we should
configure resolution of ADC0804 to be 10mV.This can be done by connecting Vref/2 of the
ADC0804 to 1.28V, this will lead to input voltage range of 0 to 2.56 for full scale output. For
every change of 10mV at the input of the ADC, the digital output will be incremented by 1.
Therefore for every change of 1ºC temperature, output of ADC will change by 1. The different
values of output of ADC corresponding to temperatures is shown in Table 19.16.

Table 19.16 Vout of ADC0804 for different temperatures.

Temperature(ºC) Vin for ADC (mV) Vout (binary)


10 100 0000 1010
20 200 0001 0100
50 500 0011 0010
100 1000 0110 0100

The interfacing circuit for given problem is shown in figure 19.16. It includes interfacing of
LCD, ADC0804, LM35 and a buzzer with the89C51. Note that power on reset circuit is not
shown for simplicity.
Figure 19.16 Temperature monitoring system

The output of LM35 is given to Vin (+) input of ADC. Vin (-) is grounded, therefore effective
analog input is equal to voltage at Vin (+) pin. Data output pins D7-D0 of ADC are connected to
port 1 and data pins D7-D0 of LCD are connected to port 2. The pins of port 3 are used as control
signals of ADC and LCD. P3.7 pin is connected to buzzer. Vref/2 of ADC chip is connected to
1.28 V to get resolution of 10mV.

Steps to develop a program for temperature monitoring system are:


• Configure port connected with D7-D0 of ADC as inputs.
• Initialize LCD for 8 bit mode and 5x7 character size
• Start analog to digital conversion by making WR  = 0
• Wait for end of conversion by monitoring 
INTR pin
• Read digital output from ADC by reading port 1
• The output of ADC is directly calibrated to give temperature (in binary)
• Compare temperature with 100 (64H),
If temperature is greater than 100, make buzzer ON by making P3.7=1.
Otherwise, keep buzzer of OFF
• Convert temperature in binary in to ACII
• Display ASCII value of temperature on LCD
• Start the analog to digital conversion again and repeat the process continuously.

Main Program
CLR P3.7 // make buzzer OFF
MOV P1, #0FFH //configure port 1 as an input port
REPEAT: LCALL AD_CONVERSION // call A to D conversion subroutine
LCALL COMPARE // If temperature > 100 ºC make buzzer ON
//otherwise, keep buzzer OFF
ACALL BIN_ASCII //convert temperature in binary to ASCII
ACALL DISP_LCD //display temperature on LCD
SJMP REPEAT //repeat the process of monitoring continuously

// Analog to digital conversion subroutine

AD_CONVERSION:
SETB P3.6 // configure P3.6 as input to monitor end of
 pin)
// conversion (INTR
CLR P3.5 // make CS low for selecting chip
CLR P3.4 // make WR low
SETB P3.4  =1, low to high pulse for starting ADC
// WR
HERE: JB P3.6, HERE // wait until end of conversion
CLR P3.3 // make 
RD low to read conversion result
MOV A, P1 // read data from ADC
MOV 40H, A // store result at address 40H
SETB P3.3 // make 
RD high before taking next sample
RET // return to main program

// Temperature comparison routine

COMPARISON:
CLR C
MOV A, 40H //get temperature from address 40H
SUBB A, #100 //compare temperature with 100 ºC
JNC B_ON // if temp > 100, make buzzer ON
CLR P3.7 // otherwise, make buzzer OFF
SJMP OVER
B_ON: SETB P3.7
OVER: RET // return to main program

// Binary to ASCII conversion subroutine

BIN_ASCII:
MOV A, 40H // get temperature in binary from address 40H
MOV B, #64H // divisor (100)
DIV AB // A has 100’s digit (unpacked BCD digit)
ORL A, #30H // convert BCD digit in to ASCII
MOV 50H, A // store 100’s ASCII digit at address 50H
MOV A, B // copy remainder in to A for next division
MOV B, #0AH // next divisor (10)
DIV AB // A has 10’s digit (unpacked BCD digit)
ORL A, #30H // convert BCD digit in to ASCII
MOV 51H, A // store 10’s ASCII digit at address 51H
MOV A, B // B has 1’s digit (unpacked BCD digit)
ORL A, #30H // convert BCD digit in to ASCII
MOV 52H, A // store 1’s ASCII digit at address 52H
RET // return to main program

// LCD display subroutine

DISP_LCD:
MOV A, #38H // initialize LCD, 8 bit interface, 5X7
//dots/character
LCALL COMMAND // send command to LCD
MOV A, #0FH // display on, cursor on with blinking
LCALL COMMAND // send command to LCD
MOV A, #06 // shift cursor right
LCALL COMMAND // send command to LCD
MOV A, #01H // clear LCD screen and memory
LCALL COMMAND // send command to LCD
MOV A, #86H // set cursor at line 1, 6th position
LCALL COMMAND // send command to LCD
MOV R2, #03H // counter for three digits
MOV R0, #50H // ASCII digits are stored 50H onwards
MOV A,@R0 // read ASCII digit
NEXT: LCALL DISPLAY // send data to LCD for display
INC R0 // point to next ASCII digit
DJNZ R2, NEXT
MOV A, # “C” // display C for centigrade
LCALL DISPLAY
RET //

// command write subroutine


COMMAND: ACALL READY // check busy flag
MOV P2, A // place command on P1
CLR P3.0 // RS=0 for command
CLR P3.1 // R/W=0 for write operation
SETB P3.2 // E=1 for high pulse
LCALL WAIT // wait for some time
CLR P3.2 // E=0 for H-to-L pulse
RET

// data write subroutine


DISPLAY: ACALL READY // check busy flag
MOV P2, A // send data to port 1
SETB P3.0 // RS=1 for data
CLR P3.1 // R/W=0 for write operation
SETB P3.2 // E=1 for high pulse
LCALL WAIT // wait for some time
CLR P3.2 // E=0 for H-to-L pulse
RET

READY: SETB P2.7 // configure P2.7 as input


CLR P3.0 // RS=0 for command
SETB P1.1 // RW=1 for reading
WAIT: CLR P3.2 // E=1 for high pulse
ACALL DEALY
SETB P3.2 // high to low pulse on E
JNB P2.7, WAIT // wait until busy flag is 0
RET

(b) Write an 8051 C program to send letters ‘M’, ‘D’, and ‘E’ to the LCD
using the busy flag method.
The complete interfacing diagram of the LCD with the 89C51 is shown in figure. Note that the
LCD is connected in 8 bit mode. Port 2 of the microcontroller is connected with data lines of the
LCD (P2.0 to P2.7 with D0 to D7 respectively). RS, R/W and E signals are connected with P1.0,
P1.1 and P1.2 respectively. A potentiometer (10KΩ) is connected between VCC and ground to
set the contrast of the display.
Figure. 8-bit mode LCD interfacing with the 8051

To make program more efficient two subroutines are defined, COMMAND for sending
command and DATADISPLAY for sending data. The corresponding routines are called when
data or command is to be given to the LCD, note that delay routine is a part of both routines to
wait before issuing next command or data.
#include<Reg51.h>
sbit RS=P1^0 ;
sbit RW=P1^1 ;
sbit E=P1^2 ;
sbit BUSY= P2^7;
void READY (void);
void COMMAND (unsigned char);
void DATADISPLAY (unsigned char);
void DELAY (void);
void main()
{ COMMAND(0x38) ; // LCD command for LCD 2 lines 5*7 matrix
COMMAND(0x0F) ; // display on, cursor on with blinking
COMMAND(0x06) ; // shift cursor right
COMMAND(0x01) ; // clear display
COMMAND(0x80) ; // cursor at line 1, position 0
DATADISPLAY(‘M’) ; // send data to LCD
DATADISPLAY(‘D’) ;
DATADISPLAY(‘E’) ;
}
void COMMAND(unsigned char cmd)
{
READY(); // check busy flag
P2=cmd ; // send command
RS=0; // RS=0 for command
RW=0; // R/W=0 for write command
E=1; // high to low pulse for write
DELAY();
E=0;
}
void DATADISPLAY(unsigned char data1)
{
READY(); // check busy flag
P2=data1; // send data
RS=1; // RS=1 for data
RW=0; // R/W=0 for write command
E=1; // high to low pulse for write
DELAY();
E=0;
}

void READY (void)


{
BUSY=1; // configure P2.7 as input
RS=0; // RS=0 for command
RW=1; // R/W=0 for read command
while (BUSY==1)
{
E=1; // high to low pulse for read
DELAY();
E=0;
}

}
void DELAY(void)
{
unsigned int i ;
for(i=0; i<7000; i++);
}

OR
Q.4 (a) Describe working of R-2R type digital to analog converter. Write ALP
to read first 30 bytes of data stored in external ROM as lookup table starting
at 1000H and send it to Port P1.
Refer Interfacing example 19.16 in the book for detailed explanation of digital to analog
converter.
ORG 0000H
MOV DPTR, #1000H // Initialize DPTR to point to ROM address 1000H
MOV R2, #30 // counter for number of bytes
NEXT: CLR A
MOVC A, @A+DPTR // Read byte from ROM address 1000H onwards
// and place in to A
MOV P1, A // Copy byte read from ROM to P1
INC DPTR // Increment DPTR to point to next byte
DJNZ R2, NEXT

ORG 1000H // lookup tale from address 1000H onwards


DB ……….. // The data bytes are stored in ROM 1000H onwards

Q.4 (b) Prepare ALP and explain necessary configuration to interface 4 digits
LED for display 0000 to 1999 digit for 8051 microcontroller.
When two or more digits are required for an application we need to use digit multiplexing. In this
approach, only one display module (digit) is activated at a time and all modules are refreshed one
by one at fast rate so that all display modules appear to be simultaneously active. Figure shows
interfacing of four 7segment modules with the 8051.

Figure Interfacing four 7 segment common cathode modules with 8051.


IC7448 is BCD to 7 segment code converter and digit driver (provides required drive current for
a display module). It provides active high output which can be directly used for common cathode
seven segment (or common anode seven segment if used through transistor driver i.e. inverter).
Four port pins of the microcontroller are connected as an input to 7448 and its output are
connected to all 7 segment modules in parallel. To display the digit, BCD code for digit is sent to
7448 from port pins (lower 4 bits of port 1 in figure) and to select a particular digit (module) the
common pin of corresponding module is grounded by making corresponding pin of port high
(port 2 in figure). IC7447 provides active low outputs which can be directly used with common
anode 7 segment modules.

Refer topic 18.2.2 for the development of the program

Q.5 (a) Prepare ALP and explain necessary configuration to interface SPDT
relay with 8051 microcontroller for turning On-Off Red and Green LED

The relay shown in figure is single pole double throw (SPDT), when coil is energized, C will be
connected to NO and disconnected from NC, therefore any load connected to NC will be
switched off and load connected with NO will be switched on.

The required connection is shown in the figure. Normally, the supply will be given to the red
LED, indicating that relay is not energized. When relay is energized, the Common terminal will
be connected to NO pin, therefore the green LED will glow indicating that relay is energized.

When P1.0 pin is at logic 0, the transistor will be in cutoff, therefore no current will flow through
relay coil and it will not be energized, and red LED will glow. When P1.0 is at logic 1, the
transistor will be conducting and relay will be energized and green LED will glow (red led will
be off).
Program:

REPEAT: SETB P1.0 // Green LED ON, red LED OFF


LCALL DELAY // delay
CLR P1.0 // Green LED OFF, red LED ON
LCALL DELAY // delay
SJMP REPEAT // repeat above task continuously

DELAY: MOV R0, #10 // delay of 1s approx


THERE1: MOV R1, # 200
THERE: MOV R2, # 250
HERE: DJNZ R2, HERE
DJNZ R1, THERE
DJNZ R0, THERE1
RET

The above program will turn ON and OFF red and green LEDs alternatively after every second.

Q.5 (b) Prepare ALP and explain necessary configuration to run 12V DC
motor in both forward and reverse direction.
The practical motor drive circuit for bidirectional control of DC motor is shown in figure 20.11.
VCC

2.2K 2.2K
D1 D2
T3 T5
DC Motor

4.7K 4.7K
T1 T6
A B
(P1.0) Driver D3 D4 Driver (P1.1)
T2 T4

Figure 20.11 *DC motor drive circuit for directional control

As already mentioned in the above section, the direction of the DC motor is controlled using four switch (four
transistors) circuit known as H-bridge. The H-bridge (or full-bridge) consists of two pairs of push-pull drivers
(T2-T3 and T4-T5 in Figure 20.11), called half-bridges and with the motor (or other load) connected between
them. Two digital signals A (port pin P1.0) and B (P1.1) control the direction of the rotation. If A = 0 and B =
1, the transistors T3 and T4 will conduct and the current will flow in path Vcc – T3 – Motor – T4 – Ground, and
the motor will rotate in a clockwise direction. When A = 1 and B = 0, the transistors T2 and T5 will conduct
and the current will flow in path Vcc – T5 – Motor – T2 – Ground, and the motor will rotate in an anti-
clockwise direction. Four diodes (known as freewheeling diodes) protect the transistors from a voltage that is
generated when the motor is suddenly turned off.

Example Assume that switch SW is connected to pin P2.0 and P1.0 is connected to input of half
bridge (point A in figure 20.11) and P1.1 is connected to input of other half bridge (point B).
Write a program to monitor status of SW, if SW=1, rotate DC motor in clock wise direction, or if
SW=0, reverse the direction of the motor.

Solution To rotate the motor in clockwise direction A should be 1(P1.0=1) and B should be 0
(P1.1=0) and to reverse the direction, A should be 0(P1.0=0) and B should be 1 (P1.1=1)

SETB P2.0 // configure P2.0 as input because switch is


// connected with it
REPEAT: JB P2.0, CLKWISE // if SW =1, make P1.1=0, P1.0=1
MOV P1, #00000010B // otherwise make P1.1=1, P1.0=0 to rotate
// motor in anti clock wise direction
SJMP REPEAT // repeat the operation
CLKWISE: MOV P1, #00000001B // rotate motor in clock wise direction
SJMP REPEAT // repeat the operation

OR
Q.5(a) Prepare ALP and explain necessary configuration to interface 0-5VDC
using PWM technique for 8051 microcontroller.
In pulse width modulation the duty cycle of digital pulse train used to drive the motor is varied
(modulated), this will change average DC voltage (and hence power of a pulse train is varied),
which will effectively change the speed of the DC motor.

Example Assume that switch SW (ON-OFF) is connected to port P2.0 for circuit shown in
figure 20.14. Write a program to monitor the status of the switch, if it is low apply 25% DC
power, otherwise apply 50% DC power to motor using PWM technique.

The interfacing circuit is shown in figure 20.14.

Figure 20.14** Interfacing DC motor with 8051 through opto-coupler

The circuit uses opto-coupler which isolates the motor and the 8051. Note that separate power
supplies are used for microcontroller and motor driver circuit, this will also allow to use high
power DC motors (high power loads). For the circuit shown in figure 20.14, the port pin P1.7 is
used to control the switching of the DC motor. When P1.7 is made low, LED in opto-coupler
will glow and photo transistor will conduct, and transistor base current will flow and transistor is
switched ON, and motor will rotate and when P1.7 is high, the motor is switched OFF.
Remember, for the circuit shown in figure 20.14 motor can rotate only in one direction.

Solution: The desired operation can be achieved using following steps:


The status of SW is monitored using JB/JNB instruction.
If switch SW=1 (P2.0=1),
Turn ON motor by making P1.7=0 (see description of figure 20.14)
Apply delay1 (wait),
Turnoff motor by making P1.7=1
Apply delay2, it will be three times delay1,
Else,
Turn ON motor by making P1.7=0
Apply delay3 (wait), it will be two times delay 1
Turnoff motor by making P1.7=1
Apply delay3 (wait), it will be two times delay 1

Repeat above process continuously.

ORG 0000H
SETB P2.0 // configure P2.0 as input because switch is connected with it
SETB P1.7 // initially turn OFF motor
REPEAT: JB P2.0, DUTY50
CLR P1.7 // turn ON motor (high portion of pulse)
MO V R2, #25 // 25 % ON time
ACALL DELAY
SETB P1.7 // turn OFF motor (low portion of pulse)
MO V R2, #75 // 75 % OFF time
ACALL DELAY
SJMP REPEAT // repeat the task of monitoring switch
DUTY50: CLR P1.7 // turn ON motor (high portion of pulse)
MO V R2, #50 // 50 % ON time
ACALL DELAY
SETB P1.7 // turn OFF motor (low portion of pulse)
MO V R2, #50 // 50 % ON time
ACALL DELAY
SJMP REPEAT // repeat the task of monitoring switch
DELAY:
HERE1: MO V R3, #255
HERE: DJNZ R3, HERE
DJNZ R2, HERE1
RET
END
Q.5(b) Draw and describe unipolar stepper motor driver circuit using ULN
2003 with 8051 microcontroller.
Example Interface unipolar stepper motor with the 8051 using suitable driver circuit and write a
program to rotate stepper motor in clockwise direction using full step sequence
Solution: Interfacing of unipolar stepper motor with the 8051 is shown in figure 20.8. The leads
A, A′, B and B′ are connected with microcontroller pins through driver circuit. The sequences
discussed above are generated using microcontroller and given to the stepper motor. The
operation of the circuit is further explained with help of the program.

Figure 20.8 Interfacing of stepper motor with the 8051


Circuit operation

The four leads of stator winding (A, A’, B, B’) are connected with the microcontroller port pins
P2.0 to P2.3 through a driver circuit. The common terminals are connected with Vcc. The driver
circuit used here is ULN2000 because it has 7 Darlington drivers with internal free wheeling
diodes for protection as discussed in above section. (we may also use discrete transistor driver
but it will require extra diodes to be interfaced, which will unnecessarily increase circuit
complexity).
When we make any port pin high, the output of driver is low, the current will flow through
corresponding coil. For example, when P2.0 is made low, the current will flow from Common
terminal (Vcc) to winding A to output A of the driver circuit, this way coil A is energized,
similarly any other coil can be energized by making corresponding port pin high.

Program development steps to rotate stepper motors are:


• To generate a full step sequence, the port pins are loaded with any one of the four values.
The values are CC, 66, 33, 99 (these are inverted values because of inverting nature of a
driver circuit).
To generate wave drive sequence, the four values are 88, 44, 22, 11 (inverted)
To generate half step sequence, the eight values are 08, 0C, 04, 06, 02, 03, 01, 09
• To rotate motor in clock wise direction rotate the initial value loaded in port pins in right
direction using RR A instruction.
To rotate motor in anti clock wise direction rotate initial value in left direction using RL
A instruction.
Note that half step sequence will not be generated simply by rotation therefore we need to
store these values in a lookup table and access these values one after other.

• Repeat above step continuously for rotation of motor.

The program to rotate a stepper motor in clockwise direction using full step sequence is given
below.

In figure 20.8, a driver circuit is used to interface microcontroller with stepper motor, and as
discussed above drivers usually inverts the inputs, therefore we need to use inverted full step
sequence as shown in Table 20.4 (b) (Inverted Full step sequence when interfaced through
driver).
ORG 0000H
MOV A, #0CCH // full step sequence, the first entry in table is 1011= C.
// Here CC is used for continuous rotation.
REPEAT: MOV P2, A // send sequence to motor
RR A // next step of sequence to rotate in clock wise rotation.
LCALL DELAY // wait before going to next step
SJMP REPEAT // next step
DELAY: MOV R1, # 60H // delay, it can be varied to change speed of rotation
THERE: MOV R2, #0FFH
HERE: DJNZ R2, HERE
DJNZ R1, THERE
RET
END
Refer topic 20.3 for other type of driving sequences

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy