Documente Academic
Documente Profesional
Documente Cultură
INTRODUCTION TO MICROCONTROLLER
A microcontroller is a computer-on-a-chip. Micro suggests that the device is small, and controller tells you that the device might be used to control objects, processes, or events. Another term to describe a microcontroller is embedded controller, because the microcontroller and its support circuits are often built into, or embedded in, the devices they control.
8051 MICROCONTROLLER
BOOLEAN PROCESSOR 4K ROM 128BYTE RAM 4-INPUT OUTPUT PORT(8-BIT) 2-TIMER(16 -BIT) 2 INTERRUPT 2 SERIAL I/O PORT 2 XTAL OSCILATOR 210 BIT ADDRESS BIT
[I/O]
8051 ARCHITECTURE
8 bit CPU with registers A and B 16 bit program counter(PC) and data pointer(DPTR) 8 bit program status word(PSW) 8 bit stack pointer Internal ROM of 0(8031) to 4K(8051) Internal RAM of 128 Bytes 4 register banks 00-1f 16 bytes(bit addressable) 20-2f 80 bytes of general purpose data memory 30-7f 32 I/O pins arranged as four 8 bit ports (P0 P3) 2 16-bit timer/counters: T0 and T1 Full duplex serial data receiver/transmitter: SBUF Control registers: TCON, TMOD, SCON, PCON, IP and IE 2 external and 3 internal interrupt sources Oscillator and clock circuits
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
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
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 MOV A,#20h Direct Addressing MOV A,30h Indirect Addressing MOV A,@R0 External Direct MOVX A,@DPTR Code Indirect MOVC A,@A+DPTR Each of these addressing modes provides important flexibility.
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 (hexadecimal). 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 (hexadecimal) and store it in the Accumulator. Direct addressing is generally fast since, although the value to be loaded isn't included in the instruction, it is quickly accessible 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. Indirect addressing always refers to Internal RAM; it never refers to an SFR.
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. 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 END: Indicates the end of the source file.
STACKS
Section of RAM used by the CPU to store information temporarily. Information can be data or an address. The CPU needs this storage area since there are only a limited number of registers.
ACCESSING STACKS
The register used to access the stack is called the SP (stack pointer) and is 8-bits wide (00hffh). When the 8051 is powered up the SP contains the value 07. This means that RAM location 08 is the first location used for the stack. Final location is 1F (20h -> used for bitaddressable memory) Storing of a CPU register in the stack is called a PUSH. Loading the contents of the stack back into a CPU register is called a POP.
UPPER LIMIT
Ram locations 08 1F used for the stack. If more than 24 bytes 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 program needs register bank1,2 or 3.
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.
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.
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 of 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.