Model Question 5
Model Question 5
Harvard Architecture: It has physically separate memory storage to hold program instructions
and data i.e. separate program and data space. Since it has separate buses to access program and
data memory, it is possible to access program memory and data memory simultaneously. The
organization of memory and buses in this architecture is shown in figure.
Harvard architecture
The advantage of a Harvard architecture microcontroller is that it is faster for a given circuit
complexity because it offers greater amount of parallelism. The disadvantage is that it requires
more hardware, because two sets of buses and memory blocks are required.
Microprocessor Microcontroller
Microprocessor is complete functional Microcontroller is complete functional
CPU i.e. it contains ALU, registers, microcomputer i.e. it contains the circuitry of
stack pointer, program counter, microprocessor and in addition it has built in
instruction decode and control unit and memory (ROM, RAM), I/O circuits and
interrupt processing circuits. peripherals necessary for an application.
Microprocessor instruction sets are data Microcontrollers have instruction sets that are
processing intensive, means powerful related to the control of inputs and outputs,
addressing modes and many instructions means they have many bit handling
to move data between memory and CPU instructions along with byte processing
to handle large volumes of data. instructions.
(7) What is the difference between 8031 and 89C51 microcontroller IC?
8031 is ROMless version of the 8051 and 89C51 has 4Kbytes of Flash memory (Program
memory). Moreover, 8031 is made from HMOS transistors, a letter ‘C’ in a part number
indicates that the chip is made from CHMOS transistors. The CHMOS devices have the
following advantages:
Low power consumption
High stability (noise immunity)
High speed
Support power down and idle modes
(b) Draw the internal architecture of 8051 microcontroller and explain each block in
detail.
The architectural block diagram of the 8051 is shown in figure 2.1, it shows organization of all
hardware components and data path connections between them. It includes 8 bit ALU along with
Boolean processing capabilities, program and data memory, four 8 bit I/O ports, two
timers/counters, UART, timing and control circuits and oscillator circuit.
ALU
Arithmetic and logic unit performs all arithmetic (addition, subtraction, multiplication and
division) and logical (AND, OR, NOT, EXCLUSIVE-OR and rotating) operations on 8 bit data
i.e. the 8051 has 8 bit ALU. The ALU also updates information about the nature of the result in
the flag register (PSW).
Memory
The 8051 family has separate on-chip program and data memory. The program instructions are
stored in a program memory (ROM/EPROM/EEPROM/Flash based on a family member). The
amount and type of on-chip program memory is the key factor that differentiate all the members
of the family, for example, 80C51 has 4Kbytes of on-chip ROM, whereas 80C52 has 8Kbyte
(ROM) , 87C51 has 4Kbytes (EPROM) and 87C52 has 8Kbytes(EEPROM) of on-chip program
memory. Total program memory (including on-chip ROM) that can be connected with the 8051
is 64Kbytes. Similarly, data memory can be on-chip or off-chip. Internal data memory (RAM) in
80C51 is 128 bytes and in 80C52 is 256 bytes. There are also on-chip RAM locations which are
used to program and control various on-chip hardware peripherals and features of the 8051.They
are known as Special Function Registers (SFRs). These memories are discussed in detail in the
next section.
Peripherals
The 8051 has two 16 bit timers (8052 has three timers) that are used for timing and counting
applications. It has full duplex serial port (UART) to handle serial data transmission and
reception.
Timing and control unit
This unit generates all timing and control signals necessary for the execution of instructions and
synchronizes all internal activities with the clock.
PORT0 PORT2
DRIVERS DRIVERS
PROGRAM
ADDR.
REGISTER
BUFFER
ALU
INTERRUPT, SERIAL PORT AND
TIMER BLOCK PROGRAM
COUNTER
PSW
INSTRUCTION
ALE
REGISTOR
TIMING DPTR
AND
CONTROL
RST
PORT3
PORT1 LATCH
DRIVERS
OSC
PORT3
PORT1 DRIVER
DRIVERS
Q.2
(a) Explain TCON and TMOD Special function register (SFR) in detail.
TMOD register
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
MSB LSB
TCON register
(7) WR
WR (external Data Memory write strobe)
Write signal is activated while writing data to a RAM. It is usually connected with WE (write
enable) pin of RAM chip. Instructions MOVX @Ri, A or MOVX @DPTR, A will activate this
signal.
OR
(b) Explain following instructions of 51 family microcontrollers
(1) DA A:
DA A instruction performs the following operations. After ADD or ADDC instruction,
1) If lower nibble is greater than 9 or if AC=1, add 6 to lower nibble (4 bits)
2) If upper nibble is greater than 9 or if CY=1, add 6 to upper nibble.
(2) DEC A
Decrement content of accumulator by 1.
DEC A // Decrement the contents of A by 1, A=A–1
DEC A // A=A–1, If A=10H→A=0FH
(3) RRC A
Rotate content of accumulator right side through carry.
RRC A // rotate A one bit position to the right through carry flag, bit D0 to CY, CY to D7,
bit D7 to D6, …, bit D2 to D1 and bit D1 to D0 as illustrated in figure5.6.
(4) DEC @R0
Decrement content of address pointed by R0 by 1.
DEC @Ri // Decrement the contents of address pointed by Ri by 1,(Ri)= (Ri) –1
DEC @R0 // (R0)= (R0) –1, If R0=10H, (10H)=55H→ (R0)=(10H)=54H
(5) ADDC A, R7
Addition of accumulator and content of R7 with carry, store the result in A
(6) SETB C
Set carry flag to logic ‘1’.
(7) SWAP A
Exchange content of lower nibble of A to higher nibble.
Q.3
(a) Consider that switch is connected with port pin P3.2 in pull-up configuration so that
when it is pressed logic at P3.2 is 0 and 1 otherwise. 8 LEDs are connected with Port P0
using common anode configuration. Draw interfacing circuit diagram for above
requirements. Write program to monitor the switch. If switch is pressed, all LEDs should
glow and if switch is not pressed all LEDs should be OFF. Check switch continuously.
Assuming Pull-up register bank connected to port 0.
ORG 0000H
SETB SWITCH // configure P3.2 as input pin
AGAIN: JB SWITCH, THERE // check status of switch, it is high, OFF LEDs
MOV P0, #00H // otherwise glow LEDs (common anode)
SJMP AGIAN
THERE: MOV P0, #0FFH // For OFF, apply FF to port (common anode)
SJMP AGIAN
END
(b) Interface LCD with 8051 microcontroller. Connect data lines with port P0, control
lines with any three port pins of port P1. Write program to display message
‘Microcontroller” on the first line and “Interfacing” on the second line.
;P0.0-P0.7 are connected to LCD data pins D0-D7, Assuming Pull-Up Register Bank on P0.
;P1.0 is connected to RS pin of LCD
;P1.1 is connected to R/W pin of LCD
;P1.2 is connected to E pin of LCD
ORG 0000H
LCALL WAIT ; Initialization of LCD by software
LCALL WAIT ; this part of program is not mandatory but
MOV A, #38H ; recommended to use because it will
LCALL COMMAND ; guarantee proper initialization even when
LCALL WAIT ; power supply reset timings are not met
MOV A, #38H
LCALL COMMAND
LCALL WAIT
MOV A, #38H
LCALL COMMAND ; initialization complete
Q.3
(a) Consider that common anode seven segment display is connected with port P0 and
switch is interfaced at port pin P1.0 of 8051 microcontroller. Draw interfacing
diagram. Write assembly language program to monitor the switch. If switch is pressed,
display “0” on common anode display and if switch is not pressed display “1” on
common anode display.
ORG 0000H
SETB SWITCH ; Make input pin
AGAIN: MOV P0, #03FH ; 7 Seg code for '0'
ACALL DELAY
HERE: JB SWITCH, HERE
MOV P0, #06H ; 7seg code for '1'
ACALL DELAY
SJMP AGIAN
(b) Write program to generate square wave of 1 KHz on port pin P1.7. Use timer generated
delay by timer 0 in mode 0. Consider crystal frequency 12 MHz.
ORG 0000H
MOV TMOD, #0x00
REPEAT: MOV TL0, #0BH
MOV TH0, #0F0H
SETB TR0
WAIT: JNB TF0, WAIT
CPL SQ_PIN
CLR TR0
CLR TF0
SJMP REPEAT
Q.4
(a) Explain serial data transmission and reception. Write steps required for serial
data transmission using 8051.
Transmission: Data transmission begins by writing data to the SBUF register. The START and
STOP bits are added by hardware to form a 10 bit frame, then, 10 bit parallel to serial conversion
is performed and one bit (LSB first) at a time is transmitted through TXD pin, once complete
frame is transmitted, TI flag is set automatically by serial port hardware to indicate end of the
data transmission. We need to monitor TI flag to conform that SBUF register is not overloaded.
If TI flag is set, it implies that last character transmission is completed and now SBUF is empty
and new byte can be written to it to start next transmission. If a new byte is written to SBUF
before TI is raised, the untransmitted part of previous byte will be lost.
It should be noted that microcontroller sets TI flag when it completes byte transfer, whereas it
must be cleared by the programmer after next byte is loaded in to SBUF.
Reception: The data reception begins when REN=1 and high to low transition (start bit) is
detected on RXD pin. The received byte is loaded in to SBUF register (the START and STOP
bits are separated by UART hardware once complete frame is received) and stop bit in to RB8
(SCON bit 2) only if following two conditions are met.
If these two conditions are not met, received character is ignored and RI is not set and receiver
circuit waits for next start bit.
1. Configure Timer 1 in 8 bit auto reload mode. (This is most commonly used
configuration).
2. TH1 is loaded with appropriate value to set required baud rate.
3. Configure SCON register to set serial port in mode 1. REN is set to 1 to enable serial data
reception.
4. TR1 is set to start timer 1 to generate clock at desired baud rate.
5. The byte to be transmitted is written to SBUF register.
6. For the transmission, TI flag is monitored to make sure that byte has been transmitted
completely. For the reception, RI flag is monitored to check that byte is received or not.
When RI is raised, SBUF contains a received byte. It must be read from SBUF. This
monitoring of RI and TI can be done either using polling method or interrupt method.
7. Once TI is set when a byte is transmitted, (or RI for reception) it is cleared (TI for
transmission or RI for reception) by software so that next transmission (reception) can be
initiated.
8. Repeat steps 5 to 7 for next byte transmission and steps 6 and 7 for next byte reception.
(b) Write program to transmit message “GTU EXAM 2013” using serial port of
8051 at baud rate 9600.
ORG 0000H
MOV TMOD, #20H ; timer 1configured in mode 2
MOV TH1, #0FDH ; set 9600 bps baud rate
MOV SCON, #50H ; 8-bit data, 1 stop bit, REN enabled
SETB TR1 ; start timer 1 to generate clock (at baud rate)
REPEAT: MOV A, #“G” ; load “G” in to A, and call subroutine that will
ACALL SEND ; transmit the character
MOV A, #“T” ; send “T”
ACALL SEND
MOV A, #“U” ; send “U”
ACALL SEND
MOV A, #“ ” ; send BACK SPACE “ ”
ACALL SEND
MOV A, # “E” ; send “E”
ACALL SEND
MOV A, # “X” ; send “X”
ACALL SEND
MOV A, #“A” ; send “A”
ACALL SEND
MOV A, #“M” ; send “M”
ACALL SEND
MOV A, #“ ” ; send “ ”
ACALL SEND
MOV A, #“2” ; send “2”
ACALL SEND
MOV A, #“0” ; send “0”
ACALL SEND
MOV A, #“1” ; send “1”
ACALL SEND
MOV A, #“3” ; send “3”
ACALL SEND
OR
Q.4
(a) Write program to receive 16 data bytes from computer to microcontroller 8051
through serial port. Store data from memory location 0400h onwards
ORG 0000H
MOV R4, #16
MOV DPTR,#0400H
MOV TMOD, #20H ; timer 1configured in mode 2
MOV TH1, #0FDH ; set 9600 bps baud rate
MOV SCON, #50H ; 8-bit data, 1 stop bit, REN enabled
SETB TR1 ; start timer 1 to generate clock (at baud rate)
REPEAT: JNB RI, REPEAT ; wait until character byte is received
MOV A, SBUF ; read and save the received character
MOVX @DPTR, A
INC DPTR
CLR RI ; get ready to receive next byte
DJNZ R4, REPEAT ; go to receive next character
END
(b) Explain how timer 1 can be used as 16 bit counter to count external pulses
which are given at pin T1. Write program to count external pulses and save it in
register R6 and R7.
The major difference between interval timer and event counter is source of the clock pulse, when
timers are used as an event counters, pin T0/T1 (P3.4/P3.5) are basically used to provide external
pulses for timer 0/1, therefore external pulses will increment the timer registers TLX and THX.
The timer can be configured as an event counter by setting C/T = 1 in TMOD register. The other
difference between interval timer and event counter is that counter is normally started with initial
value of “0000” so TLX and THX normally initialized with value 00H. The operation of event
counter is illustrated in figure.
As shown in the figure 14.10, the pulses on external pin T0 (or T1 for timer 1) are selected as
clock source for the timer operation by setting C/T = 1 in TMOD register. The timer registers are
normally initialized with value 0000 and the count will increment by 1 when pulse on pin T0/1 is
applied, therefore, the circuit will count the number of pulses (events) applied externally to the
timer pins. The count in timer registers at any time will represent the number of pulses applied
(or external events occurred) till that time.
ORG 0000H
MOV TMOD, #50H ; timer 1 configured in mode1 as a counter
MOV TL1, #00H ; initial count loaded in TH0-TL0 pair is 0000H
MOV TH1, #00H
MOV P2, #00H ; clear P2 and P1
MOV P1, #00H
SETB P3.5 ; configure T1 pin as an input
SETB TR1 ; start counting
AGAIN: MOV R6, TL1 ; send count on P1, P2 continuously.
MOV R7, TH1
SJMP AGAIN ; repeat the process of reading timer registers
END
Q.5
(a) Explain interfacing of DC motor with 8051 using IC L293D. Write program to
rotate motor in clockwise as well as anticlockwise direction.
(b) Draw circuit diagram to interface stepper motor with 8051. Write program to
rotate motor in clockwise direction using half step mode.
Interfacing of unipolar stepper motor with the 8051 is shown in figure. 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.
OR
Q.5
(a) Explain interfacing of RTC with 8051 microcontroller. Write program to get
values of hour, minute and second from RTC to RAM locations 80h, 81h and 82h
respectively.
The interfacing of RTC chip DS12887 with the 8051 is shown in figure. Since DS12887 has
multiplexed 8 bit address/data bus (AD0-7), these pins are directly connected with the AD0-7
(lower address and data bus i.e. P0) of the 8051. The chip select signal can be generated using
address decoder circuit, the input to the address decoder circuit will be high order address bus.
(Refer topic 21.4 for details of address decoding). For simplicity, the chip select (CS) signal is
permanently grounded, therefore the RTC chip is always selected. The RD and WR of the 8051
is connected with DS and R/W of the DS12887, therefore the DS12887 is mapped on external
data memory space i.e. addresses 00-7FH of the DS12887 is seen as external data memory
connected from 00H to 7FH. The ALE signal of the 8051 is connected directly with the AS
signal of the DS12887 to demultiplex address and data. Note that there is no need of external
latch circuit (like 74373) to demultiplex address and data because DS12887 does have internal
latch. Since only 8 address bits (A0-7) are used in our example, the contents of the DS12887 can
be accessed using instructions MOVX A, @Ri or MOVX @Ri, A. (If we use A8-15 to generate
chip select signal, then the DS12887 can be accessed using instructions MOVX A,@DPTR or
MOVX @DPTR,A).
ORG 0000H
MOV R0, #80H
MOV R1, #04H ; initialize R1 as a pointer to hours register (address 04)
MOVX A, @R1 ; read hours in to A
MOV @R0, A
INC R0
(b) Explain interfacing of External 32K EPROM and 16K RAM with 8051. Draw
circuit diagram.
The 16KByte chip requires 14 address lines (A13-A0). RAM signals WE and OE are connected
with WR and RD of the 8051 respectively and ROM signals OE is connected with PSEN of the
8051as shown in figure.
The 32KByte chip requires 15 address lines (A14-A0). The chip enable CE signals of both chips
are generated as shown in the figure.