Eiot-Unit 1
Eiot-Unit 1
Introduction
• Microcontrollers are processor chips that generally have memory,
input ports, and output ports within the chip itself.
• Therefore, they can also be called single-chip computers,
computer-on-a-chip, or system-on-a-chip.
• Microcontrollers are used in machine control applications, where
there is no need to change the program.
• Equipments that use microcontrollers include computer printers,
plotters, fax machines, Xerox machines, telephones, automotive
engine control mechanisms, and electronic instruments such as
oscilloscopes, multimeters, planimeters, IC testers, etc.
Introduction
• The major difference between microprocessors and
microcontrollers is that microcontrollers are comparatively faster
because of reduced external memory accessing.
• Intel’s 8031, 8051, and 8096 and Motorola’s 68HC11 are examples
of microcontrollers.
DSP processor - Introduction
• DSP processors are processing chips that have flexibility in
hardware and software, to implement signal-processing
algorithms.
• All the microcontroller chips listed above have the same basic
architecture.
Intel 8051 Architecture
• The main features -8051chips are
o 8 bit CPU,
o 4Kbytes of on chip Program memory,
o 128 bytes of on chip data RAM,
o 4 ports of 8bit each,
o Two 16 bit timers,
o Full duplex serial port and
o On-chip clock oscillator.
• In addition to the above features, the 8051 provide Boolean
processing; six interrupt capabilities and full-fledged CPU for
control applications.
8051 Architecture
8051 Block Diagram
8051 Microcontroller - Introduction
• 8051 is an 8 bit micro-controller i.e. data bus within and outside
the chip is 8 bits wide.
• The address bus of the 8051 is 16 bits wide, so it can address
64Kbytes of memory.
• The lower order address bus is multiplexed with the data bus as in
-8085 processors.
• The port0 and port 2 pins of 8051 forms the multiplexed address
and data bus.
8051 Pin details of 8051 DIP IC
8051 – Pin details
• The 8051 is a 40-pin chip.
• The power supply +VCC and VSS takes two pins and the built-in
clock oscillator requires two pins (–XTAL1 and XTAL2) for
connecting the crystal.
• The four control signals pins of 8051 are PSEN {Program Store
Enable}, ALE, EA(External Access) and RST (Reset).
• The register banks are identified with 2 bits in the processor status word.
• The PSW has two bits for identifying the register bank, i.e., 00 represents bank
0, 01 represents bank 1, 10 represents bank 2, and 11 represents bank 3.
• In the 8051, bitwise operations are also possible with special instructions using
the bit addresses. The bit-addressable memory is both bit-addressable (from
00H to 7FH) and byte-addressable (from 20H to 2FH). Bit operations are helpful
in many control algorithms.
• Using general-purpose scratch pad memory, programmers can read and write
data at any time for any purpose. This memory ranges from the byte address
30H to the address 7FH.
SPECIAL FUNCTION REGISTERS
(SFRs)
• SFR, which occupies upper 128 bytes of internal memory are the
registers, that control the entire processor
• They can e accessed by DIRECT addressing.
• The registers available in the 8051 are as follows :
LIST OF REGISTERS
• Accumulators - A and B
• Process Status Word - PSW
• I/O port registers - P0, P1, P2, P3
• Data pointers - DPH and DPL
• Serial data buffer register - SBUF
• Stack pointer - SP
• Timer registers - TH0, TH1 and TL0, TL1
• Timer Control Registers - TCON, TMOD
• Power and Port control - PCON, SCON
• Interrupt Control Registers - IP, IE.
Special Function Registers
• Programmers should not use the addresses in the range 80H to
FFH (other than SFR) as it is used by INTEL CORPORATION for
expansion functions of 8051.
• The bit addresses of all the bits of the accumulator and B registers
are given as
Bit addresses of Accumulator & B
register bits
Accumul ACC. ACC. ACC. ACC. ACC. ACC. ACC. ACC.
ator bits 7 6 5 4 3 2 1 0
Bit
Address
E7 E6 E5 E4 E3 E2 E1 E0
Upon 0 0 0 0 0 0 0 0
Reset
Bit
Address
F7 F6 F5 F4 F3 F2 F1 F0
Upon 0 0 0 0 0 0 0 0
Reset
POWER CONTROL IN 8051
• The 8051 has various power control modes, which are used to
control the power consumed by the microcontroller chip.
6 - Reserved
5 - Reserved
4 - Reserved
• In this mode, the clock generator -switched off and only the
internal memory is active.
• Only way to revoke the processor from power down mode -reset
the system.
THE STACK OPERATION
• In the 8051, the stack is configured as a series of memory locations
following the Last-In First-Out (LIFO) pattern.
• The stack pointer (SP) is an 8-bit register within the SFR area, with
the address 81H.
THE STACK OPERATION
• This register can hold one 8-bit address at a time, which is actually
the memory location at top of the stack.
• A push operation in the 8051 is used to store an 8-bit data in the
stack.
• The PUSH instruction first increments the value of SP and then
stores the data mentioned in the instruction in the memory
location pointed to by SP.
• Similarly, the POP instruction stores the value from the top of the
stack in the register mentioned in the instruction and then
decrements the value of SP.
THE STACK OPERATION
• The stack pointer is initialized to the value 07H when the 8051
microcontroller is reset.
• The other instructions of the 8051 that affect the stack and the
stack pointer are ACALL, LCALL, RET, and RETI.
• The data pointer register (DPTR) is used to access the data in the
external memory with 16-bit addresses.
Mnemonic Operation
Direct Indirect Register Immediate
MOV A,
A = <src> √ √ √ √
<src>
MOV <dest>,
<dest> = A √ √ √
A
MOV <dest>,
<dest> = <src> √ √ √ √
<src>
INC SP:
PUSH <src> √
MOV “@SP”, <scr>
• Data can only be read from the program memory and not written
into because the program memory is generally ROM.
• The ADDC instruction is also used to add any 8 bit data with
Accumulator along with Carry bit.
Arithmetic Instructions
• The SUBB instruction -subtract contents of a register from the
Accumulator content and during this subtraction, the Carry bit is
also subtracted from the accumulator.
• The lower order byte -result is stored in A register and the higher
order byte - stored in B register.
<byte> =
ORL <byte>,
<byte> OR # √
# data
data
Logical Instructions
XRL A, A = A XOR
√ √ √ √
<byte> <byte>
<byte> =
XRL
<byte> XOR √
<byte>, A
A
XRL <byte> =
<byte>, # <byte> XOR √
data # data
CLR A A = 00H Accumulator only
CLP A A = NOT A Accumulator only
Rotate ACC
RL A Accumulator only
Left 1 bit
Rotate Left
RLC A through Accumulator only
Carry
Rotate ACC
RR A Accumulator only
Right 1 bit
Rotate Right
RRC A through Accumulator only
Carry
Branching Instructions
• 8051 supports unconditional jumping and subroutine calling in
three different ways.
• They are Absolute jump AJMP, ACALL, long jump LJMP, LCALL, and
short jump SJMP.
Un Conditional Branching Instructions
Conditional Branching Instructions
Addressing Modes
Mnemoni
Operation Immediat
c Direct Indirect Register
e
CJNE Jump if A
A,<byte> ≠= √ √
,rel <byte>
CJNE Jump if
<byte>,# <byte> = √ √
data,rel #data
Jump if A
JZ rel Accumulator only
=0
Jump if A
JNZ rel Accumulator only
≠0
Branching Instructions
• The syntax for short jump instruction- SJMP 8-bit address.
• This 8 bit address is a relative address- to the program counter.
• The branching address -by adding the address given in the
instruction with the program counter content.
• The 8-bit address is a 2's complement number i.e., the most
significant bit -sign + or -. The remaining 7 bits - specify the
address. using SJMP -branch to anywhere between 127 bytes after
the program counter content and 128 bytes before it.(From (PC-
128 bytes) to (PC+127 bytes))
Branching Instructions
• For example,
8800: SJMP 06h
• This instruction shift the execution to the location 8808h. The
program counter content after fetching the 2 byte - SJMP
instruction is 8802h. So, 06h added to 8802H results in 8808h.
• The syntax for LJMP -“LJMP 16-bit address”.
• After the execution of this instruction the Program counter -loaded
with the 16 bit address and the execution shifts to that location.
• The syntax for AJMP instruction is “AJMP 11 bit jump address”.
Branching Instructions
• The destination branching address -absolute jumping is calculated -
keeping MSB 5 bits of the Program counter as it is and changing the
LSB 11 bits to that as given -instruction.
• For example,
8800: AJMP 7F0h
• This instruction branch the execution address 8FF0h. After fetching-
program counter content will be 8802h. Keeping the MSB 5 bits of
the PC (10001) as it is, and changing the LSB 11 bits to that given in
the instruction (111 1111 0000) , the branching address becomes
8FF0h.
Branching Instructions
• The micro controller 8051 -single instruction for counter operation
to decrement -result (DJNZ). -very useful in looping using a counter
similar to “for loop” in high level languages.
• The internal data memory map of 8051 has a bit- addressable area
also.
• Figure 10.6 shows the flag bits affected by the various instructions.
ANL C,/bit √
ORL C,bit √
ORL C,/bit √
MOV C,bit √
CJNE √
Hardware Features of 8051
Introduction
• The major benefit of microcontrollers lies in their built-in
parallel ports. The parallel ports can be used to interface all
data converters (ADCs, DACs, etc.) and display devices (LEDs,
LCDs, etc.).
• Any microcontroller-based system needs to transfer data
between the external peripherals and the microcontroller.
The microcontroller needs to read data fed by the user from
the external interface, process it, and give the output to the
peripherals or to the user again. To communicate data with
the external world, the microcontroller needs ports. The ports
may support either parallel or serial data transfer.
Parallel Ports of 8051
• 8051 has 4 I/O ports namely Port0, Port1, Port2 and Port3.
The major constraint -the number of pin count. to reduce -
number of pins of the microcontroller ICs, the pins allotted for
the parallel ports -alternate functions also.
• Out of the available four parallel ports of 8051, Port 1 is used
exclusively for input and output functions alone.
Parallel Ports of 8051
• The other port pins have -distinct function in addition to -
used for input and output functions. So, all the 24 pins of Port
0, port 2, port 3 -two different functions based on the
commands or programs running.
• The output of the port latch is connected to the port pin through a transistor
driver with internal pull up resistor. The port can be operated as an input after
writing 1 to all the bits of port 1 latch.
• 8051 ports are organized such that most instructions read the data from the
pin for read operation and some instructions read the data from the latch.
• So, the input buffer consists of the select logic and the related control signals
– ‘Read Latch’, ‘Read Pin’ for discriminating this.
Internal structure of PORT 1
Structures of Port 0 and Port 2
• Pins of ports 0 and 2 can be used as input port pins if a 1 is
written to the corresponding port latches by the programmer.
• But, as it is reload mode, the TL0 will be loaded with TH0 i.e.,
FDh. The value of TH0 will never be changed. TH0/1 is set to a
known value and TL0/1 is the SFR that is constantly
incremented.
Mode 2 – 8-bit Timer Auto
Reload Mode
• The auto-reload mode is very commonly used for
establishing a baud rate for Serial Communications.
• In mode 3, all the bits that are related to Real Timer 1 will
simply hold its count and will not run and the situation is
similar to keeping TR1=0.
Mode 3 – Split Timer Mode
• In Split Timer mode of Timer 0, the real Timer 1 (i.e. TH1
and TL1) can not be started or stopped since the bits that
do that are now linked to TH0. The real timer 1, in this
case, will be incremented every machine cycle no matter
what.
Write TCON
Start timer
Continue
Timer Control and Operation
• To set the bit TR1 of TCON (D6 bit), any one of the following
two commands can be used -MOV TCON, #40h OR SETB TR1
• If the event being counted occurs more than 500,000 times per
second, it will not be able accurately counted by the 8051.
Interrupt Sources and Interrupt
Vector Addresses
• 8051 basically has following five interrupt sources so
that any of the following events will make 8051 to
execute an interrupt service routine.
• Timer 0 Overflow.
• Timer 1 Overflow.
• Reception/Transmission of Serial Character.
• External hardware interrupt 0.
• External hardware interrupt 1.
Interrupt Sources and Interrupt
Vector Addresses
• Different interrupt sources have to be distinguished
and 8051 must execute different subroutines
depending –interrupt triggered. This is accomplished
by jumping or calling to a fixed address when
interrupt occurs.
• These addresses are called interrupt vector addresses
or interrupt handler addresses.
Interrupt Sources and Interrupt
Vector Addresses
Interrupt Flag Interrupt Vector Address
Bit AF AC AB AA A9 A8
Address
Bit position D7 D6 D5 D4 D3 D2 D1 D0
Bit Address BC BB BA B9 B8
Name EA - - PS PT1 PX1 PT0 PX0
Explanation
Enable
Interrupts
Serial Timer 1 External 1 Timer 0 External 0
- Undef Undefin
Interrupt Interrupt Interrupt Interrupt Interrupt
Made 0 to ined ed
Priority Priority Priority Priority Priority
disable all
interrupts
Interrupt Priorities and Polling
Sequence
When considering interrupt priorities, the following rules apply:
•Nothing can interrupt a high-priority interrupt--not even another
high priority interrupt.
•A high-priority interrupt may interrupt a low-priority interrupt.
•A low-priority interrupt may only occur if no other interrupt is
already executing.
•If two interrupts occur at the same time, the interrupt with
higher priority will execute first. If both interrupts are of the same
priority the interrupt which is serviced first by polling sequence
will be executed first.
Interrupt Priorities and Polling
Sequence
• The complete structure of the 8051 interrupts can be well
understood -figure 11.16. The five interrupt sources are passed
first - IE register, which decides the enabling and disabling of
interrupts. The global interrupt enable -shown -figure.
1
INT1
IT1 IE1
Interrupt
0 polling
sequence
TF1
RI
TI
Interrupt
enables
Global
Enable Interrupt
enabled
Timing of Interrupts
• 8051 micro-controller samples the hardware signal level on its pins once in
every machine cycle. A machine cycle is the time taken by the controller to
access one memory location or I/O device.
• As 8051 takes 12 clock cycles to complete one machine cycle, the interrupt
signal applied at the pins of 8051 must be available for at least 12 clock
periods.
• External interrupts are applied at the pins INT0 and INT1. The sensing of
voltage level applied to this pin can also programmed in 8051. The interrupts
can be either level triggered or edge triggered as set by the IT0 and IT1 bits of
the SFR TCON
• A ‘0’ on these bit positions will make both the hardware interrupts to be level
triggered. Level triggered means a low level voltage on the interrupt pins will
activate the interrupts.
Bit Patterns for the TCON SFR
Bit position D7 D6 D5 D4 D3 D2 D1 D0
Bit Address 8F 8E 8D 8C 8B 8A 89 88
Name TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Interrupt 0
Interrupt 1 type
Timer 0
Timer 1 Extern type control. control. Set
run
run al Set to 1 by External to 1 by
Timer control
Timer 1 control Interru software for Interrupt software
0 bit. Set
Explanation Overflow bit. Set to pt 1 edge 0 edge for edge
Overflo to 1 by
flag 1 by edge triggering detect triggering
w flag softwar
software detect and cleared bit and
e to
to run. bit for level cleared for
run.
triggering level
triggering
Bit Address 8F 8E 8D 8C 8B 8A 89 88
Interrupt Priorities and Polling
Sequence
• A ‘1’ on the IT0 and IT1 bits of the SFR TCON will program the
hardware interrupts as edge triggered. Edge triggering means
the change of voltage from high state to low state will activate
the interrupt.
• When an interrupt is triggered, the micro controller takes the
following actions automatically:
• The current Program Counter is saved on the stack, low-byte
first.
• Interrupts of the same and lower priority are blocked.
• In the case of Timer and External interrupts, the corresponding
interrupt flag is cleared.
Interrupt Priorities and Polling
Sequence
• Program execution transfers to the corresponding interrupt
handler vector address.
• The Interrupt Handler Routine executes.
• An interrupt ends when your program executes the RETI
(Return from Interrupt) instruction. When the RETI instruction
is executed the micro controller takes the following actions:
• Two bytes are popped off the stack into the Program Counter
to restore normal program execution.
• Interrupt status is restored to its pre-interrupt status.
Interrupt Priorities and Polling Sequence
• The internal architecture of 8051 is such that the external
hardware interrupts will be cleared automatically when the
interrupt service routine is executed only if is programmed to be
edge or transition triggered.
• However, if the first byte still hasn’t been read by the time
reception of the second byte is complete, one of the bytes will
be lost. The serial port receive and transmit registers are both
accessed at Special Function Register SBUF.
8051 Serial Ports
• Data to be transmitted are written to the register SBUF and
the data received by the serial port are read from the register
SBUF. Physically reading and writing SBUF actually accesses
two separate registers.
• The SM0 and SM1 bits can select any one of the four
operating modes described in the next section.
Definition of Bits SM0 and SM1 in
SCON SFR
Bit Pattern - Description
• The next bit, SM2, is a flag used for enabling "Multiprocessor
communication" in modes 2 and 3. If SM2 is set to 1 in modes 2
and 3, the “Receive Interrupt” RI flag will not be activated if the
received 9th data bit is 0. If SM2 is set in Mode1, then “Receive
Interrupt” RI flag will not be activated if valid stop bit is not
received.
• The reception is started by enabling REN in SCON register. Once the data
reception is complete, the RI flag is set.
• The baud rate in Mode 0 is fixed at one twelfth of the clock frequency. Baud
rate= (Clock frequency/12)
Signal Transmission / Reception
pattern
Reception / Transmission
300 A0h
1200 D0h
2400 FAh
9600 FDh
Baud Rate
Following set of instructions will set the timer for the baud
rate of 9600.
– MOV TMOD, #00100000B ;timer/counter 1 set for mode 2, 8-
bit TIMER
– ;operation
– MOV TH1, #0FDh ; timer/counter 1 is
timed for 9600 baud
– SETB TR1 ; timer/counter 1 is
enabled for free run
•For initializing the serial port for mode 3 operation, the
following instruction can be used.
– MOV SCON, #11010000B
Transmitting and Receiving Data
Using Serial Port
• Once the Serial Port has been properly configured as explained
above, the serial port is ready to be used to send data and
receive data.
• To write a byte to the serial port one must simply write the
value to be transmitted to the SBUF (99h) SFR. For example, to
send the letter "A" to the serial port, the following instruction
can be written.
MOV SBUF, #’A’
Transmitting and Receiving Data
Using Serial Port
• Upon execution of the above instruction the 8051 will begin
transmitting the character via the serial port. Once the transmission is
complete, the serial port transmit interrupt flag TI is set. Since the 8051
does not have a serial output buffer, a character can not be written to
SBUF before the previous written character is completely transmitted.
This can be accomplished by checking the TI flag.
• Reading data received by the serial port is equally easy. To read a byte
from the serial port one just needs to read the value stored in the SBUF
(99h) SFR after the 8051 has automatically set the RI flag in SCON.
8051 Interface Examples
Interfacing 8255 to 8051
• When the 8051 is connected to external memory, port 0 (P0) is used for the
lower-order address and data bus and port 2 (P2) is used for the higher-
order address bus.
• Since the port 3 pins have an alternative function, the net result is that only
P1 is left for input and output operation.
• One way to expand the number of I/O ports is to connect the 8255
programmable peripheral interface with the 8051.
• The interfacing of the 8255 with the 8051 is done assuming the 8255 as a
memory location, because the 8051 supports only memory-mapped I/O.
• For accessing the external memory in the 8051, the MOVX instruction is
used.
• The lower-order address bus and the data bus are multiplexed and are
available in the port 0 pins.
• This is de-multiplexed using a latch and the ALE signal.
Learning Outcomes
• The circuit connections are shown in Figure are such that the port is not
damaged and also the port is not sourcing over current. This ensures safe
operation of the ports and switches.
• An LED gives out light illuminating when it is forward biased and has enough
current flowing through it. LED indicators are easy to interface with the
microcontrollers as output device.
• LEDs can be driven with a minimum current of about 10mA. A driver IC can be
used for illuminating LEDs.
Interfacing LED with driver to port pins
• Figure shows a method of using an inverter driver for connecting ports to LEDs.
Description
– Start: MOV A, P3
– MOV P1, A
• In common anode display, the anode of all segment LEDs are tied
together and taken out.
• So, to illuminate a segment, the common anode is connected to
the supply and then the segment input i.e. a to f is connected to a
low level logic 0.
• In common cathode display, the cathodes of all the LEDs are
connected together and taken out.
• So, to illuminate a segment, the corresponding segment input is
connected to the high level voltage or logic 1 and the common
cathode is connected to the ground. This will forward bias the LEDs
and illuminate them.
Seven Segment Display
Interfacing Description
• Interfacing of Seven segment displays to 8051 microcontroller needs a driver
as shown in Figure. Here 74240 TTL inverter driver IC is used for driving the
seven segment displays.
• Also the seven segment display is assumed to be common anode type. The
common anode is connected to the supply +5V.
• The interfacing diagram for displaying the BCD code obtained from key
connected to port3 and displaying the same in seven segment display
connected to port 1
Interfacing Switches and seven segment
display to 8051
Interfacing Description
• The segments can be illuminated
if the segments pins are
connected to the low level of
supply or to ground.
• This is done by connecting the
segment inputs of logic 0 through
74240. The 74240 IC will invert
the inputs applied to it. So, if we
want to illuminate a particular
segment, the corresponding
output for that segment has to be
logic 1.
• The data output for displaying
decimal digits 0 to 9