Model Question1
Model Question1
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,
F0: Flag 0
It is a general purpose flag. It can be used as a one bit memory location to record some event.
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.
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,
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.
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,
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,
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.
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.
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
Addressing modes
1. Immediate addressing
2. Register addressing
3. Direct addressing
4. Indirect addressing
• Register indirect addressing
• Indexed addressing
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,
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 direct1, direct2 // copy data from address direct2 to address direct1
MOV 50H, 83H // If (83H)=10H→ (50H)=10H
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
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
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
(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
(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
(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.
(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
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
(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
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.
#include <reg51.h>
sbit SWAVE = P2^0;
sbit COUNTER_IN=P3^4; // define P3.4 as COUNTER_IN
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.
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.
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
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
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
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
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 //
(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 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
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.
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:
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
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)
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 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.
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.
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.
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