Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Nowadays there is a version of this memory that can be rewritten and it is called EEPROM (Electrically Erasable Programmable Read Only Memory). RAM: RAM is Random Access Memory. It is a volatile type of memory that needs electricity to flow to retain information. It is the type of memory that computers/ CPU of microcontroller use to process programs. The RAM of a microcontroller (8051) is 128bytes or more (depending upon the company of microcontroller). The RAM consists of registers that can be of one or two bytes (generally).
Registers: In CPU registers are used to store information temporarily. That information can be in the form of bytes of data to be processed or some address pointing to a data to be fetched. In 8051 there is only one data type: 8 bits. With an 8 bit data registers, any data more than 8 bit should be divided into 8 bit chunks before being processed.
The most widely used registers of 8051 are A (accumulator), R0, R1, R2, R3, R4, R5, R6, R7, DPTR (data pointer), PC (program counter). A register is used for all the arithmetic and logic operations. These registers are divided into two groups:
y y
For better understanding of microcontroller I will use assembly language. You may not enjoy this language as it is quite lengthy and tedious to write programs, so in the end I will also discuss the C platform for writing programs easily and much faster.
STRUCTURE OF ASSEMBELY LANGUAGE: Lets see an example (Below is a statement taken from an assembly code)
y y y y
USER: It is label (this field is not necessary) MOV: mnemonic (easy to remember). Others mnemonic are SUBC, MUB, DIV, etc A is an operand, (this field is not necessary) ; load 25hex in accumulator register - This is a comment. Though this field is not necessary you should always use it to understand program better)
MOV R2,#3BH ; load R2= 3B hex 1. This instruction can be used to load any register with a hex value. 2. # signifies that it is a number (and not a byte address). 3. H symbolizes that it is hexadecimal number. Similarly if we write a binary number it should end with B like 1111B & a decimal number can be written as it is or followed by D, 17 or 17D. 4. In the second line of the above code we used 0FFH. The 0 symbolizes that F is a hex number and not letter. This is always necessary if we are writing any hexadecimal number. It should always start with a 0 like 0ABH or 0CFH 5. All the register should be assumed to be of 1 byte. Unless otherwise mentioned. So if we load A= FFFH > 255D (a byte can hold), it is illegal.
Now suppose I wish to transfer contents of R1 into R6. I can do so by writing the following code:
Assembly Code MOV R6,R1 ; copy contents of R1, R6 Here R6 is a destination operand whereas R1 is a source operand. We always write a destination operand first.
Assembly
; start at location 0 (origin) ; load 31 hex in R1 ; load 20 hex in R2 ; copy contents of R2 into R3 ; copy contents of R3 into R4 ; copy contents of R3 into R4
HERE:
SJMP
END 1. ORG and END are the directives of assembly language. The origin directs the CPU to start at ROM location 0H (this is discussed in detail later on). 2. HERE: - this label tells 8051 to stay in the loop. If your system has a monitor program you do not need this line and it should be deleted from your code.
ADD instruction Assembly Code ADD A,source ;add the value in A & source register y This instruion is used to add two numbers y When you do an operation between two registers then the first one is called the destination and second is the source. y The result of an operation is always stored in destination. y The operation ADD should always have register A as its destination. Assembly ADD ; A is the destination & R1 the source ;A=A+R1 Code A, R1
0H A,#30H A,#2BH
MOV ADD
R2,#2CH HERE
All the addition and subtraction should have A as their destination; otherwise the statement will be illegal. For example Assembly Code ADD R2,A ; an illegal statement R2,#12H ADD ; an illegal statement Now suppose I wish to add contents of two register to R1.
Assembly
HERE:
MOV SJMP
R1,A HERE
; copy contents of A to R1
(You can see 1 is carried from 4th bit to 5th bit) As we know that register A is a single byte register, so what will happen if addition of two numbers exceed FF hex (255) and a carry is generated {the maximum value a byte can hold is 255 or FF hex}
1. Where will this 9th bit go? The A register will hold all the bits except the first one. This highest significant bit will move to Carry Flag.
2. What is a carry flag? All the microcontrollers have a register to indicate arithmetic conditions such as carry bit. The register for this operation in 8051 is called a PSW register or flag register.
y y y y y
CY (PSW.7): This is the carry flag. This flag is set (become high or CY=1) whenever there is a carry from D7 bit. You can set this flag equal to one like this SETB C, and clear it by this instruction - CLR C. You will see the application in coming sessions. AC (PSW.6): This is the auxiliary carry flag. This flag is set whenever there is carry from D3 to D4. P (PSW.0): This is parity flag. This flag is set whenever the number of 1s in accumulator is odd otherwise it is zero. OV (PSW.2): Over flow flag, this will be explained later. RS1 (PSW.4) and RS0 (PSW.3) - These are used to switch register banks. (Explained in detail later). F0 (PSW.5) and -- (PSW.1) They do not have any specific function and are userdefinable bits.
Now suppose if I have these two instructions Assembly Code MOV A, #55H ; load A with 55hex ADD A, #25H ; A= A+25hex
SUBB instruction Assembly Code Subtraction: SUBB A,source ; A= A- source-CY y This instruction is used for subtracting two numbers. y Here also the destination is always register A. y The result is stored in the register A.
Before we move to this instruction, lets first discuss the subtraction of two decimal numbers. 1s Complement In a binary number take a complement, that is replace all 1s by 0 & 0s by 1 For e.g.: 1111 0000 1s complement: 0000 1111 2s Complement Take 1s complement and add 1H to it: In the above example 2s complement is:
How SUBB instruction works? There are three main steps involved:y y y
Take 2s complement of subtrahend (source operand) Add it to the A Invert the carry
y y
If CY= 0 after implementation (step three) then the result is positive. If the CY=0 the result is negative & the answer is 2s complement of result
Assembly
; clear the carry flag ; load A= 24 hex ; subtract 6B hex from A ; jump to next if CY=0 ; if CY=1 take complement of result
NEXT:
MOV
R1,A
MUL instruction
Assembly Multiply:
Code MUL
AB
; Multiply the contents of A & B ; the result is stored in 16 bit ;the lower byte in A & higher in B
y y
The numbers to be multiplied should be moved into registers A & B. The result is stored in 16 bit or 2 bytes in the same two registers A & B. The lowest significant bit is stored in the D0 (or the lowest bit of register A) and the highest in D7 of register B. We cannot multiply the value of two registers until we move their content to register A & B.
Example
Assembly
A,#B2H B,#D6H AB
; load A= B6 hex ; load B= D8 hex ; B2* D6= 94 CC ; we get A= CC & B= 94 ; since 94 is higher byte & CC lower byte
DIV instruction
Assembly Divide:
Code DIV
AB
y y y y
This instruction divides the number stored in register A by the number stored in register B. A is the numerator and B is the denominator. The quotient is stored in register A. The reminder is stored in Register B.
Example
Assembly
A, # 82 B, # 5 AB
; load A with 82 (decimal) ; load B with 5 ; divide A by B ; the result is A= 16 & B= 2 (in decimal)
SIGNED/ UNSIGNED NUMBERS Till now all the 8 bit was used to store the magnitude of number. We considered only positive numbers. But there are numerous occasions where we have to deal with negative & positive number both. 1. The highest bit carry the sign i.e. +/-. If the number is negative then highest bit set to1 or else in positive number it is zero. 2. All other bits carry the magnitude of the number.
If you dont have any use of negative number then you surely can skip this topic. How to convert a negative number in decimal to hex? 1. Write the number in binary form
2.
The binary form of 10: 0000 1010 2s complement of binary number: 1111 0110
HEX number: F6 (The above discussion is in regard with an 8 bit data. But the 8051 only support 8bit so dont worry. )
Assembly
The bit encircled represent a sign, this should have be zero since we have added two positive numbers, so the result should be positive. {Since the highest bit represent the sign, which is 1 when the number is one} This is called overflow. Therefore a flag called over flow flag or OV flag is set to one.
When is OV set to one? 1. 2. 3. When there is a carry from D6 to D7 When there is carry from D7 (to carry flag) OV is zero when both or none of them happen.
Summary:
Let us recap what we have covered so far: Types of memories Types of registers: General and special Assembly instruction set like MOV and arithmetic operations