Presentation on theme: "Embedded Software 1. General 8051 features (excluding I/O) CPU 8 bit microcontroller The basic registers include (more discussed later) The 8-bit A (accumulator)"— Presentation transcript:
General 8051 features (excluding I/O) CPU 8 bit microcontroller The 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) Memory Separate program and data memory Internal ROM used to store the program memory. Internal data memory consists of 256 bytes of internal RAM
8051 Program Memory Program 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 2 16 = 64k locations. Each program memory location stores a single byte. The CPU can only read from program memory.
8051 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 8051s internal RAM is small 256 bytes in total ! Data memory map shows 256 locations Bottom 32 locations are used 224 locations available Addresses are shown in hex
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
Instruction Sets The 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 Encoding The 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 opcode Examples are ADD JMP 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 Encoding Details for the 8051 (8 bit machine) are in notes The 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 Encoding Multiples of bytes 8 bit opcode gives 2 8 =256 possible opcodes 255 actually used 139 are 1 byte instructions 92 are 2 byte instructions 24 are 3 byte instructions Most instructions are one byte, some two, a few three See back of notes for complete details on instructions Example of a 1 byte instruction is the following CLR C which is represented by machine code C3H or the equivalent bit pattern 11000011
Instruction 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 cycles There 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 instructions Note that the instruction size does not directly imply how long the instruction cycle is!
8051 Instruction set ACALL addr11 DIV AB LJMP addr16RETI ADD A, DJNZ, MOV, RL A ADDC A, INC MOV DPTR,#data16RLC A AJMP addr11 INC DPTR MOV bit,bitRR A ANL JB bit,rel8 MOVC A,@A+ RRC A ANL C, JBC bit,rel8 MOVX, SETB bit CJNE,,rel8 JC rel8 MUL ABSJMP rel8 CLR A JMP @A+DPTR NOPSUBB A, CLR bit JNB bit,rel8 ORL, SWAP A CPL A JNC rel8 ORL C,bitXCH A, CPL bit JNZ rel8 POP directXCHD A,@Ri DA A JZ rel8 PUSH direct XRL, DEC LCALL addr16 RET How many instructions??
Getting started with the instruction set A key requirement is the ability to move data Between registers Between memory and registers Between memory The MOV instruction can be used Different modes of addressing available
Introducing the MOV instruction The MOV instruction is of the form MOV, And has the effect of moving a byte from to the location. There are 15 variations!!! Example MOV A,#12 12
Aside: Representing hexadecimal values On paper, a hexadecimal value is represented in one of the following ways 0xA4 A4h However, 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 instruction and 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 12
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 32
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 32 Destination operand A Source operand Uses register addressing R3
MOV R3,#32 MOV A,R3 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 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 15
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?
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!!! 20h 13
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? 20h 13
Summary of addressing modes Immediate value #data8 e.g. MOV A,#56h Regsister addressing Ri e.g. MOV R6,R3 Direct Addressing direct e.g. MOV R6,4Eh Indirect addressing @Ri e.g. 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 MOV A,source MOV A,#data MOV dest,source MOV dest,#data Where dest and source can be any of Rn, direct or @Ri
Instruction 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
Exercises 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?
Exercises 6. 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 machine 11. What is an instruction set? 12. List the general groupings of instructions that make up an instruction set?
Exercises 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?
Exercises 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.
Exercises 24. What is indirect addressing? Give an example 25. 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.