You are on page 1of 40

INTEL 8051 MICRCONTROLLER

Introduction : A decade back the process and control operations were totally implemented by the

Microprocessors only. But now a days the situation is totally changed and it is occupied by the new devices called Microcontroller. The development is so drastic that we cant find any electronic gadget without the use of a microcontroller. This microcontroller changed the embedded system design so simple and advanced that the embedded market has become one of the most sought after for not only entrepreneurs but for design engineers also.
What is a Microcontroller?

A single chip computer or A CP with all the peripherals like !AM" !#M" $%# Ports" Timers " A&Cs etc... on the same chip. 'or e() Motorolas *+,," $ntels +-.," /ilogs /+ and P$C ,*0 etc1
MICROPROCESSORS & MICROCONTROLLERS:

Microprocessor: A CP built into a single 234$ chip is called a microprocessor. $t is a general5purpose device and additional e(ternal circuitry are added to make it a microcomputer. The microprocessor contains arithmetic and logic unit 6A3 7" $nstruction decoder and control unit" $nstruction register" Program counter 6PC7" clock circuit 6internal or e(ternal7" reset circuit 6internal or e(ternal7 and registers. But the microprocessor has no on chip $%# Ports" Timers" Memory etc. 'or e(ample" $ntel +-+. is an +5bit microprocessor and $ntel +-+*%+-++ a ,*5bit microprocessor. The block diagram of the Microprocessor is shown in 'ig., MICROCONTROLLER : A microcontroller is a highly integrated single chip" which consists of on chip CP Processing 6Central

nit7" !AM 6!andom Access Memory7" 8P!#M%P!#M%!#M 68rasable

Programmable !ead #nly Memory7" $%# 6input%output7 9 serial and parallel" timers" interrupt
,

controller.

'or e(ample" $ntel +-., is +5bit microcontroller and $ntel +-:* is ,*5bit

microcontroller. The block diagram of Microcontroller is shown in 'ig.;.

Fig 1 !"oc# di$gr$% o& $ Microprocessor

Fig.2.Block Diagram of a Microcontroller

'istinguis( )et*een Microprocessor $nd Microcontro""er S No , Microprocessor A microprocessor is a general purpose device which is called a CP ; A microprocessor do not contain on5chip $%# Ports" Timers" Memories etc.. A microcontroller includes !AM" !#M" serial and parallel interface" timers" interrupt circuitry 6in addition to CP 7 in a single chip. + Microprocessors most Microcontrollers are used in small" commonly used as the CP in minimum component designs performing control5oriented applications. microcomputer systems Microprocessor instructions are Microcontroller instructions are both bit addressable as well as byte addressable. have instruction sets are Microcontro""er A microcontroller is a dedicated chip which is also called single chip computer.

<

mainly nibble or byte addressable .

Microprocessor instruction sets are Microcontrollers mainly intended large volumes of data. outputs.

for catering to catering to the control of inputs and

Microprocessor

based

system Microcontroller based system design is rather simple and cost effective of The instruction set of a Microcontroller is simple with less number of instructions. 'or" e() P$C microcontrollers have only =. instructions

design is comple( and e(pensive , The $nstruction set

microprocessor is comple( with very large number of instructions.

A microprocessor has >ero status A microcontroller has no >ero flag. flag

E-OL.TION OF MICROCONTROLLERS :

The first microcontroller TM4,--- was introduced by Te(as $nstrumentsin the year ,:?<. $n the year ,:?*" Motorola designed a Microprocessor chip called *+-, which replaced its earlier chip *+-- with certain add5on chips to make a computer. This paved the way for the new revolution in the history of chip design and gave birth to a new entity called @Microcontro""er/ 3ater the $ntel company produced its first Microcontroller +-<+ with a CP and ,A bytes of 8P!#M" *< Bytes of !AM an +5Bit Timer and ;? $%# pins in ,:?*. Then followed the most popular controller +-., in the year ,:+- with <A bytes of !#M",;+ Bytes of !AM " a serial port" two ,*5bit Timers " and =; $%# pins. The +-., family has many additions and improvements over the years and remains a most acclaimed tool for todays circuit designers. $BT83 introduced a ,* bit microcontroller +-:* in the year ,:+; . 3ater $BT83 introduced +-c,:* series of ,*5bit Microcontrollers for mainly industrial applications. Microchip" another company has introduced an +5bit Microcontroller P$C ,*C*< in the year ,:+..The =;5bit microcontrollers have been developed by $BM and Motorola. MPC .-. is a =;5 bit !$4C controller of Motorola. The <-= CA is a =; 5bit !$4C embedded controller of $BM. $n recent times A!M company 6Advanced !$4C machines7 has developed and introduced =; bit controllers for high5end application devices like mobiles " $pods etc... T0PES OF MICROCONTROLLERS: Microcontrollers can be classified on the basis of internal bus width" architecture" memory and instruction set as <5bit"+5bit",*5bit and =;5bit micrcontrollers. 12)it Microcontro""ers) These <5bit microcontrollers are small si>e" minimum pin count and low cost controllers which are widely used for low end applications like 38& D 3C& display drivers "portable battery chargers etc.. Their power consumption is also low. The popular <5bit controllers are !enasa M=<.-, which is a ;- pin &$P chip with <kB of !#M";.* Bytes of !AM";5Counters and ,< $%# Pins. 4imilarly ATAM+*; series from ATM83. 82)it Microcontro""ers: These are the most popular and widely used microcontrollers .About ..E of all CP s sold in the world are +5bit microcontrollers only. The +5bit microcontroller has +5bitinternal bus and the A3 performs all the arithmetic and logical operations on a byte instruction. The well known +5bit microcontroller is +-., which was designed by $ntel in the year ,:+- for the use in embedded systems. #ther +5bit microcontrollers are $ntel +-=,%+-.; and

<

Motorola MC*+FC,, and A2! Microcontrollers" Microchips P$C Microcontrollers ,;C.00 " ,*C.0 and ,*C.-. etc. 132)it Microcontro""ers : Ghen the microcontroller performs ,*5bit arithmetic and logical operations at an instruction" the microcontroller is said to be a ,*5bit microcontroller. The internal bus width of ,*5bit microcontroller is of ,*5bit. These microcontrollers are having increased memory si>e and speed of operation when compared to +5bit microcontrollers. These are most suitable for programming in Fighlevel languages like C or CHH .They find applications in disk drivers" modems" printers" scanners and servomotor control. 8(amples of ,*5bit microcontrollers are $ntel +-:* family and Motorola MC*+FC,; and MC*+==; families" The performance and computing capability of ,* bit microcontrollers are enhanced with greater precision as compared to the +5bit microcontrollers. +42!it Microcontro""ers :These microcontrollers used in highend applications like Automative control" Communication networks" !obotics" Cell phones "CP!4 D P&As etc..'or 80)P$C=;"A!M ?"A!M: "4FA!P 3F?:.;- "ATM83 =; 6A2!7 "Te(as $nstruments 9. TM4=;-';+-;(%;+-=( etc..are some of the popular =;5bit microcontrollers. COMMERCI5L MICROCONTROLLERS There are various manufacturers who are supplying various types of microcontrollers suitable for different applications depending on the power consumption and the available features..They are given below in tables . 'irst the various members of $BT83 ., family are given in below table. INTEL MCS 51 F$%i"6
Microcontro""e r On c(ip R5M 7!6tes8 On c(ip progr$% %e%or6 Ti%ers9Counters Interrupts Seri$" ports

+-=, +-=; +-., +-.; +?.,

,;+ ;.* ,;+ ;.* ,;+

Bone Bone <A !#M +A !#M <A 8P!#M

; = ; = ;

. * * * .

, , , , ,
.

+?.;

;.*

+A 8P!#M

The following table gives the <5bit microcontrollers from different manufacturers.

12!it Microcontro""ers.

82!it Microcontro""ers

132!it Microcontro""ers The following table gives the list of P$C microcontrollers from Micro chip $nc

Microcontroller

Pins

$%# 3ines

#n chip A&Cs

8P!#M 0 ,; words

#n chip !AM 6Bytes7 ;. ;< ;. ?; ;=; <.< <.< =* *?+

,*C.< ,*C.. ,*C.* ,*C.? ,?C<;A ,?C<= ,?C<< ,?C?, ,?C?.;

,+ ;+ ,+ ;+ <<<,+ <-

,; ;,; ;== == == ,= ==

Bone Bone Bone Bone Bone Bone Bone +bit A&Cs ,-Bit A&C

.,; .,; ,k ;k ;k <k +k ,k(,< +k(,*

MICROCONTROLLER

'E-ELOPMENT TOOLS:

To develop an assembly language program we need certain program development tools. An assembly language program consists of Mnemonics which are nothing but short abbreviated

8nglish instructions given to the controller.The various development tools reIuired for Microcontroller programming are e(plained below. 1 Editor : An 8ditor is a program which allows us to create a file containing the assembly language statements for the program. 8(amples of some editors are PC write Gordstar. As we type the program the editor stores the AC4$$ codes for the letters and numbers in successive !AM locations. $f any typing mistake is done editor will alert us to correct it. $f we leave out a program statement an editor will let you move everything down and insert a line. After typing all the program we have to save the program . This we call it as source file. The ne(t step is to process the source file with an assembler. 8() 4ample. asm 4 5sse%)"er : An Assembler is used to translate the assembly language mnemonics into

machine language6 i.e binary codes7. Ghen you run the assembler it reads the source file of your program from where you have saved it. The assembler generates a filee with the e(tension (e:. This file consists of he(adecimal values encoding a seIuence of data and their starting offset or absolute address. + Co%pi"er : A compiler is a program which converts the high level language program like @CJ into binary or machine code. sing high level languages it is easy to manage comple( data structures which are often reIuired for data manipulation. Because of its ease " fle(ibility and debug options now a days the compilers have become very popular in the market. Compilers like Aeil "!ide and $A! workbench are very popular. + 'e)ugger9Si%u"$tor : A debugger is a program which allows e(ecute the program" and

troubleshoot or debug it. The debugger allows to look into the contents of registers and memory locations after the program runs. Ge can also change the contents of registers and memory locations and rerun the program. 4ome debuggers allows to stop the program after each instruction so that you can check or alter memory and register contents. This is called single step debug. A debugger also allows to set a breakpoint at any point in the program. $f we insert a break point " the debugger will run the program up to the instruction where the breakpoint is put and then stop the e(ecution.

A simulator is a software program which virtually e(ecutes the instructions similar to a microcontroller and shows the results. This will help in evaluating the results without committing any errors. By doing so we can detect the possible logic errors INTEL 8051 MICRCONTROLLER : The +-., microcontroller is a very popular +5bit microcontroller introduced by $ntel in the year ,:+, and it has become almost the academic standard now a days. The +-., is based on an +5bit C$4C core with Farvard architecture. $ts +5bit architecture is optimi>ed for control applications with e(tensive Boolean processing. $t is available as a <-5pin &$P chip and works at H. 2olts &C. The salient features of +-., controller are given below. S5LIENT FE5T.RES : The salient features of +-., Microcontroller are i. < AB on chip program memory 6!#M or 8P!#M77. ii. ,;+ bytes on chip data memory6!AM7. iii. +5bit data bus iv. ,*5bit address bus v. =; general purpose registers each of + bits vi. Two 5,* bit timers T- and T, vii. 'ive $nterrupts 6= internal and ; e(ternal7. i(. 'our Parallel ports each of +5bits 6P#!T-" P#!T,"P#!T;"P#!T=7 with a total of =; $%# lines. (. #ne ,*5bit program counter and #ne ,*5bit &PT! 6 data pointer7 (i. #ne +5bit stack pointer (ii. #ne Microsecond instruction cycle with ,; MF> Crystal. (iii. #ne full duple( serial communication port. 5RC;ITECT.RE & !LOC< 'I5=R5M OF 8051 MICROCONTROLLER:

The architecture of the +-., microcontroller can be understood from the block diagram. $t has Farward architecture with !$4C 6!educed $nstruction 4et Computer7 concept. The block diagram of +-., microcontroller is shown in 'ig =. below,.$t consists of an +5bit A3 " one +5 bit P4G6Program 4tatus !egister7" A and B registers " one ,*5bit Program counter " one ,*5bit &ata pointer register6&PT!7",;+ bytes of !AM and <kB of !#M and four parallel $%# ports each of +5bit width.

Fig + !"oc# 'i$gr$% o& 8051 Microcontro""er +-., has +5bit A3 which can perform all the +5bit arithmetic and logical operations in one machine cycle. The A3 is associated with two registers A D B 5 $nd ! Registers ) The A and B registers are special function registers which hold the results of many arithmetic and logical operations of +-.,.The A register is also called the 5ccu%u"$tor and as its name suggests" is used as a general register to accumulate the results of a large number of instructions. By default it is used for all mathematical operations and also data transfer operations between CP and any e(ternal memory.

,-

The B register is mainly used for multiplication and division operations along with A register.
M 3 AB ) &$2 AB.

$t has no other function other than as a location where data may be stored. T(e R registers) The K!K registers are a set of eight registers that are named !-" !," etc. up to and including !?. These registers are used as au(illary registers in many operations. The K!K registers are also used to temporarily store values. Progr$% Counter7PC8 : +-., has a ,*5bit program counter .The program counter always points to the address of the ne(t instruction to be e(ecuted. After e(ecution of one instruction the program counter is incremented to point to the address of the ne(t instruction to be e(ecuted. $t is the contents of the PC that are placed on the address bus to find and fetch the desired instruction. 4ince the PC is ,*5bit width" +-., can access program addresses from ----F to ''''F "a total of *<kB of code. St$c# Pointer Register 7SP8: $t is an +5bit register which stores the address of the stack top. i.e the 4tack Pointer is used to indicate where the ne(t value to be removed from the stack should be taken from. Ghen a value is pushed onto the stack" the +-., first increments the value of 4P and then stores the value at the resulting memory location. 4imilarly when a value is popped off the stack" the +-., returns the value from the memory location indicated by 4P" and then decrements the value of 4P. 4ince the 4P is only +5bit wide it is incremented or decremented by two. 4P is modified directly by the +-., by si( instructions) P 4F" P#P" ACA33" 3CA33" !8T" and !8T$. $t is also used intrinsically whenever an interrupt is triggered. ST5C< in 8051 Microcontro""er : The stack is a part of !AM used by the CP to store

information temporarily. This information may be either data or an address. The CP needs this storage area as there are only limited number of registers. The register used to access the stack is called the 4tack pointer which is an +5bit register..4o"it can take values of -- to '' F.Ghen the +-., is powered up "the 4P register contains the value -?.i.e the !AM location value -+ is the first location being used for the stack by the +-., controller

,,

There are two important instructions to handle this stack.#ne is the P 4F and the #ther is the P#P. The loading of data from CP 80 ) M#2 !* "L=. F M#2 !, "L;, F P 4F !* P 4F !, $n the above instructions the contents of the !egisters !* and !, are moved to stack and they occupy the -+ and -: locations of the stack. Bow the contents of the 4P are incremented by two and it is -A 4imilarly P#P = instruction pops the contents of stack into != register. Bow the contents of the 4P is decremented by , $n +-., the !AM locations -+ to ,' 6;< bytes7 can be used for the 4tack. $n any program if we need more than ;< bytes of stack" we can change the 4P point to !AM locations =-5?' F. this can be done with the instruction M#2 4P"L 00. '$t$ Pointer Register 7'PTR8: $t is a ,*5bit register which is the only user5accessible. &PT!" as the name suggests" is used to point to data. $t is used by a number of commands which allow the +-., to access e(ternal memory. Ghen the +-., accesses e(ternal memory it will access e(ternal memory at the address indicated by &PT!. This &PT! can also be used as two +5registers &PF and &P3. Progr$% St$tus Register 7PS>8 : The +-., has a +5bit P4G register which is alsoknown as 'lag register.$n the +5bit register only *5bits are used by +-.,.The two unused bits are user definable bits.$n the *5bits four of them are conditional flags .They are Carry 9CM"Au(iliary Carry5AC" Parity5P"and #verflow5#2 .These flag bits after an instruction was e(ecuted. indicate some conditions that resulted registers to the stack is done by P 4F and the loading of the contents of the stack back into aCP register is done by P#P.

,;

The bits P4G= and P4G< are denoted as !4- and !4, and these bits are used th select the bank registers of the !AM location. The meaning of various bits of P4G register is shown below. CM AC '# !4, !4#2 555 P P4G.? P4G.* P4G.. P4G.< P4G.= P4G.; P4G., P4G.Carry 'lag Au(iliary Carry 'lag 'lag - available for general purpose . !egister Bank select bit , !egister bank select bit #verflow flag ser difinable flag Parity flag .set%cleared by hardware.

The selection of the register Banks and their addresses are given below. RS1 , , RS0 , , Register !$n# , ; = 5ddress --F5-?F -+F5-'F ,-F5,?F ,+F5,'F

Me%or6 org$ni?$tion : The +-., microcontroller has ,;+ bytes of $nternal !AM and <kB of on chip !#M .The !AM is also known as &ata memory and the !#M is known as program memory. The program memory is also known as Code memory .This Code memory holds the actual +-., program that is to be e(ecuted. $n +-., this memory is limited to *<A .Code memory may be found on5chip" as !#M or 8P!#M. $t may also be stored completely off5chip in an e(ternal !#M or" more commonly" an e(ternal 8P!#M. The +-., has only ,;+ bytes of $nternal !AM but it supports *<kB of e(ternal !AM. As the name suggests" e(ternal !AM is any random access memory which is off5chip. 4ince the memory is off5chip it is not as fle(ible interms of accessing" and is also slower. 'or e(ample" to increment an $nternal !AM location by
,=

,"it reIuires only , instruction and , instruction cycle but to increment a ,5byte value stored in 8(ternal !AM reIuires < instructions and ? instruction cycles. 4o" here the e(ternal memory is ? times slower. Intern$" R5M OF 8051 : This $nternal !AM is found on5chip on the +-., .4o it is the fastest !AM available" and it is also the most fle(ible in terms of reading" writing" and modifying its contents. $nternal !AM is volatile" so when the +-., is reset this memory is cleared. The ,;+ bytes of internal !AM is organi>ed as below. 6i7 'our register banks 6Bank-"Bank," Bank; and Bank=7 each of +5bits 6total =; bytes7. The default bank register is Bank-. The remaining Banks are selected with the help of !4- and !4, bits of P4G !egister. 6ii7 ,* bytes of bit addressable area and 6iii7 +- bytes of general purpose area 64cratch pad memory7 as shown in the diagram below. This area is also utili>ed by the microcontroller as a storage area for the operating stack. The =; bytes of !AM from address -- F to ,'F are used as working registers organi>ed as four banks of eight registers each.The registers are named as !-5!? .8ach register can be addressed by its name or by its !AM address. 'or 80 ) M#2 A" !? or M#2 !?"L-.F

,<

Intern$" ROM 7On @c(ip ROM8: The +-., microcontroller has <kB of on chip !#M but it can be e(tended up to *<kB.This !#M is also called program memory or code memory. The C#&8 segment is accessed using the program counter 6PC7 for opcode fetches and by &PT! for data. The e(ternal !#M is accessed when the 8A6active low7 pin is connected to ground or the contents of program counter e(ceeds -'''F.Ghen the $nternal !#M address is e(ceeded the +-., automatically fetches the code bytes from the e(ternal program memory.

,.

SPECI5L F.NCTION RE=ISTERS 7SFRs8: $n +-., microcontroller there certain registers which uses the !AM addresses from +-h to ''h and they are meant for certain specific operations .These registers are called 4pecial function registers 64'!s7.4ome of these registers are bit addressable also. The list of 4'!s and their functional names are given below. $n these 4'!s some of them are related to $%# ports 6P-"P,"P; and P=7 and some of them are meant for control operations 6TC#B"4C#B" PC#B..7 and remaining are the au(illary 4'!s" in the sense that they donNt directly configure the +-.,.

,*

P5R5LLEL I 9O PORTS : The +-., microcontroller has four parallel $%# ports" each of +5bits .4o" it provides the user =; $%# lines for connecting the microcontroller to the peripherals. The four ports are P- 6Port -7" P, 6Port,7" P; 6Port ;7 and P= 6Port=7. .pon reset $"" t(e ports $re output ports . $n order to make them input" all the ports must be set i.e a high bit must be sent to all the port pins. This is normally done by the instruction @48TBJ. 8() M#2 A" L-''F M#2 P-" A PORT 0: Port - is an +5bit $%# port with dual purpose. $f e(ternal memory is used" these port pins are used for the lower address byte address%data 6A&-5A&?7" otherwise all bits of the port are either input or output. nlike other ports" Port 0 is not proAided *it( pu""2up resistors intern$""6 " so for P#!T- pull5up resistors of nearly ,-k are to be connected e(ternally as shown in the fig.;. 'u$" ro"e o& port 0) Port - can also be used as address%data bus 6A&-5A&?7" allowing it to be used for both address and data. Ghen connecting the +-., to an e(ternal memory" port provides both address and data. The +-., multiple(es address and data through port - to save the pins. A38 indicates whether P- has address or data. >(en 5LE B 0" it provides d$t$ '02'," and when 5LE B1 it provides $ddress and data with the help of a ?<34=?= latch. O A P '' O make P- an input port

,?

Port 1: Port , occupies a total of + pins 6pins , through +7. $t has no dual application and acts only as input or output port. $n contrast to port -" this port does not need any pull5up resistors since pull5up resistors connected internally. pon reset" Port , is configured as an output port. To configure it as an input port" port bits must be set i.e. a high bit must be sent to all the port pins. This is normally done by the instruction @48TBJ.

Port 4: Port ; is also an eight bit parallel port. 6Pins ;,5 ;+7. $t can be used as input or output port. As this port is provided with internal pull5up resistors it does not need any e(ternal pull5up resistors. pon reset" Port ; is configured as an output port. $f the port is to be used as input port" all the port bits must be made high by sending '' to the port. 'u$" ro"e o& port 4) Port; lines are also associated with the higher order address lines A+5A,.. $n systems based on the +?.," +:.," and &4.---" Port; is used as simple $%# port. But" in +-=,5 based systems" port ; is used along with P- to provide the ,*5bit address for the e(ternal memory. 4ince an +-=, is capable of accessing *<A bytes of e(ternal memory" it needs a path for the ,* bits of the address. Ghile P- provides the lower + bits via A-5A?" it is the Qob of P; to provide bits A+5A,. of the address. $n other words" when +-=, is connected to e(ternal memory" Port ; is used for the upper + bits of the ,* bit address" and it cannot be used for $%# operations. PORT +) Port= is also an +5bit parallel port with dual function. 6Pins ,- to ,?7. The port pins can be used for $%# operation as well as for control operations. The details of these additional operations are given below in the table. Port = also do not need any e(ternal pull5up resistors as they are provided internally similar to the case of Port; D Port ,. pon reset port = is configured as an output port. $f the port is to be used as input port" all the port bits must be made high by sending '' to the port. 5"tern$te Functions o& Port + : P=.- and P=., are used for the !(& 6!eceive &ata7 and T(& 6Transmit &ata7 serial communications signals. Bits P=.; and P=.= are meant for e(ternal interrupts. Bits P=.< and P=.. are used for Timers - and , and P=.* and P=.? are used to provide the write and read signals of e(ternal memories connected in +-=, based systems

,+

S No , ; = < . * ? +

Port + )it P=.P=., P=.; P=.= P=.< P=.. P=.* P=.?

Pin No ,,, ,; ,= ,< ,. ,* ,?

Function !(& T(&

TT,

T$)"e: PORT + $"tern$te &unctions

Interrupt Structure) An interrupt is an e(ternal or internal event that disturbs the


microcontroller to inform it that a device needs its service. The program which is associated with the interrupt is called the interrupt serAice routine 6$4!7 or interrupt ($nd"er. pon receiving the interrupt signal the Microcontroller " finish current instruction and saves the PC on stack. Rumps to a fi(ed location in memory depending on type of interrupt 4tarts to e(ecute the interrupt service routine until !8T$ 6return from interrupt7 pon e(ecuting the !8T$ the microcontroller returns to the place where it was interrupted. Cet pop PC from stack The +-., microcontroller has FI-E interrupts in addition to !eset. They are Timer - overflow $nterrupt Timer , overflow $nterrupt 8(ternal $nterrupt -6$BT-7 8(ternal $nterrupt ,6$BT,7 4erial Port events 6buffer full" buffer empty" etc7 $nterrupt

8ach interrupt has a specific place in code memory where program e(ecution 6interrupt service routine7 begins. 8(ternal $nterrupt -) ---= F

,:

Timer - overflow)

---B F

8(ternal $nterrupt ,) --,= F Timer , overflow) 4erial $nterrupt ) --,B F --;= F

pon reset all $nterrupts are disabled D do not respond to the Microcontroller. These interrupts must be enabled by software in order for the Microcontroller to respond to them. This is done by an +5bit register called $nterrupt 8nable !egister 6$87. Interrupt En$)"e Register :

8A ) Clobal enable%disable. To enable the interrupts this bit must be set Figh. 555 ) ndefined5reserved for future use.

8T; ) 8nable %disable Timer ; overflow interrupt. 84 ) 8nable%disable 4erial port interrupt. 8T, ) 8nable %disable Timer , overflow interrupt. 80, ) 8nable%disable 8(ternal interrupt,. 8T- ) 8nable %disable Timer - overflow interrupt. 80- ) 8nable%disable 8(ternal interruptpon reset the interrupts have the following priority.6Top to down7. The interrupt with the highest P!$#!$TM gets serviced first. ,. 8(ternal interrupt - 6$BT-7 ;. Timer interrupt- 6T'-7 =. 8(ternal interrupt , 6$BT,7 <. Timer interrupt, 6T',7

;-

.. 4erial communication 6!$HT$7 Priority can also be set to @highJ or @lowJ by +5bit $P register.5 $nterrupt priority register

IP.7: reserved $P.*) reserved $P..) Timer ; interrupt priority bit 6+-.; only7 $P.<) 4erial port interrupt priority bit $P.=) Timer , interrupt priority bit $P.;) 8(ternal interrupt , priority bit $P.,) Timser - interrupt priority bit $P.-) 8(ternal interrupt - priority bit

TIMERS in 8051 Microcontro""ers:


The +-., microcontroller has two ,*5bit timers Timer - 6T-7 and Timer ,6T,7 which can be used either to generate accurate time delays or as event counters. These timers are accessed as two +5 bit registers T3#" TF# D T3," TF, because the +-., microcontroller has +5bit architecture. TIMER 0 : The Timer - is a ,*5bit register and can be treated as two +5bit registers 6T3- D TF-7 and these registers can be accessed similar to any other registers like A" B or !,"!;"!= etc1 8() The instruction Mov T3-" L-? moves the value -? into lower byte of Timer-. 4imilarly Mov !." TF- saves the contents of TF- in the !. register.

;,

TIMER 1 : The Timer , is also a ,*5bit register and can be treated as two +5bit registers 6T3, D TF,7 and these registers can be accessed similar to any other registers like A"B or !,"!;"!= etc1 8() The instruction M#2 T3," L-. moves the value -. into lower byte of Timer,. 4imilarly M#2 !-" TF, saves the contents of TF, in the !- register

TMO' Register: The various operating modes of both the timers T- and T, are set by an +5bit register called TM#& register $n this TM#& register the lower <5bits are meant for Timer - and the higher <5bits are meant for Timer,.

=5TE) This bit is used to start or stop the timers by hardware .Ghen CAT8P ," the timers can be started % stopped by the e(ternal sources. Ghen CAT8P -" the timers can be started or stopped by software instructions like 48TB T!- or 48TB T!, C9T 7c"oc#9Ti%er8: This bit decides whether the timer is used as delay generator or event counter. Ghen C9T B 0C the Timer is used as de"$6 gener$tor and if C9TB1 the timer is used as an eAent counter. The clock source for the time delay is the crystal freIuency of +-.,.

;;

M1C M0 7Mode8: These two bits are the timer mode bits. The timers of the +-., can be configured in three modes.Mode-" Mode, and Mode;.The selection and operation of the modes is shown below.

M0 -

M1 -

Mode

Oper$tion ,=5bit Timer mode +5bit Timer%counter. TF( with T3( as .5bit prescalar ,*5bit Timer mode.,*5bit timer %counter without pre5scalar +5bit auto reload. TF( contains a value that is to be loaded into T3( each time it overflows 4plit timer mode.

, ,

, ,

, ; =

PIN 'i$gr$% o& 8051 Microcontro""er: The +-., microcontroller is available as a <- pin &$P chip and it works at H. volts &C. Among the <- pins " a total of =; pins are allotted for the four parallel ports P-"P,"P; and P= i.e. each port occupies +5pins .The remaining pins are 2CC" CB&" 0TA3," 0TA3;" !4T" 8A "P48B. DT5L1C DT5L4) These two pins are connected to Suart> crystal oscillator which runs the on5 chip oscillator. The Iuart> crystal oscillator is connected to the two pins along with a capacitor of =-p' as shown in the circuit. $f we use a source other than the crystal oscillator" it will be connected to 0TA3, and 0TA3; is left unconnected.

;=

RST) The !848T pin is an input pin and it is an active high pin. Ghen a high pulse is applied to this pin the microcontroller will reset and terminate all activities. pon reset all the registers e(cept PC will reset to ---- 2alue and PC register will reset to ---? value.

;<

7E:tern$" 5ccess8: This pin is an active low pin. This pin is connected to ground when microcontroller is accessing the program code stored in the e(ternal memory and connected to 2cc when it is accessing the program code in the on chip memory. This pin should not be left unconnected.

7Progr$% Store En$)"e8 : This is an output pin which is active low. Ghen the
microcontroller is accessing the program code stored in the e(ternal !#M "this pin is connected to the #8 6#utput 8nable7 pin of the !#M. 5LE 75ddress "$tc( en$)"e8: This is an output pin" which is active high Ghen connected to e(ternal memory " port - provides both address and data i.e. address and data are multiple(ed through port - .This A38 pin will demultiple( the address and data bus .Ghen the pin is Figh " the A& bus will act as address bus otherwise the A& bus will act as &ata bus. P0 02 P0 ,75'025',8 : The port - pins multiple(ed with Address%data pins .$f the microcontroller is accessing e(ternal memory these pins will act as address%data pins otherwise they are used for Port - pins. P4 02 P4 ,75825158 : The port; pins are multiple(ed with the higher order address pins Ghen the microcontroller is accessing e(ternal memory these pins provide the higher order address byte otherwise they act as Port ; pins. P1 02 P1 , :These +5pins are dedicated for Port, to perform input or output port operations. P+ 02 P+ , :These +5pins are meant for Port= operations and also for some control operations like !ead"Grite"Timer-"Timer, "$BT-"$BT, "!(& and T(& 5''RESSIN= MO'ES OF 8051 : The way in which the data operands are accessed by different instructions is known as the addressing modes. There are various methods of denoting the data operands in the instruction. The +-., microcontroller supports mainly . addressing modes. They are ,.$mmediate addressing mode ;.&irect Addressing mode

;.

=.!egister addressing mode <. !egister $ndirect addressing mode ..$nde(ed addressing mode I%%edi$te $ddressing %ode : The addressing mode in which the data operand is a constant and it is a part of the instruction itself is known as $mmediate addressing mode. Bormally the data must be preceded by a L sign. This addressing mode can be used to transfer the data into any of the registers including &PT!. 8() M#2 A " L ;? F ) The data 6constant7 ;? is moved to the accumulator register A&& !, "L<. F ) Add the constant <. to the contents of the accumulator M#2 &PT! "L +;<.F )Move the data +;<. into the data pointer register. M#2 P,"L;, F 'irect $ddressing %ode) The addressing mode in which the data operand is in the !AM location 6-- 5?'F7 and the address of the data operand is given in the instruction is known as &irect addressing mode. The direct addressing mode uses the lower ,;+ bytes of $nternal !AM and the 4'!s M#2 !," <;F ) Move the contents of !AM location <; into !, register M#2 <:F"A ) Move the contents of the accumulator into the !AM location <:. A&& A" .*F ) Add the contents of the !AM location .* to the accumulator Register $ddressing %ode )The addressing mode in which the data operand to be manipulated lies in one of the registers is known as register addressing mode. M#2 A"!- ) Move the contents of the register !- to the accumulator A&& A"!* )Add the contents of !* register to the accumulator M#2 P," !; ) Move the contents of the !; register into port , M#2 !." !; ) This is invalid .The data transfer between the registers is not allowed.

;*

Register Indirect $ddressing %ode : The addressing mode in which a register is used as a pointer to the data memory block is known as !egister indirect addressing mode. M#2 A"T !- )Move the contents of 6accumulator7 M#2 T !, " B ) Move the contents of B into !AM location whose address is held by !, Ghen !- and !, are used as pointers" they must be preceded by T sign One o& t(e $dA$nt$ges o& register indirect $ddressing %ode is t($t it %$#es $ccessing t(e d$t$ %ore d6n$%ic t($n st$tic $s in t(e c$se o& direct $ddressing %ode Inde:ed $ddressing %ode : This addressing mode is used in accessing the data elements of lookup table entries located in program !#M space of +-.,. 8( ) M#2C A"T AH&PT! The ,*5bit register &PT! and register A are used to form the address of the data element stored in on5chip !#M. Fere C denotes code .$n this instruction the contents of A are added to the ,*5bit &PT! register to form the ,*5bit address of the data operand. Inter&$cing o& 5'C 0801 to 8051 Microcontro""er : A&C -+-< is a single channel analog to digital converter i.e." it can take only one analog !AM location whose address is in !- into 5

signal. A&C -+-< has + bit resolution. The higher resolution A&C gives smaller step si>e. 4tep si>e is smallest change that can be measured by an A&C. 'or an A&C with resolution of + bits" the step si>e is ,:..=m2 6.2%;..7. The time taken by the A&C to convert analog data into digital form depends on the freIuency of clock source. The conversion time of A&C -+-< is around ,,-us. To use the internal clock a capacitor and resistor are used as shown in the circuit. The input to the A&C is given from a regulated power supply and a ,-A potentiometer
The 8051 Microcontroller is used to provide the control signals to the ADC. C !chip select" pin o# ADC is directl$ connected to ground. The pin P1.1% P1.0 and P1.& are connected to the pin '(% (D and I)T( o# the ADC respectivel$. 'hen the input voltage #ro* the preset is varied the output o# ADC varies also varies.

;?

'rom the circuit it is clear that the A&C interfaced directly to the microcontroller. The Port, is used as an input port which receives the digital data from the A&C.Port pins P;.. and P;.* are used for 4#C and 8#C operation.Ghen the conversion is over the A&C will send an interrupt signal to the microcontroller through the pin P;.? .Bow the Microcontroller receives digital data through the Port,.This data after conversion to decimal data is displayed on the 3C& module .
The assembly language program for A&C is given below . M#2 P, " -'' F O Make the port, high and configure port, as $nput port !5C<) C3! P;.* 48TB P;.. LOOP RB P;.? " 3##P O Ceneration of 4#C pulse O O Gait for conversion" $s conversion overU

;+

C3! P;..

O 8nable !ead the digital data

M#2 A "P, 48TB P;..

O !ead digital data through Port, O &isable read after read operation

CA33 &$4P3AM O &isplay the data on 3C& module 4RMP BACA O Continue the conversion process

Stepper %otor Inter&$cing


A stepper motor is a device that translates electrical pulses into mechanical movement. The stepper motor rotates in steps in response to the applied signals. $t is used in applications such as disk drives" dot matri( printers" plotters and robotics.$t is mainly used for position control. 4tepper motors have a permanent magnet called rotor 6also called the shaft7 surrounded by a stator . There are also steppers called variable reluctance stepper motors that do not have a PM rotor. The most common stepper motors have four stator windings that are paired with a center5 tapped. This type of stepper motor is commonly referred to as a. four5phase or unipolar stepper motor. The center tap allows a change of current direction in each of two coils when a winding is grounded" thereby resulting in a polarity change of the stator.

;:

5SSEM!L0 L5N=.5=E PRO=R5M Main mov stepper" L-CF O move the code to phase, into the port acall delay mov stepper" L-*F O phase $$ code acall delay mov stepper" L-=F acall delay mov stepper" L-:F acall delay sQmp Main CA33 &83AM P!#C!AM ) mov r?"L< wait;) mov r*"L-''F wait,) mov r."L-''F wait) dQn> r."wait dQn> r*"wait, dQn> r?"wait; ret end OPhase $$$ code OCall delay subroutine program OPhase $2 code

=-

80512SERI5L COMM.NIC5TION : !$sics o& Seri$" co%%unic$tion


&ata transfer between two electronic devices 68( Between a computer and microcontroller or a peripheral device7 is generally done in two ways 6i7.4erial data Transfer 6ii7.Parallel data Transfer 4erial communication uses only one or two data lines to transfer data and is generally used for long distance communication. $n serial communication the data is sent as one bit at a time in a timed seIuence on a single wire. 4erial Communication takes place in two methods" Asynchronous data Transfer and 4ynchronous data Transfer. and

Seri$" '$t$ Tr$ns&er 5s6nc(ronous d$t$ tr$ns&er allows data to be transmitted without the sender having to send a clock signal to the receiver. $nstead" special bits will be added to each word in order to synchroni>e the sending and receiving of the data. Ghen a word is given to the A!T for Asynchronous transmissions" a bit called the K4tart BitK is added to the beginning of each word that is to be transmitted. The 4tart Bit is used to alert the receiver that a word of data is about to be sent" and to force the clock in the receiver into synchroni>ation with the clock in the transmitter.

=,

Seri$" d$t$ tr$ns%ission

After the 4tart Bit" the individual bits of the word of data are sent .Fere each bit in the word is transmitted for e(actly the same amount of time as all of the other bits. Ghen the entire data word has been sent" the transmitter may add a Parity Bit that the transmitter generates. The Parity bit may be used by the receiver to perform simple error checking. Then at least one 4top Bit is sent by the transmitter. $f the 4top Bit does not appear when it is supposed to" the considers the entire word to be corrupted and will report a 'raming 8rror. Baud rate is a measurement of transmission speed in asynchronous communication " it represents the number of bits%sec that are actually being sent over the serial link. The Baud count includes the overhead bits 4tart" 4top and Parity that are generated by the sending A!T and removed by the receiving A!T. $n the S6nc(ronous d$t$ tr$ns&er method the receiver knows when to @readJ the ne(t bit coming from the sender. This is achieved by sharing a clock between sender and receiver. $n most forms of serial 4ynchronous communication" if there is no data available at a given time to transmit" a fill character will be sent instead so that data is always being transmitted. 4ynchronous communication is usually more efficient because only data bits are transmitted between sender and receiver" however it will be more costly because e(tra wiring and control circuits are reIuired to share a clock signal between the sender and receiver. &evices that use serial cables for their communication are split into two categories. ,. &T8 6&ata Terminal 8Iuipment7. 8(amples of &T8 are computers" printers D terminals. ;. &C8 6&ata Communication 8Iuipment7. 8(ample of &C8 is modems. P$r$""e" '$t$ Tr$ns&er : Parallel communication uses multiple wires 6bus7 running parallel to each other" and can A!T

transmit data on all the wires simultaneously. i.e all the bits of the byte are transmitted at a time.

=;

4o" speed of the parallel data transfer is e(tremely high compared to serial data transfer. An +5bit parallel data transfer is +5times faster than serial data transfer. Fence with in the computer all data transfer is mainly based on Parallel data transfer. But only limitation is due to the high cost "this method is limited to only short distance communications. 'i&&erences )et*een Seri$" d$t$ tr$ns&er $nd P$r$""e" d$t$ tr$ns&er S No , ; = < .. Seri$" Co%%unic$tion P$r$""e" Co%%unic$tion

&ata is transmitted bit after the bit in a &ata is transmitted simultaneously single line through group of lines6Bus7 &ata congestion takes place 3ow speed transmission Bo" &ata congestion Figh speed transmission are easily

$mplementation of serial links is not an Parallel data links easy task. implemented in hardware $n terms of transmission channel costs $t is more e(pensive such as data bus cable length" data bus buffers" interface connectors" it is less e(pensive Bo " crosstalk problem

* ?

Crosstalk creates interference between the parallel lines.

Bo effect of inter symbol interference and Parallel ports suffer e(tremely from noise inter5symbol interference 6$4$7 and noise" and therefore the data can be corrupted over long distances. The bandwidth of serial wires is much The bandwidth of parallel wires is much higher. lower. 4erial interface is more fle(ible to Parallel data transfer mechanism rely on upgrade % without changing the hardware hardware resources and hence not fle(ible to upgrade. 4erial communication work effectively Parallel buses are hard to run at high even at high freIuencies. freIuencies.

,-

SERI5L COMM.NIC5TION IN 8051 MICROCONTROLLER


==

The +-., has two pins for transferring and receiving data by serial communication. These two pins are part of the Port=6P=.- DP=.,7 .These pins are TT3 compatible and hence they reIuire a line driver to make them !4;=; compatible .Ma(;=; chip is one such line driver in use. 4erial communication is controlled by an +5bit register called 4C#B register"it is a bit addressable register. SCON 7Seri$" contro"8 register :

4M4M, 4M;

4C#B.? 4C#B.* 4C#B..

4erial port mode selector 4erial port mode selector sed for multiprocessor mode communication 6not applicable for +-.,7

!8B

4C#B.<

!eceive enable. 4et or cleared by making this bit either , or - foe enable %disable reception.

TB+ !B+

4C#B.= 4C#B.;

:th data bit transmitted in modes ; and = :th data bit received in modes ; and =.it is not used in mode - D mode ,.$f 4M; P - !B+ is the stop bit .

T$ !$

4C#B., 4C#B.-

Transmit interrupt flag !eceive interrupt flag.

M-

" 4M, ) These two bits of 4C#B register determine the framing of data by specifying the number of bits per character and start bit and stop bits. There are < serial modes. 4M4M, ) 4erial Mode -

=<

, ,

, ,

) 4erial Mode ," + bit data" ) 4erial Mode ; ) 4erial Mode =

, stop bit" , start bit

!8B 6!eceive 8nable7 also referred as 4C#B.<. Ghen it is high"it allows the +-., to receive data on the !(& pin. 4o to receive and transfer data !8B must be set to ,.Ghen !8BP-"the receiver is disabled. This is achieved as below 48TB 4C#B.< D C3! 4C#B.<

T$ 6Transmit interrupt7 is the &, bit of 4C#B register. Ghen +-., finishes the transfer of +5bit character" it raises the T$ flag to indicate that it is ready to transfer another byte. The T$ bit is raised at the beginning of the stop bit. !$ 6!eceive interrupt7 is the &- bit of the 4C#B register. Ghen the +-., receives data serially "via !(&" it gets rid of the start and stop bits and places the byte in the 4B ' register. Then it raises the !$ flag bit to indicate that a byte has been received and should be picked up before it is lost. !$ is raised halfway through the stop bit. Co%%unic$tion t(roug( RS4+4
A personal computer has a serial port known as communication port or C#M Port used to connect a modem for e(ample or any other device" there could be more then one C#M Port in a PC. 4erial ports are controlled by a special chip called A!T 6 niversal Asynchronous !eceiver Transmitter7.

!4 ;=; standard describes a communication method where information is sent bit by bit on a physical channel. The RS st$nds &or Reco%%ended St$nd$rd.The information must be broken up in data words. The length of a data word is variable. $t is one of the popularly known interface standard for serial communication between &T8 D &C8. This !45;=;5C is the commonly used standard when data are transmitted as voltage .This standard was first developed by 8lectronic industries association68$A7. 'or the !45;=;C" a ;. pin & type connector is used . &B5;.P male and

=.

&B5;.4 female. !45;=; standard was first introduced in ,:*-s by Telecommunications $ndustry Association6T$A7. Inter&$cing t(e 8051 Microcontro""er to PC: As the !45;=; standard is developed earlier to TT3 devices "a 4A!T such as +;., is not

directly compatible with these signal levels .Because of this "voltage transistors called line drivers and line receivers are used to interface TT3 logic with !45;=; signals . The line driver MC ,<++ is used to convert !45;=; to TT3.The microcontroller is connected to the PC using the &B: connector.

The T(& and !( & pins are connected to the T$ in and !$ in pins of the MA0 ;=; $C and the T$ out and !$ in pins of the MA0 $C are connected to the !(& and T(& pins of the &B: connector as shown in the interface diagram.

INTERF5CIN= 'C MOTOR2 8051

=*

A &C motor runs with the help of &irect Current. $t produces torIue by using both electricity and magnetic fields. The &C motor has rotor" stator" field magnet" brushes" shaft" commutator. The &C motor reIuires more current to produce initial torIue than in running state.$nterfacing the &C motor directly to +-., microcontroller is not possible. Because the &C motor uses large current 6;--5=--mA in small &C motors7 to run. Ghen this current flow into the +-., microcontroller" the $C will get damaged. Therefore we use a driving circuit with an opto isolator and a +&,8
Dual -./ridge driver. The opto.isolator provides additional protection to the *icrocontroller.

Continuous" sustained operation of the motor will cause the 3;:= &ual F5Bridge driver to overheat. 4o"a suitable heat sink must be used.

=?

5sse%)"6 L$ngu$ge progr$%

OR= M5IN

0000; CLR CLR CLR SET! P1 0 P1 1 P1 4 P4 ,

Re%$r#s

MONITOR SET! EN! CLR SET! SEMP CLOC<>ISE SET! CLR P1 4 SEMP MONITOR P1 0 P4 , CLOC<>ISE P1 1 P1 4 MONITOR P1 1 10 is &or c"oc#*ise 01 is &or Counter c"oc#*ise En$)"e t(e ;2)ridge driAer

INTERF5CIN= '5C 28051 MICROCONTROLLER


The DAC 0800 is a si*ple *onolithic 8.0it D1A converter. It has #ast settling ti*e o# 100ns. It can 0e directl$ inter#aced to TT+% CM2 % PM2 and others. It operates at 3.54 to 5184 suppl$. The nu*0er o# data 0it inputs decides the resolution o# the DAC since the nu*0er o# analog output levels is e6ual to & % 7here n is the nu*0er o# data 0it inputs. There#ore% an 8.input DAC such as the DAC0808 provides &58 discrete voltage !or current" levels o# output.

The interfacing circuit is shown below. port ,6+ bits of the microcontroller is connected to the input data lines of &AC5-+.The reference current is determined by the resistor ! , and the reference voltage 2 ref. The resistor !; is generally eIual to !, to match the input impedance of

=+

reference source. The output 6taken from pin number < is observed either on a digital multimeter or on a cathode ray oscilloscope.
The output current Io is calculated as #ollo7s:

$o P 2ref%!,VAo%; H A,%< H A;%+ H 1 HA?%;.*W


The output voltage 4o is o0tained as #ollo7s: 4o 9Io : (1

5sse%)"6 L$ngu$ge Progr$%


M24 TA(T : M24 I)C A +=MP TA(T < (epeat A% ;DATA: ,0-% A < !A" 9 ;Data < !port .1" 9 !A"

555555555555555555555(((555555555555555555555

=:

REFERENCE ) Muhammed Ali Ma>idi" Ranice Cillies Pie Ma>idi" @The +-., Microcontroller and 8mbedded 4ystemsJX Pearson 8ducationAsia.

<-

You might also like