Transcript Document
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) 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 216 = 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 8051’s 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 FFh Upper 128 bytes 80h Lower 128 bytes 20h 00h 8 bits 32 bytes used 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 8-bits A B R7 R6 R5 R4 R3 R2 R1 R0 Operations on data are performed on data in A and result is placed in A E.g Add A,#23 PC 16-bits 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 00100100 24h 2 byte instruction Encoding 00011011 1Bh Instruction Encoding Multiples of bytes 8 bit opcode gives 28=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 ADD A,<src> ADDC A,<src> AJMP addr11 ANL <dest,<src> ANL C,<bit> CJNE <dest>,<src>,rel8 CLR A CLR bit CPL A CPL bit DA A DEC <byte> DIV AB DJNZ <byte>,<rel8> INC <byte> INC DPTR JB bit,rel8 JBC bit,rel8 JC rel8 JMP @A+DPTR JNB bit,rel8 JNC rel8 JNZ rel8 JZ rel8 LCALL addr16 LJMP addr16 MOV <dest>,<src> MOV DPTR,#data16 MOV bit,bit MOVC A,@A+<base> MOVX <dest>,<src> MUL AB NOP ORL <dest>,<src> ORL C,bit POP direct PUSH direct XRL RET How many instructions?? RETI RL A RLC A RR A RRC A SETB bit SJMP rel8 SUBB A,<src> SWAP A XCH A,<byte> XCHD A,@Ri <dest>,<src> Lecture 2 Getting started with the instruction set FFh A key requirement is the ability to move data Between registers Between memory and registers Between memory The MOV instruction can be used 8-bits A Upper 128 bytes B 20h R7 R6 R5 R4 R3 R2 R1 R0 00h PC 80h Lower 128 bytes 8 bits 32 bytes used Different modes of addressing available 16-bits Introducing the MOV instruction The MOV instruction is of the form MOV <dest>,<src> And FFh has the effect of moving a byte from <src> to the <dest> location. 8-bits A Upper 128 bytes B 20h R7 R6 R5 R4 R3 R2 R1 R0 00h PC 80h Lower 128 bytes 8 bits 32 bytes used 12 There are 15 variations!!! Example MOV A,#12 16-bits 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 FFh immediate values are preceded by the hash symbol 8-bits A Upper 128 bytes B 20h R7 R6 R5 R4 R3 R2 R1 R0 00h PC 80h Lower 128 bytes 8 bits 32 bytes used 12 Using an immediate value means that the source byte is constant The value can be decimal or hex 16-bits Introducing the MOV instruction and register addressing FFh 8-bits A Upper 128 bytes B 20h R7 R6 R5 R4 R3 R2 R1 R0 00h PC 80h Lower 128 bytes 8 bits 32 bytes used 32 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 R0R7 can be used 32 16-bits Use Rn to indicate any register when describing register addressing Introducing the MOV instruction with register addressing and immediate values Destination operand Uses register addressing R3 Source operand MOV R3,#32 Uses immediate value Destination operand 32 A MOV A,R3 Source operand Uses register addressing R3 Introducing the MOV instruction showing instruction encoding MOV R3,#32 Aside, what is the benefit of short instructions? 01111011 7B 20 MOV A,R3 11101011 EB 00100000 Introducing the MOV instruction and direct addressing To access data in lower RAM, direct addressing is used Example MOV 0x20,A Here, FFh 8-bits A Upper 128 bytes B 20h R7 R6 R5 R4 R3 R2 R1 R0 00h PC 80h Lower 128 bytes 8 bits 32 bytes used 15 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 16-bits 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 Here, the value 64h is stored in A MOV A,0x64 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 FFh 8-bits A Upper 128 bytes 20h 00h PC Lower 128 bytes 8 bits 32 bytes used 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 13 B R7 R6 R5 R4 R3 R2 R1 R0 80h To 20h when discussing indirect addressing, it is common to refer to @Ri, where i could be 1 or 0. How 16-bits does it work?? Read on!!! Introducing the MOV instruction and indirect addressing FFh 8-bits A Upper 128 bytes B 20h R7 R6 R5 R4 R3 R2 R1 R0 00h PC 80h Lower 128 bytes 8 bits 32 bytes used 13 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 20h 16-bits Have you seen this idea before, C programmers? 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 Appendix 8051 Block diagram Timer 1 ROM RAM Timer 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 3 bits 5 bits 0011 1 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. 7. 8. 9. 10. 11. 12. Can program memory be written to during program execution? What is the purpose of the 8051 data memory? What size is the data memory? Draw a simplified programming model for the 8051, making sure to label all registers. What does it mean to say that a processor is an accumulator based machine What is an instruction set? 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. 20. 21. 22. 23. 24. What is an immediate value? How is it represented? What instruction is used to move the immediate decimal value 12 into the accumulator? What is register addressing? Move the value in register R4 into the accumulator. What is the resulting machine code? What is direct addressing? Give an example. Exercises 24. 25. 26. 27. 28. What is indirect addressing? Give an example Write out the code to move the decimal values 0,1,15 and 8 into the regsiters R0 to R3 respectively? Write out the code to mode the hexadecimal values 0,a5,15 and c into the registers R0 to R4 respectively. Write out the code to move the initialise the memory locations 20h to 24h with zero. Write out the code to swap the values in R0 and R1.