EMBEDDED SYSTEM
Covered Topics: Programming the 8051 Microcontroller: Different Addressing modes supported by 8051. The 8051 Instruction Set: Data transfer instructions, Arithmetic instructions, Logical instructions, Boolean instructions, and Program Control Transfer instructions. Embedded Firmware Design Approaches, Assembly Language based Embedded Firmware development.UNIT 1: Programming the 8051 Microcontroller:
Different Addressing modes supported by 8051.
The 8051 microcontroller supports various addressing modes, providing flexibility in how operands are specified in assembly language instructions. Different addressing modes enable efficient and concise programming. Here are the addressing modes supported by the 8051:
1. **Immediate Addressing Mode:**
- Operand data is specified directly in the instruction.
- Example: `MOV A, #25H`
2. **Register Addressing Mode:**
- Operand data is located in one of the registers (R0 to R7).
- Example: `MOV A, R0`
3. **Direct Addressing Mode:**
- The operand is specified directly by an address.
- Example: `MOV A, 30H` (moves the content of the memory location at address 30H to register A)
4. **Indirect Addressing Mode:**
- The address of the operand is stored in a register, and the data is located at that address.
- Example: `MOV A, @R0` (moves the content of the memory location whose address is in register R0 to register A)
5. **Indexed Addressing Mode:**
- The operand address is formed by adding an offset to a register value.
- Example: `MOV A, 20H+R1` (moves the content of the memory location at address 20H + content of register R1 to register A)
6. **Relative Addressing Mode:**
- Used with jump and branch instructions, where the operand is a signed offset relative to the current program counter.
- Example: `JZ LABEL` (jumps to the label if the zero flag is set)
7. **Bit Addressing Mode:**
- Allows operations on individual bits in the Special Function Registers (SFRs) or in the bit-addressable RAM.
- Example: `CLR P1.3` (clears bit 3 in port 1)
8. **Long Addressing Mode:**
- Used for accessing data in external memory.
- Example: `MOVX A, @DPTR` (moves data from external memory location pointed by DPTR to register A)
### Example Code Illustrating Addressing Modes (in Assembly Language):
```assembly
; Immediate Addressing
MOV A, #25H
; Register Addressing
MOV A, R0
; Direct Addressing
MOV A, 30H
; Indirect Addressing
MOV A, @R0
; Indexed Addressing
MOV A, 20H+R1
; Relative Addressing (Jump if Zero)
JZ LABEL
; Bit Addressing
CLR P1.3
; Long Addressing (External Memory Access)
MOVX A, @DPTR
```
Understanding the addressing modes is essential for efficient and effective programming with the 8051 microcontroller. The choice of addressing mode depends on the specific requirements of the task and the characteristics of the data involved. Always refer to the 8051 microcontroller's datasheet and documentation for precise details on addressing modes and instruction set.
The 8051 Instruction Set:
The 8051 microcontroller has a relatively simple and straightforward instruction set, which makes it suitable for a wide range of embedded applications. The instructions can be categorized into several groups based on their functions. Here is an overview of the key categories and representative instructions in the 8051 instruction set:
### Data Transfer Instructions:
1. **MOV A, source:**
- Move data from the source (register, immediate, memory) to the accumulator.
2. **MOV destination, source:**
- Move data from the source to the destination (registers, memory).
3. **MOVC A, @A+DPTR:**
- Move code byte from the code memory to the accumulator using DPTR and A as pointers.
### Arithmetic and Logic Instructions:
4. **ADD A, source:**
- Add the contents of the source (register, immediate, memory) to the accumulator.
5. **SUBB A, source:**
- Subtract the contents of the source (register, immediate, memory) from the accumulator with borrow.
6. **INC destination:**
- Increment the contents of the destination (register, memory).
7. **DEC destination:**
- Decrement the contents of the destination (register, memory).
8. **MUL AB:**
- Multiply the contents of A and B, storing the result in AB.
### Bit Manipulation Instructions:
9. **ANL destination, source:**
- Bitwise AND operation between the source and destination.
10. **ORL destination, source:**
- Bitwise OR operation between the source and destination.
11. **XRL destination, source:**
- Bitwise XOR operation between the source and destination.
12. **CJNE A, data, rel:**
- Compare A with data, jump to the relative address if not equal.
### Control Transfer Instructions:
13. **JMP addr:**
- Jump to the specified absolute address.
14. **CALL addr:**
- Call a subroutine at the specified absolute address.
15. **RET:**
- Return from a subroutine.
16. **JZ/JNZ/JC/JNC/JB/JNB/JBC addr:**
- Jump instructions based on the status of flags (Zero, Carry, Bit).
### Data Exchange Instructions:
17. **XCH A, destination:**
- Exchange the contents of the accumulator with the contents of the destination.
18. **XCHD A, @R0/@R1:**
- Exchange the lower nibble of the accumulator with the contents of the address formed by the upper nibble of A and R0/R1.
### Stack and Data Pointer Instructions:
19. **PUSH source:**
- Push the source onto the stack.
20. **POP destination:**
- Pop the top of the stack into the destination.
21. **MOV DPTR, data16:**
- Load the Data Pointer (DPTR) with a 16-bit immediate value.
### Miscellaneous Instructions:
22. **NOP:**
- No operation.
23. **CLR/CPL:**
- Clear/Set the specified bit or register.
24. **SETB bit:**
- Set the specified bit.
25. **ACALL/AJMP addr11:**
- Absolute Jump/Call to an 11-bit address.
These instructions represent a subset of the 8051 instruction set. The actual instruction set may include additional instructions, and some features might vary depending on the specific 8051 variant. Always refer to the datasheet and documentation for your specific 8051 model for accurate information on the instruction set and addressing modes.
Data transfer instructions
Data transfer instructions in the 8051 instruction set are responsible for moving data between registers, memory, and the accumulator. Here are some of the key data transfer instructions:
1. **MOV A, source:**
- Move data from the source (register, immediate, or memory) to the accumulator.
```assembly
MOV A, R0 ; Move the content of register R0 to accumulator A
MOV A, #25H ; Move the immediate value 25H to accumulator A
MOV A, 30H ; Move the content of the memory location at address 30H to accumulator A
```
2. **MOV destination, source:**
- Move data from the source to the destination (registers, memory).
```assembly
MOV R1, A ; Move the content of accumulator A to register R1
MOV 40H, A ; Move the content of accumulator A to the memory location at address 40H
```
3. **MOVC A, @A+DPTR:**
- Move code byte from the code memory to the accumulator using DPTR and A as pointers.
```assembly
MOVC A, @A+DPTR ; Move the code byte from the code memory to the accumulator using DPTR and A
```
4. **MOVX A, @DPTR:**
- Move data from external memory to the accumulator using the Data Pointer (DPTR).
```assembly
MOVX A, @DPTR ; Move data from external memory to accumulator A using DPTR
```
5. **MOV DPTR, data16:**
- Load the Data Pointer (DPTR) with a 16-bit immediate value.
```assembly
MOV DPTR, #1000H ; Load DPTR with the immediate value 1000H
```
6. **MOVX @DPTR, A:**
- Move data from the accumulator to external memory using the Data Pointer (DPTR).
```assembly
MOVX @DPTR, A ; Move data from accumulator A to external memory using DPTR
```
These instructions provide a way to manipulate data within the 8051 microcontroller's registers and memory. The flexibility of these data transfer instructions is crucial for performing various operations and calculations in embedded systems. Always refer to the specific datasheet and documentation for your 8051 variant to ensure accurate usage and understanding of these instructions.
Arithmetic instructions,
Arithmetic instructions in the 8051 instruction set perform various mathematical operations on data stored in registers and memory. Here are some key arithmetic instructions:
1. **ADD A, source:**
- Add the contents of the source (register, immediate, or memory) to the accumulator.
```assembly
ADD A, R1 ; Add the content of register R1 to accumulator A
ADD A, #0AH ; Add the immediate value 0AH to accumulator A
ADD A, 20H ; Add the content of the memory location at address 20H to accumulator A
```
2. **ADDC A, source:**
- Add the contents of the source and the carry flag to the accumulator.
```assembly
ADDC A, R2 ; Add the content of register R2 and the carry flag to accumulator A
ADDC A, #05H ; Add the immediate value 05H and the carry flag to accumulator A
ADDC A, 30H ; Add the content of the memory location at address 30H and the carry flag to accumulator A
```
3. **SUBB A, source:**
- Subtract the contents of the source (register, immediate, or memory) and the borrow from the accumulator.
```assembly
SUBB A, R3 ; Subtract the content of register R3 and the borrow from accumulator A
SUBB A, #02H ; Subtract the immediate value 02H and the borrow from accumulator A
SUBB A, 40H ; Subtract the content of the memory location at address 40H and the borrow from accumulator A
```
4. **INC destination:**
- Increment the contents of the destination (register, memory).
```assembly
INC R4 ; Increment the content of register R4
INC 50H ; Increment the content of the memory location at address 50H
```
5. **DEC destination:**
- Decrement the contents of the destination (register, memory).
```assembly
DEC R5 ; Decrement the content of register R5
DEC 60H ; Decrement the content of the memory location at address 60H
```
6. **MUL AB:**
- Multiply the contents of A and B, storing the result in AB.
```assembly
MOV A, #0AH ; Load A with immediate value 0AH
MOV B, #02H ; Load B with immediate value 02H
MUL AB ; Multiply A and B, result in AB
```
These arithmetic instructions provide the means to perform addition, subtraction, and other mathematical operations within the 8051 microcontroller. Understanding and correctly utilizing these instructions are essential for implementing various algorithms and calculations in embedded systems. Always refer to the specific datasheet and documentation for your 8051 variant for accurate usage and details of these instructions.
Logical instructions
Logical instructions in the 8051 instruction set perform bitwise logical operations on data stored in registers and memory. Here are some key logical instructions:
1. **ANL A, source:**
- Bitwise AND operation between the accumulator and the source (register, immediate, or memory).
```assembly
ANL A, R1 ; Perform AND operation between accumulator A and register R1
ANL A, #0AH ; Perform AND operation between accumulator A and immediate value 0AH
ANL A, 20H ; Perform AND operation between accumulator A and the content of memory location at address 20H
```
2. **ANL destination, source:**
- Bitwise AND operation between the destination (register or memory) and the source.
```assembly
ANL R2, #05H ; Perform AND operation between register R2 and immediate value 05H
ANL 30H, #0FH ; Perform AND operation between the content of memory location at address 30H and immediate value 0FH
```
3. **ORL A, source:**
- Bitwise OR operation between the accumulator and the source (register, immediate, or memory).
```assembly
ORL A, R3 ; Perform OR operation between accumulator A and register R3
ORL A, #02H ; Perform OR operation between accumulator A and immediate value 02H
ORL A, 40H ; Perform OR operation between accumulator A and the content of memory location at address 40H
```
4. **ORL destination, source:**
- Bitwise OR operation between the destination (register or memory) and the source.
```assembly
ORL R4, #08H ; Perform OR operation between register R4 and immediate value 08H
ORL 50H, #0EH ; Perform OR operation between the content of memory location at address 50H and immediate value 0EH
```
5. **XRL A, source:**
- Bitwise XOR operation between the accumulator and the source (register, immediate, or memory).
```assembly
XRL A, R5 ; Perform XOR operation between accumulator A and register R5
XRL A, #03H ; Perform XOR operation between accumulator A and immediate value 03H
XRL A, 60H ; Perform XOR operation between accumulator A and the content of memory location at address 60H
```
6. **XRL destination, source:**
- Bitwise XOR operation between the destination (register or memory) and the source.
```assembly
XRL R6, #0AH ; Perform XOR operation between register R6 and immediate value 0AH
XRL 70H, #0FH ; Perform XOR operation between the content of memory location at address 70H and immediate value 0FH
```
7. **CPL bit:**
- Complement (invert) the specified bit.
```assembly
CPL C ; Complement the Carry flag
CPL 80H ; Complement bit 7 in the register or memory location at address 80H
```
These logical instructions provide the means to perform bitwise AND, OR, and XOR operations, as well as bit complement operations. Understanding and correctly utilizing these instructions are essential for implementing various logical operations in embedded systems. Always refer to the specific datasheet and documentation for your 8051 variant for accurate usage and details of these instructions.
Boolean instructions
In the context of the 8051 instruction set, there are no specific "Boolean instructions" per se. However, the 8051 assembly language provides instructions that can be used for Boolean operations. Boolean operations involve logical operations like AND, OR, XOR, and NOT, which are commonly used in Boolean algebra. Let's discuss how these logical operations can be used in a Boolean context:
1. **AND Operation:**
- The `ANL` (AND logical) instruction performs a bitwise AND operation between two operands.
```assembly
ANL A, #0FH ; Perform AND operation between accumulator A and immediate value 0FH
```
This instruction performs a bitwise AND between the accumulator and the immediate value 0FH.
2. **OR Operation:**
- The `ORL` (OR logical) instruction performs a bitwise OR operation between two operands.
```assembly
ORL A, #0AH ; Perform OR operation between accumulator A and immediate value 0AH
```
This instruction performs a bitwise OR between the accumulator and the immediate value 0AH.
3. **XOR Operation:**
- The `XRL` (XOR logical) instruction performs a bitwise XOR operation between two operands.
```assembly
XRL A, #03H ; Perform XOR operation between accumulator A and immediate value 03H
```
This instruction performs a bitwise XOR between the accumulator and the immediate value 03H.
4. **NOT Operation:**
- While there isn't a dedicated NOT instruction, you can use the `CPL` (Complement) instruction to invert the bits.
```assembly
CPL A ; Invert all bits in accumulator A
```
This instruction complements (inverts) all the bits in the accumulator.
These instructions, combined with the data transfer instructions and arithmetic instructions, enable you to perform a wide range of Boolean operations in the 8051 assembly language. Always refer to the specific datasheet and documentation for your 8051 variant for accurate usage and details of these instructions.
Program Control Transfer instructions
Program control transfer instructions in the 8051 instruction set manage the flow of program execution by enabling jumps and calls to different locations in the code. Here are some key program control transfer instructions:
1. **JMP addr:**
- Jump to the specified absolute address.
```assembly
JMP LABEL ; Jump to the address specified by the label "LABEL"
JMP 3000H ; Jump to the absolute address 3000H
```
2. **CALL addr:**
- Call a subroutine at the specified absolute address.
```assembly
CALL SUBROUTINE ; Call the subroutine specified by the label "SUBROUTINE"
CALL 4000H ; Call the subroutine at the absolute address 4000H
```
Note: Subroutines are typically used for modular programming, and the `RET` instruction is used to return from a subroutine.
3. **RET:**
- Return from a subroutine.
```assembly
RET ; Return from the current subroutine
```
4. **ACALL addr11:**
- Absolute Call to an 11-bit address.
```assembly
ACALL SUB ; Absolute Call to the subroutine specified by the label "SUB"
```
Note: `ACALL` is similar to `CALL`, but it allows for a shorter 11-bit address, suitable for small subroutines.
5. **AJMP addr11:**
- Absolute Jump to an 11-bit address.
```assembly
AJMP TARGET ; Absolute Jump to the address specified by the label "TARGET"
```
Note: `AJMP` is similar to `JMP`, but it allows for a shorter 11-bit address.
6. **JZ/JNZ/JC/JNC/JB/JNB/JBC addr:**
- Conditional jump instructions based on the status of flags (Zero, Carry, Bit).
```assembly
JZ TARGET ; Jump to the address specified by the label "TARGET" if the Zero flag is set
JC LABEL ; Jump to the address specified by the label "LABEL" if the Carry flag is set
```
These instructions provide the means to control the flow of a program, allowing for conditional and unconditional jumps and calls. Proper utilization of these instructions is essential for creating efficient and organized code in embedded systems. Always refer to the specific datasheet and documentation for your 8051 variant for accurate usage and details of these instructions.
Embedded Firmware Design Approaches
Embedded firmware design involves creating the software that runs on embedded systems, such as microcontrollers or other dedicated hardware. Several approaches can be taken when designing embedded firmware, and the choice often depends on the requirements of the specific application. Here are some common approaches to embedded firmware design:
1. **Procedural/Imperative Programming:**
- This is a traditional approach where the firmware is written using procedural or imperative programming languages like C or Assembly.
- It involves writing step-by-step instructions for the processor to execute.
- Well-suited for low-level programming and direct hardware interaction.
2. **Object-Oriented Programming (OOP):**
- Some embedded systems use object-oriented programming principles, especially if the firmware is complex and requires modularity and abstraction.
- C++ is a commonly used language for embedded OOP.
- OOP can improve code organization and reusability.
3. **Real-Time Operating System (RTOS):**
- In applications requiring real-time responsiveness, an RTOS can be used.
- An RTOS provides a structured way to manage tasks, scheduling, and resources.
- Examples include FreeRTOS, Micrium, and VxWorks.
4. **Bare-Metal Programming:**
- In some cases, firmware is developed without an operating system, known as bare-metal programming.
- Suitable for applications where resource constraints or real-time requirements make an operating system impractical.
- Requires direct management of hardware and peripherals.
5. **Event-Driven Programming:**
- In an event-driven approach, firmware responds to external events or interrupts.
- Common in systems with asynchronous input sources, such as sensors or user input.
- Well-suited for applications where power consumption can be optimized.
6. **Model-Driven Development (MDD):**
- MDD involves creating models that represent the behavior and structure of the embedded system.
- Code is generated automatically from these models, reducing manual coding errors.
- Tools like Simulink, MATLAB, or Altair Embed are used for MDD.
7. **Finite State Machines (FSM):**
- Finite state machines are used to model the behavior of the system in discrete states.
- Useful for systems with well-defined states and transitions.
- Improves clarity and maintainability of code.
8. **Functional Safety and Certification:**
- In safety-critical applications (e.g., automotive, medical), firmware may need to adhere to specific safety standards.
- Certification processes, such as ISO 26262 for automotive, may dictate the design approach.
9. **Firmware Over-the-Air (FOTA) Updates:**
- Designs that support FOTA updates allow firmware to be updated remotely.
- Enables bug fixes, feature enhancements, or security patches without physical access to the device.
10. **Code Size and Optimization:**
- In resource-constrained environments, firmware designers may focus on minimizing code size and optimizing performance.
- Techniques like code and data compression may be employed.
The choice of firmware design approach depends on factors such as system requirements, hardware constraints, real-time considerations, and the complexity of the application. Additionally, factors like power consumption, maintainability, and the need for future updates can influence the chosen approach.
Assembly Language based Embedded Firmware development.
Developing embedded firmware using assembly language involves writing low-level code that directly corresponds to the machine instructions executed by the target microcontroller or processor. While high-level languages like C are commonly used for embedded development due to their portability and ease of programming, there are cases where assembly language is preferred. Here are some aspects of assembly language-based embedded firmware development:
1. **Direct Hardware Interaction:**
- Assembly language provides direct access to hardware registers and peripherals.
- Developers can precisely control how the processor interacts with sensors, actuators, and other hardware components.
2. **Performance Optimization:**
- Assembly language allows developers to optimize code for size and speed.
- Fine-tuning critical sections of code can result in more efficient execution, crucial in resource-constrained environments.
3. **Real-Time Constraints:**
- For applications with strict real-time requirements, assembly language can be beneficial.
- Predictable timing and low-level control over interrupts and timing-sensitive operations can be achieved.
4. **Interrupt Service Routines (ISRs):**
- Assembly language is often used for writing ISRs, which handle hardware interrupts.
- Quick and precise handling of interrupts is crucial for time-sensitive applications.
5. **Bootloader Development:**
- In some cases, assembly language is used to develop bootloaders, especially in situations where code execution must start from a specific memory location.
6. **Legacy Systems:**
- Assembly language may be required for programming legacy systems or microcontrollers with limited support for high-level languages.
7. **Code Size Considerations:**
- For microcontrollers with small memory footprints, assembly language can help minimize code size.
- Hand-optimizing critical sections can lead to more compact firmware.
8. **Understanding Microcontroller Architecture:**
- Writing assembly code necessitates a deep understanding of the microcontroller's architecture.
- Developers need to be familiar with instruction sets, addressing modes, and register usage.
9. **Debugging Challenges:**
- Debugging assembly code can be challenging compared to high-level languages.
- Developers may need to rely on simulator tools, hardware debugging interfaces, or emulator tools.
10. **Portability and Maintenance:**
- Assembly language lacks the portability and abstraction provided by high-level languages.
- Maintenance can be challenging, especially as codebases grow in complexity.
11. **Inline Assembly in High-Level Code:**
- Some developers use inline assembly within high-level languages like C to achieve performance optimizations for specific code segments.
While assembly language offers fine-grained control and performance benefits, it comes at the cost of increased development effort, reduced portability, and potentially more challenging maintenance. The decision to use assembly language should be based on careful consideration of the specific requirements of the embedded system and the trade-offs involved. In many cases, a combination of assembly and higher-level languages may be used to strike a balance between performance and productivity.