Microcontroller 8051

5,151 views
4,968 views

Published on

Published in: Technology, Business
0 Comments
14 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,151
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide
  • Program memory – RAM or ROM? Data memory – RAM or ROM?
  • Microcontroller 8051

    1. 1. MICROCONTROLLER-8051MICROCONTROLLER-8051Features & ApplicationsFeatures & ApplicationsDr. Y .Narasimha Murthy Ph.D.,Dr. Y .Narasimha Murthy Ph.D.,Sri Saibaba National CollegeSri Saibaba National College(Autonomous)(Autonomous)ANANTAPUR-515001(A.P)ANANTAPUR-515001(A.P)
    2. 2. …….... Man’s glory lies in his knowledge,Man’s glory lies in his knowledge,his upright conduct, his praise-worthy character,his upright conduct, his praise-worthy character,his wisdom, and not in his nationality or rankhis wisdom, and not in his nationality or rank--Baha’ullah--Baha’ullah(From, the book The 8051 Microcontroller and Embedded systems- Mazidi )(From, the book The 8051 Microcontroller and Embedded systems- Mazidi )
    3. 3. OverviewOverviewIntroductionIntroductionBlock Diagram andBlock Diagram and PinPinDescription of the 8051Description of the 8051RegistersRegistersMemory mapping in 8051Memory mapping in 8051Stack in the 8051Stack in the 8051I/O Port ProgrammingI/O Port ProgrammingTimersTimersInterrupts &Interrupts &ApplicationsApplications
    4. 4.  Its not an exaggeration if I say that ,todaythere is no electronic gadget on the earthwhich is designed without a Microcontroller.Ex: communication devices, digitalentertainment, portable devices etc…Not believable ??? See the next slideWhy do we need to learnWhy do we need to learnMicrocontrollers ?Microcontrollers ?
    5. 5.  Personal information products: Cell phone, pager,Personal information products: Cell phone, pager,watch, pocket recorder, calculatorwatch, pocket recorder, calculator Laptop components: mouse, keyboard, modem,Laptop components: mouse, keyboard, modem,fax card, sound card, battery chargerfax card, sound card, battery charger Home appliances: door lock, alarm clock,Home appliances: door lock, alarm clock,thermostat, air conditioner, TV remote, VCR,thermostat, air conditioner, TV remote, VCR,small refrigerator, exercise equipment,small refrigerator, exercise equipment,washer/dryer, microwave ovenwasher/dryer, microwave oven Industrial equipment: Temperature/pressureIndustrial equipment: Temperature/pressurecontrollers, Counters, timers, RPM Controllerscontrollers, Counters, timers, RPM Controllers Toys: video games, cars, dolls, etc.Toys: video games, cars, dolls, etc.
    6. 6. So, A good designer should alwaysknow what type of controller he/sheis using ,their architecture, advantages ,disadvantages , ways to reduceproduction costs and product reliabilityetc….O.K ????O.K ????
    7. 7. Then What is a Microcontroller ? A smaller computer On-chip RAM, ROM, I/O ports... Example : Motorola’s 6811, Intel’s 8051,Zilog’s Z8 and PIC 16XRAM ROMI/OPortTimerSerialCOMPortCPUA single chipMicrocontroller
    8. 8. How is it different from aHow is it different from aMicroprocessor ??Microprocessor ?? General-purpose microprocessorCPU for ComputersNo RAM, ROM, I/O on CPU chip itselfExample : Intel’s x86, Motorola’s 680x0CPUGeneral-PurposeMicro-processorRAM ROM I/OPortTimerSerialCOMPortData BusAddress Bus
    9. 9. Microprocessor CPU is stand-alone, RAM,ROM, I/O, timer are separate designer can decide on theamount of ROM, RAM andI/O ports. expansive versatility general-purposeMicrocontroller• CPU, RAM, ROM, I/O andtimer are all on a single chip• fix amount of on-chip ROM,RAM, I/O ports• Highly bit addressable• for applications in which cost,power and space are critical• single-purposeMicroprocessor vs. Microcontroller
    10. 10. EVOLUTIONEVOLUTIONFlashback !!!!Flashback !!!!In the year 1976, Motorola created a MicroprocessorIn the year 1976, Motorola created a Microprocessorchip called 6801 which replaced its brother 6800 withchip called 6801 which replaced its brother 6800 withcertain add-on chips to make a computer. This pavedcertain add-on chips to make a computer. This pavedthe way for the new revolution in the history of chipthe way for the new revolution in the history of chipdesign and gave birth to a new entity calleddesign and gave birth to a new entity calledMICROCONTROLLER.MICROCONTROLLER.The INTEL bagged the credit of producing the firstThe INTEL bagged the credit of producing the firstMicrocontroller 8048 with a CPU and 1K bytes ofMicrocontroller 8048 with a CPU and 1K bytes ofEPROM, 64 Bytes of RAM an 8-Bit Timer and 27 I/OEPROM, 64 Bytes of RAM an 8-Bit Timer and 27 I/Opins in 1976.pins in 1976.
    11. 11. EvolutionEvolution contdcontd…… Then followed the most popular controller 8051Then followed the most popular controller 8051in the year 1980 with 4K bytes of ROM,128in the year 1980 with 4K bytes of ROM,128Bytes ofBytes of RAMRAM , a serial port, two 16-bit Timers ,, a serial port, two 16-bit Timers ,and 32 I/O pins.and 32 I/O pins. The 8051 family has many additions andThe 8051 family has many additions andimprovements over the years and remains aimprovements over the years and remains amost soughtafter tool for todays circuitmost soughtafter tool for todays circuitdesigners.designers. The same INTEL introduced a 16 bit controllerThe same INTEL introduced a 16 bit controller8096 in the year 19828096 in the year 1982
    12. 12.  Later INTEL introduced 80c196 series of 16-bitLater INTEL introduced 80c196 series of 16-bitmicrocontrollers for mainly industrialmicrocontrollers for mainly industrialapplicationsapplications Microchip, another company has introduced aMicrochip, another company has introduced amicrocontroller PIC 16C64 an 8-bit in the yearmicrocontroller PIC 16C64 an 8-bit in the year19851985.. 32-bit microcontrollers have been developed by IBM32-bit microcontrollers have been developed by IBMand Motorola-MPC 505 is a 32-bit RISC controller ofand Motorola-MPC 505 is a 32-bit RISC controller ofMotorolaMotorola The 403 GA is a 32 -bit RISC embedded controller ofThe 403 GA is a 32 -bit RISC embedded controller ofIBMIBM
    13. 13. ARM ControllersARM Controllers In recent times ARM company (Advanced RiscIn recent times ARM company (Advanced Riscmachines) has developed and introduced 32 bitmachines) has developed and introduced 32 bitcontrollers which are highend applicationcontrollers which are highend applicationdevices,especially communication devices likedevices,especially communication devices likemobiles , ipods etc..(Refer www.arm.com)mobiles , ipods etc..(Refer www.arm.com)
    14. 14. Types of MicrocontrollersTypes of Microcontrollers
    15. 15. Microcontrollers from different manufacturersMicrocontrollers from different manufacturers•Atmel•ARM•Intel•8-bit•8XC42•MCS48•MCS51•8xC251•16-bit•MCS96•MXS296•National Semiconductor•COP8•Microchip•12-bit instruction PIC•14-bit instruction PIC•PIC16F84•16-bit instruction PIC•NEC•Motorola•8-bit•68HC05•68HC08•68HC11•16-bit•68HC12•68HC16•32-bit•683xx•Texas Instruments•TMS370•MSP430•Zilog•Z8•Z86E02
    16. 16. MCS-51 “Family” of MicrocontollersMCS-51 “Family” of MicrocontollersFeature 8031 8051 8052 8751Feature 8031 8051 8052 8751ROMROM NONO 4kB 8kB 4kB UV Eprom4kB 8kB 4kB UV EpromRAM (Bytes) 128 128 256 128RAM (Bytes) 128 128 256 128TIMERS 2 2 3 2TIMERS 2 2 3 2I/O PINS 32 32 32 32I/O PINS 32 32 32 32SERIAL PORTS 1 1 1 1SERIAL PORTS 1 1 1 1INTERRUPTINTERRUPT 6 6 8 66 6 8 6SOURCESSOURCES
    17. 17. Microcontroller ArchitecturesMicrocontroller ArchitecturesCPUProgram+ DataAddress BusData BusMemoryVon NeumannArchitectureCPUProgramAddress BusData BusHarvardArchitectureMemoryDataAddress BusFetch Bus0002n
    18. 18. Important Features of 8051Important Features of 8051 4K bytes ROM4K bytes ROM 128 bytes RAM128 bytes RAM Four 8-bit I/O portsFour 8-bit I/O ports Two 16-bit timersTwo 16-bit timers Serial interfaceSerial interface 64K external code memory space64K external code memory space 64K data memory space64K data memory space
    19. 19. ““Original” 8051 MicrocontrollerOriginal” 8051 MicrocontrollerOscillatorand timing4096 BytesProgram Memory(ROM)128 BytesData Memory(RAM)Two 16 BitTimer/EventCounters8051CPU64 K Byte BusExpansionControlProgrammableI/OProgrammableSerial Port FullDuplex UARTSynchronous ShifterInternal data busExternal interruptssubsystem interruptsControl Parallel portsAddress Data BusI/O pinsSerial InputSerial Output
    20. 20. Pin Description of the 8051Pin Description of the 8051 The 8051 is a 40 pinThe 8051 is a 40 pindevice, but out ofdevice, but out ofthese 40 pins, 32 arethese 40 pins, 32 areused for I/O.used for I/O. 24 of these are dual24 of these are dualpurpose, i.e. they canpurpose, i.e. they canoperate as I/O or aoperate as I/O or acontrol line or as partcontrol line or as partof address or dateof address or datebus.bus.
    21. 21. 8051 CPU Registers8051 CPU RegistersA (8-bit Accumulator)B (8-bit register for Mul &Div)PSW (8-bit Program Status Word)SP (8-bit Stack Pointer)PC (16-bit Program Counter)DPTR (16-bit Data Pointer)
    22. 22. Special Function RegistersSpecial Function RegistersDATA registersCONTROL registers•Timers•Serial ports•Interrupt system•Analog to Digital converter•Digital to Analog converteretc..Addresses 80h – FFhDirect Addressing is used toaccess SFRs
    23. 23. List of RegistersList of Registers(*Denotes the SFRs)(*Denotes the SFRs)
    24. 24. Contd…Contd…
    25. 25. PSW REGISTERPSW REGISTER
    26. 26. Memory mapping in 8051Memory mapping in 8051ROM memory map in 8051 family0000H0FFFH0000H1FFFH0000H7FFFH805187524kDS5000-328k 32kfrom AtmelCorporationfrom DallasSemiconductor
    27. 27. RAM memory space allocation in the 80517FH30H2FH20H1FH17H10H0FH07H08H18H00HRegister Bank 0)Stack) Register Bank 1Register Bank 2Register Bank 3Bit-Addressable RAMScratch pad RAM
    28. 28. PORTS OF 8051PORTS OF 8051 8051 has 4 Ports. Port 0, Port1, Port2 , Port38051 has 4 Ports. Port 0, Port1, Port2 , Port3 Port 0 is a dual purpose port, it is located from pin 32Port 0 is a dual purpose port, it is located from pin 32to pin 39 (8 pins). To use this port as bothto pin 39 (8 pins). To use this port as bothinput/output ports each pin must be connectedinput/output ports each pin must be connectedexternally to a 10 k ohm pull-up resistor.This isexternally to a 10 k ohm pull-up resistor.This isbecause Port 0 is an open drain.because Port 0 is an open drain.Simple ex:Simple ex: MOV A, #22MOV A, #22BACKBACK MOV P0 ,AP0 ,AACALL DELAYACALL DELAYCPL ACPL ASJMP BACKSJMP BACK
    29. 29. Port 0 with Pull-Up ResistorsPort 0 with Pull-Up Resistors
    30. 30.  Port 1 is a dedicated I/O port from pin 1 topin 8.Upon reset it is configured as outport.It is generally used for interfacing toexternal device thus if you need to connectto switches or LEDs, you could make use ofthese 8 pins,but it doesn’t need any pull-upresistors as it is having internally Like port 0, port 2 is a dual-purpose port.(Pins 21Like port 0, port 2 is a dual-purpose port.(Pins 21through 28) It can be used for general I/O or asthrough 28) It can be used for general I/O or asthe high byte of the address bus for designs withthe high byte of the address bus for designs withexternal code memoryexternal code memory.Like P1 ,Port2 also doesn’t.Like P1 ,Port2 also doesn’trequire any pull-up resistorsrequire any pull-up resistorsPorts….Ports….
    31. 31. Ports contd…Ports contd… Port 3 is also dual purpose but designers generallyPort 3 is also dual purpose but designers generallyavoid using this port unnecessarily for I/Oavoid using this port unnecessarily for I/Obecause the pins have alternate functions whichbecause the pins have alternate functions whichare related to special features of the 8051.are related to special features of the 8051.Indiscriminate use of these pins may interfere withIndiscriminate use of these pins may interfere withthe normal operation of the 8051.the normal operation of the 8051. However, for a programmer, it is the same toprogram P0, P1, P2 and P3. All the ports upon RESET are configured asoutput. To use any of the ports as an input port,itmust be set(Programmed)
    32. 32. Alternate functions of P3Alternate functions of P3
    33. 33. I/O Port structureI/O Port structure The internal circuitry for the I/O port is shown inThe internal circuitry for the I/O port is shown inthe next slidethe next slide If you want to read in from a pin, you must firstIf you want to read in from a pin, you must firstgive a logic ‘1’ to the port latch to turn off thegive a logic ‘1’ to the port latch to turn off theFET otherwise the data read in will always be logicFET otherwise the data read in will always be logic‘0’.‘0’. When you write to the port you are actuallyWhen you write to the port you are actuallywriting to the latch e.g. a logic 0 given to the latchwriting to the latch e.g. a logic 0 given to the latchwill be inverted and turn on the FET which causewill be inverted and turn on the FET which causethe port pin to be connected to Gnd (logic 0).the port pin to be connected to Gnd (logic 0).
    34. 34. I/O Port contd…I/O Port contd…
    35. 35. Timers /CountersTimers /Counters The 8051 has 2 timers/counters:The 8051 has 2 timers/counters: Timer/Counter 0Timer/Counter 0 Timer/Counter 1Timer/Counter 1They can be used asThey can be used as1.1. TheThe TimerTimer :Uused as a time delay generator.:Uused as a time delay generator. The clock source is theThe clock source is the internalinternal crystal frequency of thecrystal frequency of the8051.8051.2.2. An eventAn event countercounter.. External inputExternal input from input pin to count the number offrom input pin to count the number ofevents on registers.events on registers. These clock pulses cold represent the number of peopleThese clock pulses cold represent the number of peoplepassing through an entrance, or the number of wheelpassing through an entrance, or the number of wheelrotations, or any other event that can be converted to pulses.rotations, or any other event that can be converted to pulses.
    36. 36. TimerTimer Set the initial value of registersSet the initial value of registers Start the timer and then the 8051 counts up.Start the timer and then the 8051 counts up. Input from internal system clock (machine cycle)Input from internal system clock (machine cycle) When the registers equal to 0 and the 8051 sets aWhen the registers equal to 0 and the 8051 sets abit to denote time outbit to denote time outtoLCDP18051TL0TH0P2SetTimer 0
    37. 37. CounterCounter Count the number of eventsCount the number of events Show the number of events on registersShow the number of events on registers External input from T0 input pin (P3.4) for Counter 0External input from T0 input pin (P3.4) for Counter 0 External input from T1 input pin (P3.5) for Counter 1External input from T1 input pin (P3.5) for Counter 1 External inputExternal input from Tx input pin.from Tx input pin. We use Tx to denote T0 or T1.We use Tx to denote T0 or T1.T0toLCDP3.4P18051a switchTL0TH0
    38. 38. RegistersRegisters Used in Timer/CounterUsed in Timer/Counter 8051 has two 16-bit Timer registers ,Timer 0 &8051 has two 16-bit Timer registers ,Timer 0 &Timer 1.Timer 1. As 8051 has 8-bit architecture , each TimerAs 8051 has 8-bit architecture , each Timerregister is treated as two 8-bit registers namelyregister is treated as two 8-bit registers namelyTH0, TL0, TH1, TL1.TH0, TL0, TH1, TL1. One 8-bit mode register -TMOD.One 8-bit mode register -TMOD. One 8-bit control register-TCON.One 8-bit control register-TCON.
    39. 39. TMOD RegisterTMOD Register Both Timer 0 &Timer 1 use the same Mode registerBoth Timer 0 &Timer 1 use the same Mode registerTMOD.TMOD. It is an-8-bit register .The lower 4-bits are meant forIt is an-8-bit register .The lower 4-bits are meant forTimer 0 &the uooer 4-bits are meant for Timer 1Timer 0 &the uooer 4-bits are meant for Timer 1 It is not bit addressible.It is not bit addressible. It is used similar to any other register of 8051 . For ex:It is used similar to any other register of 8051 . For ex:MOV TMOD,#21HMOV TMOD,#21HGATE C/T M1 M0 GATE C/T M1 M0Timer 1 Timer 0(MSB) (LSB)
    40. 40. GateGate Every timer has a mean of starting and stopping.Every timer has a mean of starting and stopping. GATE=0GATE=0 InternalInternal controlcontrol The start and stop of the timer are controlled by way ofThe start and stop of the timer are controlled by way ofsoftware.software. Set/clear the TR for start/stop timer.Set/clear the TR for start/stop timer.SETB TR0SETB TR0CLR TR0CLR TR0 GATE=1GATE=1 ExternalExternal controlcontrol The hardware way of starting and stopping the timer byThe hardware way of starting and stopping the timer bysoftwaresoftware andand an external sourcean external source.. Timer/counter is enabled only while the INT pin is high andTimer/counter is enabled only while the INT pin is high andthe TR control pin is set (TR).the TR control pin is set (TR).
    41. 41. TMod contd….TMod contd….C/T :C/T : Timer or counter selected cleared for timer operation (inputTimer or counter selected cleared for timer operation (inputfrom internal system clock). Set for counter operationfrom internal system clock). Set for counter operation(input from Tx input pin).(input from Tx input pin).M1,M0 : Used for mode selection.Because the Timers of 8051 canUsed for mode selection.Because the Timers of 8051 canbe set in 4-different modes.be set in 4-different modes.M1 M0 Mode OperationM1 M0 Mode Operation0 00 0 00 113-bit timer3-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1)mode 8-bit THx + 5-bit TLx (x= 0 or 1)00 1 11 1 16-bit timer16-bit timer mode 8-bit THx + 8-bit TLxmode 8-bit THx + 8-bit TLx1 01 0 22 8-bit auto reload8-bit auto reload 8-bit auto reload timer/counter;8-bit auto reload timer/counter;THx holds a value which is to be reloaded intoTHx holds a value which is to be reloaded intoTLx each time it overflows.TLx each time it overflows.11 11 33 Split timer modeSplit timer mode
    42. 42. TMod contd…TMod contd…
    43. 43. Let us understand the working of Timer Mode 1Let us understand the working of Timer Mode 1 For this , let us consider timer 0 as an example.For this , let us consider timer 0 as an example. 16-bit16-bit timer (TH0 and TL0)timer (TH0 and TL0) TH0-TL0 is incremented continuously when TR0 is setTH0-TL0 is incremented continuously when TR0 is setto 1. And the 8051 stops to increment TH0-TL0 whento 1. And the 8051 stops to increment TH0-TL0 whenTR0 is cleared.TR0 is cleared. The timer works with the internal system clock. In otherThe timer works with the internal system clock. In otherwords, the timer counts up each machine cycle.words, the timer counts up each machine cycle. When the timer (TH0-TL0) reaches its maximum ofWhen the timer (TH0-TL0) reaches its maximum ofFFFFH, it rolls over to 0000, and TF0 is raised.FFFFH, it rolls over to 0000, and TF0 is raised. Programmer should check TF0 and stop the timer 0.Programmer should check TF0 and stop the timer 0.
    44. 44. Steps of Mode 1Steps of Mode 11.1. Choose mode 1 timer 0Choose mode 1 timer 0 MOV TMOD,#01HMOV TMOD,#01H2.2. Set the original value to TH0 and TL0.Set the original value to TH0 and TL0. MOV TH0,#FFHMOV TH0,#FFH MOV TL0,#FCHMOV TL0,#FCH3.3. You better to clear the TF: TF0=0.You better to clear the TF: TF0=0. CLR TF0CLR TF04.4. Start the timer.Start the timer. SETB TR0SETB TR0
    45. 45. Mode 1 contd…Mode 1 contd…5.5. The 8051 starts to count up by incrementing theThe 8051 starts to count up by incrementing theTH0-TL0TH0-TL0.. TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000HTH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000HFFFC FFFD FFFE FFFF 0000TF = 0 TF = 0 TF = 0 TF = 0 TF = 1TH0 TL0Start timerStop timerMonitor TF until TF=1TR0=1 TR0=0TF
    46. 46. Mode 1 contd…Mode 1 contd…6.6. When TH0-TL0 rolls over from FFFFH toWhen TH0-TL0 rolls over from FFFFH to 0000,0000,the 8051 set TF0=1.the 8051 set TF0=1.TH0-TL0= FFFE H, FFFF H, 0000 H (NowTH0-TL0= FFFE H, FFFF H, 0000 H (NowTF0=1)TF0=1)6.6. Keep monitoring the timer flag )TF) to see if it isKeep monitoring the timer flag )TF) to see if it israised.raised.AGAIN: JNB TF0, AGAINAGAIN: JNB TF0, AGAIN6.6. Clear TR0 to stop the process.Clear TR0 to stop the process.CLR TR0CLR TR06.6. Clear the TF flag for the next round.Clear the TF flag for the next round.CLR TF0CLR TF0
    47. 47. TCON RegisterTCON RegisterTimer control register TMOD is a 8-bitregister which is bit addressable and inwhich Upper nibble is for timer/counter,lower nibble is for interrupts
    48. 48. Tcon contd…Tcon contd… TR (Timer run control bit) TR0 for Timer/counter 0; TR1 for Timer/counter 1. TR is set by programmer to turn timer/counter on/off. TR=0 : off (stop) TR=1 : on (start) TF (timer flag, control flag) TF0 for timer/counter 0; TF1 for timer/counter 1. TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000from FFFFH, the TF is set to 1. TF=0 : not reach TF=1: reach If we enable interrupt, TF=1 will trigger ISR.
    49. 49. Equivalent Instructions for the TimerEquivalent Instructions for the TimerControl RegisterControl RegisterFor timer 0For timer 0SETB TR0SETB TR0 == SETB TCON.4SETB TCON.4CLR TR0CLR TR0 == CLR TCON.4CLR TCON.4SETB TF0SETB TF0 == SETB TCON.5SETB TCON.5CLR TF0CLR TF0 == CLR TCON.5CLR TCON.5For timer 1For timer 1SETB TR1SETB TR1 == SETB TCON.6SETB TCON.6CLR TR1CLR TR1 == CLR TCON.6CLR TCON.6SETB TF1SETB TF1 == SETB TCON.7SETB TCON.7CLR TF1CLR TF1 == CLR TCON.7CLR TCON.7TF1 IT0IE0IT1IE1TR0TF0TR1TCON: Timer/Counter Control Register
    50. 50. Simple applications using ports &TimersSimple applications using ports &Timers Using a port ,by a simple program you can generate a SquareUsing a port ,by a simple program you can generate a Squarewave of any duty cycle.wave of any duty cycle.HEREHERE :: SETB P1.0 (Make bit of Port 0 High)SETB P1.0 (Make bit of Port 0 High)LCALL DELAYLCALL DELAYCLR P1.0CLR P1.0LCALL DELAYLCALL DELAYSJMP HERE : Keep doing itSJMP HERE : Keep doing itHere same delay is used for bothHere same delay is used for bothHigh & lowHigh & low
    51. 51. Square-wave of 66%duty cycle.Square-wave of 66%duty cycle.HEREHERE : SETB P1.0 ( Make bit of Port 0 High): SETB P1.0 ( Make bit of Port 0 High)LCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAYCLR P1.0CLR P1.0LCALL DELAYLCALL DELAYSJMP HERE : Keep doing itSJMP HERE : Keep doing it
    52. 52. Square-wave generation using TimerSquare-wave generation using TimerDELAY:SETB TR0 ;start the timer 0AGAIN: JNB TF0,AGAINCLR TR0 ;stop timer 0CLR TF0 ;clear timer 0 flagRET Square wave of 50% duty on P1.5 Timer 0 is used; each loop is a half clockMOV TMOD,#01 ;Timer 0,mode 1(16-bit)HERE: MOV TL0,#0F2H ;Timer value = FFF2HMOV TH0,#0FFHCPL P1.5ACALL DELAYSJMP HERE
    53. 53. 8051- SERIAL COMMUNICATION8051- SERIAL COMMUNICATION
    54. 54. Basics of serial communicationBasics of serial communication
    55. 55. Types of Serial communicationsTypes of Serial communications
    56. 56. RxD and TxD pins in the 8051RxD and TxD pins in the 8051 The 8051 has two pins for transferring andThe 8051 has two pins for transferring andreceiving data by serial communication. Thesereceiving data by serial communication. Thesetwo pins are part of the Port3(P3.0 &P3.1)two pins are part of the Port3(P3.0 &P3.1) These pins are TTL compatible and hence theyThese pins are TTL compatible and hence theyrequire a line driver to make them RS232require a line driver to make them RS232compatiblecompatible Max232 chip is one such line driver in use.Max232 chip is one such line driver in use. Serial communication is controlled by an 8-bitSerial communication is controlled by an 8-bitregister called SCON register,it is a bitregister called SCON register,it is a bitaddressable register.addressable register.
    57. 57. Interfacing to PCInterfacing to PC
    58. 58. SCON (Serial control) registerSCON (Serial control) register
    59. 59. SM0 , SM1SM0 , SM1 These two bits of SCON register determine the framingThese two bits of SCON register determine the framingof data by specifying the number of bits per characterof data by specifying the number of bits per characterand start bit and stop bits. There are 4 serial modes.and start bit and stop bits. There are 4 serial modes.SM0 SM10 0 Serial Mode 00 1 Serial Mode 1, 8 bit data,1 stop bit, 1 start bit1 0 Serial Mode 21 1 Serial Mode 3
    60. 60. RENREN,, TITI,, RIRI REN (Receive Enable) also referred asREN (Receive Enable) also referred as SCON.4SCON.4..When it is high,it allows the 8051 to receive dataWhen it is high,it allows the 8051 to receive dataon the RxD pin. So to receive and transfer dataon the RxD pin. So to receive and transfer dataREN must be set to 1.When REN=0,theREN must be set to 1.When REN=0,thereceiver is disabled. This is achieved as belowreceiver is disabled. This is achieved as belowSETB SCON.4SETB SCON.4& CLR SCON.4& CLR SCON.4
    61. 61. Contd…Contd… TI (Transmit interrupt) is the D1 bit of SCON register.TI (Transmit interrupt) is the D1 bit of SCON register.When 8051 finishes the transfer of 8-bit character, itWhen 8051 finishes the transfer of 8-bit character, itraises the TI flag to indicate that it is ready to transferraises the TI flag to indicate that it is ready to transferanother byte. The TI bit is raised at the beginning ofanother byte. The TI bit is raised at the beginning ofthe stop bit.the stop bit. RI (Receive interrupt) is the D0 bit of the SCONRI (Receive interrupt) is the D0 bit of the SCONregister. When the 8051 receives data serially ,via RxD,register. When the 8051 receives data serially ,via RxD,it gets rid of the start and stop bits and places the byteit gets rid of the start and stop bits and places the bytein the SBUF register.Then it raises the RI flag bit toin the SBUF register.Then it raises the RI flag bit toindicate that a byte has been received and should beindicate that a byte has been received and should bepicked up before it is lost.RI is raised halfway throughpicked up before it is lost.RI is raised halfway throughthe stop bit.the stop bit.
    62. 62. 8051 Interrupts8051 Interrupts AnAn interruptinterrupt is an external or internal event thatis an external or internal event that disturbsdisturbsthethe microcontroller to inform it that a device needs itsmicrocontroller to inform it that a device needs itsservice.service.AA MMicrocontroller can serveicrocontroller can serve variousvarious devices.devices. ThThereere are two ways to do that:are two ways to do that: interruptsinterrupts && polling.polling. The program which is associated with the interrupt isThe program which is associated with the interrupt iscalled thecalled the interrupt service routineinterrupt service routine (ISR) or(ISR) or interruptinterrupthandlerhandler..
    63. 63. Steps in executing an interruptSteps in executing an interrupt Upon receiving the interrupt signal theUpon receiving the interrupt signal theMicrocontroller , fMicrocontroller , finishinish currentcurrent instruction andinstruction andssaaves the PC on stack.ves the PC on stack. JJumps to a fixed location in memoryumps to a fixed location in memory dependingdependingon type of interrupton type of interrupt SStarts to execute the interrupt service routinetarts to execute the interrupt service routineuntil RETI (return from interrupt)until RETI (return from interrupt) Upon executing the RETI the microcontrollerUpon executing the RETI the microcontrollerreturns to the place where it was interrupted.returns to the place where it was interrupted.GetGet poppop PC from stackPC from stack
    64. 64. Interrupt SourcesInterrupt Sources Original 8051 has 6 sources of interruptsOriginal 8051 has 6 sources of interrupts ResetReset Timer 0 overflowTimer 0 overflow Timer 1 overflowTimer 1 overflow External Interrupt 0External Interrupt 0 External Interrupt 1External Interrupt 1 Serial Port events (buffer full, buffer empty, etc)Serial Port events (buffer full, buffer empty, etc) Enhanced version has 22 sourcesEnhanced version has 22 sources More timers, programmable counter array, ADC,More timers, programmable counter array, ADC,more external interrupts, another serial port (UART)more external interrupts, another serial port (UART)
    65. 65.  Each interrupt has a specific place in codeEach interrupt has a specific place in codememory where program execution (interruptmemory where program execution (interruptservice routine) begins.service routine) begins. External Interrupt 0:External Interrupt 0: 0003h0003h Timer 0 overflow:Timer 0 overflow: 000Bh000Bh External Interrupt 1:External Interrupt 1: 0013h0013h Timer 1 overflow:Timer 1 overflow: 001Bh001Bh Serial : 0023hSerial : 0023h Timer 2 overflowTimer 2 overflow(8052+)(8052+) 002bh002bhInterrupt Vectors
    66. 66. Interrupt Enable RegisterInterrupt Enable Register Upon reset all Interrupts are disabled & donot respond to the Microcontroller These interrupts must be enabled by softwarein order for the Microcontroller to respond tothem. This is done by an 8-bit register calledInterrupt Enable Register (IE).
    67. 67.  EA : Global enable/disable. --- : Undefined. ET2 : Enable Timer 2 interrupt. ES : Enable Serial port interrupt. ET1 : Enable Timer 1 interrupt. EX1 :Enable External 1 interrupt. ET0 : Enable Timer 0 interrupt. EX0 : Enable External 0 interrupt..
    68. 68. Enabling and disabling aninterruptBy bit operationRecommended in the middle of programSETB EA ;Enable AllSETB ET0 ;Enable Timer0 ovrfSETB ET1 ;Enable Timer1 ovrfSETB EX0 ;Enable INT0SETB EX1 ;Enable INT1SETB ES ;Enable Serial portBy Mov instructionRecommended in the first of programMOV IE, #10010110BSETB IE.7SETB IE.1SETB IE.3SETB IE.0SETB IE.2SETB IE.4
    69. 69.  What if two interrupt sources interrupt at the sameWhat if two interrupt sources interrupt at the sametime?time? The interrupt with the highest PRIORITY getsThe interrupt with the highest PRIORITY getsserviced first.serviced first. All interrupts have a power on default priority order.All interrupts have a power on default priority order.1.1. External interrupt 0 (INT0)External interrupt 0 (INT0)2.2. Timer interrupt0 (TF0)Timer interrupt0 (TF0)3.3. External interrupt 1 (INT1)External interrupt 1 (INT1)4.4. Timer interrupt1 (TF1)Timer interrupt1 (TF1)5.5. Serial communication (RI+TI)Serial communication (RI+TI) Priority can also be set to “high” or “low” by IP reg.Priority can also be set to “high” or “low” by IP reg.Interrupt PrioritiesInterrupt Priorities
    70. 70. Interrupt Priorities (IP) RegisterInterrupt Priorities (IP) RegisterIP.7: reservedIP.7: reservedIP.6: reservedIP.6: reservedIP.5: Timer 2 interrupt priority bit (8052 only)IP.5: Timer 2 interrupt priority bit (8052 only)IP.4: Serial port interrupt priority bitIP.4: Serial port interrupt priority bitIP.3: Timer 1 interrupt priority bitIP.3: Timer 1 interrupt priority bitIP.2: External interrupt 1 priority bitIP.2: External interrupt 1 priority bitIP.1: Timer 0 interrupt priority bitIP.1: Timer 0 interrupt priority bitIP.0: External interrupt 0 priority bitIP.0: External interrupt 0 priority bit--- PX0PT0PX1PT1PSPT2---
    71. 71. Interrupt Priorities ExampleInterrupt Priorities Example MOV IP , #00000100BMOV IP , #00000100B oror SETB IP.2SETB IP.2 gives prioritygives priorityorderorder1.1. Int1Int12.2. Int0Int03.3. Timer0Timer04.4. Timer1Timer15.5. SerialSerial MOV IP , #00001100BMOV IP , #00001100B gives priority ordergives priority order1.1. Int1Int12.2. Timer1Timer13.3. Int0Int04.4. Timer0Timer05.5. SerialSerial--- PX0PT0PX1PT1PSPT2---
    72. 72. Interrupt inside an interruptInterrupt inside an interrupt--- PX0PT0PX1PT1PSPT2--- A high-priority interrupt can interrupt a low-A high-priority interrupt can interrupt a low-priority interrupypriority interrupy All interrupt are latched internallyAll interrupt are latched internally Low-priority interrupt wait until 8051 hasLow-priority interrupt wait until 8051 hasfinished servicing the high-priority interruptfinished servicing the high-priority interrupt
    73. 73. Applications of MicrocontrollersApplications of Microcontrollers
    74. 74. Simple Interfacing ExamplesSimple Interfacing Examples
    75. 75. Seven segment InterfacingSeven segment Interfacing
    76. 76. Traffic light controllerTraffic light controller
    77. 77. Closed loop control system-Closed loop control system-Temperature control exampleTemperature control example
    78. 78. Recent Wonders
    79. 79. Recent Wonders contd ........Recent Wonders contd ........
    80. 80. Recent Wonders contd ........Recent Wonders contd ........
    81. 81. Books that have helped me to understand theBooks that have helped me to understand theMicrocontrollers & embedded systemsMicrocontrollers & embedded systems1)1) Barr, Michael, Programming embedded sytemsBarr, Michael, Programming embedded sytemsin C and C++ - OReilly Publ.in C and C++ - OReilly Publ.2)2) Raj Kamal, Embedded systems,Raj Kamal, Embedded systems, TMGTMG3)3) Mazidi and Mazidi, The 8051 microcontrollerMazidi and Mazidi, The 8051 microcontrollerand embedded sytems - Pearson education.and embedded sytems - Pearson education.4)4) Peatman,J.B. Design with microcontrollers andPeatman,J.B. Design with microcontrollers andmicrocomputers, McGraw Hilmicrocomputers, McGraw Hil
    82. 82. 5)5) Sewart. J.W. The 8051 Microcontroller Hardware,Sewart. J.W. The 8051 Microcontroller Hardware,Software and Interfacing – Prentice HallSoftware and Interfacing – Prentice Hall6) Ayala Kenneth, The 8051 Microntroller –Ayala Kenneth, The 8051 Microntroller –Architecture, Programming and Applications –Architecture, Programming and Applications –Delmar Publ.Delmar Publ.7)7) Ajay Deshmukh, Microcontrollers – TATAAjay Deshmukh, Microcontrollers – TATAMcGraw HillMcGraw Hill8)8) Rajkamal, Microcontrollers - Architecture,Rajkamal, Microcontrollers - Architecture,Programming – Pearson Publ.Programming – Pearson Publ.9) Myke Predko, Programming the 8051 Microcontroller –Myke Predko, Programming the 8051 Microcontroller –McGraw HillMcGraw Hill10)10) Michael J. Pont, Embedded C - Addison WeselyMichael J. Pont, Embedded C - Addison WeselyPubl.Publ.
    83. 83. Useful websitesUseful websites
    84. 84. Useful websitesUseful websites contd…contd…1.1. http://www.eg3.comhttp://www.eg3.com2.2. http://www.ARM.MCU.comhttp://www.ARM.MCU.com3.3. http://www.mcjournal.comhttp://www.mcjournal.com4.4. http://www.iar.comhttp://www.iar.com5.5. http://http://http://www.embedded.comhttp://www.embedded.com6.6. http://www.powersoftsystems.comhttp://www.powersoftsystems.com
    85. 85. EpilogueEpilogue The woods are lovely, dark and deep,But I have promises to keep,And miles to go before I sleep,And miles to go before I sleep.---- Robert Frost GOOD LUCK!

    ×