Unit 1
Unit 1
Unit 1
8051 ARCHITECTURE
Microprocessors
Microprocessor
Microcontroller
• CPU, RAM, ROM, I/O and timer are all on a single chip
• fixed amount of on-chip ROM, RAM, I/O ports hence used for applications in
which cost, power and space are critical
• single-purpose Embedded System
• Embedded system means the processor is embedded into that application.
• An embedded product uses a microprocessor or microcontroller to do one task
only.
• In an embedded system, there is only one application software that is typically
burned into ROM.
Example:printer, keyboard, video game player
• speed, the amount of ROM and RAM, the number of I/O ports and timers, size,
packaging, power consumption
• easy to upgrade
• cost per unit
• availability of software development tools
assemblers, debuggers, C compilers, emulator, simulator, technical
support
• wide availability and reliable sources of the microcontrollers.
(3). Port 0 is also the multiplexed low-order address and data bus during accesses to
external program and data memory.
(2). Port 2 emits the high-order address byte during fetches from external program
memory and during accesses to external data memory that use 16-bit addresses
(MOVX @DPTR).
Timer / Counter:
Many microcontroller applications require the counting of external events or
the generation of time delay between two actions. Timing and counting tasks can be
implemented by software. But software loops keep the microcontroller to be occupied
and other tasks can not be done. To avoid this, hardware timers/counters are available
in 8051.
8051 has two 16 bit timer/counter registers namely Timer 0 and Timer 1. They
can be independently configured to operate as timer or counter.
When used as a timer, the register is incremented after every machine cycle. Since
one machine cycle consists of 12 oscillator periods, the count rate is 1/12 of the
oscillator frequency.
When used as a counter, the register is incremented in response to a high to low
transition of the corresponding external input pin T0 and T1.
Timer / Counter mode control register (TMOD):
Timer0 and Timer1 have four operating modes. The Timer or Counter
operation and the mode of operation can be configured using TMOD register in the
special function registers.
TMOD is dedicated to the two timers and can be considered to be two
duplicate 4 bit registers.
The timers may operate in any of four modes that are determined by the mode
bits M1 and M0 in the TMOD register.
MODE 0:
In this mode, the timer register is configured as a 13 bit register ( TL 5 bits and
TH 8 bits ). The 5 bits in the TL sets the divide by 32 prescale to the TH as an 8 bit
counter. The timer is enabled when TR=1 and G=0 or INT =1. Setting G=1 allows the
timer to be controlled by external input INT.
Timer 0 in mode 0 (13-bit timer)
This mode is for compatibility with the previous versions of microcontrollers.
This mode configures timer 0 as a 13-bit timer which consists of all 8 bits of TH0 and
the lower 5 bits of TL0. As a result, the Timer 0 uses only 13 of 16 bits. How does it
operate? Each coming pulse causes the lower register bits to change their states. After
receiving 32 pulses, this register is loaded and automatically cleared, while the higher
byte (TH0) is incremented by 1. This process is repeated until registers count up 8192
pulses. After that, both registers are cleared and counting starts from 0.
Timer 0 in mode 1 (16-bit timer)
Mode 1 configures timer 0 as a 16-bit timer comprising all the bits of both
registers TH0 and TL0. Timer operates in the same way as in mode 0, with difference
at the registers count up to 65 536 as allowable by the 16 bits.
Framed data
Framed data including a parity bit
RS-232 Serial Communications
The EIA RS-232 serial communication standard is a universal standard,
originally used to connect teletype terminals to modem devices. Figure shows a PC
connected to a device such as a modem or a serial printer using the RS-232
connection. In a modern PC the RS-232 interface is referred to as a COM port. The
COM port uses a 9-pin D-type connector to attach to the RS-232 cable. The RS-232
standard defines a 25-pin D-type connector but IBM reduced this connector to a 9-pin
device so as to reduce cost and size. Figure shows a simple simplex serial
communication link where data is being transmitted serially from left to right. A
single Tx (transmit) wire is used for transmission and the return (Gnd) wire is
required to complete the electrical circuit. Figure shows the inclusion of another
physical wire to support full-duplex (or half-duplex) serial communication. The
RS-232 (COM port) standard includes additional signal wires for “hand-shake”
purposes, but the fundamental serial communication can be achieved with just two or
three wires as shown. The serial data is transmitted at a predefined rate, referred to as
the baud rate. The term baud rate refers to the number of state changes per second
which is the same as the bit rate for this particular communication scheme. Typical
baud rates are: 9600 bps; 19,200 bps; 56kbps etc.
SBUF Register
SBUF is an SFR register which can be written to, so as to hold the next data
byte to be transmitted. Also it can be read from to get the latest data byte received by
the serial port. SBUF is thus effectively two registers: one for transmitting and one
for receiving.
SCON Register
The SCON (Serial Control) register is an SFR register, used for configuring
and monitoring the serial port status.
• SM0, SM1 bits define the mode of operation, such as the number of data bits
(8 or9), the clock source etc
• SM2 is set to 0 for normal operation
• REN is set to 1 to enable reception, 0 to disable reception
• TB8 is the ninth bit (parity bit) to be transmitted
• RB8 is the ninth bit received (parity bit)
• TI Transmit Interrupt flag. A logic 1 indicates that transmit buffer (SBUF) is
empty.
• This flag must be cleared by software.
• RI Receive Interrupt flag. A logic 1 indicates that data has been received in the
receive buffer (SBUF). This flag must be cleared by software.
Setting the BAUD rate
Timer/Counter 1 (in SCON mode 3) provides the serial port baud rate clock.
Usually the 8-bit auto reload operation (Timer/Counter mode 2) is used. The table
shows some values defined for the TH1 register to achieve some of the more common
baud rates.The values shown assume a processor clock rate of 11.059MHz. This is a
common crystal value for 8051 based designs as it divides down to provide accurate
baud rates.
Note. The most significant bit of the PCON register is assumed to be at 0. If this were
set to 1 the baud rate value would be doubled.
8051 Microcontroller Interrupts
There are five interrupt sources for the 8051, which means that they can
recognize 5 different events that can interrupt regular program execution. Each
interrupt can be enabled or disabled by setting bits of the IE register. Likewise, the
whole interrupt system can be disabled by clearing the EA bit of the same register.
Refer to figure below.
Now, it is necessary to explain a few details referring to external interrupts-
INT0 and INT1. If the IT0 and IT1 bits of the TCON register are set, an interrupt will
be generated on high to low transition, i.e. on the falling pulse edge (only in that
moment). If these bits are cleared, an interrupt will be continuously executed as far as
the pins are held low.
EA - global interrupt enable/disable:
0 - disables all interrupt requests.
1 - enables all individual interrupt requests.
ES - enables or disables serial interrupt:
0 - UART system cannot generate an interrupt.
1 - UART system enables an interrupt.
ET1 - bit enables or disables Timer 1 interrupt:
0 - Timer 1 cannot generate an interrupt.
1 - Timer 1 enables an interrupt.
EX1 - bit enables or disables external 1 interrupt:
0 - change of the pin INT0 logic state cannot generate an interrupt.
1 - enables an external interrupt on the pin INT0 state change.
ET0 - bit enables or disables timer 0 interrupt:
0 - Timer 0 cannot generate an interrupt.
1 - enables timer 0 interrupt.
EX0 - bit enables or disables external 0 interrupt:
0 - change of the INT1 pin logic state cannot generate an interrupt.
1 - enables an external interrupt on the pin INT1 state change.
Interrupt Priorities
It is not possible to foreseen when an interrupt request will arrive. If several
interrupts are enabled, it may happen that while one of them is in progress, another
one is requested. In order that the microcontroller knows whether to continue
operation or meet a new interrupt request, there is a priority list instructing it what to
do.
The priority list offers 3 levels of interrupt priority:
1. Reset! The absolute master. When a reset request arrives, everything is stopped
and the microcontroller restarts.
2. Interrupt priority 1 can be disabled by Reset only.
3. Interrupt priority 0 can be disabled by both Reset and interrupt priority 1.
4. The IP Register (Interrupt Priority Register) specifies which one of existing
interrupt sources have higher and which one has lower priority. Interrupt priority is
usually specified at the beginning of the program. According to that, there are several
possibilities:
If an interrupt of higher priority arrives while an interrupt is in progress, it will
be immediately stopped and the higher priority interrupt will be executed first.
If two interrupt requests, at different priority levels, arrive at the same time then the
higher priority interrupt is serviced first.
If the both interrupt requests, at the same priority level, occur one after another,
the one which came later has to wait until routine being in progress ends.
If two interrupt requests of equal priority arrive at the same time then the interrupt to
be serviced is selected according to the following priority list:
1. External interrupt INT0
2. Timer 0 interrupt
3. External Interrupt INT1
4. Timer 1 interrupt
5. Serial Communication Interrupt
The IP register bits specify the priority level of each interrupt (high or low priority).
Where first part describes WHAT should be done, while other explains HOW
to do it. The latter part can be a data (binary number) or the address at which the data
is stored. Depending upon the number of bytes required to represent 1 instruction
completely.
Instructions are divided into 3 types;
If operand is not given in the instruction or there is no digits present with instruction,
the instructions can be completely represented in one byte opcode.
OPCODE 8 bit
Two/double byte instruction:
If 8 bit number is given as operand in the instruction, the such instructions can
be completed represented in two bytes.
Addressing modes are used to know where the operand located is.
• Register addressing.
• Direct addressing.
• Immediate addressing.
• Index addressing.
For example;
ADD A, R5 (This instruction will add the contents of register R5 with the
accumulator contents).
Direct Addressing Mode
In direct addressing mode; the address of memory location containing data to
be read is specified in instruction. In this case; address of the data is given with the
instruction itself.
For example;
MOV A, 25H (This instruction will read/move the data from internal RAM address
25H and store it in the accumulator.
MOV A, #25H (This instruction will move the data 25H to accumulator.
MOVC A, @ A + DPTR ( This instruction moves the data from the memory to
accumulator; whose address is computed by adding the contents of accumulator and
DPTR)
8051 Instruction Sets
Types Of Instructions
• Data transfer instructions.
• Arithmetic instructions.
• Logical instructions.
• Branch instructions.
Data Transfer Instructions
These instructions move the content of one register to another one. Data can be
transferred to stack with the help of PUSH and POP instructions.
MNEMONIC DESCRIPTION BYTES
CLR A (A) 0 1
CPL A (A) (/A) 1
SWAP A (A3-0) (A7-4) 1
RL A (An + 1) (An) 1
RLC (An + 1) (An) 1
RR A (An) (An + 1) 1
RRC A (An) (An + 1) 1
CLR C (C=0) 1
SETB C (C=1) 1
SETB bit Set directly 2
addressed bit
CPL C (1 = 0, 0 = 1) 1
ANL C, bit Logical AND operation 2 between Carry bit and directly
addressed bit.
ANL C,/bit Logical AND operation 2 between Carry bit and inverted
directly addressed bit.