Showing posts with label EDUCATIONAL INFO. Show all posts
Showing posts with label EDUCATIONAL INFO. Show all posts

Jan 3, 2013

FREE ANDROID APPLICATIONS FOR THE ELECTRONICS & ELECTRICAL ENGINEERS

The world around us is changing at a much faster pace than any one can anticipate. The world of “computing” has already seen great shifts from Desktops to Notebooks to Smartphones and Tablets. The coming decade will be more focused on mobile computing and cloud computing. Here in this article, I am listing some of the best and really useful applications released in Android market (aka Google Play), that comes handy for any one who is working in Electrical and Electronics industries/professions. 

Best and Free Android Applications for Electronics and Electrical Engineers

ElectroDroid – is the most popular and useful application in Android market for an electronics engineer. This app is a collection of many simple and useful tools like Resistor color code calculator, Filter value calculator, Inductor color code calculator, SMD resistor code calculator,  LED resistor calculator etc. The App also has a great collection of pin out diagrams of USB port, Parallel port, Ethernet port, VGA connector, Firewire connector etc and other resources and references which lists PIC micro controller database, ISP specs of AVR and PIC, Circuit schematic symbol reference, ASCII table references, Battery references etc.  This application has been downloaded by more than 10,000,00 smart phone devices and has been rated by more than 30,000 users. If you are looking for a much better version of the app without ads, you can buy it from the Google Play store (Electrodroid Pro) for less than 3 USD

Best and Free Android Applications for Electronics and Electrical Engineers

EveryCircuit - is a simple and beautiful application for android which helps you to build and simulate circuit ideas. It has a really good & simple user interface which begins with a workspace where you can start building your circuits. You can add your components like resistors, capacitors, inductors, power sources, signal sources etc and wire them together to complete the circuit. You can alter the values of each and every component and then finally Run/Simulate them. You will see the current flow, input and output waveforms graphically represented etc when you run the circuit you have built. You can alter the component values in real time and see the changes in output instantly. Additionally the app developers have provided a set of built in circuit applications like inverting amplifier, rectifier circuits, voltage regulator etc which the user can simulate instantly to learn the working of these circuits. The attractive feature is the graphical representation (animation) of the electron flow, input and output signals which helps the user to understand the circuit functioning within no time.  Their free version doesn’t have a large work space area, which limits the number of components you can play with. They also have limited their component library in their free application. I think they have deliberately done this to promote their paid application – EveryCircuit Premium – which they sell for around 10 USD. They have sold more than 10,000 apps in Play store, which shows people are quiet interested in their app. I suggest you try their Free app first and if you like it, go ahead and buy the premium version.
Best and Free Android Applications for Electronics and Electrical Engineers
PartSeeker – is an app for searching electronic parts and components. Unfortunately this app is a paid one and the good side is that you can buy it for less than 2 USD. This app is made and released by the same company (IERO) which made ElectroDroid (see above) app. IERO has made this app by using the extensive component library of Octopart (the electronic component search engine). This app comes handy while you are away travelling and is keen to search for a component using your mobile device (may be smart phone or a tab!). 

Best and Free Android Applications for Electronics and Electrical Engineers

PIC Microdatabase - another free app from IERO, which integrates well with ElectroDroid app. This app is nothing more than a database of PIC micro controllers manufactured by Microchip. This app lists all PIC and dsPIC family of controllers in an easy to use user interface. Features and specifications of all controllers along with pinout diagrams of select ones are available. The most attractive feature is a search functionality in which you can search for a controller with particular features you would like to have.As an example you can search for controller from PIC that belongs to a particular PIC family like PIC 10, with specific EPROM values, specific RAM, USB 2.0 or higher, specific internal oscillator values  etc. The app will output all PIC controllers that matches your search criterias. So far more than 10,0000 users have downloaded this application.
Best and Free Android Applications for Electronics and Electrical Engineers

DroidTesla – is another free app for simulating electronic circuits. This SPICE simulation tool is quiet similar to the app “EveryCircuit” mentioned above in its functionality - means you can build and simulate a circuit. But they both (EveryCircuit and DroidTesla) differ in user interface and features provided. I found the free version of “EveryCircuit” much more appealing than DroidTesla. EveryCircuit has certain predefined circuits like halfwave rectifier, inverting amplifier etc which you can simply load to workspace and simulate in realtime. DroidTesla has given a list of examples in their free app version but none of them loads properly to workspace. The reason is most of the example circuits contain a particular component which may be available only on DroidTesla’s commercial version. User interface of  EveryCircuit is much better than that of DroidTesla. DroidTesla’s commercial version might be much better than EveryCircuits premium version, as DroidTesla has large set of component library to choose from. DroidTesla will definitely outshine EveryCircuit – if the number of component libraries available is taken into account. Both of the apps commercial versions are priced competitively. If “user interface” is your primary preference, I will recommend EveryCircuit premium. On the other hand, if number of component library is your first preference, I shall recommend DroidTesla commercial version
ElectronicsToolKit – is another free app which is a collection of simple tools like resistor color code calculator, series and parallel calculator etc. Almost all those tools are available in ElectroDroid app too, except for a Power Triangle calculator. I have listed this app here as it is free (and I have spent some time to download and test this app in my Galaxy) and you guys can try out, if you have time.More than 10,000 users have tried this application.
Best and Free Android Applications for Electronics and Electrical Engineers

AllDataSheet App – This app is free version of the Datasheet website Alldatasheet.com. This app is nothing more than a book mark to alldatasheet website’s mobile version. I dont recommend you to download this app as your purpose will be served by visiting Alldatasheet.com from your mobile browser (which will get automatically redirected to mobile version) 

EquivalentResistanceSolver – is another free app which helps you to calculate the equivalent resistance of a circuit. This app may not be that useful for a professional as the “user interface” is average. It takes lots of time to build a “Series-Parallel” combination. Might be handy for students who are learning about the concepts for the first time. 
RF Pad Calculator – is a free app that helps to calculate the resistor values required to build an RF attenuator. 
AmpliCalc - is a simple and free app to calculate inverting and non inverting operational amplifier values.
FilterCalc – is another free app from the same developer of Amplicalc, which helps to realize filter design by finding the appropriate values. 
ControlCalc – is yet another free app from the same developer of Amplicalc, which helps in simulating closed and open systems, calculate transfer functions, state response & you can save the output as PNG. 
M32 Assembly – is another interesting application which helps you to learn assembly language by yourself. This app infact is a simulator for the M32 processor.
Electronica – is another app which is open source (the source code of this app is available for free download). This app is also a collection of simple and easy to use tools that comes handy for an every day electronics enthusiast/professional or even a student.   
There are a handful of other free android applications like Ohms Law calculator, 555 Timer Calculator which I have not mentioned here. The reason is simple, most of the “electronics tools apps like ElectroDroid, Electronica, ElectronicsToolsKit etc has Ohms Law calculation, Resistor Colourcode calculation and 555 Timer calculator embedded in them!

Jan 2, 2013

8051 PROGRAMMING TUTORIAL

This article series is developed to teach you 8051 micro controller programming. I have divided this programming tutorial into a series of chapters as shown below. So you can start with Chapter 1 and then move to chapter 2 and chapter 3 and so on. So let’s begin the journey right now! 
Note: Next chapters 1,2,3.. are under development phase. Please visit this page again for updates.
Note: To test any of these program or to write one your own and test, you dont need to buy a microcontroller board now. You can test your program using an 8051 simulator. Here is a big list of 8051 simulators available. In the beginning try the first one given in the list, Edsim51. Its an easy to use tool. 
To program any microcontroller available in this world, first you need to learn and understand it’s instruction sets. Instruction set contains a set of instructions that is available for the programmer to create any kind of program he likes. Or in another way, using the instruction set a programmer can create the program required for the specific application he is making. So first of all one needs to master all available instructions, how an instruction works, how the execution of an instruction affects the microcontroller (affecting the registers, psw, stack etc) and the way it is used in a program. Once the instruction set is mastered, you can start playing with programs. Before getting into programming, there are some prerequisites. If you are really new to micro controller and if 8051 is the first one you are playing with, please read the following articles first.
3. Addressing modes of 8051 You must read this article before writing any program for 8051 as this documents the root of instruction handling.
Now lets come to instruction sets of 8051 micro controller.  The 8051 instruction set can be classified as shown below.
  • Instructions for data transfer/ data move
  • Instructions for arithmetic operations
  • Instructions for branching a program
  • Instruction for creating subroutines
  • Instructions for logical operations
  • Instructions for boolean operations 
  • Special purpose instructions
Follow the given link, where you can access the complete list of instructions for 8051 micro controller – 8051 Instruction Set  (see the heading Alphabetical order below first table).
Note:- 8051 micro controller belongs to the MCS-51 family of micro controllers.  This basically means,any 8051 variant micro controller (that comes under the MCS-51 family) made by any other manufacturer must use the same set of instructions made for MCS-51. So the “instruction decoder” part of all micro controllers under MCS-51 family is same. Example: Atmels AT89c2051 is one such micro controller that falls under MCS-51 family. So a program written for Intel 8051 can be used to run AT89C2051 too (you may have to make slight modifications to match hardware disparities).
1. Instructions for data transfer includes – MOV, MOVC, MOVX, PUSH, POP, XCH, XCHD
2.Instructions for arithmetic operations are – ADD, ADDC, SUBB, MUL, DIV, INC, DEC, DA A
3. Instructions for branching and subroutines – LJMP, AJMP, SJMP, LCALL, ACALL, JZ, JNZ, CJNE, DJNZ, JMP, NOP, RET, RETI
4. Instructions for logical operations – ANL, ORL, XRL, CLR, CPL, RL, RLC, RR, RRC, SWAP
5. Instructions for boolean variable operations – SETB, MOV, CLR, JB, JNB, JBC, ANL, ORL, CPL, JC, JNC
6. Special purpose instructions involves – MOVC, MOVX, SWAP, XCH, XCHD, JBC, RETI, DA A
You may learn about all instructions in detail by following that link given above. There are 44 instructions in 8051 or MCS-51 instruction set.
I assume you have gone through data transfer/arithmetic/branching and subroutine instructions by now. Now lets write a very simple program.

A program to find sum of N natural numbers and store the sum

Program description:- The number “N” is stored in location 35H. Natural numbers generated from 0 to N must be stored from location 55H. The sum of natural numbers must be stored in location 36H.
Analyzing the program description, we need 3 registers. R0 to store the value of “N”  (given in location 35H)and to act as a counter for generating natural numbers upto N. R5 is used to save the value of first storage location of natural numbers and then R5 is incremented by one each to store each newly generated natural number. R7 is initiated as 0 and is incremented by 1 to generate natural numbers.

The Program:-

MOV PSW, #00H // Register bank '0' is selected by executing this instruction.
MOV R0, 35H // The value of 'N' stored in location 35H is transfered to R0.
MOV R5, #55H// The starting location for storing natural numbers '#55H' is transfered to R5
MOV A, #00H// Accumulator is initiated with value 0 for adding natural numbers cumulatively.
MOV R7, #00H// R7 is initialized to '0' to generate natural numbers. Note: '0' is not a natural number.
LOOP: INC R7// R7 is incremented by 1 to generate next natural number.
MOV @R5, 07H// This is indirect addressing mode used here.It is not possible to transfer data from one register to another register directly. So an instruction like MOV R5, R7 is invalid.Instead we use the direct address (07) of register R7 of register bank #00 to transfer the generated natural number to it's storage location in register R5.Indirect addressing is used as we need to save the generated natural number directly to memory address. R5 holds the starting location address (of the storage area) as its value i.e #55H.By indirectly addressing, we can save what ever value in R7 directly to location #55H.
INC R5// The storage location is incremented by 1 from #55H to #56H to store the next generated natural number
ADD A, R7// The generated natural number is added to contents in accumulator.
DJNZ R0, LOOP// The value of register Ro (value of 'N') is decremented by 1. It is checked against stopping condition zero. If its R0 is not equal to zero, the program control will move to label LOOP again and the steps from INC R7 will be executed again until R0 is equal to zero. When R0 is equal to zero, program control will exit the loop and move to next instruction given below.
MOV 36H,A// The sum of natural numbers in accumulator is moved to storage location 36H.
STOP: SJMP STOP// An infinite loop written at the end of the program. When this instruction is reached program control will get stuck at this instruction as it's an infinite loop.To get out of this infinite loop system reset must be applied.

A simple program to copy a block of data from one location to another

Program Description:- 10 bytes of data stored from 30H is to be copied to another location staring from 50H.
Analyzing the program, we see, we need two registers to store starting locations of source and destination. Lets say we take R0 as the source and R2 as the destination registers. Now we need a counter to count 10 bytes of data transfered from source to destination. Lets take R3 for that. It is not possible to transfer data from one register to another register directly by using any kind of addressing mode. So we need accumulator to stand in between as a temporary register. So here is it:

The Program:

MOV R0,#30H // Address of the starting location of source data is moved to R0.
MOV R1,#50H // Address of the starting location of destination is moved to R1
MOV R3,#OAH// Set the counter R3 with 10. You can also use decimal number as MOV R3,#10d.
LOOP: MOV A, @R0// Indirect addressing mode is used. Contents at the location of Ro (30H) is copied to accumulator.
MOV @R1, A// Contents in accumulator is copied to location pointed by Ra (that is 50H).
INC R0 // Ro is incremented by 1 to point to next location.
INC R1// R1 is incremented by 1 to point to next location.
DJNZ R3, LOOP // Counter register R3 is decremented by 1 and checked against zero. See the explanation DJNZ in the first program "sum of natural numbers"
STOP: SJMP STOP // Infinite loop to terminate programh

To generate a Fibonacci series 

Note:- Follow this link, If you are interested in learning about the Origin of Fibonacci series and the story behind
Program description:- We all know, a fibonacci series is an infinite series in mathematics that goes like 0,1,1,2,3,5,8,13,21…. 
So here we are going to write a program which generates fibonacci series upto N terms. The value of N is available in location 30H and we are going to save the series from location 40H. 
Note: If you are hearing the term Fibonacci series for the first time, here have a good read – Fibonacci series generation

The Program:- 

BEGIN: MOV R1,30H // Getting the value of "N"
MOV R7,#40H // The first number '0' of series is stored here.
MOV @R7,#00H // Loading value 'o' to address 40H using indirect addressing
INC R7 // Incrementing value of R7 from 40H to 41H to store next number '1'
MOV @R7, #01H // Storing value '1' to location 41H. Note that 'o' and '1' are seed values of a fibonacci series and has to be generated manually.
MOV R5,#42H // New register R5 is loaded with location address 42H to store next values of series, generated by adding the 2 previously generated numbers.
DEC R1
DEC R1 // The count value "N" is decremented by 2, as we have already generated and stored 1st two numbers.
DEC R7 // R7 is now reduced from 41H to 40H. We need to add contents of 40H and 41 H to get the number that is to be stored in 42H.
LOOP: MOV A, @R7 // Contents in R7 is moved to accumulator.
INC R7 // R7 is incremented to get the next value.
ADD A,@R7 // The two values are added and stored in Acc.
MOV @R5,A // The newly generated value of the series is stored in the address held by R5.
INC R5 // R5 is incremented to store next value.
DJNZ R1,LOOP // The count "N" is checked to zero (to know if all the numbers upto N are generated).
STOP: SJMP STOP // Run infinitely here or end of program execution.
 These 3 programs will be enough for a “kick start” in 8051 programming. More programs and concepts will be explained in upcoming articles. I recommend you to download Edsim51 simulator (or any other one you prefer) and try out these programs. Make changes to these codes and write simple codes based on your ideas – like Multiplying 2 numbers, Generating a particular series, Arithmetic calculator etc. Only by doing yourself, you can master the art of programming!

8051 SPECIAL FUNCTION REGISTERS AND PORTS

So you may have guessed something from the name itself – “Special Function Registers” known with an acronym SFR. Well, your guess is right :) Okay! Lets come to the point. There are 21 Special function registers (SFR) in 8051 micro controller and this includes Register A, Register B, Processor Status Word (PSW), PCON etc etc.  There are 21 unique locations for these 21 special function registers and each of these register is of 1 byte size. Some of these special function registers are bit addressable (which means you can access 8 individual bits inside a single byte), while some others are only byte addressable. Let’s take a look at them in detail.

Register A/Accumulator

The most important of all special function registers, that’s the first comment about Accumulator which is also known as ACC or A. The Accumulator (sometimes referred to as Register A also) holds the result of most of arithmetic and logic operations. ACC is usually accessed by direct addressing and its physical address is E0H. Accumulator is both byte and bit addressable. You can understand this from the figure shown below. To access the first bit (i.e bit 0) or to access accumulator as a single byte (all 8 bits at once), you may use the same physical address E0H. Now if you want to access the second bit (i.e bit 1), you may use E1H and for third bit E2H and so on.

 

Register B

The major purpose of this register is in executing multiplication and division. The 8051 micro controller has a single instruction for multiplication (MUL) and division (DIV). If you are familiar with 8085, you may now know that multiplication is repeated addition, where as division is repeated subtraction. While programming 8085, you may have written a loop to execute repeated addition/subtraction to perform multiplication and division. Now here in 8051 you can do this with a single instruction.
Ex: MUL A,B  – When this instruction is executed, data inside A and data inside B is multiplied and answer is stored in A.
Note: For MUL and DIV instructions, it is necessary that the two operands must be in A and B.
Note: Follow this link if you are interested in knowing about differences between a microprocessor and microcontroller.
Register B is also byte addressable and bit addressable. To access bit o or to access all 8 bits (as a single byte), physical address F0 is used. To access bit 1 you may use F1 and so on. Please take a look at the picture below.


Note: Register B can also be used for other general purpose operations.

 

Port Registers

As you may already know, there are 4 ports for 8051. If you are unfamiliar of the architecture of 8051 please read the following article:- The architecture of 8051
So 4 Input/Output ports named P0, P1, P2 and P3 has got four corresponding port registers with same name P0, P1, P2 and P3. Data must be written into port registers first to send it out to any other external device through ports. Similarly any data received through ports must be read from port registers for performing any operation. All 4 port registers are bit as well as byte addressable. Take a look at the figure below for a better understanding of port registers.


From the figure:-
  • The physical address of port 0 is 80
  • The physical address of port 1 is 90
  • And that of port 2 is A0
  • And that of port 3 is B0

Stack Pointer

Known popularly with an acronym SP, stack pointer represents a pointer to the the system stack. Stack pointer is an 8 bit register, the direct address of SP is 81H and it is only byte addressable, which means you cant access individual bits of stack pointer. The content of the stack pointer points to the last stored location of system stack. To store something new in system stack, the SP must be incremented by 1 first and then execute the “store” command. Usually after a system reset SP is initialized as 07H and data can be stored to stack from 08H onwards. This is usually a default case and programmer can alter values of SP to suit his needs.

 

Power Management Register (PCON)

Power management using a microcontroller  is something you see every day in mobile phones. Haven’t you noticed and got wondered by a mobile phone automatically going into stand by mode when not used for a couple of seconds or minutes ? This is achieved by power management feature of the controller used inside that phone.
As the name indicates, this register is used for efficient power management of 8051 micro controller. Commonly referred to as PCON register, this is a dedicated SFR for power management alone. From the figure below you can observe that there are 2 modes for this register :- Idle mode and Power down mode.


Setting bit 0  will move the micro controller to Idle mode and Setting bit 1 will move the micro controller to Power down mode.

Processor Status Word (PSW)

Commonly known as the PSW register, this is a vital SFR in the functioning of micro controller. This register reflects the status of the operation that is being carried out in the processor. The picture below shows PSW register and the way register banks are selected using PSW register bits – RS1 and RS0. PSW register is both bit and byte addressable. The physical address of PSW starts from D0H. The individual bits are then accessed using D1, D2 … D7.  The various individual bits are explained below.






Bit No
Bit Symbol
Direct Address
Name
Function
0 P D0 Parity This bit will be set if ACC has odd number of 1’s after an operation. If not, bit will remain cleared.
1 - D1
User definable bit
2 OV D2 Overflow OV flag is set if there is a carry from bit 6 but not from bit 7 of an Arithmetic operation. It’s also set if there is a carry from bit 7 (but not from bit 6) of Acc
3 RS0 D3 Register Bank select bit 0 LSB of the register bank select bit. Look for explanation below this table.
4 RS1 D4 Register Bank select bit 1 MSB of the register bank select bits.
5 F0 D5 Flag 0 User defined flag
6 AC D6 Auxiliary carry This bit is set if data is coming out from bit 3 to bit 4 of Acc during an Arithmetic operation.
7 CY D7 Carry Is set if data is coming out of bit 7 of Acc during an Arithmetic operation.
At a time registers can take value from R0,R1…to R7. You may already know there are 32 such registers. So how you access 32 registers with just 8 variables to address registers? Here comes the use of register banks. There are 4 register banks named 0,1,2 and 3. Each bank has 8 registers named from R0 to R7. At a time only one register bank can be selected. Selection of register bank is made possible through PSW register bits PSW.3 and PSW.4, named as RS0 and RS1.These two bits are known as register bank select bits as they are used to select register banks. The picture will talk more about selecting register banks.
So far we have discussed about all major SFR’s in 8051. There many other still waiting! Please remember there are 21 SFR’s and we have discussed only 9 specifically. The table below lists all other 12 SFR’s.


SFR

Address

Function

DPH 83 Data pointer registers (High). Only byte addressing possible.
DPL 82 Data pointer register (Low). Only byte addressing possible.
IP B8 Interrupt priority. Both bit addressing and byte addressing possible.
IE A8 Interrupt enable. Both bit addressing and byte addressing possible.
SBUF 99 Serial Input/Output buffer. Only byte addressing is possible.
SCON 98 Serial communication control. Both bit addressing and byte addressing possible.
TCON 88 Timer control. Both bit addressing and byte addressing possible.
TH0 8C Timer 0 counter (High). Only byte addressing is possible.
TL0 8A Timer 0 counter (Low). Only byte addressing is possible.
TH1 8D Timer 1 counter (High). Only byte addressing is possible.
TL1 8B Timer 1 counter (Low). Only byte addressing is possible.
TMOD 89 Timer mode select. Only byte addressing is possible.








8051 ADDRESSING MODES


8051 Addressing modes

Lets begin this article with a simple question. “What is an addressing mode ?. A simple question always has a simple answer too. Addressing mode is a way to address an operand.  Operand means the data we are operating upon (in most cases source data). It can be a direct address of memory, it can be register names, it can be any numerical data etc. I will explain this with a simple data move instruction of 8051.
MOV A,#6AH
Here the data 6A is the operand, often known as source data. When this instruction is executed, the data 6AH is moved to accumulator A. There are 5 different ways to execute this instruction and hence we say, we have got 5 addressing modes for 8051. They are explained below

Immediate Addressing Mode

Let’s begin with an example.
MOV A, #6AH
In general we can write MOV A, #data
This addressing mode is named as “immediate” because it transfers an 8-bit data immediately to the accumulator (destination operand).



The picture above describes the above instruction and its execution.  The opcode for MOV A, # data is 74H. The opcode is saved in program memory at 0202 address. The data 6AH is saved in program memory 0203. (See, any part of the program memory can be used, this is just an example) When the opcode 74H is read, the next step taken would be to transfer whatever data at the next program memory address (here at 0203) to accumulator A (E0H is the address of accumulator). This instruction is of two bytes and is executed in one cycle. So after the execution of this instruction, program counter will add 2 and move to o204 of program memory.
Note: The ‘#’ symbol before 6AH indicates that operand is a data (8 bit). If ‘#’ is not present then the hexadecimal number would be taken as address.

Direct Addressing Mode

This is another way of addressing an operand. Here the address of the data (source data ) is given as operand. Lets take an example.
MOV A, 04H
Here 04H is the address of register 4 of register bank#0. When this instruction is executed, what ever data is stored in register 04H is moved to accumulator. In the picture below we can see, register 04H holds the data 1FH. So the data 1FH is moved to accumulator.
Note: We have not used ‘#’ in direct addressing mode, unlike immediate mode. If we had used ‘#’, the data value 04H would have been transferred to accumulator instead 0f 1FH.




As shown in picture above this is a 2 byte instruction which requires 1 cycle to complete. Program counter will increment by 2 and stand in 0204. The opcode for instruction MOV A, address is E5H. When the instruction at 0202 is executed (E5H), accumulator is made active and ready to receive data. Then program control goes to next address that is 0203 and look up the address of the location (04H) where the source data (to be transferred to accumulator) is located. At 04H the control finds the data 1F and transfers it to accumulator and hence the execution is completed.

Register Direct Addressing Mode

In this addressing mode we use the register name directly (as source operand). An example is shown below.
MOV A, R4
At a time registers can take value from R0,R1…to R7. You may already know there are 32 such registers. So how you access 32 registers with just 8 variables to address registers? Here comes the use of register banks. There are 4 register banks named 0,1,2 and 3. Each bank has 8 registers named from R0 to R7. At a time only one register bank can be selected. Selection of register bank is made possible through a Special Function Register (SFR) named Processor Status Word (PSW). PSW is an 8 bit SFR where each bit can be programmed. Bits are designated from PSW.0 to PSW.7 Register banks are selected using PSW.3 and PSW.4 These two bits are known as register bank select bits as they are used to select register banks. A picture below shows the PSW register and the Register Bank Select bits with status.




So  in register direct addressing mode, data is transferred to accumulator from the register (based on which register bank is selected).
Take a look at the picture below.

So we see that opcode for MOV A, R4 is EC. The opcode is stored in program memory address 0202 and when it is executed the control goes directly to R4 of the respected register bank (that is selected in PSW). If register bank #0 is selected then the data from R4 of register bank #0 will be moved to accumulator. (Here it is 2F stored at 04 H). 04 H is the address of R4 of register bank #0. Movement of data (2F) in this case is shown as bold line. Now please take a look at the dotted line. Here 2F is getting transferred to accumulator from data memory location 0C H. Now understand that 0C H is the address location of Register 4 (R4) of register bank #1. Programmers usually get confused with register bank selection. Also keep in mind that data at R4 of  register bank #0 and register bank #1 (or even other banks) will not be same. So wrong selection of register banks will result in undesired output.
Also note that the instruction above is 1 byte and requires 1 cycle for complete execution. This means using register direct addressing mode can save program memory.

Register Indirect Addressing Mode

So in this addressing mode, address of the data (source data to transfer) is given in the register operand.
MOV A, @R0
Here the value inside R0 is considered as an address, which holds the data to be transferred to accumulator.
Example: If R0 holds the value 20H, and we have a data 2F H stored at the address 20H, then the value 2FH will get transferred to accumulator after executing this instruction. Got it? See  the picture below.


So the opcode for MOV A, @R0 is E6H. Assuming that register bank #0 is selected. So the R0 of register bank #0 holds the data 20H. Program control moves to 20H where it locates the data 2FH and it transfers 2FH to accumulator.
This is a single byte instruction and the program counter increments 1 and moves to 0203 of program memory.
Note: Only R0 and R1 are allowed to form a register indirect addressing instruction. In other words programmer can must make any instruction either using @R0 or @R1. All register banks are allowed.

Indexed Addressing Mode

Well lets see two examples first.
MOVC A, @A+DPTR and MOVC A, @A+PC
where DPTR is data pointer and PC is program counter (both are 16 bit registers). Lets take the first example.
MOVC A, @A+DPTR
What’s the first impression you have now? The source operand is @A+DPTR and we know we will get the source data (to transfer) from this location. It is nothing but adding contents of DPTR with present content of accumulator. This addition will result a new data which is taken as the address of source data (to transfer). The data at this address is then transferred to accumulator.  Take a look at the picture below.
The opcode for the instruction is 93H. DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits) and FE is located in DPL (lower 8 bits). Accumulator now has the value 02H. A 16 bit addition is performed and now 01FE H+02 H results in 0200 H. What ever data is in 0200 H will get transferred to accumulator. The previous value inside accumulator (02H) will get replaced with new data from 0200H. New data in the accumulator is shown in dotted line box.
This is a 1 byte instruction with 2 cycles needed for execution. What you infer from that? The execution time required for this instruction is high compared to previous instructions (which all were 1 cycle).
The other example MOVC A, @A+PC works the same way as above example. The only difference is, instead of adding DPTR with accumulator, here data inside program counter (PC) is added with accumulator to obtain the target address.
I hope you now have a clear cut understanding of 8051 addressing modes.








8051 MICROCONTROLLER ARCHITECTURE AND DESCRIPTION

8051 Microcontroller


A micro controller is an integrated circuit or a chip with a processor and other support devices like program memory, data memory, I/O ports, serial communication interface etc integrated together. Unlike a microprocessor (ex: Intel 8085), a microcontroller does not require any external interfacing of support devices. Intel 8051 is the most popular microcontroller ever produced in the world market. Now lets talk about 8051 microcontroller in detail.
Before going further, it will be interesting for you to understand the difference between a Microprocessor and Microcontroller. We have a detailed article which describes the basic difference between both.

Introduction

Intel first produced a microcontroller in 1976 under the name MCS-48, which was an 8 bit microcontroller. Later in 1980 they released a further improved version (which is also 8 bit), under the name MCS-51. The most popular microcontroller 8051 belongs to the MCS-51 family of microcontrollers by Intel. Following the success of 8051, many other semiconductor manufacturers released microcontrollers under their own brand name but using the MCS-51 core. Global companies and giants in semiconductor industry like Microchip, Zilog, Atmel, Philips, Siemens released products under their brand name. The specialty was  that all these devices could be programmed using the same MCS-51 instruction sets. They basically differed in support device configurations like improved memory, presence of an ADC or DAC etc. Intel then released its first 16 bit microcontroller in 1982, under name MCS-96

8051 Microcontroller Packaging

There is no need of explaining what each package means, you already know it. So I will skim through mainly used packaging for 8051. See, availability of various packages change from device to device. The most commonly used is Dual Inline Package (40 pins) – known popularly as DIP. 8051 is also available in QFP (Quad Flat Package), TQFP (Thin Quad Flat Package), PQFP (Plastic Quad Flat Package) etc. For explaining the pin diagram, we have used a 40 pin DIP IC as model.

8051 Microcontroller Architecture

Its possible to explain microcontroller architecture to a great detail, but we are limiting scope of this article to internal architecture, pin configuration, program memory and data memory organization. The basic architecture remains same for the MCS-51 family. In general all microcontrollers in MCS- 51 family are represented by XX51, where XX can take values like 80, 89 etc.

Schematic and Features
The general schematic diagram of 8051 microcontroller is shown above. We can see 3 system inputs, 3 control signals and 4 ports (for external interfacing). A Vcc power supply and ground is also shown. Now lets explain and go through each in detail. System inputs are necessary to make the micro controller functional. So the first and most important of this is power, marked as Vcc with a GND  (ground potential). Without proper power supply, no electronic system would work. XTAL 1 and XTAL 2 are for the system clock inputs from crystal clock circuit. RESET input is required to initialize microcontroller to default/desired values and to make a new start.
There are 3 control signals, EA,PSEN and ALE. These signals known as External Access (EA), Program Store Enable (PSEN), and Address Latch Enable (ALE) are used for external memory interfacing.
Take a look at the schematic diagram below (a functional microcontroller)

As mentioned above, control signals are used for external memory interfacing. If  there is no requirement of external memory interfacing then, EA pin is pulled high (connected to Vcc) and two others PSEN and ALE are left alone. You can also see a 0.1 micro farad decoupling capacitor connected to Vcc (to avoid HF oscillations at input).
There are four ports numbered 0,1,2,3 and called as Port 0, Port 1, Port 2 and Port 3 which are used for external interfacing of devices like DAC, ADC, 7 segment display, LED etc. Each port has 8 I/O lines and they all are bit programmable.

8051 Pin Diagram & Description

For describing pin diagram and pin configuration of 8051, we are taking into consideration a 40 pin DIP (Dual inline package). Now lets go through pin configuration in detail.
Pin-40 : Named as Vcc is the main power source. Usually its +5V DC.
You may note some pins are designated with two signals (shown in brackets).
Pins 32-39: Known as Port 0 (P0.0 to P0.7) – In addition to serving as I/O port, lower order address and data bus signals are multiplexed with this port (to serve the purpose of external memory interfacing). This is a bi directional I/O port (the only one in 8051) and external pull up resistors are required to function this port as I/O.
Pin-31:- ALE aka Address Latch Enable is used to demultiplex the address-data signal of port 0 (for external memory interfacing.)  2 ALE pulses are available for each machine cycle.
Pin-30:- EA/ External Access input is used to enable or disallow external memory interfacing. If there is no external memory requirement, this pin is pulled high by connecting it to Vcc.
Pin- 29:- PSEN or Program Store Enable is used to read signal from external program memory.
Pins- 21-28:- Known as Port 2 (P 2.0 to P 2.7) – in addition to serving as I/O port, higher order address bus signals are multiplexed with this quasi bi directional port.
Pin 20:- Named as Vss – it represents ground (0 V) connection.
Pins 18 and 19:- Used for interfacing an external crystal to provide system clock.
Pins 10 – 17:- Known as Port 3. This port also serves some other functions like interrupts, timer input, control signals for external memory interfacing RD and WR , serial communication signals RxD and TxD etc. This is a quasi bi directional port with internal pull up.
Pin 9:- As explained before RESET pin is used to set the 8051 microcontroller to its initial values, while the microcontroller is working or at the initial start of application. The RESET pin must be set high for 2 machine cycles.
Pins 1 – 8:- Known as Port 1. Unlike other ports, this port does not serve any other functions. Port 1 is an internally pulled up, quasi bi directional I/O port.

8051 Internal Architecture

There is no need of any detailed explanation to understand internal architecture of 8051 micro controller. Just look at the diagram above and you observer it carefully. The system bus connects all the support devices with the central processing unit. 8051 system bus composes of an 8 bit data bus and a 16 bit address bus and bus control signals. From the figure you can understand that all other devices like program memory, ports, data memory, serial interface, interrupt control, timers, and the central processing unit are all interfaced together through the system bus. RxD and TxD (serial port input and output) are interfaced with port 3.

8051 Memory Organization

Before going deep into the memory architecture of 8051, lets talk a little bit about two variations available for the same. They are Princeton architecture and Harvard architecture. Princeton architecture treats address memory and data memory as a single unit (does not distinguish between two) where as Harvard architecture treats program memory and data memory as separate entities. Thus Harvard architecture demands address, data and control bus for accessing them separately where as Princeton architecture does not demand any such separate bus.
Example:- 8051 micro controller is based on Harvard architecture and 8085 micro processor is based on Princeton architecture.
Thus 8051 has two memories :- Program memory and Data memory

Program memory organization

 

Now lets dive into the program memory organization 0f 8051. It has an internal program of 4K size and if needed an external memory can be added (by interfacing ) of size 60K maximum. So in total 64K size memory is available for 8051 micro controller.  By default, the External Access (EA) pin should be connected Vcc so that instructions are fetched from internal memory initially. When the limit of internal memory (4K) is crossed, control will automatically move to external memory to fetch remaining instructions. If the programmer wants to fetch instruction from external memory only (bypassing the internal memory), then he must connect External Access (EA) pin to ground (GND).
You may already know that 8051 has a special feature of locking the program memory (internal) and hence protecting against software piracy. This feature is enable by program lock bits. Once these bits are programmed, contents of internal memory can not be accessed using an external circuitry. How ever locking the software is not possible if external memory is also used to store the software code. Only internal memory can be locked and protected.  Once locked, these bits can be unlocked only by a memory-erase operation, which in turn will erase the programs in internal memory too.
8051 is capable of pipelining. Pipelining makes a processor capable of fetching the next instruction while executing previous instruction. Its some thing like multi tasking, doing more than one operation at a time. 8051 is capable of fetching first byte of the next instruction while executing the previous instruction.

Data memory organization

 

In the MCS-51 family, 8051 has 128 bytes of internal data memory and it allows interfacing external data memory of maximum size up to 64K. So the total size of data memory in 8051 can be upto 64K (external)  +  128 bytes (internal).  Observe the diagram carefully to get more understanding. So there are 3 separations/divisions of the data memory:- 1) Register banks 2) Bit addressable area 3) Scratch pad area.

 
Register banks form the lowest 32 bytes on internal memory and there are 4 register banks designated bank #0,#1, #2 and #3. Each bank has 8 registers which are designated as R0,R1…R7. At a time only one register bank is selected for operations and the registers inside the selected bank are accessed using mnemonics R0..R1.. etc. Other registers can be accessed simultaneously only by direct addressing.  Registers are used to store data or operands during executions.  By default register bank #0 is selected (after a system reset).
The bit addressable ares of 8051 is usually used to store bit variables. The bit addressable area is formed by the 16 bytes next to register banks. They are designated from address 20H to 2FH (total 128 bits). Each bits can be accessed from 00H to 7FH within this 128 bits from 20H to 2FH. Bit addressable area is mainly used to store bit variables from application program, like status of an output device like LED or Motor (ON/OFF) etc. We need only a bit to store this status and using a complete byte addressable area for storing this is really bad programming practice, since it results in wastage of memory.
The scratch pad area is the upper 80 bytes which is used for general purpose storage. Scratch pad area is from 30H to 7FH  and this includes stack too.

8051 System Clock

 An 8051 clock circuit is shown above.  In general cases, a quartz crystal is used to make the clock circuit. The connection is shown in figure (a) and note the connections to XTAL 1 and XTAL 2.  In some cases external clock sources are used and you can see the various connections above. Clock frequency limits (maximum and minimum) may change from device to device. Standard practice is to use 12MHz frequency. If serial communications are involved then its best to use 11.0592 MHz frequency.







Okay, take a look at the above machine cycle waveform. One complete oscillation of the clock source is called a pulse. Two pulses forms a state and six states forms one machine cycle. Also note that, two pulses of ALE are available for 1 machine cycle.

8051 Reset Circuit




8051 can be reset in two ways 1) is power-on reset – which resets the 8051 when power is turned ON and 2) manual reset – in which a reset happens only when a push button is pressed manually. Two different  reset circuits are shown above. A reset doesn’t affect contents of internal RAM. For reset to happen, the reset input pin (pin 9) must be active high for atleast 2 machine cycles.  During a reset operation :- Program counter is cleared and it starts from 00H, register bank #0 is selected as default, Stack pointer is initialized to 07H, all ports are written with FFH.