Micro 1
Micro 1
Micro 1
Microcontroller
the byte or character and also used to set the receiving mechanism to prepare for
Chapter 1 : Peripheral Devices [Total Marks - 4] the reception of the next character. A common kind of start-stop transmission is
ASCII over RS·232, for example for use in teletypewriter operation
Q. 1 I Describe synchronous and asynchronous data transfer techniques used
I •.:
in microprocessor. (4 Marks)
Mark -+j~~~~:-:=
!.~.I.a .. ~.~~~i-~~-1ldJe ~,6 o~7 da.~_~~~.
l nchronous communication:
With
themselves
synchronous commWlications,
to each other, and then continually
the two devices
send characters
initially
to stay in sync.
synchronize
Space
y:
Bit time
.
eE,
PROGfC'E"l
ALE
RESET
1m
Fig. 1 : Synchronous serial communications j(jJ1
lOW
eLK
Asynchronous communication:
Voo
Asynchronous serial communication describes an asynchronous Vcc
transmission protocol in which a start hit is sent prior to each byte or character GND
and a stop bit is sent after each data byte as shown in Fig. 2. The start bit is used
to synchronize transmitter and receiver. The stop signal is used to indicate end of Fig. 3
1-3 Microcontroller (MSBTE) 1-4
Microcontroller (MSBTE) Winter 2008 Winter 2008
Q. 1 (c) Compare between microprocessor. microcontroller and microcomputer. Q.4(a) Draw complete interfacing diagram of Ie 8085 with IC 8155. Write a
Ans. : program to read dala from po~ of 8155 and out it ~rt B. (8 Marks)
(4 Marks)
Ans .
The 8155 has inbuilt de-mulliplexing circuitry to de-multiplexed
S,. Few instruction
InbuiltTimer
Inbuilt
I/O Ports
Separate
Many
Boolean
Inbuilt RAM
are or to
Microcontroller
available.
ROM
multifunction
serial
operation
memory
port i.e.
pins Ie.
read/write
Do
ill
to store
Many not
not
not
rend/write
external
possible
Boolean
Program
Less
011 same havehave
have
instruction
directly.
portsoperation
and
Microprocessor
I/O are inbuilt
inbuilt
memory
dnta data
not
multifunction toisTimer
inbuilt
from/to RAM
arc
not serial
or
stored
available,
pins on addrtss/data bus i.e. ADo-Ao, using ALE signal. Hmce, extanal circuitry for do-
nal bit8.is
idual 8155
like
ROM or
8250
requires
pon,
1. 8255
or 825extra
extra
requires I.device like
devices multiplexing of AD bus is not required; the ADo-AD7 lill(~ of 8085 can be
101M
----
of 8085 can also be connected directly to the corrtsponding pins of8155,
so no need 10 generate control signal like lOW, lOR, MEMR and MEMW
using decoder. The RESET OUT of 8085 can be connected directly to RESET
--
related opcr~tions.
pin Of8155\ The 8155 requirts a CS signal to select cbip during 110 or memory
A"
\A" G2A 'WB 01
~of8155
A"~C 74~~~38
I \ _\"~12 B
A DecOder V 4
TableB
A~5 III
0I0I0A4H
0IA2
A3H
A2H
Au
A13
l'A5H
IAOH
AIH
A"
Addr«:ss
Port
CWR
~PortA
TimcrLSB
TimerMSB
A;Port
PortB C Select Ports
ADD-AD? IIII Used
I ADD-AD?
1-
portAl~
10 generate CS signal
ALE ALE
lo/Kif IOIM
8 bit
lID
lID Port B I<::==:>
WR WR
8085
6 bit
Microprooessor
Port C I<::==:>
ResetOul Reset
A"
A"
A"
0wi~~: f~*:bi:~~
,--
as ~~~: 8 memory locations ifd~njt care X are treated
Program
Q.5(a) Draw the complete interfacing diagram of IC 8085 with DAC using 8255.
Write a program to generate staircase wave. (8 Marks) =80H
Ans. :
The complete interfacing of DAC 0808 with microprocessor using 8255
PPI in I/O mapped I/O is shown in Fig. 6.
Fig. 7
Using DAC 0800, the different waves signal can be generated such as
square wave, triangular wave, saw-tooth wave, staircase tic.
MVJ A, 80H
MYIA, 01 H
VEE __ 15V
OUTPort_B ; enable latch
UP, MVr B, FF H
UPl: OUT Port_A ; send data to DAC
Fig. 6 : Interfacing of DAC 0808 with 8085 using 8255 PPI MOV A,S
cpr 00 H ; compare with max cooot
Now, the DO-07 pins ofDAC 0800 are conneGted to port A pins Le. P~-
PA7• The DAC0800 has a current output on lOUT pin' s~ LF 351 should be used to JZUP
conven current into voltage signal as shown in Fig. A. The control word to CALL delay ; add delay
initialize 8255 is given below where Port A is configured as an output in mode O. JMPUPI ; continue same processes
The remaining ports Le. Port B and Port C are not used in interfacing of
Q.6(a) Interface a stepper motor to IC 8085 using 8255 and write a program to
DAC 0800 with 8085 microprocessor.
run motor in clockwise direction. (8 Marks)
Ans. : An ordinary DC motor will turn aroWld and aroWld as long as power is
supplied. No intelligent circuitry is required to drive such a motor, unless you
StlllPcr motor can be interfaced with 8085 cru using 8255 PPI as shown
in Fig. 8. Stepper motor has four windings i.e. A, B, C, D and connected to PAl,
PA", PAl ami p~ ofa Port A 01'8255. The speed of motor CWI be controlled by
adding delay in steps normally wrilh:11 as a subroutine and direction call be
controlled by loading bit pUIICIlI for steps in reverse or forward direction as
shown below:
Bits patlern to rotatc stcppa- motor in half stepping method arc givcn
Table B wherc one bit is changed at a time for 0.90
Table B
A 8CODE!
1010C
000
000
01 0D
01
102
010
09
05
OA011 H
08
06H
04H H
H
H
=80H
easy solution
easy solution
Microcontroller (MSBTE) 1-11 Winter 2008 Microcontroller (MSBTE) 1-12 Winter 2008
Program
A,
CUPl 80
LXI
H
UP
CWR
Port_A
H,
C
Sp, C200
08 H to
HA, HH
FFEO M Rotate
;;.; If
Write
InitiaLize
decrement
InitiaLize
Initialize
Read
increment inPort
toadd
byte;bits clockwise
8255
Byte
byte
counter
memory
stack
pattern
memoryPPI
Acounter
deLay counter
between
0pointer
then
Le. direction
gobyfor
pointer
pointer
step to 1 UPl
steps
code
for lookusing
took up half
tabLestepping method
uptabLe Ans. :
Continuous rotation
T
LXI
T
Fig, 9
Microcontroller (MSBTE) 1-14 Winter 2008
Microcontroller (MSBTE) 1w13 Winter 2008
SFRs are a kind of control table used for running and monitoring
microcontroller's operating.
Each of these registers, even each bit they include, has its name, address in
the scope of RAM and clearly defined purpose ( for example: timer control,
interrupt, serial connection etc.). Even though there are 128 free memory
locations intended for their storage, the basic core, shared by all types of 8051
controllers, has only 21 such registers
List of SfR is given below
I. A - accumulator
2. B-register
3. PSW - Program status word
4. IP - Interrupt priority register
5. P3 - port 3 register
6. IE - Interrupt enable
7. P2 - Port 2 register
8. SeON - Serial control register
9. PI - Port 1 register
10. TeON - Timer control register
II. SBUF - ~erial buffer
12. PO- Port 0 register F;g.10
13. TMOD - Timer mode register
easy solution
easy solution
Microcontroller (MSBTE) 1-15 Winter 2006 Microcontroller (MSBTE) 1-16 Winter 2008
registers. Be;ides, the DPTR Register is usually used for storing data and
The 8051 microcontroller has two separate reading signals -!ill (P3.7) and intermediate results which have nothing to do with memory locations.
PSEN. The first one is aclivatoo byte from external data memory (RAM) should a 0 a a a a a a Value afttt' Re;et
re;ides in external memory (ROM), the microcontroller will activate its control
output ALE and set the first 8 bits of addre;s (AO-A7) on PO. In this way, IC o 0 0 0 0 0 0 0 Value after Reset
ein.'llit 74HCT573 which "lets in" the first 8 bits to memory addre;s pins is DPHI I I I I I I I I Bit name
activated. bit? bit6 bitS bit4 bitJ bit2 bit1 bitO
A signal' on the pin ALE closes the IC circuit 74HCT573 immediately
after 8 higher bits ofaddre;s (A8-AI5) appear on the port. Fig. 11 , DPTR
In this way, a de;ired location in additional program memo;")' is Program Counter PC :
completely addressed. The only thing left over is to read its content. Pins on PO
The 16 bit program counter register is an engine which starts the program
are configured as inputs, the pin PSEN is activated and the microcontroller reads execution and indicates the address in memory from which next instruction is to
content from memory chip. The same connections are used both for data and be fetched. Immediately after its execution, the value of the program counter is
lower addre;s byte. Similar occurs when it is a needed to read some location from incremented by 1. For this automatic increment, the program executes one
external Data Memory. Now, addressing is performed in the same way, while instruction at a time as it is wrinen.
reading or writing is performed via signals which appear on the control outputs Howevcr... the program counter value could be changed at any moment, .
RDorWR. which will cause '~ump" to a new location in the program memory. This is how
subroutines or branch instructions are executed
(4 Marks)
SP Register (Stack Pointer, Address 81h) :
Q. 3(b) Describe the fUl1ction of DPTR. PC and Stack Poil")ter.
Ans. ; This is the stack pointer of the microcontrollcr. This SFR indicate; wha-e
the next value to be taken from the staCkwill be read from in Internal RAM. If
DPTR Registers (DPUDPH (Data Pointer Low/High, Addre;se; 82h183h)
you push a value onto the stack, the value will be written to the addre;s ofSP + 1.
The SFRs DPL and DPH work together to represent a 16-bit value called
That is to say, if SP holds the value 07h, a PUSH instruction will push the
the Data Pointer. The data pointer is used in operations regarding external RAM
value onto the stack at address 08h. This SFR is modified by all instructions
and some instructions involving code memory. Since it is an unsigned two-byte
which modif)' the stack, such as PUSH, POP, LCALL, RET, RETI, and wheneva-
integer value, it can represent values from oooaH to FFFFH (0 through 65,535
decimal). interrupts are provoked by the microcontrolleT.
A value of the Stack Pointer ensures that the Stack Pointer will point to
The;e registers are not true ones because they do not physically exist.
They consist of two separate registers: DPH (Data Pointer High) and (Data valid RAM and permits Stack availability. By starting each subprogram, the
value in the Stack Pointer is incremented by I. In the same manner, by ending
Pointer Low) as shown in Fig. Their 16 bits are used for external memory
addressing. They may be handled as a 16-bit register or as two independent 8-bit subprogram, this value is decremented by I. After any reset, the value 7 is written
to the Stack Pointer, which means that the lopaceof RAM reserved for the Stack
easy solution easy solution
Microcontroller (MSBTE) 1-17 Winter 2008
Microcontroller (MSBTE) 1-18 Winter 2008
starts from this location. Ifanother value is written to this register then the entire
Stack is moved to a new location in the memory.
(il) INC@Rp'
INC instruction increments the indicated byte variable by one. Alx>ve
o 0 0 0 0 I I J Value after Rcset
instruction increment tIle memory location addressed by the register Rp An
original value of OFFH overflows to OOH. No flags are affected. Rp varies £i:om
SP I I Bit name RO to R7 in bank 0 or I in internal RAM.
bit7 bit6 bitS bit4 bit3 bit2 bit! bitO
Q.1{t) List the different addressing modes of microcontroller 8051. (4 Marks)
Q.4(C) Give any two examples to prove the importance of Boolean processor. Ans. :
Chapter 5 : MCS·51 Addressing Modes Indirect addressing is a very powerful addressing mode which in many
and Instructions [Total Marks· 24} cases provides an exceptional level of flexibility. In this addressing mode, vnly
register RO and Rl can be used as a pointer for the indirect operation. The register
Q. 1(e) Describe the function of following instruction of IC 8051. (4 Marks) points the memory location from which required data can be read or write.
(i) MOV A. @Rp (ii) INC@Rp For example
Ans. : MOV A, @RO : load tIle accumulator with the value from Internal
; RAM which is fOWId at the address indicated by RO.
OJ MOY A, @Rp ,
(d) Register addressing:
The byte variable addressed by the Registcr Rp is copied into A register.
The source byte is not affected. No otha-register or flag is affected. Rp varies In this addressing mode, all operand i.e. source and destination are located
from RO to R7 in bank 0 or 1 in internal RAM. in working registers i.e. RO to R7, DPTR, B and A afthe microcontroller. In this
addressing mode, instruction may have one or two operands.
easy soh '!ion easy solution
Microcontroller (MSBTE) 1-19 Winter 2008
Microcontroller (MSBTE) 1-20 Winter 2008
For example
MOV RJ, A : Copy the contents of Accumulator into R3 register memory. The instruction with 'C' in mnemonic is used to access internal and
MOV A, B : Copy the contents of B register to Accumulator external program memories. The mnemonic sj1Ilbol used for this addressing
mode is '@' sign. The length of this instruction always I byte.
Q.4{b) With the help of ANL instruction explain (8 Marks) For example:
(i) Direct addressing mode ANLA,@RO
This instruction will read the data out of Internal RAM whose address is
(ii) Indirect addressing mode
(iii) Register addressing mode stored in RO and perfonns the bitwise logical-AND operation with Accumulator,
store result in Accumulator. Register Addressing
(iv) Immediate addressing mode of 8051 Microcontroller
In this addressing mod<; all operand i.e. source and destination are located
Ans. :
in working registers i.e. RO to R7, DPTR, B and A of the microcontroller. Source
Direct addressing mode: and destination registers are specified by op-code itself. So, the length of
Direct addressing is so-named because the value to be stored in memory is instruction in this addressing mode is One byte. In this addressing mod<;
obtained by directly retrieving it from another memory location. However, the instruction may have one or m'o operands.
address of the opcnmd (data) memory location is specified by instruction itself. In For Example
this mode, the source and destination or both operands can be data memory ANLA, B
location .•
This instruction will performs the bitwise logical-AND operation
Hence the length of instruction is 2 byte for one memory operand and 3 between Accumulator and B register, store result in Accumulator.
byte Jor two memory operands. In this mode, the internal registers accept PC can
be used as dal3. memory location. This type of instruction can not be used to Immediate addressing:
.access external data memory. The address of the data memory is always g bit. Immediate addressing is so-named because the value to be stored in
The SFR's can be used as a data memory location.
memory or register immediately follows the operation code in memory. That is to
For example: say, the instnlction itself dictates what value will be stored in memory. The way,
ANLA,30H we identifY and use the immediate addressing method, is by using # sign.
This instruction will read the data out ofIntemal RAM address 30H and When any number (Operand) is preceded by tlJ.e /I sign then the addressing
performs the bitwise logical-AND operation with Accumulator, store result in of the instruction will be immediate addressing mode. This addressing mode can .
Accumulator. be used when number is to be place in a register or memory location.
Indirect Addressing (Register Indirect) : Hence the len.gth of the instruction is 2 byte for 8 bit operand and 3 byte
Indirect addressing is a very powerful addressing mode which in many for 16 bit operand.
cases provides an exceptional level Of flexibility. In this addressing mode, only For example
register RO and RI can be used a<; a pointer for the indirect operation. ANLA,#OFH
The register points the memory location from which required data can be This instruction will performs the bitwise logical-AND opcration between
read or write. Using this addressing mode, one can access data from internal and Accumulator and immediate data OFH, store rendt in Accumulator.
external data memories.
The instruction without 'X' in mnemonic is used to access internal data Q. 6(b) Write a program to add' two BCD numbers. Draw the flowchart also.
Store result in the data memory location. (8 Marks)
memory. The instruction with 'X' in mnemonic is used to access external data
easy solution
easy solution
MicrocontroUer (MSBTE) 1-21 Winter 2008
Microcontroller (MSBTE) 1-22 Winter 2008
Ans. : Suppose two BCD data bytes are stored in memory locations 3000H and
Program:
300lH. Write a program to add these two bytes from external memory locations
and store result in memory location 3002H of the external memory. MOV ,DPTR, 113000H ; Initialize memory pointer to external memory
Algorithm: MOVX A, @DPTR ; Read fitst number in Accumulator
Step 1 Initialize memory pointer using DPTR register with 3000H MOV RO,A ; Store in RO Register
Step 2 Load first BCD number from the memory location 3000H INC DPTR ; Increment memory pointer by I
Step 3 Increment memory pointer by 1 MOVX A, @DPTR ; Load second number- in Accumulator
Step 4 Load second BCD number from memory location 300lH ADD A,RO ; Add first number with second number
Step 5 Add both BCD numbers and Adjust the result to BCD DAA ; Adjust result to BCD
INC DPTR ; Increment memory pointer by I
Step 6 Store Result in memory location 3002H by incrementillg memory
Pointer MOVX @DPTR, A ; Store result to external memory
Step 7 Stop
LOOP, AJMP LOOP ; Stop
(8 Marks)
Ans. :
DB Directive:
ORG Directive:
ORG 1000h
TABLE.
Program begins at location 100. The table with data will start at location
1024 (1000h).
EQ U Directive:
Algorithm:
Q. 3(a) Explain the operating modes of IC 8051 timer. (4 Marks)
Step 1: Set Port 1 Bits 1,2,3,5. Ails.: Timcr- moderegistu sdxts mQoe of the timers TO and Tl.
Step 2: Res~ Port 1 Bits 0,4,6,7
St'-'t) 3: Stop
chosen value (0- 255) saved in another register. The advantages of this way of
Table B
counting are described in the following example:
TnM1 Split
12TuMO
16·bittimer
310
8-bit Mode
.13-bittimer
auto-reload
Description
timer mode Suppose that for any reason it is continuously needed to count up 55
pulses at a time from the clock generator. When using mode I or mode 0, it is
needed to write number 200 to the timer registers and check constantly alterwards
wbether overflow occurred, i.e. whether the value 255 is reached by counting.
When it has occurred, it is needed to rewrite number 200 and repeat the
whole procedure. The microcontroller performs the same procedure in mode 2
automatically. Namely, in this mode it is only register TLOoperating as a timer
Mode 0 (13 Bit Timer) : (nonllally 8-bit), while the value from which counting should start is saved in the
THOregister.
This is onc of the rarities being kept only for compatibility with thl
previous versions of the microcontrollers. When using this mode, the higher b)tl Mode 3 (Split Timer Mode) :
THOll and only the first 5 bits of the lower byte TLO/I are in use. Beinl By configuring Timer 0 to operate in Mode 3, the 16-bit counter
configured in this way, the Timer 0 uses,only 13 of all 16 bits. With each nC\i,consisting of two registers THO and TLO is split into two independent 8-bit
pulse coming, the state of tile lower register (that one with 5 bits) is changed. timers. In addition, all control bits which belonged to the initial Timcr I
After 32 pulses received it becomes full and automatically is reset, whih (consisting of the registers THI and TLl), now control newly created Timer 1.
the higher byte THon is incremented by 1. This action will be repeated unti This means that even though the initial Timer I still can be configured to
registers count up 8192 pulses. After that, both registers are reset and countin! operate in any mode (mode I, 2 or 3), it is no longer able to stor, simply because
starts from O. there is no bit to do that. Therefore, in this mode, it will uninterruptedly "operate
in the background".
Mode 1 (16 Bit Timer) :
All bits from the registers THOand TLOare used in this mode. That is wh]
for this mc.deis being more commonly used. Counting is performed in the SaID(
way as in mode 0, with difference that the timer counts up to 65 536, i.e. as far al
the use of 16 bits allows
What does auto·reload mean? Simply, it means that such timer uses ani)
one 8-hit register for COWlting,but it never counts from ° but from an arbitrar)easy solution
easy solution
Microcontroller (MSBTE) 1·27 Winter 2008 Microcontroller (MSBTE) 1·28 Winter 2008
Mode 1 of UART :
Q. 3(c) Explain the operating modes of serial port of IC 8051 microcontroller.
(4 Marks) In Mode 1, ten hits are transmitted through TXD or received through RXD
Ans. : Serial port should be configured prior to being used. using SFR SCON in the following rnalUler :
Register as given below. (a) START bit (always 0), 4rst
Yalue after (b) Then 8 data bits (LSB fil'~t! (c) STOP bit (always J) last.
'oset
The START bit is not regis,ered in this pulse train. Its purpose d to start
SCON -I S-M-O-jS-M-l-'-S-M-2-I-REN--j-T-B-g-I-RB-g-I-T-l
-I-R-I-I Bit name
dala receiving ntecb lIlism. On receive the STOP bit is automatically written to
bit7 bit6 bitS bit4 bit3 bit2 bitt bitO
the RE8 bit in Dill SCON register.
Fig. 12: Format of SCON register Mode 2 ofu'\RT:
SMO and 8M! - bit seJects mode given in table A.
In mode 2, II bits are sent through TXD or received through RXD : a
SMO 8-bitUART
9-bitUART
8·bit Shift START bit (always 0), 8 data bits (lSB first), additional 9th data bit and a STOP
01SM!
Mode
302 Baud
1Description
1/32
frequency
JIl2 theRate
Determined
the quartz
quartz
by
Register
(1/64 the quartz the timer I
the timer I
frequency)
frequency bit (always I) last. On trallSlnit, the 9th data bit is actually the TB8 bit from the
UART
SeON rcgister. This bit commonly has the 'purpose of parity bit.
Upon transmission, the 9th data bit is copied to tlle RB8 bit in tlle same
register (SeON). The baud rate is eitllCf"1/32 or l/64 the quartz oscillator
frequency.
Mode 3 of UART :
Mode 3 is tllCsame as Mode 2 except the baud rate. In Mode 3, baud rate
is variable and Callbe selected using Timer I.
In mode 0, the data <U'etransferred through the RXD pin, while clock
PGON ISMOD~ijill1jl'iililliliiilll GFl I GFO I PO 1'0' I BUo,me
bit? bit6 bitS bit 4 bit 3 bit 2 bit 1 bitO
pulses appear on the TXD pin. The baud rat..:is fixed at 1/12 the quartz oscillator
frequency. On transmit/receive, the least significant bit (lSB bit) is being Fig. 13
sent/received first.
easy solution
easy solution
Microcon1ro!ler (MSBTE) 1-29 Winter 2008 1~"'1
Microcontroller (MSBTE) Winter 2008
Q. 5(c) Explain the interrupts used in IC 8051 and how will you implement single oA
step operation in IC 8051. (8 Marks) R~rnterruPt
Ans. : w
z
There are five interrupt sources for the 8051 as shown in Fig. 14 which
~
means that they can recognize 5 ~ifferent events that can interrupt regular
program execution. Each interrupt can be enabled or disabled by setting bits in
the IE register. Also, as seen from the figure A, the whole interrupt system can be Fig. 14
disabled by clearing bit EA f!om the same register. Now, one detail should be Irtwo interrupts of equal priority requests arrive at the same time then the
explained which is not COmi)letelyobvious but refers to external interrupts- INTO interrupt to be serviced is selected according to the following priority list:
I. External interrupt INTO
and INTI.
2. Timer 0 illlerrupt
Namely, if the bits ITO and ITl stored in the TeON register are set, •
program interrupt will occur on changing logic state from I to 0, (only at the 3. Extcmallnterrupt INTI
moment). lfthese bits arc cleared, the same signal will generate interrupt request 4. Timer I interrupt
and it will be cOntinuouslyexecuted as far as the pins are held low. 5. Serial Comnnmicatioll Interrupt
The microcontrollers normally operate at very high speed. The use of 12
Ivfhzquartz crystal enable; 1.000.000 instructions per second to be executed! In
principle, there is no need for higher operating rate.
In case it is needed, it is easy to built~in crystal for high frequency. The
problem comes up when it is necessary to slow dowIl.
l/Opins. Immediately afta' tllat, the instruction RETl is executed and processor continues
executing the main program. Afta' each executed instruction, the intarupt INTO
Interrupt system applied on the 8051 micrOCOlltrollcrs prac~cally stops
is gen~ated and the whole procedure is repcalal ( push button is still pressoo).
operating and rnables instnlctions to be executed one at a time by pushing bunou.
Button Press::: One Instruction.
Two interrupt features enable that:
[J[J[J
Inlerlllpt request is ignored if an interrupt of the same priority level is
being ill progress.
Upon illltrrupt routine has been executed, a IH."W interrupt is not executed
until allC<1S1one instnlction from the main program is executed. In order to apply
this in practice, Ihe following steps should be done:
~~~~~ J == ~::~:
___
;:::~
~:;:
~~::
:~:
::~
~~:~::~;~~::
::~
~:~:
:
Means: go back to the main program.
Fig. 15
Whal is going on? Once the pin P3.2 is sa 10 "0" (for example, by pushing
button), the microcontroller will interrupt program execution jump to the address I
easy solution
easy SOlution
2-1 Summer 2009 MicrocontfolJer (MSBTE) 2-2 Summer 2009
Microcontroller (MSBTE)
A8-A10
READY 8355
I Summer 20091 IO/Fl
ADO-AD?
essmce, and acknowledgment, so do nOI send next byte. IBF is set by STB input
block diagram
as an
ADO-AD? by using
is shown
--
0iiF - (Output
The
OUI to the specified
easy SOlution
OBF
Buffer
output
Full) :
will go '1ow"
pon. This signal
to indicate
is generated
that the CPU has written
by 8255 for the
dala
output
Microcontroller (MSBTE) 2-3 Summer 2009
.. Microcontroller (MSBTE) 2-4 Summer 2009
peripheral to indicate data is available and latched on the port lme. Data IS
- Stepper motor can be interfaced with 8085 CPU using 8255 PPI as shown
guaranteed valid at the rising edge of OBF . The oaF Flip/flop will be set by in fig. 2. Stepper motor has four windings Le. A, B, C, D and connected,to PA],
. thedsing edge of the WR input and reset by ACK input being low. PA2, PAl and P~ ofa Port A of8255.
Q.4(a) Draw the interfacing diagram of stepper motor with microprocessor 8085
using Ie 8255. Write a program for rotating stepper motor by 1800 in
clockwise direction. (8 Marks)
Ans.: Interfacing of Stepper Motor with Microprocessor 8085 :
Now, suppose we want to rotate stqJper motor by 180° in clockwise
direction, then circular position of the rotor can be controlled by applying
appropriate numbers of steps.
The stqJ codes of the stepper motor are normally stored as an array in the
memory, so we can access these four step codes from the array in a turn. So, we
can find out how many turns are required to rota~e stepper motor by desired
angle, can be calculated as given below.
easy solution
",,'''''' ",,,Io,t;,,,,
Microcontroller (MSBTE) 2-7 Summer 2009
~rocontroller (MSBTE) 2·8 Summer 2009
""
ADC
v.
The
inSlnlctions
~ architecture
and data, requiring
use)
dedicated
physically separate memories
buses for each of them as shown in t'1g.
for their
Program 14-blls
Fig. 3 : Interfacing of ADC 0808/0809 with 8085 using 8255 PPI
Program
M~"",
Chapter 3: Introduction to Mlcrocontroller [Total Marks· 4]
Q.1(c) Give the meaning of following terms (4 Marks) The given blt8 widths al9 examplesontyl
arc sel mid cleared by 805 I instructions. The PSW SFR contains ;he carry flag, AC - Auxiliary Carry Flag: It is llsed for BCD oper(ltjons only illld set when
the lluxiliary carry flag, the OVl..Tflow flag, and the parity flag, two register bank there is carry from Ol to D4 durillg ADD or SUB operation, otherwise it is
selcct bits, and user-definable Slatus flag. cleared.
Additionally, the PSW rcgistcr contains the register b,mk select flags
CY - Carry Flag: It is the ninth auxiliary bit used for all arithmetical operations
which arc llSCl:1to select which of tile "R" register banks arc currently selt:ctCl:1.
The Program Status Word (PSW) contains several status bits that reflect the and shift instnlCliolls. This flag is set whenever there is a CarryOllt lrom the D7 bit
currt1lt stllle of tile CPU. The ALU Ilutomatically changes sollie ofrcgistcr's bits, and alTcctoo by ADD and SUB operation. This flag can be set or IdiC! by using
which is uSlwlly IIsed in reguilltioll of the program performing. instructions like SETB C or CLR C.
(J 0 0 0 0 0 0 0 Value after Reset Q. 1{e) Describe two instructions each related with external data memory and
program memory of 8051 Microconlroller. (4 Marks)
PSW i Cy I AC I FO I RS I I RSO I OV I . I P IBU name Ans. :
bit? bit6 bit5 bit4 bit] bit2 bit! hi to 1. MOVX <destination-byte>,<aource-byte>
Fig. 6 Function : Move External
P - Pari!) bit: If a number of binary I's ii' ae<:umulator is even then thiS bit will
Description:
be UlllOJlllllically set (I), otherwise it will be cleared (0). It is Illninly used during The MOVX instructions transfer data bctwC61 the accumulator and a byte
data transmission and ru::civing via serial communicatioll. ofcxlemal data memory, which is why "X" is appendoo to MQV. Th~e are two
ea"y solution easy SOlution
Micfocontroller (MSBTE) 2-11 Summer 2009
Microcontroller (MSBTE) 2-12 Summer 2009
types of instructions, differing in whcther they provide an g-bit or 16-bit indirect --;- MOVe A, @A+ <base-register>
address to 1he extemal data RAM. In the firs1 type, the contents of RO or RI in
the current register bank provide an 8-bit address multiplexed with data on PO.
Function: Move Code byte
Eight bits nre sufficient for external lIO explU1Sioll decoding or for a Desenption :
relatively small RAM array. For somewhm larger arrays, lUly output pon pins can The MOve instnlctions load the uccumulator with a code byte or constant
be used to outl)Ut higher-order address bits. These pins are controlled by an from program memory. The address of the byte fetelled is the sum of the original
output instruction preceding the MOVX. In the second type of MOVX ulIsib>1l00 g-bit ~ccumlliator contents illHllhe contents of a l6-bit base registCl',
instntction, the Dnta Pointer generates a 16·bit address. which may be either the Data Pointt."l' or lhc PC.
P2 outputs the high-ordt:r eight address bits (the contents of OPH), while In the latla' case, lhe PC is incrementoo to the address of the following
PO multiplcxes the low-order eight bits (OPL) with data. The P2 Special FWlction instruction before being added with the Accumulator; otherwise the base register
Register retains its previous contents, while the P2 output buffers emit the is not altcred. Sixtccn·bit addition is performed so a carry-out from the low-order
CQIl1<'-'JltSof DPH. This fonn of MOVX is faster lUld more efficient when eight bits may propagate through higher-order bits. No nags are affected.
accessing very large data arrays (up to 64K bytes), since no lldditional
Example:
instructions are n<."C(ledto set up the output ports.
It is possible to use both MOVX types in some situmions. A large RAM Suppose a valuc bctwew 0 and 3 is in the accumulator. The following
arruy with its high-order address lines driven by P2 elUl be addressed via the Data instructions will trlUlslate the value ill the accumulator to one of four values
Pointer, or with code to output high-order address bits to P2, followed by a dcfinoo by the DB (defille bj1e) directive.
MOVX instruction using RO or RI. REL_PC, INC A
MOVC A,@A+PC
Example: RET
An cxternal 256 byte RAM using multiplexed address/data lines is DB 66H
connected to the 8051 Port O. Port 3 providcs controllincs for the external RAM. DB 77H
Ports I and 2 are usoo for norlllal I/O. Suppose. Registers 0 and I contain DB88H
12H lUld 34H. Location 34H of the cx!<'1Tlal RAM holds the value 56H. The DB 99H
instntetioll sequcnce, If the subroutine is calloo with the accumulator equal to OlH, it returns
MOVXA,@RI with 77H in the accumulator. The fNe A before the MOVe instruction is IlcOOed
MOVX@RO,A to "get aroWld" the RET instruction above the table. If s<.'Vcral b)1es of code
Copies the value 56H into both the Accumulator and external RAM separate the MOVC from the table, the corresponding number is addoo to the
location 12H. accumulator instead.
Q. 2(a) List the 110 ports of 8051 Microcontroller. Explain their alternative signals for EPROM programming ilnd prognml verificutioll.
Table A
functions. (4 Marks) External
Extcmal data
data memory
memoryI read write strobe
strobe
Pin Extcmal
Name
TXD
RXD
Alternate
Timer
Time,.
TO
TJ
Serial internl!'t
EXlemal
0Ioutput Function
cxtcmal
extemal interrupt 0
lineinput
Ans. : The microcontroller hus four VO ports as listed below. SL-,.ial input line
INTO iNTI
WR
RD
I. Port 0 2. Port 1 3. Pan 2 4. Port 3 P3.0
P3.1
P3.2
P3.6
P3.5 P3.3
P3.4
P3.7
The FtL'lction of I/O ports arc describe below:
Port 0 (PO.I/ADu-PO.7/AD,):
Port 0 is an 8-bit open dmin bidirectional I/O port. Altemative]y, Port 0 is
also the multiplexed with low-order address/data bus during accesses to cxtelllal
memory. Port 0 also receives the code bytes during EPROM programming, (Uld
outputs the code bytes during program verification.
Port 1 (PI.O - P1.7) :
Port I is an 8-bit bidirectional I/O l}Ort with inl(.Tnal pull ups. Port I pins
that have Is wrillen to them are pulled high by the intemal pull ups, and in that
state can be used as inputs. As inputs, Port I pins that are cxtt71lally being pulled
low will source current because of the internal pull ups. Port I also receives the
0.2(c) Interface external program memory with 8051 microcontrol1er explain
low-ordcr address bytes during EPROM programming and program verification.
how the data is transferred? (4 MarkS)
Port 2 (P2.0/ArP2.7/A1s): Ans. :
Port 2 is an 8-bit bidirectional I/O port with internal pull ups. Port 2 pins
that have Is written to them are pulled high by the internal pull ups, and in that Interlacing of external memory :
state can be used as inputs. Port 2 emits the high-ord(2" address byte during In case on-chip memory is 1101Cllough. it is possible to add two extcmal
fetches from external Program memory and during accesses to external Data memory ('11 ips with capacityof64Kh each. 110 ports P2 unci P3 (Ire used for their
addressing and data transmission (1'__sh_own_i_n_F_ig_.
7_. _
resides in external memory (ROM), the microcontroller will activate its C()ntrol
output ALE and set the first 8 bits of address (, O-A7) on PO. In this way, Ie
circuit 74HCT573 which "lets in" tlle first 8 bit Iv memory address pins is
activated. A signal on tlle pin ALE closes the Ie circuit 74HCT573 immediately
afla' 8 higher bits ofaddrcss (A8-A 15) appear on the port. In this way. a desiral
location ill additional program memory is C()mpletely addressoo. The ollly thing
left over is to read its content.
Pins on PO are C()nfigured as inputs, the pin PSEN is activated and the
microcontrolk,.. reads C()ntent from memory chip. The same connections are used
both for data and lower address byte. Similar occurs when it is a needed to read
some location from extcmlll Data Memory. Now, addressing is perfonncd in the
same way, while reading or writing is performed via signals which appear on the
itself. The 8051 microcontroller has two separate rc.1ding signals RD (P3.7) and
PSEN. The first one is activated byte from external data memory (RAM) should
be read, while another olle is activaloo to read byte from external program
memory (ROM). These both signals are active at logical zero (0) level.
Microcontroller (MSBTE) 2-17 Summer 20U9
Thro the IDL bit is autolllatically cleared and the program continues
exccuting from instmctioll following that instmction which has set the IDL bil.
They do llot perform any operation but ke<.1J the microcontroller from
undesired changes on the I/O ports. In Idle Mode, the CPU puts itself to sleep
while all the on-chip pcripherals remain active. The mode is invoked by software.
AA~V I'<nllllinn
~ler(MSBTE) 2-19 Summer 2009 Microcontroller (MSBTE) 2-20 Summer 2009
Q.3(d) Give the specifications of any two microcontrollers used for commercial
Q.3(c) Explain internal and external interrupt of 8051 Microcontrol!er. Explain applications. (4 Marks)
the function IP register. (4 Marks) Ans. :
Ans. :
IP I x
bit7
I
X
hit6
0
I PT21
bitS
0
PS
bit4
0
bit3
0
bitl
0
hilO
Value after reset
Bit name
to make them up to 33MHz.
executing
The standard
54k of external
MCS-51 instruction
up to 64k of external
with 64 of them
program, and
PS - Serial Port Interrupt priority bit The Intel 8048 microcontroller, Intel's first I1C, was used in the.Magnavox
Priority 0
OdysseY video game console (as a 100 KHz 8021) and (in its 8042 variant) in
Priority 1 the original IBM PC keyboard. The 8048 is probably the most prominent memoo-
PTl - Timer I interrupt priority of Intel's MCS-48 family of microcontrollers. It was inspired by, and is
Priority 1 The MCS-48 has over 90 instructions with 90% of them being single byte.
The 8048 has a modified Harvard architecture, with internal or extemal program
PXl - External Interrupt INTI priority
ROM and 64---256 bytes of intemal (on-chip) RAM. The I/O is mapped into its
Priority 0
own address space, separate from programs and data.
Priority 1
Priority I
Priority 1
8051
RST
aND
aND
Upon the power is on, electrical cond1.11St'1"is being chargo:l lor several
Fig. 10
milliseconds through resistor connocted to the ground and during this proccss the
pm voltage sup?Iy is on. When the condenser is chargo:l, power supply voltage is
Q. 4(b)(ii) Draw and explain Power ON reset circuit of microcontroller 8051. Give
stable. and the pin keeps being connected to the ground providing normal
the content of PO·P3and SP register on reset. (4 Marks)
operatmg in that way. If later on, during the operation, nl<illual reset button is
easy solution
~
2-24 Summer 2009
2-23 Summer 2009 Microcontroller (MSBTE)
Microcontroller (MSBTE)
pu: hcd, the condenser is being temporarily discharged and the microcontrolla is (III) DJNZ Rn, rndd :
bei 19 reset. Upon the button release, the whole process is repeated ... through the
Function: Decrcml.-'llt and Jump if Not Zero
pre ~ram- step by step.
Description: DJNZ decrl.-'111ents the location indicated by I, and branches to the
Q.• (c) Explain the following instruction of 8051 microcontroller. (8 Marks)
address indicated by the second operand if the resulting value is ~ zero. An
(i) ANl C,Io (ii) SWAPA
original value of OOH undcrflows to OFFH. No flags are affected. The branch
(iii) DJNZ Rn. radd (iv) RETI dtslination is computed by adding the signed relativo-displacement value in the
Ans. : last instruction byte to the PC, afttT incrcmentillg the PC to the first byte of the
following instmctioll. The location decremented may be a register or directly
(i) ,ANL C,/b :
addressed byte.
Function: Logical-AND for bit variables
Example:
Description: If the Boolean vlllue of the source bit is a logical 0, lh(.11 ANL C Internal RAM locations 40H, 50H, .md 60H contain the values OIH, 70H,
clears the carry flag; otherwise, this instruction leaves lhecarry flag in its current and 15H, respectively. The following instruction sequence,
state. A slash (/) preceding the operand in the assembly language indicates that DJNZ 40H,LABEL_1
the logical complement of the addressed bit is used as lhe source value, but the
DINZ 50H,LABEL_2
source hit itselfis not affected. No other flags are affected. Ouly din,;;t addressing
DJNZ 6OH,LABEL_3
is allowed for the source operand.
causes ajuOlp to the instnlction at label LABEL_2 with the values OOH, 6FH, and
Example: SClthecarry (Jag if, and only if, PI.O =: 1, ACC.7 z: I, and OV =: 0: 15H in the three RAM locations. The first jump was not taken because the result
was zero. This instruction provides a simple way to exocute a prot,'Tam loop a
MOVC,no ; LOAD CARRY WITH INPlIT pfN STATE
given number of times or for adding a moderate time delay (from 2' to 512
ANLC,ACC.? ;AND CARRY WITH ACCUM. BIT 7 machine cycles) with a single instmction. The following instruction sequence,
ANLC,IOV ;AND WITH INVERSE OF OVERFLOW FLAG MOV R2,# 8
(ii) SWAP A: TOGGLE: CPL Pl.?
DJNZ R2,TOGGLE
Function: Swap nibbles within the Accumulator
togglQl PI.7 cighttimes, causing four output pulses to appear at bit 7 of output
Description: SWAP A interchanges the low· and high-order nibbles (four-bit Pon I. Each pulse lasts three machine cycles; twO for DJNZ and one to alter the
fields) of the Accumui<:tor (bits 3 through 0 and bits 7 through 4). The operation pin.
can also be thought of as a 4~bil rolate instruction. No flags are affected.
(Iv) RETI:
Example:
Function: Return from interrupt
The Accumulator holds the value OC5H (I 1000101 B). The instruction,
Description: RETI pops the high- and low-order bytes of the PC successively
SWAP A leavc<; the Accumulator holding the value5CH (0101 IIOOB).
from the stack and restores the interrupt logic to accept additional interrupts ai the
easy solution
easy solution
2-26 Summer 2009
Microcontroller (MSBTE) 2-25 Summer 2009 Microcontroller (MSBTE)
same priority level as the one just processed. The Stack Pointer is left -;:;;: Compare two numbers
docreme:nto:! by two. ~o oth.cr rcgistcrs arc affected; th~ PS\\'. is not Step 6: rflllllllbcr > ncxtllumbet', then go to Sll>p 8
automatically restored to Its pre-mt('Tn1pt status. Program executlon contmucs at 7' Replace number with next number which is largest
'.he rcsulting address, which is gcncra!ly thc inSlruction immediately after the Step : Increment memory pointcr to reml next number in the array
point at which the int(.'1T\lpt request was detected. If a lower- or same-Icvel Step 8 .
intClTUpt was pending whCII the RETI instruction is exccuto:l, that onc instruction Step 9: Dccrcl11C11t byte counter by I
is exC(uted before the p<'llding interrupt is processed. Step 10: Ifbyte coWlter 1: 0 then go to step 5
Example: The Stack Pointcr originally contains the value OSH. An intCfnlpt was Step II: Store result
dt.1octoo during the instruction ending at location 0122H. Internal RAM locations Step 12: Stop
OAH and OSH contain the values 2JH and 01 H, respectively. The following .
instruction Program.
e&5v.§Q!utLoo
easy solution
2-28 Summer 2009
Q. 3(8) Explai'1 the function PSEN and ALE pins of microcontro!ler 8051 the array ( in this example number 22).
(4 Marks)
Ans. :
(i1) EQU,
By means of this directive, a numeric value is rcplac<x1 by a symbol.
For example:
PSEN (Program Store Ellable) :
MAXIMUM EQU 99
Program Store Enahle is the Read strobe to External Program Memory.
After this directive, every appearance of the label "MAXIMUM" in the
W11CTI the 8051 is executing C()de from Internal Program Memor)', ~ program, the asscmbler will interprctt: US number 99 (MAXIMUM = 99). It is
is inactive (high). When the device is executing code from Ext<.Tllal Program only once possible to define symbols ill this way so the EQU directive is mostly
used at the beginning of tile program.
Memory, PSEN is activated twice each machine cycle, except that two PSEN
activations are skipped during each access to Extemal Data Memory. (iii) ORG,
This directive is used to define location in program memory wherc the
ALE,
program following directivc is to be placed.
Address Latch Enable output signal for latching the low byte of the For example:
address during accesses 10 cxtt:mal memory. III normal opa-ation ALE is emitted BEGINNING ORG 100
at a C()IlS1anl rate of 1/6 the oscillator frequency, and may be llsed for external
liming or clocking purposes.
ORG lO00h
Chapter 6 : Assembly Language Programming
TABLE ...
[Total Marks ~6]
Q.5(b) Explain the function of following directives. (6 Marks) Program begins at iqcation 100. The table with data win start atlocatiol\
1024 (IOOOh).
(i) DB (ii) EQU (iii) ORG
(Iv) DATA'
(iv) DATA (v) END (vi) CODE By means of this dirt'ctive, an address within internlll RAM is designated
Ans. : as a symbol (address must be inlhe range of 0-255). In oth ••.
,. words, any selected
register may change its name or be assigned a new one.
(I) DB, For example:
This directive is used for writing indicated value to program mcmory. If TEMPI2 DATA 32 : Register at address 32 is named;as "TEMPI2"
scv(:ral values arc indicatcd one after another thcn they are separated byC()nunas. STATUS _R DATA DOb PSW register is assigned the name
If ASCI[ llrray should be indicated it is t11closed with single quotation marks.
;"STATUS_R"
This directive can be uso:l only if the segment CSEG is active.
easy solution
easy solution
MicrocoritroUer (MSBTE) 2-29 Summer 2009
(v) END,
Microcontroller (MSBTE) 2-30 Summer 2009
This directive must be at :.he end of every program. Once it encounters this
directive, the assembler will stop interpreting program into machine code.
Value after
For example: , Reset
TCON I TFI I TRI I TFO I TRO I lEI I IT! I IEO lITO I Bh ""me
END ; End of program bit? bit6 bit5 bit4 bit3 bit2 bit! bitO
(v;) CODE,
TFI (Timer 1 Overflow Flag) : This bit is automatically set with the
By means of this directive, an address in program memory is designated as Timer 1 overflow means when the timer changes from FFFF to 0000. It is cleared
a symbol. Since the maximal capacity of program memory is 64K, the address automatically when interrupt is vectored to location OOIBH. When this bit set, the
must be in the range of 0-65535. 8051 activates timer interrupt. This bit can be set through software.
For example; TRI (Timer Run Bit 1) : This bit turns the Timer 1 on
TABLE CODE 1024: Memory location 1024h called ''TABLE'' 0- Timcr I is turned off
TFO (Timer 0 Overflow Flag) : This bit is automatically set with the
Chapter 7 : MCS-51 Timers/Counters, Interrupts
and Serial Communication [Total Marks -16] Timer 0 overflow means when the timer changes from FFFF to 0000. It is cleared
automatically when interrupt is vectored to location OOOBH. When this bit set, the
Q. 2(b) Draw the format of Timer Control (TCON) register and describe it.
8051 activates timer interrupt. This bit can be set through software.
(4 Marks)
Ans. : TRO (Timer Run Bit 0) : This bit turns tho timcr 0 on
Format of TCON Register (Timer Control, Addresses 88h, Bit- 1 - Timer 0 is turned on
The Timer Control SFR is used to I configure and modify the way in IEO and lEI (External Interrupt Flag) : This bit indicates whether
which the 8051's two timers operate. This SFR controls whether each of the two external interrupt is activated or not. In edge triggered mode, it is set at the falling
timers is running or stopped and contains a flag to indicate that each timer has mode of the external interrupt signal. In level triggered mode, this bit set at the
overflowed. This is also one of the registers whose bits directly control timer low level of the external interrupt signal. Whro. this bit is set, it activate external
operating. Only 4 of all 8 bits this register has are used for timer control, while signal and cleared automatically when vectored to location 0013(IE1) and
remaining four are used for interrupt control. 0003(1£0). It can be set through software.
easy solution