Ece541 Microprocessor and Embedded System Design
Ece541 Microprocessor and Embedded System Design
Addressing in 8051
16-bit Address Bus: The 8051 uses a 16-bit address bus, which allows it to address up to 64 KB
of memory (2^16 = 65,536 bytes).
Banked Memory: The 8051 employs banked memory for accessing various memory spaces. It
divides the memory into multiple banks, each accessible via different addressing modes.
Memory Map
The 128 bytes of RAM inside the 8051 are assigned the address 00 to 7FH. They can be accessed
directly as memory locations and are divided into three different groups as follows:
32 bytes from 00H to 1FH locations are set aside for register banks and the stack.
16 bytes from 20H to 2FH locations are set aside for bit-addressable read/write memory.
80 bytes from 30H to 7FH locations are used for read and write storage; it is called as
scratch pad. These 80 locations RAM are widely used for the purpose of storing data and
parameters by 8051 programmers.
Register Banks in 8051
A total of 32 bytes of RAM are set aside for the register banks and the stack. These 32 bytes are
divided into four register banks 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 the 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 locations OFH. The
third bank of R0–R7 starts at memory location 10H and goes to location to 17H. Finally, RAM
locations 18H to 1FH are set aside for the fourth bank of R0–R7.
EXERCISE
i. If RAM locations 00–1F are set aside for the four registers banks, which register bank of
R0–R7 do we have access to when the 8051 is powered up? The answer is register bank 0; that
is, RAM locations from 0 to 7 are accessed with the names R0 to R7 when programming the
8051. Because it is much easier to refer these RAM locations by names such as R0 to R7, rather
than by their memory locations.
How to Switch Register Banks
Register bank 0 is the default when the 8051 is powered up. We can switch to the other banks
using PSW register. D4 and D3 bits of the PSW are used to select the desired register bank, since
they can be accessed by the bit addressable instructions SETB and CLR. For example, "SETB
PSW.3" will set PSW.3 = 1 and select the bank register 1.
RS1 RS2 Bank Selected
0 0 Bank0
0 1 Bank1
1 0 Bank2
1 1 Bank3
These 32 bytes are divided into 4 banks of registers in which each bank has 8 registers, R0-R7
RAM location 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 RAM location 2, and so on, until memory location 7 which belongs to R7
of bank 0
It is much easier to refer to these RAM locations with names such as R0, R1, and so on, than by
their memory locations
Register bank 0 is the default when 8051 is powered up.
We can switch to other banks by using the PSW register.
Bits D4 and D3 of the PSW (PROGRAM STATUS WORD) are used to select the desired
register bank.
Use the bit-addressable instructions SETB and CLR to access PSW.4 and PSW.3
Some of the examples illustrating the use of banks are shown below:
MOV A , #16H ; this instruction means move data ( 16H ) to register A, location of 16H is not
defined here.
MOV DPTR #2000H
In any addressing mode, the processor fetches instruction , decodes the instruction and executes
it. However in this type of addressing mode , processors immediately can fetch data ( operand ),
so this is known as immediate addressing mode.
Here the MOV datatype is used which is used in internal RAM memory operations only. For
other memories ( ROM, external RAM other data types are used )
As shown above, In indirect addressing of 8051 , mode we can set the value of i and increment it
and then add total code in a loop. So indirect addressing mode is used to move multiple bytes of
data.
Problem:
Move the series of data from the address of a location ( 20H is the address ) to the location 50H .
Solution:
MOV R0 , #20H ; assign first byte address of 20H location as R0
MOV R1 , #50H ; assign first byte address of 50H location as R1
MOV A , @R0 ; R0 contains location of data, that data( first byte of 20H) is moved to A
MOV @R1 , A ; first byte of 20H is moved to R1
Here R1 has values of 50H
INC R0 ; increment R0
INC R1 ; increment R1
Indirect addressing mode in external RAM:
MOVX A , @DPTR ; for external RAM, MOVX is used.
DPTR is a 16-bit address register, but the data in it is 8-bit only( data in DPTR is address of
another data location ) .
Index addressing mode:
This addressing mode of 8051 microcontroller is the sum of two registers. ROM doesn’t only
consist only of program memory, it also has bit data memory in some cases.
MOVC A , @A+DPTR ;
MOVC @A+DPTR , A ; this instruction is not allowed , because ROM is only readable memory,
we cannot write in it.
For instance, Code for 8-segment displays never changes, that code is fixed and we are not
interested in changing the code, so we store this data in ROM.
In this way there are fixed codes for every number in the 7 segment display.
Then these codes ( here 8 codes ) are stored in a table known as look up table .
Look up table:
Address Code
xxxx Code for number 0
xxxx + 1 Code for number 1
…
xxxx + 8 Code for number 9
xxxx is the base address that could be anything, and to access other codes we have to add another
address to it.
Address of a particular code = base address + index of the code
This is the reason this type of addressing is known as index addressing mode.
Arithmetic Instructions:
ADD A, reg: Add the contents of a register to the accumulator.
ADD A, #data: Add immediate data to the accumulator.
INC reg: Increment the content of a register.
INC A: Increment the accumulator.
Logical Instructions:
ANL A, reg: Perform a bitwise AND between the accumulator and a register.
ANL A, #data: Perform a bitwise AND between the accumulator and immediate data.
ORL A, reg: Perform a bitwise OR between the accumulator and a register.
Loop Instructions:
DJNZ reg, label: Decrement and jump if not zero.
Subroutine Instructions:
CALL subroutine: Call a subroutine.
RET: Return from a subroutine.
Bit-Level Instructions:
SETB bit: Set a specified bit in the RAM.
CLR bit: Clear a specified bit in the RAM.
Stack Instructions:
PUSH reg: Push the content of a register onto the stack.
POP reg: Pop the top of the stack into a register.
PROGRAM EXAMPLE:
ORG 0x8000 ; Set the origin of the program
START:
MOV A, #25 ; Move immediate data 25 to accumulator A
MOV R0, A ; Move the content of A to register R0
ADD A, R0 ; Add the content of R0 to A
JZ LOOP ; Jump to LOOP if A is zero
JMP END ; Jump to END
LOOP:
INC R0 ; Increment the content of R0
NOP ; No operation (placeholder)
END:
NOP ; No operation at the end
In this program example, ORG sets the origin of the program, START is a label, and instructions
like MOV, ADD, JZ, and JMP perform specific operations. The program uses registers (A and
R0) and includes labels for branching.
ASSEMBLER DIRECTIVE
Assembler directives help in organizing the assembly code, defining constants, allocating
memory space, and indicating code and data segments. The exact set of directives and their
syntax may vary slightly depending on the assembler being used. Always refer to the
documentation of the specific assembler for accurate and detailed information. In the context of
the 8051 microcontroller, assembler directives are specific commands that provide instructions to
the assembler, helping in the organization and layout of the program. Here are some common
8051 assembler directives:
ORG (Origin): Syntax: ORG address
Description: Specifies the origin (starting) address of the program in memory.
Example: ORG 0x8000
END: Syntax: END
Description: Marks the end of the assembly code.
Example: END
EQU (Equate): Syntax: label EQU value
Description: Defines a constant value for a label.
Example: COUNT EQU 10
DB (Define Byte): Syntax: label DB value1, value2, ...
Description: Reserves memory space and initializes it with byte values.
Example: DATA DB 25, 30, 12
DW (Define Word): Syntax: label DW value1, value2, ...
Description: Reserves memory space and initializes it with 16-bit (word) values.
Example: TABLE DW 1000, 2000, 3000
DS (Define Space): Syntax: label DS size
Description: Reserves a block of memory without initializing it.
Example: BUFFER DS 20
SEGMENT: Syntax: SEGMENT data/code
Description: Specifies whether the following code or data is stored in the code or data segment.
Example: SEGMENT CODE
CODE: Syntax: CODE
Description: Indicates the start of the code segment.
Example: CODE
DATA: Syntax: DATA
Description: Indicates the start of the data segment.
Example: DATA
IDATA (Internal Data): Syntax: IDATA
Description: Declares the following variables to be stored in the internal RAM (data memory).
Example: IDATA
XDATA (External Data): Syntax: XDATA
Description: Declares the following variables to be stored in the external RAM (extended data
memory).
Example: XDATA
Operation of Assemblers and Linkers in 8051
The assembly and linking process for the 8051 microcontroller involves several steps. Such as:
Assembly:The assembler is a program that translates assembly language code into machine code
(binary) that the 8051 microcontrollers can execute. It converts human-readable mnemonics and
symbols into the corresponding machine instructions.
Assembly File: This is the source code written in assembly language with a file extension like
.asm.
Syntax Checking: The assembler performs syntax checking to ensure that the assembly code
follows the correct syntax for the 8051 architectures.
Symbol Resolution: It resolves symbols and assigns memory addresses to variables and labels.
Example using an imaginary assembly file, example.asm:
assembly
Copy code
ORG 0x8000
MOV A, #25
MOV R0, A
ADD A, R0
JZ LOOP
JMP END
LOOP:
INC R0
NOP
END:
NOP
Command to assemble the code using an assembler (if this is called assembler8051):
bash
Copy code
assembler8051 example.asm -o example.obj
This produces an object file (example.obj) containing the machine code.
Linker: The linker combines multiple object files, resolves addresses, and creates the final
executable file. It links together different modules, handles references between them, and ensures
that the program is correctly mapped to the memory.
Linker Script: The linker uses a script to specify the memory layout and segment placement for
the final executable.
Generating Hex File: The linker often produces a hex file that can be directly programmed into
the 8051 microcontroller's memory.
Example using an imaginary linker (let's call it linker8051):
bash
Copy code
linker8051 example.obj -o example.hex
This command links the object file (example.obj) and produces the final hex file (example.hex).
Programming the Microcontroller: The resulting hex file (example.hex) can be loaded into the
8051 microcontroller's memory using a programmer tool or hardware.
Sample Programs
Example 1: Adding Two Numbers
assembly
Copy code
ORG 0x8000 ; Start address
MAIN:
MOV A, #25 ; Load value 25 into accumulator A
MOV B, #30 ; Load value 30 into register B
ADD A, B ; Add B to A, result in A
; Result (A) is now 55
; Add your code here to use or display the result
JMP $ ; Infinite loop
END
Example2: Simple Loop
assembly
Copy code
ORG 0x8000 ; Start address
MAIN:
MOV R0, #0 ; Initialize register R0 with 0
LOOP:
INC R0 ; Increment R0
; Add your code here to use or display the value in R0
CJNE R0, #10, LOOP ; Compare R0 with 10, jump to LOOP if not equal
JMP $ ; Infinite loop
This program demonstrates a simple loop that increments a register (R0) and repeats until the
value reaches 10.
Interfacing to Keypad: A keypad is an input device that typically consists of a set of keys
arranged in a matrix. The keys are connected to the microcontroller through rows and
columns.
Uses:
Input device for human-machine interaction.
Commonly used in security systems, digital locks, etc.
Input/Output Port Expansion: Sometimes, the on-chip I/O ports of the microcontroller
may not be sufficient for a complex system. Port expander ICs can be used to increase
the number of I/O pins.
Uses:
Expanding the number of available I/O pins.
Connecting multiple devices to the microcontroller.
The Motorola M6811 is a microcontroller that belongs to the Motorola 6800 series of
microprocessors and microcontrollers. The M6811 is an 8-bit microcontroller that was widely
used in embedded systems and control applications. It descended from the Motorola 6800
microprocessor by way of the 6801. The 68HC11 devices are more powerful and more expensive
than the 68HC08 microcontrollers and are used in automotive applications, barcode readers,
hotel card key writers, amateur robotics, and various other embedded systems. The
MC68HC11A8 was the first microcontroller to include CMOS EEPROM
Here are some key features and characteristics of the Motorola M6811:
The I/O ports may also include pins with special functions, such as those dedicated to serial
communication (TXD, RXD) or clock input (XTAL1, XTAL2).
Bit Manipulation:
The software can manipulate individual bits within the I/O ports using bitwise operations,
allowing for efficient control of specific pins without affecting others.
Memory Organization
Program Memory (Flash): Stores the program code and is non-volatile, allowing the code to
persist even when power is removed.
Data Memory (RAM): Used for storing variables and temporary data during program execution.
EEPROM: Electrically Erasable Programmable Read-Only Memory, used for storing non-
volatile data that may need to be preserved across power cycles.
Instruction Set: PIC microcontrollers have a reduced instruction set computing (RISC)
architecture, which means they have a small, highly optimized set of instructions. This
contributes to faster execution of instructions. Common instructions include arithmetic and logic
operations, branching, bit manipulation, and control flow instructions.
Peripherals: PIC microcontrollers offer a variety of on-chip peripherals such as timers, counters,
USART (Universal Synchronous/Asynchronous Receiver/Transmitter), SPI (Serial Peripheral
Interface), I2C (Inter-Integrated Circuit), ADC (Analog-to-Digital Converter), and more. The
rich set of peripherals allows for interfacing with sensors, communication modules, and other
external devices.
Development Tools: Microchip provides a comprehensive set of development tools for PIC
microcontrollers, including integrated development environments (IDEs) such as MPLAB X,
compilers, simulators, and debuggers.
Programming Languages: PIC microcontrollers can be programmed using assembly language or
high-level programming languages such as C. MPLAB XC compilers are commonly used for C
programming.
Variants and Families: PIC microcontrollers come in various families and series, each designed
for specific applications and requirements. Examples include PIC10, PIC12, PIC16, and PIC18
families.
Community Support: The PIC microcontroller community is active, with a wealth of online
resources, forums, and documentation available. This makes it easier for developers to find
support and share knowledge.
Low Power Options: Many PIC microcontrollers are designed for low-power applications,
making them suitable for battery-powered devices and other energy-efficient applications.
PIC microcontrollers typically follow Harvard architecture, featuring separate program
memory and data memory spaces. This allows simultaneous access to program instructions and
data, contributing to enhanced performance.
Central Processing Unit (CPU): The CPU executes instructions fetched from program memory
and manipulates data stored in data memory.
Memory Organization: Program Memory (Flash): Stores the firmware or program code.
Data Memory (RAM): Used for storing variables and temporary data during program
execution.
EEPROM: Electrically Erasable Programmable Read-Only Memory for non-volatile data
storage.
Peripherals: On-chip peripherals include timers, counters, communication interfaces (USART,
SPI, I2C), analog-to-digital converters (ADC), pulse-width modulation (PWM), and more. These
peripherals enhance the microcontroller's ability to interface with external devices and sensors.
Clock and Reset: PIC microcontrollers have an internal oscillator, and some variants support an
external crystal oscillator for accurate timing. Reset circuits ensure reliable startup and operation.
I/O Ports: Input/Output ports facilitate communication with external devices and sensors.
Applications of PLC
Embedded Systems: PIC microcontrollers are widely used in embedded systems for control
applications in appliances, automotive systems, industrial automation, and consumer electronics.
Automotive: Used in engine control units (ECUs), airbag systems, and various automotive
control applications.
Medical Devices: Employed in medical instruments and monitoring devices.
Industrial Control: Used in programmable logic controllers (PLCs), motor control, and other
industrial automation applications.
Consumer Electronics: Found in devices like smart home appliances, remote controls, and
electronic gadgets.
Communication Devices: Applied in communication devices, including modems and
networking equipment.
Selection Criteria for Microcontroller
Application Requirements: Choose a microcontroller that meets the specific requirements of
the application, considering factors such as processing power, memory, and peripherals.
Power Consumption: Evaluate power consumption based on the application's power
requirements, especially for battery-powered devices.
Peripherals and Interfaces: Select a microcontroller with the necessary peripherals and
communication interfaces for interfacing with sensors, actuators, and other external devices.
Cost: Consider the cost of the microcontroller and associated development tools in relation to the
project budget.
Development Tools and Support: Evaluate the availability and quality of development tools,
including compilers, IDEs, and community support.
Package and Size: Consider the physical size and package type of the microcontroller,
especially in space-constrained applications.
Advantages of PIC Microcontrollers
Versatility: PIC microcontrollers are versatile and suitable for a wide range of applications.
Rich Peripheral Set: On-chip peripherals provide flexibility and reduce the need for external
components.
Low Power Options: Many PIC microcontrollers offer low-power modes, making them suitable
for battery-operated devices.
Development Tools: Microchip provides comprehensive development tools, including free
MPLAB X IDE and XC compilers.
Community Support:. Low-End and High-Performance PIC Microcontrollers:
Low-End PIC Microcontrollers: Examples include PIC10 and PIC12 families. Suited for simple
control applications with low resource requirements.
Limited program and data memory, basic peripherals. High-Performance PIC Microcontrollers:
Examples include PIC18, PIC32 families.
Suitable for complex applications with higher processing power and memory requirements.
Advanced peripherals, larger memory capacity, and enhanced features.