Model Question 2
Model Question 2
MICROCONTROLLERS
4th SEM B.E. Degree Examination
Part – A
Question 1
In the early days of the computer development, most of the program development work was done
in assembly language because high level languages were not developed. Therefore CPU
designers tried to make instructions that will perform as much work as possible, which in turn
has led to development of instructions with many addressing modes for arithmetic, logical and
data transfer operations. These powerful instructions provide greater flexibility in performing
operations. The CISC architecture require less the number of instructions per program at the cost
of number of cycles per instruction. The 8051 microcontroller is based on CISC architecture, for
example multiply and divide instructions are complex instructions for which operation is
performed in hardware.
The common characteristics of CISC architecture are:
Complex hardware: complex as well as more addressing modes, variable instruction size.
Many clock cycles to execute an instruction.
High code density- small program size.
Complex data types.
The simple instructions which perform a few operations at a time will provide high performance
because of less hardware requirements for instruction decoder. Thus, instructions require very
less time to execute. The RISC instructions have few addressing modes for supported by all
instructions. It reduces the cycles per instruction at the cost of the number of instructions per
program. The Microchip PIC microcontrollers are based on RISC architecture.
Simple hardware: simple and less addressing modes, fix instruction size.
Single clock cycle execution, uniform instruction format.
Low code density- Larger program size.
Few data types in hardware.
Emphasis is on software: Compiler design is more complex.
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 1.8.
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. MCS 51 (8051
family) and PIC microcontrollers are based on Harvard architecture.
b. Interface 8051 to external ROM and RAM and explain how 8051 access
them?
Interfacing of 8Kbytes of SRAM (6264) and 8Kbytes of EEPROM (2864) with the 8051 both
starting at address 0000H is shown below.
c.
Interfacing RAM as well as ROM with 8051
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 and RD of the 8051 respectively and ROM signals OE and CE are connected with
PSEN of the 8051as shown in figure.
The 8051 has 64 Kbyte data memory space. It is accessed using ‘MOVX’ instructions. RD and
WR signals from the 8051 are used to access RAM.
PSEN is generated either when program memory is accessed by ‘MOVC’ instructions or during
program byte fetches. It may also be connected with CE (chip enable) of memory chip if only
one ROM chip is present in a system.
Refer example 21.12 for detailed description.
Question 2
a. Explain briefly the five addressing modes of 8051 with example for each.
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,
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,
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
b. After reset, the contents of internal memory of 8051 with address 0AH and
0BH contains data 22H and 33H, respectively. Sketch the contents of
internal memory from address 07H to 0BH and the value of register SP,
after executing the following code:
PUSH 0AH
:
MOV 81H, #0BH
POP 09H.
SP 0AH
07H 00H
08H 22H
09H 33H
0AH 22H
0BH 33H
Question 3
Originate- ORG
The ORG directive allows us to place the code and data anywhere in the program address space.
The number after the ORG can be in hex or decimal. If the number is decimal, the assembler will
convert it to hex. Its format is ‘ORG Address’ For example, ORG 0000H will place the
instructions (or data) from address 0000H onwards (some assembler use this directive as ‘.ORG
Address’)
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.
Define Word- DW
This directive defines 16 bit variable. (The meaning of “Word” is microcontroller/ processor
dependent, for example, for 32 bit controller, word means 32 bits)
Equate- EQU
Equates label to the number.
EQU TEMP, #20 will assign name TEMP to data #20.
END
End directive tells the assembler to stop assembling.
There are still many other directives like DD, DBIT, DS, PROC, ENDP, LABEL, USING,
EXRERN, IF, ELSE, ELSEIF, ENDIF, NAME, PUBLIC, SEGMENT, BSEG, CSEG, BIT,
CODE, DATA, EVEN etc. used with different assemblers. Programmer should refer documents
or help for the assembler in use.
b. If the XTAL frequency of 8051 is 8 MHz, find the time taken to execute the
following program:
MOV R2,#04
MOV R1,#06
WAIT: DJNZ R2, WAIT.
c. Write 8051 APL which checks whether the ten numbers stored from
external RAM memory address, 2000H are odd/even. The program should
store accordingly 00H/FFH from internal location 30H onwards.
MOV R0,#30H //Starting address of internal memory
MOV R7,#10 //total numbers
MOV DPTR,#2000H //starting address of external memory
AGAIN: MOVX A,@DPTR //transfer number from external memory
RRC A //transfer LSB to Carry bit
JC ODD_NO //Jump if carry is set means number is odd
MOV @R0,#0FFH //Transfer FFH if Even
SJMP NEXT
ODD_NO: MOV @R0,#00H //Transfer 00H if number is even
NEXT: INC R1 //Increase internal memory index
INC DPTR //Increase external memory index
DJNZ R7,AGAIN //Repeat for 10 times
Question 4
a. Interface ADC0809 to 8051 and write ALP to convert the analog voltage
connected to second channel. Display the digital value on LEDs connected
to Port-0.
// START OF CONVERSION=P1.2, EOC=P1.1, OE=P1.0, ALE=P3.0
// ADD A=P3.3, ADD B=P3.2, ADD C=P3.1
// LEDs ON P0, CONVERTED DATA FROM ADC0809 = P2, ANALOG INPUT = IN2
CHANNEL 2
MAIN:
CLR P3.1 //SELECT CHANNEL 2 BY ADD A,B,C
SETB P3.2
CLR P3.3
AGAIN: CLR P1.0 ;OE
CLR P3.0;ALE
SETB P3.0 ;ALE
SETB P1.2 ;START
CLR P1.2;START
HERE1: JB P1.1,HERE1 //wait for end of conversion
SETB P1.0 // OE
MOV A,P2
MOV P0,A
ACALL DELAY
SJMP AGAIN
DELAY: NOP
UB2: MOV R6, #3FH
UB1: MOV R5, #5FH
HERE: DJNZ R5, HERE
DJNZ R6, UB1
DJNZ R7, UB2
RET
b. Interface 8051 to stepper motor and write an ALP to rotate the motor first
+4 steps and then -6 steps.
ORG 0000H
MOV A, #0CCH // full step sequence, the first entry in table is 1011= C.
// Here CC is used for continuous rotation.
MOV R0,#04
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
DJNZ R0, REPEAT // next step
MOV R0,#06
REPEAT: MOV P2, A // send sequence to motor
RL A // next step of sequence to rotate in ANTI-clock
//wise rotation.
LCALL DELAY // wait before going to next step
DJNZ R0, 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
PART – B
Question 5
a. What is the difference between timer and counter operation of 8051? How
to start/stop the timer/counter of 8051 when
i) GATE control is not used
ii) GATE control is used
The major difference between interval timer and event counter is source of the clock pulse, when
timers are used as a timer (interval timer), the internal clock signal is used as a clock source for
the interval timer activities,
When timers are used as 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 selection of the clock source using C/T bit is illustrated in the following figure. For timer
clock to reach timer stages the C/T bit must be 0 (interval timer operation), bit TRX (TR0 or
TR1) must be set to 1(timer run) and gate bit in TMOD register must be 0 or INTX pin must be
1.We may conclude that when timer is configured as an interval timer, the timer clock pulses are
fed to timer stages when C/T = 0, timer run bit TRX=1 AND Gate =0 or external input pins
INTX =1. The path for clock signal when C/T = 0 is shown by dotted line figure 14.4 (a).
When C/T = 1, selection switch S will be connected to point B. The point B is connected to timer
input pins T0/1. The clock source for the timer circuits is external pulses on pin T0 (Pin 14) or
T1 (Pin 15) of microcontroller for timer 0 and timer 1 respectively. The path for clock signal
when C/T = 1 is shown by dotted line figure 14.4 (b).The other control bits for timer start/stop
will remain same as described in the above section.
i) GATE control is not used : When GATE control is not used, it is referred as software
control of the timer/Counter
The software control of the timers is illustrated in the following figure (a). The software control
(start/stop of timers) is achieved when Gate bit is programmed to zero i.e. Gate=0. When
Gate=0, the output of NOT gate is 1, which is applied to OR gate, since one of the input of OR
gate is 1, its output will be 1 irrespective of status of the other inputs. The output of OR gate (=1)
is given to AND gate. Now one input of AND gate is 1, therefore out put of AND gate is
controlled by other input. The other input is TR0/1 bit, therefore output of AND gate is
controlled by status of TR0/1 bit i.e. If TR0/1=0, output of AND gate is 0 and If TR0/1=1, output
of AND gate is 1. The output of AND gate is used to control the switch which will connect clock
signal to the timer stages for their operation. When output of AND gate is 1, the switch is closed
and the clock signal is applied to the timer stage and the timer starts its operation. Therefore,
effectively we can say that TR0/1 bits only controls the timer operation. See dotted line in the
figure 14.2(a). As mentioned earlier, TR0/1 bit can be set or cleared using SETB (SETB TR0 or
SETB TR1) and CLR (CLR TR0 or CLR TR1) instructions.
Figure Software/hardware control of timers
ii) GATE control is used : When GATE control is used, it is referred as hardware control
of the timer/Counter
The hardware control of the timers is illustrated in figure 14.2 (b). The hardware control of
timers is achieved when Gate bit is programmed to one i.e. Gate=1 and TR0/1=1. When Gate=1,
the output of NOT gate is 0, which is applied to OR gate, since one of the input of OR gate is 0,
the output is controlled by the second input. The second input to OR gate is given from INT0
and INT1 pins for timer 0 and timer 1 respectively. When INT0/1 = 0, the output of OR gate is
0 and when INT0/1 = 1, the output of OR gate is 1. Since TR0/1 is already programmed to be 1,
the status of INT0/1 pin will determine the output of AND gate and finally operation of the
timer because output of AND gate is used to connect the clock source to the timers. See dotted
line in figure 14.2(b)
This feature allows the pulse width measurement i.e. a pulse applied to INTX pin will run the
timer as long as it is high, then, the timer register may be read to determine time period of the
pulse. It should be noted that the external hardware control is achieved by making corresponding
timer runs bits TR 0/1=1 as shown in figure 14.2 (b).
Five interrupts are available in the 8051.Three of them are internal interrupts i.e. they are
generated because of internal operation of the 8051. They are timer 0 (TF0), timer1 (TF1) and
serial port (TI or RI) interrupts. Remaining two are external interrupts INT0 and INT1 i.e. they
are invoked by external signals given to pins INT0 and INT1 . The bar over INT0 and INT1
indicate that they are active low interrupt inputs. The external interrupts INT0 and INT1 are also
referred as IE0 and IE1 respectively. For each interrupt source there is a fixed location in a
program memory that contains its interrupt service routine (ISR). This part of memory which
stores the ISRs is called the interrupt vector table. It is shown in Table 16.1.
Table 16.1 Interrupt vector table of the 8051
Square wave have duty cycle of 50%, so, ON period = OFF period = Total Period/2
= 100µs/2 = 50µs
We should generate the time delay of 50µs and toggle pin P2.0 every 50µs to generate square
wave of 1 KHz.
∴ The time period of timer clock = 1/0.9215MHz = 1.085µs (time period of one machine cycle)
also, Timer register will be incremented by one every 1.085µs)
Number of timer clock pulses (of 1.085uS) required to make 50 µs= 50/1.085= 46.08=
46(approx)
The steps to develop the program to generate the square wave are as follows:
Configure any one timer as an interval timer using TMOD register
Load initial count in to timer registers THX-TLX to get desired delay
Start the timer
Wait until timer overflows
Toggle port pin on which square wave should be generated
Stop timer and clear the overflow flag and repeat the process continuously
Minimum frequency will be generated when minimum count (maximum delay) is loaded.
For mode 1
The minimum count is 0000, the delay that can be generated is 65536 cycles for half cycle.
Therefore, the time for one cycle is 65536 x 2x 1.085µs = 131072 x 1.085 µs= 142213.12 µs
= 7.0317 Hz
For mode 2
The minimum count is 00, the delay that can be generated is 256 cycles for half cycle.
Therefore, the time for one cycle is 256 x 2x 1.085µs = 512 x 1.085 µs= 555.52µs
= 1800.11 Hz
Question 6
After the power is turned ON, DTE asserts the Data terminal ready (DTR) signal to inform the
DCE (modem) that it is ready, see figure 15.4. When modem is ready, it asserts the Data set
ready (DSR) signal to the DTE. Once DSR signal has been received, the DTE makes request to
use the data channel by asserting RTS signal to start transmission. Then the modem at the other
end (receiver end) is dialed. This modem (usually in answer mode) replies by sending a signal at
a carrier frequency of 2255 Hz. When the modem at the sending terminal receives this signal, it
sends Data carrier detect (DCD) to the DTE, then after the modem sends Clear to send (CTS)
showing that the channel is ready for transmission. Immediately after receiving CTS signal, the
DTE of the transmitter side sends serial data on its TXD output. For reception of data similar
handshaking process is carried out.
b. Explain how 8051 transmit the character and receives a character serially
using UART.
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, see figure 15.12, 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.
Question 7
b. With a block schematic explain the features 8255 PPI chip and its MODE-0
Operation.
8255 PPI (Programmable Peripheral Interface). 8255 is an I/O port chip used
for interfacing I/O devices with microcontroller/processor.
The features of 8255 are:
1. Two 8-bit ports (port A and port B) and two 4-bit ports (port C upper and lower)
are available. The two 4-bit ports can also be collectively used as a 8-bit port.
3. Input ports are not latched while output ports are latched.
4. A maximum of four ports are available so that overall 16 I/O configuration are
possible
c. If the internal memory 20H contains AAH and 07H contains 55H. What is
the content of register A and status of carry bit after executing the
following code:
MOV C,07H
MOV A,#20H
ADDC A,07H
A=76H
C=0 (RESET)
Question 8