Embedded Software 1. General 8051 features (excluding I/O) CPU 8 bit microcontroller The basic...
-
Upload
jordan-cluett -
Category
Documents
-
view
214 -
download
0
Transcript of Embedded Software 1. General 8051 features (excluding I/O) CPU 8 bit microcontroller The basic...
Embedded Software 1
General 8051 features (excluding I/O)
General 8051 features (excluding I/O)
CPU8 bit microcontrollerThe basic registers include (more discussed later)
The 8-bit A (accumulator) and B registers, 16-bit program counter (PC) 8-bit program status register (PSW) 8-bit stack pointer (SP)
MemorySeparate program and data memory
Internal ROM used to store the program memory.Internal data memory consists of 256 bytes of internal RAM
8051 Program Memory8051 Program MemoryProgram memory is used to store the program code.
This is the machine code that is obtained from the assembly language or C program. The machine code represents instructions and constants.
When the 8051 is powered up, program execution always starts from the location 0X0000.
After the RESET operation, the program counter (PC) contains 0000h, causing the first instruction to be read from the program location 0000h.
The program memory addresses are 16-bits long. Therefore the 8051 can directly address 216 = 64k locations.
Each program memory location stores a single byte. The CPU can only read from program memory.
8051 Data Memory8051 Data Memory Can read/write from/to data memory.
Therefore the contents of data memory can change as the program executes.
Data memory is used to temporary data used by a program. Also used to store the stack.
The total size of the 8051’s internal RAM is small
256 bytes in total ! Data memory map shows 256 locations
Bottom 32 locations are used224 locations availableAddresses are shown in hex
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
Simplified Programming Model for 8051
Simplified Programming Model for 8051
Note that there are more important registers to add to this model!!! (See these later)
The 8051 is an accumulator based CPU
Operations on data are performed on data in A and result is placed in A
E.g Add A,#23
Accumulator based machines are common for older 8-bit machines
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
Instruction SetsInstruction SetsThe instruction set is the set of instructions that the CPU can decode and
execute. It defines the processor with each processor having it's own instruction set.The general groupings of instructions for any uP are
Arithmetic/Logic Data Movement Transfer of Control Test/Compare Input/Output (only on some CPU's ) Others
An instruction stored in memory is represented by a certain number of byte(s).Some CPUs have fixed length instructions, i.e. all the instructions in the instruction set are
of equal size
Instruction EncodingInstruction EncodingThe instruction contains
what the operation is, the operands for the instruction if there are any. the destination for the result if there is one
The part of the instruction that determines the instruction is called the opcodeExamples are
ADDJMP
An operand may be an immediate value, memory address or a CPU register. The location for the result of an operation could be a
memory address or a CPU register. For an 8 bit machine such as the 8051, an instruction can be represented by 1, 2 or 3 bytes
depending on the instruction
Instruction EncodingInstruction Encoding
Details for the 8051 (8 bit machine) are in notesThe 8051 is an example of an accumulator based machine
Accumulator machines have a special purpose register that normally serves as an operand and destination for an instruction
E.g. ADD A,#27
Encoding 00100100 00011011 24h 1Bh
2 byte instruction
Instruction EncodingInstruction Encoding
Multiples of bytes 8 bit opcode gives 28=256 possible opcodes
255 actually used139 are 1 byte instructions92 are 2 byte instructions24 are 3 byte instructions
Most instructions are one byte, some two, a few threeSee back of notes for complete details on instructions
Example of a 1 byte instruction is the followingCLR C
which is represented by machine code C3H or the equivalent bit pattern 11000011
Instruction TimingInstruction Timing
The microcontroller takes a minimum of 12 clock cycles (machine cycle) to execute an instruction
Some instructions are slower to execute, taking 24 clock cyclesThere are no instructions with 36 clock cycles, but there are 2 instructions
that take 48 clock cycles (or 4 machine cycles)MUL and DIV instructions
See back of lab notes for instruction cycle times for all instructionsNote that the instruction size does not directly imply how long the
instruction cycle is!
8051 Instruction set8051 Instruction set
ACALL addr11 DIV AB LJMP addr16 RETIADD A,<src> DJNZ <byte>,<rel8> MOV <dest>,<src> RL AADDC A,<src> INC <byte> MOV DPTR,#data16 RLC AAJMP addr11 INC DPTR MOV bit,bit RR AANL <dest,<src> JB bit,rel8 MOVC A,@A+<base> RRC AANL C,<bit> JBC bit,rel8 MOVX <dest>,<src> SETB bitCJNE <dest>,<src>,rel8 JC rel8 MUL AB SJMP rel8CLR A JMP @A+DPTR NOP SUBB A,<src>CLR bit JNB bit,rel8 ORL <dest>,<src> SWAP ACPL A JNC rel8 ORL C,bit XCH A,<byte>CPL bit JNZ rel8 POP direct XCHD A,@RiDA A JZ rel8 PUSH direct XRL <dest>,<src>DEC <byte> LCALL addr16 RET
How many instructions??
Lecture 2
Getting started with the
instruction set
Getting started with the
instruction set
A key requirement is the ability to move data
Between registersBetween memory and
registersBetween memory
The MOV instruction can be used
Different modes of addressing available
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
Introducing the MOV instructionIntroducing the MOV instruction
The MOV instruction is of the formMOV <dest>,<src>
And has the effect of moving a byte from <src> to the <dest> location.
There are 15 variations!!!Example MOV A,#12
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
12
Aside: Representing hexadecimal valuesAside: Representing hexadecimal values
On paper, a hexadecimal value is represented in one of the following ways
0xA4
A4hHowever, for an assember, a hexadecimal value starting
with the digit A to F must be preceded by 0x.This is so as to distinguish the number from an identifier.Therefore, the representation 0xA4 must be used with an
assembler.
Introducing the MOV instructionand immediate
values
Introducing the MOV instructionand immediate
values
Example MOV A,#12
Here the destination is the A register and the source is an immediate value
All immediate values are preceded by the hash symbol
Using an immediate value means that the source byte is constant
The value can be decimal or hex
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
12
Introducing the MOV instruction
and register addressing
Introducing the MOV instruction
and register addressing
Register addressing is another method for accessing data
Example MOV R3,#32
MOV A,R3 Firstly the decimal value 32 is
copied into R3, while the second instruction copies the value in R3 into A.
Any one of the 8 registers R0-R7 can be used
Use Rn to indicate any register when describing register addressing
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
32
32
Introducing the MOV instruction with register addressing
and immediate values
Introducing the MOV instruction with register addressing
and immediate values
MOV R3,#32
MOV A,R3
Destination operand
Uses register addressing
R3
Source operand
Uses immediate value
32Destination operand
A
Source operand
Uses register addressing
R3
MOV R3,#32
MOV A,R3
Introducing the MOV instruction
showing instruction encoding
Introducing the MOV instruction
showing instruction encoding
01111011 00100000
7B 20
11101011
EB
Aside, what is the benefit of short instructions?
Introducing the MOV instruction
and direct addressing
Introducing the MOV instruction
and direct addressing
To access data in lower RAM, direct addressing is used
Example MOV 0x20,A
Here, the destination is hex address 20 and the source value, which is in A, is copied into RAM
Direct addressing can only be used to access the lower 128 bytes and NOT the upper 128 bytes
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
15
Note the difference between immediate
values and direct addressing: Be careful!!!
Note the difference between immediate
values and direct addressing: Be careful!!! It is important to understand the difference between an
immediate value and direct addressing
MOV A,#0x64 MOV A,0x64
Here, the value 64h is stored in A
Here, the value at address 64h is stored in A.
Find out in lab the machine code for both of these?
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
Introducing the MOV instruction
and indirect addressing
Introducing the MOV instruction
and indirect addressing
To access data ìn any one of the 256 RAM locations, indirect addressing can be used
Example MOV @R0,A
Indirect addressing can be specified using either @R0 or @R1
when discussing indirect addressing, it is common to refer to @Ri, where i could be 1 or 0.
How does it work??Read on!!!
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
20h
13
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
Introducing the MOV instruction
and indirect addressing
Introducing the MOV instruction
and indirect addressing
Example MOV @R0,A
The contents of R0 are interpreted as an address in RAM. If R0 contains the value 20h, then the above instruction will copy the value in A into address 20h in RAM.
In effect, it is now possible to indirectly access a memory location
Have you seen this idea before, C programmers?
Lower 128 bytes
8 bits 32 bytesused
Upper 128 bytes
00h
20h
80h
FFh
20h
13
Summary of addressing modes
Summary of addressing modes
Immediate value#data8e.g. MOV A,#56h
Regsister addressingRie.g. MOV R6,R3
Direct Addressingdirecte.g. MOV R6,4Eh
Indirect [email protected]. MOV @R1,R6
Good news for us this semester
The first 3 are the addressing modes that will be mostly used and that indirect addressing will be rarely used
Full details for MOV instructions
Full details for MOV instructions
MOV A,sourceMOV A,#dataMOV dest,sourceMOV dest,#data
Where dest and source can be any of Rn, direct or @Ri
AppendixAppendix
8051 Block diagram 8051 Block diagram
ROM RAMTimer
Timer 1
Instruction encodingInstruction encoding
Another example is
ADDC A,R1
This instruction adds the contents of A and the contents of R1 using the carry and stores the result in A
The machine code is 39H. In general for this instruction, it will be encoded as follows
0011 1
5 bits 3 bits
Rn
ExercisesExercises
1. Draw a block diagram of the 8051.2. What is the purpose of the program memory on the
8051?3. At what address does the 8051 start executing code
from upon powerup?4. How many memory locations in the 8051 program
memory?5. What register contains the address of the next program
instruction?a) How many bits in this register?
ExercisesExercises6. Can program memory be written to during program execution?7. What is the purpose of the 8051 data memory?8. What size is the data memory?9. Draw a simplified programming model for the 8051, making sure to
label all registers.10. What does it mean to say that a processor is an accumulator based
machine11. What is an instruction set?12. List the general groupings of instructions that make up an
instruction set?
ExercisesExercises
13. What information does an instruction contain?14. On the 8051, what size are the instructions?15. What is machine code for the following instruction and
how many clock cycles does it take?ADD A,R5
16. Do we need to remember the machine code for each instruction?
17. How many 8051 instructions are there?18. What is the purpose of the MOV instruction?
ExercisesExercises
19. What is an immediate value?20. How is it represented?21. What instruction is used to move the immediate
decimal value 12 into the accumulator?22. What is register addressing?23. Move the value in register R4 into the accumulator.
What is the resulting machine code?24. What is direct addressing? Give an example.
ExercisesExercises
24. What is indirect addressing? Give an example25. Write out the code to move the decimal values 0,1,15
and 8 into the regsiters R0 to R3 respectively?26. Write out the code to mode the hexadecimal values
0,a5,15 and c into the registers R0 to R4 respectively.27. Write out the code to move the initialise the memory
locations 20h to 24h with zero.28. Write out the code to swap the values in R0 and R1.