You are on page 1of 18

A BRIEF INTRODUCTION TO

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.

KEY FEATURES OF MICROCONTROLLERS:

HIGH INTEGRATION OF FUNCTIONALITY

Microcontrollers sometimes are called single-chip computers because


they have on-chip memory and I/O circuitry and other circuitries that
enable them to function as small standalone computers without other
supporting circuitry.

FIELD PROGRAMMABILITY, FLEXIBILITY

Microcontrollers often use EEPROM or EPROM as their storage


device to allow field programmability so they are flexible to use. Once
the program is tested to be correct then large quantities of
microcontrollers can be programmed to be used in embedded
systems.

EASY TO USE

Assembly language is often used in microcontrollers and since they


usually follow RISC architecture, the instruction set is small. The
development package of microcontrollers often includes an
assembler, a simulator, a programmer to "burn" the chip and a
demonstration board. Some packages include a high-level language
compiler such as a C compiler and more sophisticated libraries.

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

Six Interrupt Sources


Programmable Serial Channel
Low-power Idle and Power-down Modes

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)

In normal operation ALE is emitted at a constant rate of 1/6 the


oscillator frequency, and may be used for external timing or clocking
purposes. Note, however, that one ALE pulse is skipped during each
access to external Data Memory.

If desired, ALE operation can be disabled by setting bit 0 of SFR


location 8EH. With the bit set, ALE is active only during a MOVX or
MOVC instruction. Otherwise, the pin is weakly pulled high. Setting
the ALE-disable bit has no effect if the microcontroller is in external
execution mode.
PSEN - Program Store Enable is the read strobe to external program
memory. When the AT89C51 is executing code from external
program memory, PSEN is activated twice each machine cycle,
except that two PSEN activations are skipped during each access to
external data memory.
EA/VPP - External Access Enable. EA must be strapped to GND in
order to enable the device to fetch code from external program
memory locations starting at 0000H up to FFFFH. Note, however, that
if lock bit 1 is programmed, EA will be internally latched on reset.
EA should be strapped to VCC for internal program executions.
This pin also receives the 12-volt programming enable voltage (VPP)
during Flash programming, for parts that require 12-volt VPP.
XTAL1 - Input to the inverting oscillator amplifier and input to the
internal clock operating circuit.
XTAL2 - Output from the inverting oscillator amplifier.

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

Note: C1, C2 = 30 pF +/- 10 pF for Crystals


= 40 pF +/- 10 pF for Ceramic Resonators
There are no requirements on the duty cycle of the external clock
signal, since the input to the internal clocking circuitry is through a
divide-by-two flip-flop, but minimum and maximum voltage high and
low time specifications must be observed.

THE 8051 REGISTERS:


The most widely used registers of the 8051 are A (accumulator), B,
R0, R1, R2, R3, R4, R5, R6, R7, DPTR (data pointer), and PC
(program counter). All of the above registers are 8-bits, except DPTR
and the program counter. The 8 bots of a register are shown below
from the MSB (most significant bit) D7 to the LSB (least significant
bit) D0.
D7

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.

RS0 Register Bank Address


0
0
00H 07H
1
1
08H 0FH
0
2
10H 17H
1
3
18H 1FH

CY, THE CARRY FLAG


This flag is set whenever there is a carry out from the D7 bit. This flag
bit is affected after an 8-bit addition or subtraction. It can also be set
to
1 or 0 directly by an instruction such as SETB C and CLR C where
SETB C stands for set bit carry and CLR C for clear carry.

AC, THE AUXILIARY CARRY FLAG


If there is a carry from D3 to D4 during an ADD or SUB operation, this
bit is set; otherwise, it is cleared. This flag is used by instructions that
perform BCD (binary coded decimal) arithmetic.
P, THE PARITY FLAG
The parity flag reflects the number of 1s in the A (accumulator)
register only. If the A register contains an odd number of 1s, then
P=1. Therefore, P=0 if A has an even number of 1s.
OV, THE OVERFLOW FLAG
This flag is set whenever the result of a signed number operation is
too large, causing the high-order bit to overflow into the sign bit.
RAM MEMORY SPACE ALLOCATION IN THE 8051

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

Register Bank 1 (stack)


Register Bank 0

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

STACK IN THE 8051


The stack is a section of RAM used by the CPU to store information
temporarily. This information could be data or an address. The CPU
needs this storage area since there are only a limited number of
registers. The register used to access the stack is called the SP
(stack pointer) register. The stack pointer in the 8051 is only 8 bits

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

In indirect addressing, the instruction specifies a register that


specifies a register that contains the address of the operand. Both
internal and external RAM can be indirectly accessed.
The address register for 8-bit addresses can be either the stack
pointer or R0 or R1 of the selected register bank. The address
register for 16-bit addresses can be only the 16-bit data pointer
register, DPTR.
3. REGISTER INSTRUCTIONS
The register banks, which contain registers R0 through R7, can be
accessed by instructions whose opcodes carry a 3-bit register
specification. Instructions that access the registers this way make
efficient use of code, since this mode eliminates an address byte.
When the instruction is executed, one of the eight registers in the
selected bank is accessed. One of four banks is selected at execution
time by the two bank select bits in the PSW.
4. REGISTER-SPECIFIC INSTRUCTIONS
Some instructions are specific to a certain register. For example,
some instructions always operate on the Accumulator, so no address
byte is
needed to point to it. In these cases, the opcode itself points to the
correct register.
5. IMMEDIATE CONSTANTS
The value of a constant can follow the opcode in program memory.
For example,
MOV A, #100
Loads the Accumulator with the decimal number 100. The same
number could be specified in hex digits as 64H.

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
>
<>
=
->

Bits of a register inclusive. For example, PC<10:0>


means bits 0 through 10 inclusive of the PC. Bit 0 is
always the
least significant bit.
Binary addition
Binary 2s complement subtraction
Unsigned integer division
Unsigned integer multiplication
Binary complement (1s complement)
Logical And
Inclusive Or
Exclusive Or
Greater than
Not equal to
Equals
Is written into. For example, A + SOper -> A means the
result of the binary addition between A and the Source

Operand is written into A.


A
AC
CF
Doper

The 8-bit Accumulator Register.


The Auxiliary Carry Flag in the Program Status Word
The Carry Flag in the Program Status Word
The Destination Operand used in the instruction.

DPTR

16-bit Data Pointer

Interrupt Active Flag


until the Flag is cleared.

Internal Flag that holds off interrupts

Jump Relative to PC A Jump that can range between -128 bytes


and +127 bytes from the PC value of the next instruction.
Paddr

A 16-bit Program Memory address

PC

The 8051 Program Counter. This 16-bit


register points to the byte in the Program
Memory space that is fetched as part of the
instruction stream.

PM (addr)

Byte in Program Memory space pointed to by


addr.

Remainder

Integer remainder of unsigned integer division

Soper
instruction.

The Source Operand used in the

SP

8-bit Stack Pointer

STACK

The Last In First Out data structure that is


controlled by the 8-bit Stack Pointer (SP).
Sixteen bit quantities are pushed on the stack
low byte first.

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.

You might also like