Chapter 7 Microcontrollers
Chapter 7 Microcontrollers
Chapter 7 Microcontrollers
Chapter 7 : Microcontrollers
A microcontroller is different from the microprocessor. The microprocessor is a purely processing unit without
any supporting units on board while a microcontroller has a microprocessor inside it and contains all the
required supporting units like memory, I/O ports. It is computer on the chip, a small one.
Architecture
The diagram below shows the very general architecture of a 8051 microcontroller.
The CPU:
The CPU performs all the instructions of a 8051 microcontroller. Being a 8-bit system, the CPU is a 8 bit
processor.
Memory
The memory of 8051 microcontroller is organized into 2 types:
Program memory
Data memory
The address bus is 16 bit wide.
The program memory stores the program burned into the microcontroller. It is 4 Kb and is ROM.
The data memory is for storing data during computational steps. It includes
4 register banks, each containing 8 8-bit registers; R0 to R7.
general purpose registers,
bit addressable locations,
128 locations for Special Function Registers and
other general purpose locations
I/O ports
The 8051 microcontroller has 4 8-bit I/O ports: P0, P1, P2 and P3. All the ports are bit addressable which allows a
programmer to set or reset by the Bit instructions (SETB for high, CLR for low) independently.
Chapter 7 1
Embedded Systems Purushotam Shrestha
port P0:
It can be I/O port or lower address byte when the external memory chip is used. When configured as I/O, it
needs external pull up resistors.
Port P1:
P1 is a true I/O port, because it doesn't have any alternative functions as is the case with P0, but can be
cofigured as general I/O only. It has a pull-up resistor built-in and is completely compatible with TTL circuits.
Port P2:
It can be I/O port or higher address byte when the external memory chip is used.
Port P3:
The pins of the port 3 can be used as general I/O, or for an alternative function. In order to use these
alternative functions, a logic one (1) must be applied to appropriate bit of the P3 register.
Interrupts:
The 8051 microcontroller can handle 5 interrupts
ES serial communications interrupt:
ET1 - Timer 1 interrupt:
EX1 - external 1 interrupt:
ET0 - timer 0 interrupt:
EX0 - external 0 interrupt:
The interrupts can be enabled or disabled by
writing appropriate bits in Interrupt enable
register
The priorities are set in interrupt priority register.
The interrupts are handled by referring to the
appropriate interrupt vectors.
Serial Ports:
The microcontroller supports UART, Universal
Asynchronous Receiver and Transmitter. The
data to be transmitted is written in Serial Buffer
Register SBUF and received data is read from the
same register. The serial communication process
is controlled by the SCON register. The
communication may be in one of modes : Mod 0 to 3.
The Oscillator:
The oscillator provides the clock signal required for the synchronization of the digital circuitry. An external
crystal oscillator is used at pins 18 and 19 for generation of the clock pulses.
System Bus:
The system bus connects all the blocks of the 8051 microcontroller and is controlled by bus controller.
2 Chapter 7
Embedded Systems Purushotam Shrestha
The SFRs
The SFRs are special function registers. A block of memory is allocated for theses registers. Most of the control
registers described above are SFRs like TMOD, SCON etc. The operations of various functionalities of the
microcontroller are controlled by the values written in these registers. The SFRs are
DPTR(DPH + DPL) data pointer
P0, P1, P2, P3 port registers
PCON power control register
TCON timer control register
TMOD timer mode control register
T0(T0H+T0L) timer 0
T1(T1H+T1L) timer 1
SCON serial communication control register
SBUF serial communication buffer register
IE interrupt enable register
IP interrupt priority register
PSW program status word
ACC - Accumulaator
B the B register
SP stack pointer
The SFRs are accessed by their address values.
Instruction Set:
The CPU of 8051 microcontroller executes the following groups of instruction:
Arithmetic Instructions: perform arithmetic operations such as addition, subtraction, division, multiplication etc.
After execution, the result is stored in the first operand.
Eg: ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.
SUBB A,Rn
INC A
MUL AB
Branch Instructions: these cause the program execution to jump to a new location out of the sequence. They
may be
Unconditional jump instructions: upon their execution a jump to a new location from where the program
continues execution is executed.
ACALL addr11: Absolute subroutine call
Conditional jump instructions: a jump to a new program location is executed only if a specified condition is met.
Otherwise, the program normally proceeds with the next instruction.
CJNE A,direct,rel: Compares direct byte to the accumulator and jumps if not equal. Short jump.
Data Transfer Instructions: move the content of one register to another. The register the content of which is
moved remains unchanged.
MOV A,Rn: Moves the content of register to the accumulator
MOVX @DPTR,A: Moves the content of accumulator to the external RAM (16-bit address)
PUSH direct: Pushes the direct byte onto the stack
Logic Instructions: perform logic operations upon corresponding bits of two registers. After execution, the result
is stored in the first operand.
ANL A,Rn: AND register to accumulator
Bit-oriented Instructions: The bit oriented instructions perform logical or arithmetic or movement operations but
on single bits.
MOV bit,C: Moves the carry flag to the direct bit
Chapter 7 3
Embedded Systems Purushotam Shrestha
Register (direct):
Data is contained by the registers.
MOV A, R0
MOV R0, R1 is invalid
Register Indirect:
Registers R0 and R1 hold the address of the data. @ sign is used before the name of the register to indicate it
holds the address value.
MOV A, @R0
Direct Addressing:
Data is held by the address value immediately after the instruction.
MOV A,33h; Means: move a number from address 33 hex. to accumulator
Stack:
Uses POP and PUSH operations. The address from where the data is to be retrieved or to which the data is to be
stored is pointed by the stack pointer which always points to the top of the stack.
POP R0; pop the data in the top of the stack and store it in R0.
Indexed:
Data is contained by sum of 2 values, one of which is normally a register.
MOV A, @A+DPTR; move the contents of the address pointed by the value which is sum of contents of A and
DPTR
4 Chapter 7
Embedded Systems Purushotam Shrestha
Symbols are alphanumeric representations of numeric constants, addresses. The legal character set for symbols
is the set of letters, both upper and lower case (A..Z,a..z), the set of decimal numbers (0..9) and the special
characters, question mark (?) and underscore (_). All symbols must start with either a letter or special character
(? Or _).
Labels
Labels are special case of symbols and are used only before statements that have physical addresses associated
with them so as to represent address locations.
Assembler directives are used to define symbols, reserve memory space, store values in
program memory and switch between different memory spaces.
EQU Define symbol
ORG Set segment location counter
END End of assembly language source file
CSEG Select program memory space
DSEG Select internal memory data space
XSEG Select external memory data space
Directives are written in the column reserved for instructions. There is a rule allowing only one directive per
program line.
Mnemonics
ACALL Absolute call ADD Add ADDC Add with carry
AJMP Absolute jump ANL Logical and CJNE Compare & jump if not equal
CLR Clear CPL Complement DA Decimal adjust
DEC Decrement DIV Divide DJNZ Decrement & jump if not zero
INC Increment JB Jump if bit set JBC Jump & clear bit if bit set
JC Jump if carry set JMP Jump JNB Jump if bit not set
JNC Jump if carry not set JNZ Jump if accum. not zero JZ Jump if accumulator zero
Chapter 7 5
Embedded Systems Purushotam Shrestha
P1
6 Chapter 7
Embedded Systems Purushotam Shrestha
7-segment
actual format
8051 number in number
binary Number to 7-
segment format
conversion
combinational
logic
Similarly, more than one display unit can be interfaced. For 2 or 3, the available ports may be used, but if the
number exceeds the ports of microcontroller, the displays can be cascaded connecting them in parallel. The
particular display can be selected using other ports connected to a decoder at the instant the display is provided
with the value. The process is cycled through all the displays. The important point is the cycle frequency must be
higher than 60Hz in order to remove flicker.
2X4 Decoder
8051 P0.1
P0.2
Number to 7-
segment format
conversion
P2
combinational
logic
Chapter 7 7