VTU Microcontrollers Exam Questions 2011
VTU Microcontrollers Exam Questions 2011
MICROCONTROLLERS
4th SEM B.E. Degree Examination
Question 1
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.
ii) : PSEN (Pin 29) is the active low output control signal used to activate
(enable) external ROM/EPROM/EEPROM. Thus, this signal acts as the read
strobe (or output enable) to external program memory.
iii) : EA (Pin 31) is used for selection of on-chip or off-chip ROM, EA=1 will tell
the 8051 to read a program code from on-chip (internal) ROM and EA is
grounded (EA=0) when a program code is totally contained in an external ROM
only.
iv) : Read signal is activated by the microcontroller to read data from external
RAM. It is connected with OE of the RAM chip. It is activated by instructions
like MOVX A,@DPTR or MOVX A, @Riwhich read external RAM. However,
RD may be connected to OE of ROM chip, when ROM is used as data memory.
Question 2
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,
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
MOV DPTR,#0100H
MOVC A,@A+DPTR
The following program will find square of number present in A. The result will be available in A
after execution of MOVC A, @A+DPTR instruction. The square is found by accessing
corresponding entry from look up table defined in program memory
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
c. Examine the following code and analyze the result with flag register
content:
MOV A,#+96
MOV R1,#+70
ADD A,R1
The reason for getting erroneous result may also be understood this way: Since both the original
numbers were positive and therefore the result should have been positive, but we got it as
negative because of the excessive magnitude of the result (overflow) has modified (inverted) the
sign bit.
These sets of instruction will add two numbers stored in A and R1.
Question 3
The process of calling a subroutine and returning form it to resume the operation of calling
program using ACALL instruction is described in following steps.
ACALL instruction will save the return address (PC) on the stack using two push
operations, lower byte at address SP+1, and higher byte at SP+2. (Stack pointer is
automatically incremented before pushing each byte). Address of subroutine is placed in
the PC.
Subroutine is completed
A RET instruction at the end of the subroutine will retrieve the return address to the PC
from the stack using two pop operations. (Stack pointer is automatically decremented
after each pop operation).
Calling (main) program resumes it operation from the next instruction after ACALL.
current
instruction 1245 H ACALL XYZ
1. save PC
PC 1247 H (return address) subroutine XYZ
on the stack
2. jump to
subroutine
XYZ
b. What are the step executed by the 8051 microcontroller when the following
instructions is executed:
i) RET: It is used to return to main program from subroutine which was called by ACALL or
LCALL instruction. It pops (retrieves) two bytes from top of stack (return address) in to PC
and SP is decremented by two. The program execution continues at the return address,
usually the instruction immediately following an ACALL or LCALL.
PCH=(SP);
SP=SP-1;
PCL=(SP);
SP=SP-1
ii) AJMP addr-11: The instruction for absolute jump is AJMP and its format is,
It transfers the program execution to specified address within 2Kbyte page with respect to PC.
The new value of PC (destination address) is obtained by combining the five bits of the PC (PC15
through PC11), opcode bits 7 through 5, and the second byte of the instruction. Since three bits of
opcode are decided by A10 toA8 of the destination address, this instruction has 8 opcodes.
PC10-0= addr11
c. Write an ALP to add ‘N’ 8-bit numbers available from memory location START.
Display the result at Port 0 and Port 1.
ORG 0000H
CLR C
CLR A
MOV R4, #00H // clear R4H to store upper byte of result
MOV R0, # START // load address of label START in the R0
MOV R2, # N // N numbers
NEXT: ADD A, @R0 // add array elements with A
JC AHEAD
SJMP SKIP
AHEAD: INC R4 // if C=1, increase upper byte of result
SKIP: INC R0 // next array element
DJNZ R2, NEXT // check all numbers are added
MOV P0, A // display lower byte of result at P0
MOV P1, R4 // display lower upper of result at P1
HERE: SJMP HERE
END
Question 4
Different data types in the 8051 C are shown in the following table followed
by brief description.
Data type Bits Bytes Range of values
Standard C data types
signed char 8 1 -128 to +127
unsigned 8 1 0 to 255
Enum 8 1 -128 to +127
16 2 -32768 to 32767
signed short 16 2 -32768 to 32767
unsigned 16 2 0 to 65535
signed int 16 2 -32768 to 32767
unsigned 16 2 0 to 65535
signed long 32 4 -2147483648 to 2147483647
unsigned 32 4 0 to 4294967295
Float 32 4 +1.175494E-38 to + 3.402823E+38
Additional data types for the 8051
Bit 1 - 0 or 1 (bit addressable RAM only)
Sbit 1 - 0 or 1 (Bit addressable SFR only)
Sfr 8 1 0 to 255 (SFRs)
sfr16 16 2 0 to 65535
bit
The ‘bit’ data type is used to define a one bit variable. It is defined and declared as follows:
bit variable_name ; or
bit variable_name = value;
Where variable_name is a name of the bit variable and value is value assigned to the bit. For
example,
bit output; // declare one bit variable output
bit switch_flag = 0; // assign value 0 to a variable switch_flag
bit LED = input; // assign value of bit variable input to variable LED
All bit variables are stored in a bit addressable area of the 8051 i.e. from 20H- 2FH byte
addresses in internal RAM. Since this area has only 16 bytes, a maximum of 128 bit variables
may be declared within any one scope.
The limitations apply to the bit variables are that they cannot be declared as a pointers and array
of type bits. For example,
bit *addr;
bit array [10]; are invalid declarations
sfr
The ‘sfr’ data type defines a special function register (SFR) and assigns the name to it. It is
declared as follows:
Wherevariable_name is the name of the SFR and address is the address of the SFR. For
example,
sfr P0 = 0x80; // Port 0 (address 80H) is defined as P0 and will be accessed using
// name P0
sfr P1 = 0x90; // Port 1 (address 90H) is defined as P1 and will be accessed using
// name P1
sfr TMOD = 0x89; // Timer Mode register (address 89H) is defined as TMOD and
// will be accessed using name TMOD
sfr IE = 0xA8; // Interrupt enable register (address A8H) is defined as IE and will
// be accessed using name IE
Note that any symbolic name may be used in sfr declaration. The variables declared as sfr are
assigned values similar to any other C variable, for example,
P0 = 0x00; // 0x00 is written in to P0
TMOD= 0x02; // 0x02 is written in to TMOD register
sbit
The ‘sbit’ data type defines a bit within a special function register (SFR).It should be noted that
bit can be only within bit addressable SFRs. It is defined and declared as follows:
1. sbit variable_name = sfr_name ^ bit_position; or
2. sbit variable_name = sfr_address ^ bit_position; or
3. sbit variable_name = sbit_address;
Where sfr_name is a name of SFR already defined, sfr_address is address of a SFR, bit_position
is position of the bit within the register and sbit_address is the bit address. For example,
1. sfr PSW = 0xD0; //define PSW
sbit P = PSW^0; //parity flag (bit 0 of PSW) is now accessed using name P
sbit AC = PSW^6; //auxiliary flag (bit 6 of PSW) is accessed using name AC
2. sbit P = 0xD0^0; //parity flag is bit 0 of PSW, accessed using name P
sbit AC = 0xD0^6; //auxiliary flag is bit 6, accessed using name AC
b. Write an 8051 C program to get a byte of data from P1, wait ½ second and
then send it to p2.
#include<reg51.h>
void DELAY(void);
void main ()
{
unsigned char i;
P1=0xFF; // configure P1 as input
Note: The delay generated by function DELAY depends upon the crystal
frequency as well as compiler used. To get exact delay of 0.5 seconds, we can
use timers as shown below.
Delay of 50 ms is generated using Timer 0 and it is repeated 10 times using a loop.
Timer works with frequency 12MHz/12 =1MHz and time period is 1/1MHz = 1 µs.
To generate delay of 50 ms, timer clock cycles required is 50 ms/1 µs=50000 cycles.
Therefore, count to be loaded is 65536-50000= 15536 = 3CB0 H
TH0= 3CH and TL0=0B0H.
void DELAY ()
{
unsigned char i;
for (i=0; i<10; i++) // repeat loop 10 times
// 50 ms delay is repeated 10 times to
// generate delay of 0.5s
{
TMOD = 0x01; // configure timer 0 as a timer in mode 1
TL0 = 0xB0; // load count in timer registers TH0-TL0
TH0 = 0x3C;
TR0 = 1; // start timer
while (TF0!=1); // wait for TF0 to roll over
TR0 = 0; // stop timer 0
TF0 = 0; // clear TF0 flag
}
}
We need to mask the upper nibbles of both ASCII numbers to remove 3 from upper
nibbles and then both unpacked digits are packed.
ORG 0000H
MOV A, # ‘3’ // ASCII number corresponding to higher nibble
// equivalent to ‘MOV A, # 33H
ANL A, #0FH // mask upper nibble
SWAP A
MOV R1, A
MOV A, # ‘5’ // ASCII number corresponding to lower nibble
ANL A, #0FH // mask upper nibble
ORL A, R1 // pack both nibbles.
MOV P1, A // display packed BCD on port 1
HERE: SJMP HERE
END
The result is, P1=A=35H
PART-B
Question 5
TMOD:
TMOD is 8 bit special function register dedicated to both timers. The lower 4 bits configure the
timer 0 and upper 4 bits configure the timer 1. The bit assignment of the TMOD is shown in
following Table.
Table: TMOD register
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
MSB LSB
∴ 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 5000 µs= 5000/1.085= 4608
The Count to be loaded in to TH and TL=65536 – 4608(or 65535– 4608+1) = 60928=EE00H
∴ TH0=0EEH and TL0=00H.
# include <reg51.h>
sbit square_bit = P2 ^ 0; // define P2.0 pin as square_bit
void main ( )
{
square_bit = 1; // set 2.0 pin to logic ‘1’ (high portion of // square //wave
TMOD = 0x01; // Timer 1 configured as timer and in
// mode 1
for (;;) // repeat the following statements forever
// to generate square wave
{
TH0 = 0xEE; // load count in Timer registers TH1-TL1
TL0 = 0x00; // 100H so TH1 TL1
TR0=1; // start timer1
while (TF0 !=1); // wait until TF1 overflows
TR0=0; // stop timer
TF0=0; // clear timer overflow flag.
square_bit=~ square_bit; // toggle p2.0 pin to get square
//wave
}
}
Question 6
a. What is baud rate? Which timer of the 8051 is used to set the baud rate?
The term Baud rate is defined as rate at which data is being transferred serially i.e. rate at which
signal transitions occurs (or frequency of signal transitions).
For example, if signal makes transition every 5ms, the baud rate is (1/5ms) =200Baud.
Rate of data transfer is also measured in bits/s. It is number of bits transmitted per second. The
terms baud rate and bits/s are not always same. For example, if only one bit is coded per signal
transition, then bits/sec and baud rate are same (like BPSK) and when more than one bit is coded
per signal transition, then both are different. Assume 2 data bits are encoded in single signal
transition (like QPSK) and signal is changing every 5ms, so baud rate is 200 Baud as explained
above but bits/s is 2 x 200=400 Bits/s. Hence the relation between bits/s and baud rate is given
as:
Bits/s= Number of data bits encoded per signal transition x Baud rate.
The baud rates 300, 600, 1200, 2400, 4800, 9600, 19200 are used most commonly.
Timer 1 is used to set the baud rate. (most common mode is mode 2 for baud rate generation)
Bit assignment and description of SCON register is given in the following Table.
}
}
Question 7
Internal Interrupts:
Interrupt source Interrupt Interrupt Interrupt
vector type flag clearing
Timer 0 interrupt (TF0) 000BH Internal Auto
Timer 1 interrupt (TF1) 001BH Internal Auto
Serial port interrupt (TI or RI) 0023H Internal By program
When any interrupt is generated (or asserted), it forces the microcontroller to jump to fixed
address in vector table. For example when INT0 is asserted, microcontroller will automatically
jump to memory address 0003H, similarly for timer1 interrupt, it will jump to address 001BH in
interrupt vector table. Since program execution is transferred to fixed location corresponding to
each interrupt source, the interrupts in the 8051 are also referred as vectored interrupts. The
interrupt vector table of the 8051 is shown in the following figure.
TIMER INTERRUPTS
If timer interrupts are enabled in IE register and timer register overflows, then corresponding
timer overflow flag (TF0 or TF1) will set to 1, and microcontroller will jump to the
corresponding interrupt vector table (000B for timer 0 and 001B for timer 1) and clear the TF0
(or TF1) flag automatically. (Refer topic 16.5 for further details)
EXTERNAL INTERRUPTS
The pins INT0 (P3.2, pin 12) and INT1 (P3.3, pin 13) are used as an external interrupt input pins.
When these pins are activated (made low), the 8051 is interrupted and jumps to interrupt vector
table of corresponding interrupt. The external interrupts can be configured to be either level
triggered or transition (edge) triggered interrupts. (Refer topic 16.6 for further details)
Delay of 50 ms is generated using Timer 1 and it is repeated 20 times using a loop in Interrupt
service routine. The LED will be toggled every 20th time the program enters in to ISR (50ms
x20=1s)
Timer works with frequency 12MHz/12 =1MHz and time period is 1/1MHz = 1 µs.
To generate delay of 50 ms, timer clock cycles required is 50 ms/1 µs=50000 cycles.
Therefore, count to be loaded is 65536-50000= 15536 = 3CB0 H
TH1= 3CH and TL1=0B0H.
#include <reg51.h>
sbit SWAVE = P0^4;
void timer1 (void) interrupt 3 // ISR for timer 1
{ TF1=0 ; // clear timer overflow flag
TR1=0; // stop timer
i++;
if (i>20) // If ISR is entered 20 times, the delay of 1s
// is over, therefore toggle the LED
{
SWAVE = ~SWAVE; // toggle pin P0. 4 to ON-OFF LED
i=0;
}
TL1 = 0xB0; // load count in timer registers TH1-TL1
TH1 = 0x3C;
TR1=1 ; // start timer 1
}
void main (void)
{ char i=0;
P1=0xFF // configure P1 as an input
TMOD = 0x10; // configure timer 1 as a timer in mode 1
TL1 = 0xB0; // load count in timer registers TH1-TL1
TH1 = 0x3C;
IE=0x88; // enable interrupt for timer 1
TR1=1; // start timer 1 to generate delay
SWAVE=1 // turn ON LED
while (1) // perform following task forever
{
P2=P1 // read status of P1 and send to P2
}
}
Question 8
a. Interface ADC0804 to 8051 and write a program to read analog data and
display the converted data at Port 2.
ORG 0000H
SETB P3.0 // configure P1.0 as input for connecting INTR
MOV P1, #0FFH // configure P2 as input for reading data
CLR P3.1 // make CS low
NEXT: CLR P3.2 // make WR low
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,#05
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,#10
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