Micro Controller Archtecture Programming

Embed Size (px)

Citation preview

  • 8/9/2019 Micro Controller Archtecture Programming

    1/94

  • 8/9/2019 Micro Controller Archtecture Programming

    2/94

    .. Mans glory lies in his knowledge,Mans glory lies in his knowledge,

    his upright conduct, his praisehis upright conduct, his praise--worthy character,worthy character,

    his wisdom, and not in his nationality or rankhis wisdom, and not in his nationality or rank

    ----BahaullahBahaullah

    (From, the book The 8051 Microcontroller and Embedded systems(From, the book The 8051 Microcontroller and Embedded systems-- Mazidi )Mazidi )

  • 8/9/2019 Micro Controller Archtecture Programming

    3/94

    OverviewOverview

    IntroductionIntroduction

    Block Diagram andBlock Diagram and PinPinDescription of the 8051Description of the 8051

    RegistersRegisters

    Memory mapping in 8051Memory mapping in 8051

    Stack in the 8051Stack in the 8051

    I/O Port ProgrammingI/O Port Programming

    TimersTimers

    Interrupts &Interrupts &

    ApplicationsApplications

  • 8/9/2019 Micro Controller Archtecture Programming

    4/94

    I think, I am not wrong if I say that , in

    todays market it is impossible to find anyelectronic gadget designed without aMicrocontroller.

    Ex: From Consumer Electronics to

    Communication Electronics

    Not believable ??? See the next slide

    Why do we need to learnWhy do we need to learn

    Microcontrollers ?Microcontrollers ?

  • 8/9/2019 Micro Controller Archtecture Programming

    5/94

    Toys , video games, cars, dolls ..Toys , video games, cars, dolls ..

    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/pressure

    controllers, Counters, timers, RPM Controllerscontrollers, Counters, timers, RPM Controllers

  • 8/9/2019 Micro Controller Archtecture Programming

    6/94

    So, A good designer should alwaysknow what type of controller he/sheis using ,their architecture,Programming details ,advantages ,

    disadvantages , ways to reduceproduction costs and product reliabilityetc.

    O.K !!!O.K !!!

  • 8/9/2019 Micro Controller Archtecture Programming

    7/94

    Then What is a Microcontroller ?

    A single chip computer or A CPU with all theperipherals RAM, ROM, I/O ports... On the

    same chip

    ExampleMotorolas 6811, Intels 8051,

    Zilogs Z8 and PIC 16X

    RAM ROM

    I/O

    PortTimer

    Serial

    COM

    Port

    CPU

    A simpleMicrocontroller

  • 8/9/2019 Micro Controller Archtecture Programming

    8/94

    How is it different from aHow is it different from a

    Microprocessor ??Microprocessor ?? General-purpose microprocessor

    CPU for ComputersNo RAM, ROM, I/O on CPU chip itself

    ExampleIntels x86, Motorolas 680x0

    CPU

    General-

    Purpose

    Micro-processor

    RAM ROM I/O

    Port

    TimerSerial

    COM

    Port

    Data Bus

    Address Bus

  • 8/9/2019 Micro Controller Archtecture Programming

    9/94

    Microprocessor

    CPU is stand-alone, RAM,

    ROM, I/O, timer are separate

    Designer can decide on theamount of ROM, RAM and

    I/O ports.

    Expansive

    Versatility

    General-purpose

    Microcontroller

    CPU, RAM, ROM, I/O and

    timers are all on a single chip

    Fixed amount of on-chipROM, RAM, I/O ports

    Highly bit addressable

    For applications in which cost,

    power and space are critical

    Dedicated i.e. single-purpose

    Microprocessor vs. Microcontroller

  • 8/9/2019 Micro Controller Archtecture Programming

    10/94

    EVOLUTIONEVOLUTION

    Flashback !!!!Flashback !!!!In the year 1976, Motorola created a MicroprocessorIn the year 1976, Motorola created a Microprocessorchip called 6801 which replaced its senior 6800 withchip called 6801 which replaced its senior 6800 withcertain addcertain add--on chips to make a computer. This pavedon 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 called

    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 8EPROM, 64 Bytes of RAM an 8--Bit Timer and 27 I/OBit Timer and 27 I/Opins in 1976.pins in 1976.

  • 8/9/2019 Micro Controller Archtecture Programming

    11/94

    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,128

    Bytes ofBytes ofRAMRAM , a serial port, two 16, a serial port, two 16--bit Timers ,bit Timers ,and 32 I/O pins.and 32 I/O pins.

    The 8051 family has many additions andThe 8051 family has many additions and

    improvements over the years and remains aimprovements over the years and remains amost acclaimed tool for todays circuit designers.most acclaimed tool for todays circuit designers.

    The same INTEL introduced a 16 bit controllerThe same INTEL introduced a 16 bit controller

    8096 in the year 19828096 in the year 1982

  • 8/9/2019 Micro Controller Archtecture Programming

    12/94

    Later INTEL introduced 80c196 series of 16Later INTEL introduced 80c196 series of 16--bitbit

    Microcontrollers for mainly industrialMicrocontrollers for mainly industrialapplicationsapplications

    Microchip, another company has introduced aMicrochip, another company has introduced a

    Microcontroller PIC 16C64 an 8Microcontroller PIC 16C64 an 8--bit in the yearbit in the year19851985.. 3232--bit microcontrollers have been developed byIBMbit microcontrollers have been developed byIBM

    and Motorolaand Motorola--MPC 505 is a 32MPC 505 is a 32--bit RISC controller ofbit RISC controller ofMotorolaMotorola

    The 403 GA is a 32The 403 GA is a 32 --bit RISC embedded controller ofbit RISC embedded controller ofIBMIBM

  • 8/9/2019 Micro Controller Archtecture Programming

    13/94

    ARM ControllersARM Controllers

    In recent times ARM company (Advanced RISCIn recent times ARM company (Advanced RISC

    machines) has developed and introduced 32 bitmachines) has developed and introduced 32 bitcontrollers for highcontrollers for high--end application devices likeend application devices like

    mobiles , Ipods etc. (Refer www. arm.com)mobiles , Ipods etc. (Refer www. arm.com)

  • 8/9/2019 Micro Controller Archtecture Programming

    14/94

    Types of MicrocontrollersTypes of Microcontrollers

  • 8/9/2019 Micro Controller Archtecture Programming

    15/94

    Microcontrollers from different manufacturersMicrocontrollers from different manufacturers

    Atmel

    ARMIntel

    8-bit

    8XC42

    MCS48

    MCS51

    8xC251

    16-bit

    MCS96

    MXS296

    National Semiconductor

    COP8Microchip

    12-bit instruction PIC

    14-bit instruction PIC

    PIC16F84

    16-bit instruction PIC

    NEC

    Motorola

    8-bit68HC05

    68HC08

    68HC11

    16-bit

    68HC12

    68HC1632-bit

    683xx

    Texas Instruments

    TMS370

    MSP4

    30

    Zilog

    Z8

    Z86E02

  • 8/9/2019 Micro Controller Archtecture Programming

    16/94

    MCSMCS--51 Family of Microcontrollers51 Family of Microcontrollers

  • 8/9/2019 Micro Controller Archtecture Programming

    17/94

    Microcontroller ArchitecturesMicrocontroller Architectures

    CPU

    Program

    + Data

    Address Bus

    Data Bus

    Memory

    Von Neumann

    Architecture

    CPU

    ProgramAddress Bus

    Data Bus

    HarvardArchitecture

    Memory

    Data

    Address Bus

    Fetch Bus

    0

    0

    0

    2n

  • 8/9/2019 Micro Controller Archtecture Programming

    18/94

    4K bytes ROM4K bytes ROM

    128 bytes RAM128 bytes RAM

    Four 8Four 8--bit I/O portsbit I/O ports Two 16Two 16--bit timersbit timers

    Serial interfaceSerial interface

    64K external code memory space64K external code memory space 64K data memory space64K data memory space

  • 8/9/2019 Micro Controller Archtecture Programming

    19/94

    Original 8051 MicrocontrollerOriginal 8051 Microcontroller

    Oscillator

    and timing

    4096 BytesProgram Memory

    (ROM)

    128 Bytes

    Data Memory

    (RAM)

    Two 16 Bit

    Timer/Event

    Counters

    8051CPU

    64 K Byte Bus

    Expansion

    Control

    Programmable

    I/O

    Programmable

    Serial Port Full

    Duplex UART

    Synchronous Shifter

    Internal data bus

    External interrupts

    subsystem interrupts

    Control Parallel ports

    Address Data Bus

    I/O pinsSerial Input

    Serial Output

  • 8/9/2019 Micro Controller Archtecture Programming

    20/94

    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 a

    control line or as partcontrol line or as partof address or dateof address or datebus.bus.

  • 8/9/2019 Micro Controller Archtecture Programming

    21/94

    8051 CPU Registers8051 CPU Registers

    A (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)

  • 8/9/2019 Micro Controller Archtecture Programming

    22/94

    Special Function RegistersSpecial Function Registers

    DATA registers

    CONTROL registers

    Timers

    Serial ports

    Interrupt system

    Analog to Digital converter

    Digital to Analog converter

    etc..

    Addresses 80h FFh

    Direct Addressing is used to

    access SFRs

  • 8/9/2019 Micro Controller Archtecture Programming

    23/94

    List of RegistersList of Registers(*Denotes the SFRs)(*Denotes the SFRs)

  • 8/9/2019 Micro Controller Archtecture Programming

    24/94

    ContdContd

  • 8/9/2019 Micro Controller Archtecture Programming

    25/94

    PSW REGISTERPSW REGISTER

  • 8/9/2019 Micro Controller Archtecture Programming

    26/94

    Memory mapping in 8051Memory mapping in 8051

    ROM memory map in 8051 family

    0000H

    0FFFH

    0000H

    1FFFH

    0000H

    7FFFH

    8051

    8752

    4k

    DS5000-32

    8k 32k

    from Atmel

    Corporationfrom Dallas

    Semiconductor

  • 8/9/2019 Micro Controller Archtecture Programming

    27/94

    RAM memory space allocation in the 8051

    7FH

    30H

    2FH

    20H

    1FH

    17H

    10H

    0FH

    07H

    08H

    18H

    00H

    Register Bank 0

    (Stack) Register Bank 1

    Register Bank 2

    Register Bank3

    Bit-Addressable RAM

    Scratch pad RAM

  • 8/9/2019 Micro Controller Archtecture Programming

    28/94

    PORTS OFPORTS OF 80518051

    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 fromPort 0 is a dual purpose port, it is located from

    pin 32 to pin 39 (8 pins). To use this port as bothpin 32 to pin 39 (8 pins). To use this port as both

    input/output ports each pin must be connectedinput/output ports each pin must be connectedexternally to a 10 k ohm pullexternally to a 10 k ohm pull--up resistor.This isup resistor.This is

    because Port 0 is an open drain.because Port 0 is an open drain.Simple ex:Simple ex: MOV A, #22MOV A, #22

    BACKBACK MOV P0 ,AP0 ,AACALL DELAYACALL DELAY

    CPL ACPL A

    SJMP BACKSJMP BACK

  • 8/9/2019 Micro Controller Archtecture Programming

    29/94

    PortPort 00 with Pullwith Pull--Up ResistorsUp Resistors

  • 8/9/2019 Micro Controller Archtecture Programming

    30/94

    Port 1 is a dedicated I/O port from pin 1 to pin8.Upon reset it is configured as outport. It is

    generally used for interfacing to external device

    thus if you need to connect to switches or LEDs,

    you could make use of these 8 pins,but it doesntneed any pull-up resistors as it is having

    internally

    Like port 0, port 2 is a dual-purpose port.(Pins 21

    through 28) It can be used for general I/O or as the highbyte of the address bus for designs with external code

    memory.Like P1 ,Port2 also doesnt require any pull-up

    resistors

    Ports.Ports.

  • 8/9/2019 Micro Controller Archtecture Programming

    31/94

    Ports contdPorts contd

    PortPort 33 is also dual purpose but designers generallyis 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 theare related to special features of the 80518051..

    Indiscriminate use of these pins may interfere withIndiscriminate use of these pins may interfere withthe normal operation of thethe normal operation of the 80518051..

    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)

  • 8/9/2019 Micro Controller Archtecture Programming

    32/94

    Alternate functions of PAlternate functions of P33

  • 8/9/2019 Micro Controller Archtecture Programming

    33/94

    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 in

    the 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 first

    give a logic give a logic 11 to the port latch to turn off the 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

    00..

    When you write to the port you are actuallyWhen you write to the port you are actuallywriting to the latch e.g. a logicwriting to the latch e.g. a logic 00 given to the latchgiven to the latchwill be inverted and turn on the FET which causewill be inverted and turn on the FET which cause

    the port pin to be connected to Gnd (logicthe port pin to be connected to Gnd (logic 00).).

  • 8/9/2019 Micro Controller Archtecture Programming

    34/94

    I/O Port contdI/O Port contd

  • 8/9/2019 Micro Controller Archtecture Programming

    35/94

    Timers /CountersTimers /Counters

    TheThe 80518051 hashas 22 timers/counters:timers/counters: Timer/CounterTimer/Counter 00

    Timer/CounterTimer/Counter 11

    They can be used asThey can be used as

    1.1. TheTheTimerTimer :Used as a time delay generator.:Used as a time delay generator. The clock source is theThe clock source is the internalinternal crystal frequency of thecrystal frequency of the

    80518051..

    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 wheel

    rotations, or any other event that can be converted to pulses.rotations, or any other event that can be converted to pulses.

  • 8/9/2019 Micro Controller Archtecture Programming

    36/94

    TimerTimer

    Set the initial value of registersSet the initial value of registers Start the timer and then theStart the timer and then the 80518051 counts up.counts up.

    Input from internal system clock (machine cycle)Input from internal system clock (machine cycle)

    When the registers equal toWhen the registers equal to 00 and theand the 80518051 sets asets abit to denote time outbit to denote time out

    toLCD

    P1

    8051

    TL0

    TH0

    P2SetTimer 0

  • 8/9/2019 Micro Controller Archtecture Programming

    37/94

    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 TExternal input from T00 input pin (Pinput pin (P33..44) for Counter) for Counter 00

    External input from TExternal input from T11 input pin (Pinput pin (P33..55) for Counter) for Counter 11

    External inputExternal input from Tx input pin.from Tx input pin.

    We use Tx to denote TWe use Tx to denote T00 or Tor T11..

    T0

    toLCD

    P3.4

    P1

    8051

    a switch

    TL0

    TH0

  • 8/9/2019 Micro Controller Archtecture Programming

    38/94

    RegistersRegisters Used in Timer/CounterUsed in Timer/Counter

    80518051 has twohas two 1616--bit Timer registers ,Timerbit Timer registers ,Timer 00 &&TimerTimer 11..

    AsAs 80518051 hashas 88--bit architecture , each Timerbit architecture , each Timerregister is treated as tworegister is treated as two 88--bit registers namelybit registers namely

    THTH00, TL, TL00, TH, TH11, TL, TL11..

    OneOne 88--bit mode registerbit mode register --TMOD.TMOD.

    OneOne 88--bit control registerbit control register--TCON.TCON.

  • 8/9/2019 Micro Controller Archtecture Programming

    39/94

    TMOD RegisterTMOD Register

    Both TimerBoth Timer 00 &Timer&Timer 11 use the same Mode registeruse the same Mode register

    TMOD.TMOD.

    It is anIt is an--88--bit register .The lowerbit register .The lower 44--bits are meant forbits are meant for

    TimerTimer 00 &the uooer&the uooer 44--bits are meant for Timerbits are meant for Timer 11 It is not bit addressible.It is not bit addressible.

    It is used similar to any other register ofIt is used similar to any other register of 80518051 . For ex:. For ex:

    MOV TMOD,#MOV TMOD,#2121HH

    GATE C/T M1 M0 GATE C/T M1 M0

    Timer 1 Timer 0

    (MSB) (LSB)

  • 8/9/2019 Micro Controller Archtecture Programming

    40/94

    GateGate Every timer has a mean of starting and stopping.Every timer has a mean of starting and stopping.

    GATE=GATE=00

    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 TRSETB TR00

    CLR TRCLR TR00

    GATE=GATE=11

    ExternalExternal controlcontrol The hardware way of starting and stopping the timer byThe hardware way of starting and stopping the timer by

    softwaresoftware 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).

  • 8/9/2019 Micro Controller Archtecture Programming

    41/94

    TMod contd.TMod contd.

    C/T :C/T : Timer or counter selected cleared for timer operation (inputTimer or counter selected cleared for timer operation (input

    from 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 ofUsed for mode selection.Because the Timers of 80518051 cancanbe set inbe set in 44--different modes.different modes.

    MM11 MM00 Mode OperationMode Operation

    0 00 0 00 1133--bit timerbit timer modemode 88--bit THx +bit THx + 55--bit TLx (x=bit TLx (x= 00 oror 11))

    00 1 11 1 1616--bit timerbit timer modemode 88--bit THx +bit THx + 88--bit TLxbit TLx

    1 01 0 22 88--bit auto reloadbit auto reload 88--bit auto reload timer/counter;bit auto reload timer/counter;THx holds a value which is to be reloaded intoTHx holds a value which is to be reloaded into

    TLx each time it overflows.TLx each time it overflows.

    11 11 33 Split timer modeSplit timer mode

  • 8/9/2019 Micro Controller Archtecture Programming

    42/94

    TMod contdTMod contd

  • 8/9/2019 Micro Controller Archtecture Programming

    43/94

    Let us understand the working of Timer ModeLet us understand the working of Timer Mode 11

    For this , let us consider timerFor this , let us consider timer 00 as an example.as an example. 1616--bitbit timer (THtimer (TH00 and TLand TL00))

    THTH00--TLTL00 is incremented continuously when TRis incremented continuously when TR00 is setis set

    toto 11. And the. And the 80518051 stops to increment THstops to increment TH00--TLTL00 whenwhenTRTR00 is cleared.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 (THWhen the timer (TH00--TLTL00) reaches its maximum of) reaches its maximum ofFFFFH, it rolls over toFFFFH, it rolls over to 00000000, and TF, and TF00 is raised.is raised.

    Programmer should checkTFProgrammer should checkTF00 and stop the timerand stop the timer 00..

  • 8/9/2019 Micro Controller Archtecture Programming

    44/94

    Steps of ModeSteps of Mode 11

    1.1. Choose modeChoose mode 11 timertimer 00MOV TMOD,#MOV TMOD,#0101HH

    2.2. Set the original value to THSet the original value to TH00 and TLand TL00..

    MOV THMOV TH00,#FFH,#FFHMOV TLMOV TL00,#FCH,#FCH

    3.3. You better to clear the TF: TFYou better to clear the TF: TF00==00..

    CLR TFCLR TF004.4. Start the timer.Start the timer.

    SETB TRSETB TR00

  • 8/9/2019 Micro Controller Archtecture Programming

    45/94

    ModeMode 11 contdcontd

    5.5.T

    he 8051 starts to count up by incrementing theT

    he 8051 starts to count up by incrementing theTH0TH0--TL0TL0..

    TH0TH0--TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000HTL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H

    FFFC FFFD FFFE FFFF 0000

    TF = 0 TF = 0 TF = 0 TF = 0 TF = 1

    TH0 TL0Start timer

    Stop timer

    Monitor TF until TF=1

    TR0=1 TR0=0

    TF

  • 8/9/2019 Micro Controller Archtecture Programming

    46/94

    ModeMode 11 contdcontd

    6.6. When THWhen TH00--TLTL00 rolls over from FFFFH torolls over from FFFFH to 00000000,,thethe 80518051 set TFset TF00==11..

    THTH00--TLTL00= FFFE H, FFFF H,= FFFE H, FFFF H, 00000000 H (NowH (Now

    TFTF00==11))

    7.7. Keep monitoring the timer flag (TF) to see if it isKeep monitoring the timer flag (TF) to see if it israised.raised.

    AGAIN: JNB TF AGAIN: JNB TF00, AGAIN, AGAIN

    8.8. Clear TRClear TR00 to stop the process.to stop the process.

    CLR TRCLR TR00

    9.9. Clear the TF flag for the next round.Clear the TF flag for the next round.

    CLR TFCLR TF00

  • 8/9/2019 Micro Controller Archtecture Programming

    47/94

    TCON RegisterTCON Register

    Timer control register TMOD is a 8-bitregister which is bit addressable and in

    which Upper nibble is for timer/counter,

    lower nibble is for interrupts

  • 8/9/2019 Micro Controller Archtecture Programming

    48/94

    Tcon contdTcon contd

    TR(Timer run control bit) TR0 forTimer/counter 0; TR1 forTimer/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.

    l f h Tl f h T

  • 8/9/2019 Micro Controller Archtecture Programming

    49/94

    Equivalent Instructions for the TimerEquivalent Instructions for the TimerControl RegisterControl Register

    For timerFor timer 00SETB TRSETB TR00 == SETB TCON.4SETB TCON.4

    CLR TR0CLR TR0 == CLR TCON.4CLR TCON.4

    SETB TF0SETB TF0 == SETB TCON.5SETB TCON.5CLR TFCLR TF00 == CLR TCON.5CLR TCON.5

    For timer 1For timer 1

    SETB TRSETB TR11 == SETB TCON.6SETB TCON.6

    CLR TR1CLR TR1 == CLR TCON.6CLR TCON.6

    SETB TF1SETB TF1 == SETB TCON.7SETB TCON.7

    CLR TF1CLR TF1 == CLR TCON.7CLR TCON.7

    TF1 IT0IE0IT1IE1TR0TF0TR1

    TCON: Timer/Counter Control Register

  • 8/9/2019 Micro Controller Archtecture Programming

    50/94

    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.0

    LCALL DELAYLCALL DELAY

    SJMP HERE : Keep doing itSJMP HERE : Keep doing it

    Here same delay is used for bothHere same delay is used for bothHigh & lowHigh & low

  • 8/9/2019 Micro Controller Archtecture Programming

    51/94

    SquareSquare--wave ofwave of 6666%duty cycle.%duty cycle.

    HEREHERE : SET

    B P: SET

    B P11..00 ( Make bit of Port( Make bit of Port 00 High)High)LCALL DELAYLCALL DELAYLCALL DELAYLCALL DELAY

    CLR PCLR P11..00LCALL DELAYLCALL DELAY

    SJMP HERE : Keep doing itSJMP HERE : Keep doing it

  • 8/9/2019 Micro Controller Archtecture Programming

    52/94

    SquareSquare--wave generation using Timerwave generation using Timer

    DELAY:

    SETB TR0 ;start the timer 0AGAIN: JNB TF0,AGAIN

    CLR TR0 ;stop timer 0

    CLR TF0 ;clear timer 0 flag

    RET

    Square wave of 50% duty on P1.5

    Timer 0 is used; each loop is a half clock

    MOV TMOD,#01 ;Timer 0,mode 1(16-bit)

    HERE: MOV TL0,#0F2H ;Timer value = FFF2H

    MOV TH0,#0FFHCPL P1.5ACALL DELAY

    SJMP HERE

  • 8/9/2019 Micro Controller Archtecture Programming

    53/94

    80518051-- SERIAL COMMUNICATIONSERIAL COMMUNICATION

  • 8/9/2019 Micro Controller Archtecture Programming

    54/94

    Basics of serial communicationBasics of serial communication

  • 8/9/2019 Micro Controller Archtecture Programming

    55/94

    Types of Serial communicationsTypes of Serial communications

  • 8/9/2019 Micro Controller Archtecture Programming

    56/94

    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. These

    two 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 RS232

    compatiblecompatible

    Max232 chip is one such line driver in use.Max232 chip is one such line driver in use. Serial communication is controlled by an 8Serial communication is controlled by an 8--bitbit

    register called SCON register,it is a bitregister called SCON register,it is a bit

    addressable register.addressable register.

  • 8/9/2019 Micro Controller Archtecture Programming

    57/94

    Interfacing to PCInterfacing to PC

  • 8/9/2019 Micro Controller Archtecture Programming

    58/94

    SCON (Serial control) registerSCON (Serial control) register

  • 8/9/2019 Micro Controller Archtecture Programming

    59/94

    SMSM00 , SM, SM11

    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 areand start bit and stop bits. There are 44 serial modes.serial modes.

    SM0 SM10 0 Serial Mode 0

    0 1 Serial Mode 1, 8 bit data,

    1 stop bit, 1 start bit

    1 0 Serial Mode 2

    1 1 Serial Mode 3

  • 8/9/2019 Micro Controller Archtecture Programming

    60/94

    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 data

    REN 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 below

    SETB SCON.4SETB SCON.4

    & CLR SCON.4& CLR SCON.4

  • 8/9/2019 Micro Controller Archtecture Programming

    61/94

  • 8/9/2019 Micro Controller Archtecture Programming

    62/94

    80518051 InterruptsInterrupts

    AnAn interruptinterruptis 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 is

    The program which is associated with the interrupt iscalled thecalled the interrupt service routineinterrupt service routine(ISR) or(ISR) or interrupt handlerinterrupt handler..

  • 8/9/2019 Micro Controller Archtecture Programming

    63/94

    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 routine

    until RETI (return from interrupt)until RETI (return from interrupt)

    Upon executing the RETI the microcontrollerUpon executing the RETI the microcontroller

    returns to the place where it was interrupted.returns to the place where it was interrupted.

    GetGetpoppop PC from stackPC from stack

  • 8/9/2019 Micro Controller Archtecture Programming

    64/94

    Interrupt SourcesInterrupt Sources OriginalOriginal 80518051 hashas 66 sources of interruptssources of interrupts

    ResetReset

    TimerTimer 00 overflowoverflow

    TimerTimer 11 overflowoverflow

    External InterruptExternal Interrupt 00

    External InterruptExternal Interrupt 11

    Serial Port events (buffer full, buffer empty, etc)Serial Port events (buffer full, buffer empty, etc)

    Enhanced version hasEnhanced version has 2222 sourcessources 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)

  • 8/9/2019 Micro Controller Archtecture Programming

    65/94

    Each interrupt has a specific place in codeEach interrupt has a specific place in codememory where program execution (interruptmemory where program execution (interrupt

    service 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+) 002bh002bh

    Interrupt Vectors

  • 8/9/2019 Micro Controller Archtecture Programming

    66/94

    Interrupt Enable RegisterInterrupt Enable Register

    Upon reset all Interrupts are disabled & donot 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 8-bit register calledInterrupt Enable Register (IE).

  • 8/9/2019 Micro Controller Archtecture Programming

    67/94

    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..

  • 8/9/2019 Micro Controller Archtecture Programming

    68/94

    Enabling and disabling an interruptBy bit operation

    Recommended in the middle of programSETB EA ;Enable All

    SETB ET0 ;Enable Timer0 ovrf

    SETB ET1 ;Enable Timer1 ovrf

    SETB EX0 ;Enable INT0SETB EX1 ;Enable INT1

    SETB ES ;Enable Serial port

    By Mov instruction

    Recommended in the first of programMOV IE, #10010110B

    SETB IE.7

    SETB IE.1

    SETB IE.3

    SETB IE.0SETB IE.2

    SETB IE.4

  • 8/9/2019 Micro Controller Archtecture Programming

    69/94

    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 interruptExternal interrupt 00 (INT(INT00))

    2.2. Timer interruptTimer interrupt00 (TF(TF00))

    3.3. External interruptExternal interrupt 11 (INT(INT11))

    4.4. Timer interruptTimer interrupt11 (TF(TF11))5.5. Serial communication (RI+TI)Serial communication (RI+TI)

    Priority can also be set to high or low byIP reg.Priority can also be set to high or low byIP reg.

    Interrupt PrioritiesInterrupt Priorities

  • 8/9/2019 Micro Controller Archtecture Programming

    70/94

    Interrupt Priorities (IP) RegisterInterrupt Priorities (IP) Register

    IP.7: reservedIP.7: reserved

    IP.6: reservedIP.6: reserved

    IP.5: Timer2 interrupt priority bit (8052 only)IP.5: Timer2 interrupt priority bit (8052 only)

    IP.4: Serial port interrupt priority bitIP.4: Serial port interrupt priority bit

    IP.3: Timer 1 interrupt priority bitIP.3: Timer 1 interrupt priority bit

    IP.2: External interrupt 1 priority bitIP.2: External interrupt 1 priority bit

    IP.1: Timer 0 interrupt priority bitIP.1: Timer 0 interrupt priority bit

    IP.0: External interrupt 0 priority bitIP.0: External interrupt 0 priority bit

    --- PX0PT0PX1PT1PSPT2---

  • 8/9/2019 Micro Controller Archtecture Programming

    71/94

    Interrupt Priorities ExampleInterrupt Priorities Example

    MOV IP , #00000100BMOV IP , #00000100B oror SETB IP.2SETB IP.2 gives prioritygives priority

    orderorder1.1. Int1Int1

    2.2. Int0Int0

    3.3. Timer0Timer0

    4.4. Timer1Timer1

    5.5. SerialSerial

    MOV IP , #00001100BMOV IP , #00001100B gives priority ordergives priority order

    1.1. Int1Int1

    2.2. Timer1Timer1

    3.3. Int0Int0

    4.4. Timer0Timer0

    5.5. SerialSerial

    --- PX0PT0PX1PT1PSPT2---

  • 8/9/2019 Micro Controller Archtecture Programming

    72/94

    Interrupt inside an interruptInterrupt inside an interrupt

    --- PX0PT0PX1PT1PSPT2---

    A highA high--priority interrupt can interrupt a lowpriority interrupt can interrupt a low--

    priority interrupypriority interrupy

    All interrupt are latched internallyAll interrupt are latched internally

    LowLow--priority interrupt wait untilpriority interrupt wait until 80518051 hashas

    finished servicing the highfinished servicing the high--priority interruptpriority interrupt

  • 8/9/2019 Micro Controller Archtecture Programming

    73/94

    Applications of MicrocontrollersApplications of Microcontrollers

  • 8/9/2019 Micro Controller Archtecture Programming

    74/94

    Simple Interfacing ExamplesSimple Interfacing Examples

  • 8/9/2019 Micro Controller Archtecture Programming

    75/94

    Seven segment InterfacingSeven segment Interfacing

  • 8/9/2019 Micro Controller Archtecture Programming

    76/94

    Traffic light controllerTraffic light controller

  • 8/9/2019 Micro Controller Archtecture Programming

    77/94

    Cl d l lCl d l l

  • 8/9/2019 Micro Controller Archtecture Programming

    78/94

    Closed loop control systemClosed loop control system--

    Temperature control exampleTemperature control example

  • 8/9/2019 Micro Controller Archtecture Programming

    79/94

  • 8/9/2019 Micro Controller Archtecture Programming

    80/94

  • 8/9/2019 Micro Controller Archtecture Programming

    81/94

  • 8/9/2019 Micro Controller Archtecture Programming

    82/94

  • 8/9/2019 Micro Controller Archtecture Programming

    83/94

  • 8/9/2019 Micro Controller Archtecture Programming

    84/94

  • 8/9/2019 Micro Controller Archtecture Programming

    85/94

  • 8/9/2019 Micro Controller Archtecture Programming

    86/94

    Recent Wonders

  • 8/9/2019 Micro Controller Archtecture Programming

    87/94

    Recent Wonders

  • 8/9/2019 Micro Controller Archtecture Programming

    88/94

  • 8/9/2019 Micro Controller Archtecture Programming

    89/94

    Recent Wonders contd ........Recent Wonders contd ........

    B k h h h l d d d hB k h h h l d d d h

  • 8/9/2019 Micro Controller Archtecture Programming

    90/94

    Books that have helped me to understand theBooks that have helped me to understand the

    Microcontrollers & embedded systemsMicrocontrollers & embedded systems

    1)1) Barr, Michael, Programming embedded sytemsBarr, Michael, Programming embedded sytems

    in C and C++in C and C++ -- OReilly Publ.OReilly Publ.

    2)2) Raj Kamal, Embedded systems,Raj Kamal, Embedded systems, TMGTMG3)3) Mazidi and Mazidi, The 8051 microcontrollerMazidi and Mazidi, The 8051 microcontroller

    and embedded sytemsand embedded sytems -- Pearson education.Pearson education.

    4)4) Peatman,J.B. Design with microcontrollers andPeatman,J.B. Design with microcontrollers andmicrocomputers, McGraw Hilmicrocomputers, McGraw Hil

  • 8/9/2019 Micro Controller Archtecture Programming

    91/94

    5)5) Sewart. J.W. The 8051 MicrocontrollerHardware,Sewart. J.W. The 8051 MicrocontrollerHardware,Software and InterfacingSoftware and Interfacing PrenticeHallPrenticeHall

    6) Ayala Kenneth, The 8051 MicrontrollerAyala Kenneth, The 8051 Microntroller Architecture, Programming and ApplicationsArchitecture, Programming and Applications Delmar Publ.Delmar Publ.

    7)7) Ajay Deshmukh, MicrocontrollersAjay Deshmukh, Microcontrollers TATATATA

    McGrawH

    illMcGrawH

    ill8)8) Rajkamal, MicrocontrollersRajkamal, Microcontrollers -- Architecture,Architecture,ProgrammingProgramming Pearson Publ.Pearson Publ.

    9) Myke Predko, Programming the 8051 MicrocontrollerMyke Predko, Programming the 8051 Microcontroller McGrawHillMcGrawHill

    10)10) Michael J. Pont, Embedded CMichael J. Pont, Embedded C -- Addison WeselyAddison WeselyPubl.Publ.

  • 8/9/2019 Micro Controller Archtecture Programming

    92/94

    Useful websitesUseful websites

  • 8/9/2019 Micro Controller Archtecture Programming

    93/94

    Useful websitesUseful websites contdcontd

    1.1. http://www.eghttp://www.eg33.com.com

    2.2. http://www.ARM.MCU.comhttp://www.ARM.MCU.com

    3.3. http://www.mcjournal.comhttp://www.mcjournal.com

    4.4. http://www.iar.comhttp://www.iar.com

    5.5. http://http://http://www.embedded.comhttp://www.embedded.com

    6.6. http://www.powersoftsystems.comhttp://www.powersoftsystems.com

  • 8/9/2019 Micro Controller Archtecture Programming

    94/94

    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!