Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Mairtin O Conghaile
8051 Microcontroller
Complete, highly-integrated microcomputer CPU, RAM, ROM, IO Port 0 8-bit bidirectional I/O port OR multiplexed low-order address and data bus bytes Port 1 8-bit bidirectional I/O port Port 2 8-bit bidirectional I/O port OR high-order address byte Port 3 8-bit bidirectional I/O port OR various special-function signals
2
[I/O]
Internal RAM
Basic Registers
Accumulator R registers B register Data Pointer (DPTR) 16-bit register Program Counter (PC) 16-bit register Stack Pointer (SP)
MOV instruction
Simply stated, the MOV instruction copies data from one location to another. It has the following format
MOV destination, source This instruction tells the CPU to move (copy) the source operand to the destination operand, without changing the content of the source operand. Examples:
MOV MOV MOV MOV A,#55h R0,A R3, #95h A,R3 ; load 55h into register A ; copy contents of A into R0 ; load value 95h into R3 ;copy content of R3 into A
ADD instruction
The ADD instruction has the following format:
ADD A, source ;Add the source operand to A
This tells the CPU to add the source byte to reg A and put the result in reg A
Example
Calculate the content of the accumulator after the program is executed on the 8051.
MOV MOV MOV ADD ADD R5,#25h R7,#34H A,#0 A,R5 A,R7
Addressing Modes
An "addressing mode" refers to how you are addressing a given memory location. In summary, the addressing modes are as follows, with an example of each:
Immediate Addressing Direct Addressing Indirect Addressing External Direct Code Indirect MOV A,#20h MOV A,30h MOV A,@R0 MOVX A,@DPTR MOVC A,@A+DPTR
Immediate Addressing
Immediate addressing is so-named because the value to be stored in memory immediately follows the operation code in memory. That is to say, the instruction itself dictates what value will be stored in memory. MOV A,#20h This instruction uses Immediate Addressing because the Accumulator will be loaded with the value that immediately follows; in this case 20 (hexidecimal). Immediate addressing is very fast since the value to be loaded is included in the instruction. However, since the value to be loaded is fixed at compile-time it is not very flexible.
Direct Addressing
Direct addressing is so-named because the value to be stored in memory is obtained by directly retrieving it from another memory location. For example: MOV A,30h This instruction will read the data out of Internal RAM address 30 (hexidecimal) and store it in the Accumulator. Direct addressing is generally fast since, although the value to be loaded isnt included in the instruction, it is quickly accessable since it is stored in the 8051s Internal RAM. It is also much more flexible than Immediate Addressing since the value to be loaded is whatever is found at the given address-which may be variable. Also, it is important to note that when using direct addressing any instruction which refers to an address between 00h and 7Fh is referring to Internal Memory. Any instruction which refers to an address between 80h and FFh is referring to the SFR control registers that control the 8051 microcontroller itself.
Indirect Addressing
Indirect addressing is a very powerful addressing mode which in many cases provides an exceptional level of flexibility. MOV A,@R0 This instruction causes the 8051 to analyze the value of the R0 register. The 8051 will then load the accumulator with the value from Internal RAM which is found at the address indicated by R0. For example, lets say R0 holds the value 40h and Internal RAM address 40h holds the value 67h. When the above instruction is executed the 8051 will check the value of R0. Since R0 holds 40h the 8051 will get the value out of Internal RAM address 40h (which holds 67h) and store it in the Accumulator. Thus, the Accumulator ends up holding 67h.
Indirect Addressing(contd)
Indirect addressing always refers to Internal RAM; it never refers to an SFR. Thus, in a prior example we mentioned that SFR 99h can be used to write a value to the serial port. Thus one may think that the following would be a valid solution to write the value 1 to the serial port: MOV R0,#99h ;Load the address of the serial port MOV @R0,#01h ;Send 01 to the serial port WRONG!! On an 8051 these two instructions would produce an undefined result since the 8051 only has 128 bytes of Internal RAM.
Question 1
Write a short program for the 8051 microcontroller that carries out the following instructions:
Loads the accumulator with the value 40h Loads R7 with 12d Copies the content of R7 to address 30h directly Loads the register R0 with 30h Indirectly moves the contents of 30h to reg B
Indicate clearly the contents, at each stage, of all memory locations and registers involved.
HERE:
DB
The DB directive is the most widely used data directive in assembler. Used to define the 8-bit data. When DB is used, the numbers can be decimal, binary, hex or ASCII formats. The only directive that can be used to define ASCII strings larger than two characters. DATA1: DATA2: DATA3: ORG 500H DB 39H DB 2591 ;ASCII NUMBERS ORG 518H DB Computer Engineering
Assembler Directives
ORG: Used to indicate the beginning of the address. EQU: Used to define a constant without occupying a memory location.
e.g. Count EQU 25
Example
State the contents of the RAM locations after the following program:
SETB PSW.4 MOV R0,#99H MOV R1,#85H MOV R2,#99H MOV R7,#85H MOV R5,#99H
Solution
By default PSW.3 = 0 and PSW.4 =0; therefore line 1 sets RS1=1 and RS0 = 0, thereby selecting register bank 2. Register Bank 2 uses RAM locations 10H 17H. After execution of this program we have the following:
RAM RAM RAM RAM RAM location location location location location 10H 11H 12H 17H 15H has has has has has value value value value value 99H 85H 3FH 63H 12H
Example
Show the stack and stack pointer for the following: MOV R6,#25H MOV R1,#12H MOV R4,#0F3H PUSH 6 ;push onto stack from R6 PUSH 1 ;push onto stack from R1 PUSH 4 ;push onto stack from R4 POP 3 ;pop stack into R3 POP 5 ;pop stack into R5 POP 2 ;pop stack into R2
Upper Limit
Ram locations 08 1F used for the stack. If more than 24bytes of stack required, then the SP must be changed to point to RAM locations 30h-7Fh using the instruction MOV SP,#xx Also may need to shift SP if a given progam needs register bank1,2 or 3.
Example
Show the stack and stack pointer for the following instructions: MOV SP,#5FH MOV R2,#25H MOV R1,#12H MOV R4,#0F3H PUSH 2 PUSH 1 PUSH 4
Questions
Write a simple program in which the value 55h is added five times. Show the stack and the stack pointer for each line of the following:
Org 0 MOV MOV MOV
MOV PUSH PUSH PUSH CLR A MOV MOV POP 7 POP 2 POP 5
Program Flow
When an 8051 is first initialized, it resets the PC to 0000h. The 8051 then begins to execute instructions sequentially in memory unless a program instruction causes the PC to be otherwise altered. There are various instructions that can modify the value of the PC; specifically, conditional branching instructions, direct jumps and calls, and "returns" from subroutines. Additionally, interrupts, when enabled, can cause the program flow to deviate from its otherwise sequential scheme.
Example
Write a program to clear the Acc, then add 3 to the accumulator ten times. MOV MOV ADD DJNZ MOV A,#0 R2,#10 A,#03 R2,AGAIN R5,A
AGAIN:
Question
Write a program to load the accumulator with the value 10h and then complement the Acc 700times. (Hint: Try two separate loops to achieve the overall of 700)
Solution
MOV MOV MOV CPL DJNZ DJNZ A,#10H R3,#10 R2,#70 A R2,AGAIN R3,NEXT
NEXT: AGAIN:
JB
JNB JBC
Jump if bit = 1
Jump if bit = 0 Jump if bit = 1 and clear bit
Example
Find the sum of the values 79H, F5H and E2H. Put the sum of the registers in R0 (low byte) and R5 (high byte)
Solution
MOV MOV ADD JNC INC N_1: ADD JNC INC N_2: ADD JNC INC OVER: MOV A,#0 R5,A A,#79H N_1 R5 A,#0F5H N_2 R5 A,#0E2H OVER R5 R0,A ;clear A ;clear R5 ;A=A+79h ;if no carry, add next ;if CY=1, increment R5 ;A=79H+F5H=6EH and CY1=1 ;jump if CY=0 ;if CY=1, increment R5 ;A=6E+E2=50H and CY=1 ;jump if CY=0 ;if CY=1, increment R5 ;now R0=50H and R5=02
CALL instructions
CALL instruction is used to call a subroutine LCALL (long call) 3 byte instruction ACALL (absolute call) 2 byte instruction
When a subroutine is called, control is transferred to that subroutine. After finishing execution of the subroutine, the instruction RET (return) transfers control back to the caller.
Example
The following shows crystal frequency for three different 8051-based systems. Find the period of the machine cycle in each case. (a) 11.0592MHz (b) 16MHz (c) 20MHz
Solution
1/11.0592MHz = period per oscillation Machine cycle = 12x = 1.085s 1/16MHz = period per oscillation Machine cycle = 12x = 0.75s 1/20MHz = period per oscillation Machine cycle = 12x = 0.6s
Question
For an 8051 system of 11.0592MHz, find how long it takes to execute each of the following instructions: (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2,target (d) NOP (e) MUL AB
Solution
(a) (b) (c) (d) (e) MOV DEC DJNZ NOP MUL R3,#55 R3 R2,target
AB
Delay Calculation
A delay subroutine consists of two parts: (a) setting a counter (b) a loop Most of the time delay is performed by the body of the loop. Very often we calculate the time delay based on the instructions inside the loop and ignore the clock cycles associated with the instructions outside the loop. Largest value a register can hold is 255; therefore, one way to increase the delay is to use the NOP command. NOP, which stands for No Operation simply wastes time.
Example
Find the size of the delay in the following program, if the crystal frequency is 12MHz. MOV A,#55H AGAIN: MOV P1,A ACALL DELAY CPL A SJMP AGAIN DELAY: MOV R3,#200 HERE: DJNZ R3,HERE RET What does the above program do?
Solution
DELAY: HERE: MOV DJNZ RET R3,#200 R3,HERE
Crystal Cycle 12 24 12
Question
For a machine cycle of 1s, find the time delay of the following subroutine. DELAY:
AGAIN: HERE:
R2,#200 R3,#250
R3,HERE R2,AGAIN
Solution
HERE Loop: (4x250)x1s = 1000 s AGAIN Loop: Repeats the HERE loop 200 times i.e. 200x1000s = 200ms The instructions MOV R3,#250 and DJNZ R2,AGAIN at the beginning at end of the AGAIN loop will add (3x200x1s) = 600s to the delay time. Total execution time 200.6ms (an approximation since we have ignored the first and last instructions in the subroutine)