8051 At89s52
8051 At89s52
8051 At89s52
MICROCONTROLLER
Microcontrollers, as the name suggests, are small controllers. They
are like single chip computers that are often embedded into other
systems to function as processing/controlling unit. For example, the
remote control you are using probably has microcontrollers inside that
do decoding and other controlling functions. They are also used in
automobiles, washing machines, microwave ovens, toys ... etc, where
automation is needed.
EASY TO USE
MICROCONTROLLER (AT89C51)
8051 microcontroller has 128 bytes of RAM, 4K bytes of on-chip
ROM, two timers, one serial port, and four ports (each 8-bits wide) all
on a single chip. The 8051 is an 8-bit processor i.e. the CPU can
work on only 8 bits of data at a time. The fixed amount of on-chip
ROM, RAM, and number of I/O ports in microcontroller makes them
ideal for many applications in which cost and space are critical.
The AT89C51 is a low-power, high-performance CMOS 8-bit
microcomputer with 4K bytes of Flash programmable and erasable
read only memory (PEROM). The on-chip Flash allows the program
memory to be reprogrammed in-system or by a conventional
nonvolatile memory programmer. By combining a versatile 8-bit CPU
with Flash on a monolithic chip, the Atmel AT89C51 is a powerful
microcomputer, which provides a highly flexible and cost-effective
solution to many embedded control applications.
FEATURES:
Compatible with MCS-51 Products
4K Bytes of In-System Reprogrammable Flash Memory
Endurance: 1,000 Write/Erase Cycles
Fully Static Operation: 0 Hz to 24 MHz
Three-level Program Memory Lock
128 x 8-bit Internal RAM
32 Programmable I/O Lines
Two 16-bit Timer/Counters
BLOCK DIAGRAM:
External
Interrupt
s
On-chip
ROM for
program
code
Timer 0
On-chip
RAM
Timer 1
CPU
Osc
Bus
control
4 I/O Ports
P0 P1 P2 P3
ADDRESS/DATA
Serial
port
TXD
RXD
Counter Inputs
Interrupt
control
ETC.
PIN CONFIGURATION:
P1.0
40
Vcc
P1.1
39
P0.0 (AD0)
P1.2
38
P0.1 (AD1)
P1.3
37
P0.2 (AD2)
P1.4
36
P0.3 (AD3)
P1.5
35
P0.4 (AD4)
P1.6
34
P0.5 (AD5)
P1.7
33
P0.6 (AD6)
RST
32
P0.7 (AD7)
(RXD) P3.0
10
31
EA/VPP
(TXD) P3.1
11
30
ALE/PROG
(INT0) P3.2
12
29
PSEN
(INT1) P3.3
13
28
P2.7 (A15)
(T0) P3.4
14
27
P2.6 (A14)
(T1) P3.5
15
26
P2.5 (A13)
(WR) P3.6
16
25
P2.4 (A12)
(RD) P3.7
17
24
P2.3 (A11)
18
23
19
22
20
21
XTAL2
XTAL1
GND
PIN DESCRIPTION:
P2.2 (A10)
P2.1 (A9)
P2.0 (A8)
PIN DESCRIPSION:
VCC - Supply voltage.
GND - Ground.
Port 0 - Port 0 is an 8-bit open-drain bi-directional I/O port. As an
output port, each pin can sink eight TTL inputs. When 1s are written
to port 0 pins, the pins can be used as high-impedance inputs.
Port 0 may also be configured to be the multiplexed low-order
address/data bus during accesses to external program and data
memory. In this mode P0 has internal pull-ups.
Port 0 also receives the code bytes during Flash programming, and
outputs the code bytes during program verification. External pull-ups
are required during program verification.
Port 1 - Port 1 is an 8-bit bi-directional I/O port with internal pull-ups.
The Port 1 output buffers can sink/source four TTL inputs. When 1s
are written to Port 1 pins they are pulled high by the internal pull-ups
and can be used as inputs. As inputs, Port 1 pins that are externally
being pulled low will source current (IIL) because of the internal pullups. Port 1 also receives the low-order address bytes during Flash
programming and verification.
Port 2 - Port 2 is an 8-bit bi-directional I/O port with internal pull-ups.
The Port 2 output buffers can sink/source four TTL inputs.
When 1s are written to Port 2 pins they are pulled high by the internal
pull-ups and can be used as inputs. As inputs, Port 2 pins that are
externally being pulled low will source current (IIL) because of the
internal pull-ups. 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). In
this application, it uses strong internal pull-ups when emitting 1s.
During accesses to external data memory that use 8-bit addresses
(MOVX @ RI), Port 2 emits the contents of the P2 Special Function
Register.
Port 2 also receives the high-order address bits and some control
signals during Flash programming and verification.
Port 3 - Port 3 is an 8-bit bi-directional I/O port with internal pullups.
The Port 3 output buffers can sink/source four TTL inputs. When 1s
are written to Port 3 pins they are pulled high by the internal pullups
and can be used as inputs. As inputs, Port 3 pins that are externally
being pulled low will source current (IIL) because of the pullups. Port
3 also serves the functions of various special features of the
AT89C51 as listed below:
RST - Reset input. A high on this pin for two machine cycles while the
oscillator is running resets the device.
ALE/PROG - Address Latch Enable output pulse for latching the low
byte of the address during accesses to external memory. This pin is
also the program pulse input (PROG) during Flash programming.
PORT PIN
P3.0
P3.1
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
ALTERNATE FUNCTIONS
RXD (serial input port)
TXD (serial output port)
INT0 (external interrupt 0)
INT1 (external interrupt 1)
T0 (timer 0 external input)
T1 (timer 1 external input)
WR (external data memory write strobe)
RD (external data memory read strobe)
OSCILLATOR CHARACTERISTICS:
XTAL1 and XTAL2 are the input and output, respectively, of an
inverting amplifier, which can be configured for use as an on-chip
oscillator. Either a quartz crystal or ceramic resonator may be used.
To drive the device from an external clock source, XTAL2 should be
left unconnected while XTAL1 is driven.
Figure 1. Oscillator Connections
XTAL1
C1
XTAL2
C2
GND
D6
D5
D4
D3
D2
D1
D0
PROGRAM COUNTER:
The program counter points to the address of the next instruction to
be executed. As the CPU fetches the opcode from the program ROM,
the program counter is incremented to point to the next instruction.
The PC is 16 bits wide i.e. it can access program addresses 0000 to
FFFFH, a total of 64K bytes of code.
PSW (PROGRAM STATUS WORD) REGISTER
The PSW contains status bits that reflect the current state of the CPU
and is also called flag register. The PSW contains the Carry bit, the
Auxiliary Carry bit, the two register bank select bits, the overflow flag
bit, a parity bit, and two user definable status flags.
CY
CY
AC
--RS1
RS0
OV
---
AC
PSW.7
PSW.6
PSW.5
PSW.4
PSW.3
PSW.2
PSW.1
F0
RS1 RS0 OV
---
Carry flag.
Auxiliary carry flag.
Available to the user for general purpose.
Register Bank selector bit 1.
Register Bank selector bit 0.
Overflow flag.
User definable bit.
P
RS1
0
0
1
1
PSW.0
Parity flag.
There are 128 bytes of RAM in the 8051, which are assigned
addresses 00 to 7FH. These 128 bytes are divided into three different
groups:
1. A total of 32 bytes from locations 00 to 1H hex are set aside for
register banks and the stack.
2. A total of 16 bytes from locations 20H to 2FH are set aside for
bit-addressable read/write memory.
3. A total of 80 bytes from locations 30H to 7FH are used for read
and write storage, or what is normally called a scratch pad.
These
80 locations of RAM are widely used for the purpose of storing
data and parameters by 8051 programmers.
7F
Scratch pad RAM
30
2F
Bit-Addressable RAM
20
1F
18
Register Bank 3
17
10
Register Bank 2
0F
08
07
00
REGISTER BANKS IN THE 8051
The 32 bytes of RAM which is set aside for the register banks and
stack is divided into 4 banks of registers in which each bank has 8
registers, R0 R7. RAM locations from 0 to 7 are set aside for bank 0
of R0 R7
where R0 is RAM location 0, R1 is RAM location 1, R2 is location 2,
and so on, until memory location 7 which belongs to R7 of bank 0.
The second bank of registers R0 R7 starts at RAM location 08 and
goes to location 0FH. The third bank of R0 R7 starts at memory
location 10H and goes to location 17H; and finally RAM locations 18H
to 1FH are set aside for the fourth bank of R0 R7. The following
tables shows how the 32 bytes are allocated into 4 banks:
Bank 0
R7
R6
R5
R4
R3
R2
R1
R0
7
6
5
4
3
2
1
0
Bank 1
Bank 2
R7
R6
R5
R4
R3
R2
R1
R0
R7
R6
R5
R4
R3
R2
R1
R0
7
6
5
4
3
2
1
0
7
6
5
4
3
2
1
0
Bank 3
R7
R6
R5
R4
R3
R2
R1
R0
7
6
5
4
3
2
1
0
wide i.e. it can take values of 00 to FFH. When the 8051 is powered
up, the SP
register contains value 07 which implies that RAM location 08 is the
first location being used for the stack by the 8051. The storing of a
CPU register in the stack is called a PUSH, and loading the contents
of the stack back into a CPU register is called a POP. In other words,
a register is pushed onto the stack to save it and popped off the stack
to retrieve it.
PUSHING ONTO THE STACK:
In the 8051 the stack pointer (SP) is pointing to the last used location
of the stack. As data is pushed onto the stack, the stack pointer (SP)
is incremented by one and the contents of the register are saved on
the stack. To push the registers onto the stack, RAM addresses are
used.
POPPING FROM THE STACK:
Popping the contents of the stack back into a given register is the
opposite process of pushing. With every pop, the top byte of the stack
is copied to the register specified by the instruction and the stack
pointer is decremented once.
ADDRESSING MODES:
The addressing modes in the microcontroller instruction set are as
follows:
1. DIRECT ADDRESSING
In direct addressing, the operand is specified by an 8-bit address field
in the instruction. Only internal RAM and SFRs cab be directly
accessed.
2. INDIRECT ADDRESSING
6. INDEXED ADDRESSING
Program memory can only be accessed via indexed addressing. This
addressing mode is intended for reading look-up labels in program
memory. A 16-bit base register (either DPTR or the Program Counter)
points to the base of the table, and the accumulator is set up with the
table entry number. The address of the table entry in program
memory is formed by adding the accumulator data to the base
pointer.
8051 INSTRUCTION SET
MNEMONIC:
The MNEMONIC column contains the 8051 Instruction Set Mnemonic
and a brief description of the instruction's operation.
OPERATION:
The OPERATION column describes the 8051 Instruction Set in
unambiguous symbology. Following are the definitions of the
symbols used in this column.
<n:m>
+
/
X
~
^
v
v
>
<>
=
->
DPTR
PC
PM (addr)
Remainder
Soper
instruction.
SP
STACK
HEX OPCODE:
This column gives the machine language hexadecimal opcode for
each 8051 instruction.
BYTE:
This column gives the number of bytes in each 8051 instruction.
CYC:
This column gives the number of cycles of each 8051 instruction.
The time value of a cycle is defined as 12 divided by the oscillator
frequency. For example, if running an 8051 family component at 12
MHz, each cycle takes 1 microsecond.