Register are the most important part of microcontroller so it is necessary to understand different type of registers of microcontroller and their work. the registers of microcontroller can be a general purpose register or a special function register (SRF). special function register control special functionality of 8051 microcontroller like input/ouput port, serial port, timer, interrupt etc. here we only discus only some of them which as necessary to start writing code.
image show a 8 bit register and each block represent 1 bit. LSB is counted as D0 bit and MSB as D7 bit.
.
These register are divided in groups called Register Banks. Each bank have 8 register and utilize 8x8=64bits or 8 bytes which known as ( Register R0, R1, R2, R3, R4, R5, R6, R7).
Now you think that you can use 8x4=32 variables (registers) at a time. then answer is No. Because in 8051 you can use a single register bank at a time (means you can't you R1 of 1st bank and R5 of 2nd bank together).
Then what is the use of these 4 register banks. Use of these register bank is that you can switch form one bank to other bank and use more than 8 register in single program ( first you put value of R1 of 1st reg. bank in some other reg. [called A] and then switch to 2nd bank and then use R5).
These bank controlled by a SRF register called PSW(program status word).
Example:1
MOV Command : used to copy content of one Registert to other Register or Memory address
Example:2
RS1(PSW.4) and RS0(PSW.3) are used to select register bank as shown above image
Example:3
This is the second part of 8051 Tutorial Series. you can check first part here
Register:
A register is simply group of filp-flop which hold data temporarily to used by other registers or ALU. 8051 is a 8 bit microcontroller so its register are group of 8 bit ( or 8 flip-flop). but in 8051 DPTR and PC are 16 bit register. if you want to process data greater than 8 bit. you need to break it in 8 bit data and then you can process it and combine result back ( eg. addition of 16 bit data)image show a 8 bit register and each block represent 1 bit. LSB is counted as D0 bit and MSB as D7 bit.
.
Register Bank:
In assembly generally we don't need to define variable as in C, JAVA or there high level languages. Because 8051 have some general purpose register which are used as Variable and each one is 8 bit wide.These register are divided in groups called Register Banks. Each bank have 8 register and utilize 8x8=64bits or 8 bytes which known as ( Register R0, R1, R2, R3, R4, R5, R6, R7).
Now you think that you can use 8x4=32 variables (registers) at a time. then answer is No. Because in 8051 you can use a single register bank at a time (means you can't you R1 of 1st bank and R5 of 2nd bank together).
RS1 and RS0 are Bits of PSW register to select bank and right table show register bank with address range |
Then what is the use of these 4 register banks. Use of these register bank is that you can switch form one bank to other bank and use more than 8 register in single program ( first you put value of R1 of 1st reg. bank in some other reg. [called A] and then switch to 2nd bank and then use R5).
These bank controlled by a SRF register called PSW(program status word).
Accumulator Register (A):
This is the calculator of 8051 because all the arithmetic and logical operation process through this register (actually operation performed by ALU[ arithmetic and logical unit ] but data is transferred to ALU through A).Example:1
++++++++++++++++++ Addition ++++++++++++++++++++++++++++++++++++ ADD A, R0 ; value of R0 is added in A and R0 remain unchanged ; Let A=10 and R0=20 then after ADD command A=30 and R0=20 ADD R0, A ; wrong because operation can performed only through A ADD R0, R3 ; wrong because R0 and R3 can't preform operation ; between each other to do this you need to wirte as ADD A, R0 ; add A and R0 then R3 ADD A, R3 ADD A, #24h ; add 24h in A ADD A, #F2H ; wrong because in in assembly each number ; must be start from 0-9 ADD A, #0F2H; now correct ADD A, 24h ; add content at memory location 24h to reg. A +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MOV Command : used to copy content of one Registert to other Register or Memory address
Example:2
++++++++++++++++++++++++++++MOV operation++++++++++++++++++++++ MOV A, R0 ; copy content of Reg R0 to Reg A MOV A, #24h ; copy 24 in Reg A or load A with 24H MOV A, 24h ; Copy content from memory address 24h MOV R0, A ; copy content of A in Reg R0 MOV R0, R1 ; operation must be performed through A MOV A, R1 ; first copy in A and then in R0 MOV R0, A ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Program Status Word(PSW) Register:
As its name indicate that it hold the status of program( status of accumulator register). its different bits indicates different states of register A after operation as shown in fig.RS1(PSW.4) and RS0(PSW.3) are used to select register bank as shown above image
++++++++++++++++Carry, Auxiliary carry, parity bit+++++++++++++++++ MOV R0,#38H MOV A, #2FH ADD A,R0 38 0011 1000 ;CY=0 because no carry generated at D7 +2F 0010 1111 ;AC=1 because carry from D3 to D4 bit --------------------- ;P=1 because even number of 1's in A 67 0110 0111 --------------------- ;the above code can be written as MOV A,#38H ADD A,#2FH +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++SETB Command: To set a bit or bit=1
CLR command: To clear a bit or bit=0
Example:4
+++++++++++++++++++++++++Register Bank switching++++++++++++++++++++ ;add register R0 of bank 0 and R1 of bank 2 MOV R0, #22H ; By default Reg. bank 0 is selected SETB PSW.4 ; RS1=1 and RS0=0 reg. bank 2 selected MOV R1, #32H ; load reg R1 ADD A, R1 CLR PSW.4 ; switch back to Bank 0 ADD A, R0 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++In this tutorial you learn how to use registers and MOV and ADD command, in next tutorial we go through 8051 Memory, Data type and Directives.
Comments
Post a Comment