Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 7 Microcontrollers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Embedded Systems Purushotam Shrestha

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.

The 8051 family microcontroller was developed by intel.

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.

Timers and counters:


There are two timers in 8051 micrcontroller: T0
and T1. Each is composed of two registers, high
and low, and can load a 16-bit number. They can
be used for controlling timed operations. The
timers can operate in 4 modes Mode0 to 3. The
operation mode is controlled by the TMOD
register. The timers are enabled or disabled by
TCON 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

The 8051 microcontroller also supports various addressing schemes:


Immediate:
The data or the operand is contained by the opcode format and placed immediately after the mnemonic and is
preceded by # sign.
MOV A, #87H

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

MOVX is used for external memory.

Programming in Assembly Language


The assembly language is a low level programming language. Mnemonics are used to write program. The
assembly language programming is more difficult than high level programming but results in compact and
efficient programs.
The basic rules for writing a seembly language program are
Every program line may consist of a maximum of 255 characters;
Every program line to be compiled, must start with a symbol, label, mnemonics or directive;
Text following the mark ; in a program line represents a comment ignored (not compiled) by the
assembler; and
All the elements of one program line (labels, instructions etc.) must be separated by at least one space
character. For the sake of better clearness, a push button TAB on a keyboard is commonly used instead of
it, so that it is easy to delimit columns with labels, directives etc. in a program.

Each line is constructed as


[Symbol/Label:] mnemonic [operands] [;comment]
Assembler controls are used to control where the Cross Assembler gets its input source file, where it puts the
object file, and how it formats the listing file.
$MOD51 Uses 8051 predefined symbols

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

The assembly language can use following numbers


Binary, like 10010110B
Decimal, like 34D
Hexadecimal. Like A9H
Also the language supports the use of operators
Arithmetic operators, +, -, X, /
Logical Operators, AND, NOT
Relational operators, >=, <>

Once the program is written it goes through a number


of stages and the final is the hex file containing the
actual 0s and 1s, which is burned into the ROM of the
microcontroller.
There are assemblers which does all of the steps in a
single conversion.
Compiler/assembler is case insensitive.

A simple Interfacing with 7 segment Display


The 7-segment display consists of segments which
actually are LEDs, arranged in the number 8 format. The
numbers 0 to 9 and a few alphabets, all the hexadecimal
numbers, can be displayed by turning on the LEDs in
specific format. The LEDs may be common anode or
common cathode. For our example we consider a common cathode one.

Chapter 7 5
Embedded Systems Purushotam Shrestha

There are 8 lines for the display, a to h, as shown in the figure.


To display 1, b and c should be provided with high and all
others should be low. Similarly, all the numbers can be
displayed by specific combination of the input values as in the
table.
Lets say we connect the segments electrodes to port 1 such
that P1.1 is connected to a, P1.2 is connected to b and so on.
In order to display the number 5 in the display we need to
write B6h to the port P1. The instructions is

MOV P1, #B6h.

Whatever is to be displayed, the corresponding hexadecimal value is to be written to the port.

Number Binary equivalent Display segments of common cathode 7 segment Hexadecimal


to be of the number display equivalent of the
displayed a b c d e f g h 7-segment
(dot)
0 0000 1 1 1 1 1 1 0 0 FC
1 0001 0 1 1 0 0 0 0 0 60
2 0010 1 1 0 1 1 0 1 0 DA
3 0011 1 1 1 1 0 0 1 0 F2
4 0100 0 1 1 0 0 1 1 0 66
5 0101 1 0 1 1 0 1 1 0 B6
6 0110 1 0 1 1 1 1 1 0 BE
7 0111 1 1 1 0 0 0 0 0 E0
8 1000 1 1 1 1 1 1 1 0 FE
9 1001 1 1 1 1 0 1 1 0 F6
Letter A 1010 1 1 1 0 1 1 0 0 EC
The value to be displayed may come from different operations. They may come from processing an input
provided by a sensor like temperature sensor. The actual number to be displayed must be converted to the 7-
segment format. A subroutine may be written and called whenever a number is to be displayed or the process
can be implemented in a combinational circuit outside the microcontroller. The combinational circuit will take
the binary equivalent of the number to be displayed and convert to the 7-segment format. The latter will be a
faster process. The instruction would be

MOV P1, #0101b 7-segment


8051
For the number 5. Note that only 4 pins are required. format
number

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

You might also like