Skip to main content

Familiarize with 8051 Architecture

Program Counter and Data Pointer

The 8051 contains two 16-bit registers-the program counter(PC) and data pointer(DPTR). Each is used to hold the address of a byte in memory. Program instruction bytes are fetched from locations in memory that are addressed by PC. Program ROM may be on the chip at address 000h to 0FFFh, external to the chip for addresses from 0000h to FFFFh. The PC is automatically incremented after every instruction bytes is fetched and may also be altered by certain instructions. The PC is the only register that does not have an external address.

The DPTR register is made up of two 8-bit registers named DPH and DPL which are used to furnish memory address for internal and external code access. The DPTR is under control of program instructions and can be specified by its 16-bit name DPTRor by each individual byte name, DPH and DPL. DPTR does not have a single internal address: DPL and DPH are each assigned an address.

A and B CPU registers

The 8051 contains 34 general-purpose, or working registers. Two of these registers A and B hold result of many instructions, particularly math and logical operations, of the 8051 CPU. Other registers are arranged as part of internal RAM in four banks, B0-B3, of eight registers and comprise the mathematical operator. The accumulator is most versatile of the two CPU registers and is used for many operations including addition, subtraction, multiplication, and division, and boolean manipulations. The A register is also used for all data transfers between the 8051 and any external memory. The B register is used with the A register for multiplication and division operations and has no other function than as location where data may be stored.

Flags and Program Status Word

Flags are 1-bit registers provided to store results of certain program instructions. Other instructions can test the condition of the flags and make decisions based on flags states. In order that the flag may be conveniently addressed, they are grouped inside the Program Status Word(PSW) and Power Control registers(PCON) registers.
•The 8051 has 4 math flags that respond automatically to outcomes of math operations and three general purpose user flags that can set to 1 or cleared to 0 by the programmer as desired. The math flags include carry(Cs), Auxillary carry(AC), Overflow(OV) and Parity(P).
User defined flags are;
•F0
•GF0 and
•GF1
These are general purpose flags that may be used by programmer to record some events in the program. All of the flags can be set and cleared by the programmer at will. The math flags however affected by math operators.

The program status word shown in figure contains math flags, user program flag F0 and register select bits that identify which of the four general purpose register banks is currently in use by the program. The remaining two user flags are stored in PCON register. The following figure shows Program Status Word of 8051.
CY AC F0 RS1 RS0 OV - P
The table below shows the functions of PSW bits.
Bit Symbol Function
7 CY Carry flag used for arithmetic, jump, rotate and boolean instructions
6 AC Auxillary carry flag; used for BCD arithmetic
5 F0 User flag 0
4 RS1 Register bank select bit 1
3 RS0 Register bank select bit 0
2 OV Overflow flag used with arithmetic instruction
1 - Reserved for future use
0 P Parity flag: Shows Parity of Accumulator;1=odd parity

As we all know there is 4 register banks for 8051 which can be selected by setting and clearing RS0 and RS1 as shown below:
RS1 RS0 Function
0 0 Select register bank 0
0 1 Select register bank 1
1 0 Select register bank 2
1 1 Select register bank 3
 The 8051 has 128 byte of RAM. We can see internal RAM organization i next figure, and it is organized into 3 different areas.

1.32-bytes from 00H to 1FHthat make up 32 working registers organized as four banks of  eight registers each.

The 4 register banks are numbered 0 to 3 and are made up of 8 registers named R0 to R7. Each register can be addressed by name (when it's bank is selected) or by its RAM address. Thus R0 of bank 3 is R0(if bank 3 is currently selected) or address 18H(whether bank 3 is selected or not). Bit RS0 and RS1 in PSW determine which bank of registers is currently in use at any time when the program is running. Register banks not selected can be used as general purpose RAM.
Bank 0 is selected on power on reset.

2. A bit addressable area of 16-bytes occupies RAM byte addresses 20H to 2FH, forming a total of 128 addressable bits. An addressable bit may be specified by its its bit address of 00H to 7FH, or 8-bits may from any byte address from 20H to 2FH.

Thus, for example, bit address 4FH is also bit 7 of byte 29H. Addressable bits are useful when the program need only remember a binary event(switch on, light off). Internal RAM is a short supply as it is, so why use a byte when a bit will do the work?

3. A general purpose RAM area above the bit-area, area from 30H to 7FH, adressable as bytes. This area is normally known as scratch pad area.
If you don't get the idea see the picture below;

Stack and Stack Pointer

The stack refers to an area of internal RAM that is used in conjunction with certain opcodes to store and retrieve data quickly. The 8-bit stack pointer(SP) register is used by the 8051 to hold an internal RAM address that is called the top of stack. The address held in stack pointer register is the location in internal RAM where the last byte of  data was stored by a stack operation. When data is to be placed on the stack, the SP increments before storing data on the stack so that the stack grows ipad data stored. As data retrieved from stack, the bytes read from the stack, and then the SP decrements to point to the next available byte of stored data.

Operation of the stack and the SP is shown in next figure. The SP is set to 07H when the 8051 is reset and can be changed to any internal RAM address by the programmer, using data move command.

The stack is limited in height to the size of the internal RAM. The stack has the potential(if the programmer is not careful to limits its growth) to overwrite valuable data in the register banks, bit-addressable RAM, and scratch pad RAM areas. The programmer is responsible for making sure the stack does not grow beyond predefined bounds.

The stack is normally placed in high internal RAM, by an appropriate choice of the number placed in the SP register, to avoid conflict with the register, bit, and scratch pad internal RAM areas.

Comments

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers ...

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...