Vi điểu khiển 8051 chương 1 - Introduction to Microcontrollers

Embed Size (px)

Citation preview

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    1/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    1

    The 8051 Microcontroller

    L Ch Thng

    Ref. I. Scott Mackenzie, The 8051 Microcontroller

    Instructor

    L Ch Thng

    Faculty of Electrical and Electronics Engineering

    Ho Chi Minh City University of Technology

    (i Hc Bch Khoa TP.HCM)

    Email: [email protected]; [email protected]

    Website: www.tinyurl.com/thongchile

    2L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    2/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    2

    Objectives

    Introduction fundamentals and applications ofmicroprocessors and microcomputers.

    Architecture, the instruction set, and applications of8051 microcontroller family

    Basic applications of microprocessors, such asinput/output, analog-digital conversion (ADC), and

    digital-analog conversion (DAC), and data acquisition.

    3L Ch ThngRef. I. Scott Mackenzie

    Grading

    Quizzes and homework assignments: 20%

    Homework is due at the beginning of class

    Mid-term: 30%

    Final: 50%

    4L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    3/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    3

    Textbooks

    The 8051 Microcontroller, 2nd Edition, I. ScottMacKenzie, Prentice-Hall, 1995

    The 8051 Microcontroller: Architecture, Programming,and Applications, Kenneth J. Ayala, West Publishing

    Company

    5L Ch ThngRef. I. Scott Mackenzie

    Chapter 1

    Introduction to Microcontrollers

    6L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    4/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    4

    These are

    embedded systems

    An embedded system is a system in which aprocessor/microcontroller/computer is embedded toperform a specific task or tasks

    7L Ch ThngRef. I. Scott Mackenzie

    General-Purpose Microprocessor System

    Microprocessors

    Integrated ALU and CU

    No RAM, ROM, I/O on CPU chip itself

    Example: Intels x86, Motorolas 680x0

    CPU

    General-

    Purpose

    Micro-

    processor

    RAM ROM I/O

    PortTimer

    Serial

    Port

    Data Bus

    Address Bus

    Many chips on mothers board

    Control Unit(CU)

    ArithmeticLogical Unit (ALU)

    Central Processing Unit (CPU)

    8L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    5/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    5

    FIGURE 12 Block diagram of a microcomputer system

    Architecture of computers

    9L Ch ThngRef. I. Scott Mackenzie

    FIGURE 13 The central processing unit (CPU)

    Architecture of CPU

    PC-Program counter

    10L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    6/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    6

    FIGURE 14 Bus activity for an opcode fetch cycle

    Opcode fetch, decode, execution

    11L Ch ThngRef. I. Scott Mackenzie

    FIGURE 15 Levels of software

    Booster loader,BIOS

    Utilities

    Shell, GUI user- hardware

    Keyboard, monitorOther hardwires

    12L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    7/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    7

    FIGURE 16 Detailed block diagram of a microcomputer system

    13L Ch ThngRef. I. Scott Mackenzie

    Integrates CPU, RAM, ROM, I/O ports, on asingle chip

    Sometimes called a "computer on a chip"

    Typically used in embedded applications

    Example: Motorolas 6811, Intels 8051, Zilogs Z8and PIC 16X

    RAM ROM

    I/O

    PortTimer

    Serial

    COM

    Port

    Microcontroller

    CPU

    A single chip

    Microcontrollers

    14L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    8/20

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    9/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    9

    Embedded System

    Microcontroller(uC)

    sensor

    Sensorconditioning

    Outputinterfaces

    actuator

    indicator

    sensor

    sensor

    17L Ch ThngRef. I. Scott Mackenzie

    Positive radix, positional number systems

    A number with radix r is represented by a string ofdigits:

    An - 1An - 2 A1A0 . A- 1A- 2 A- m + 1A- min which 0 < Ai < r and . is the radix point.

    The string of digits represents the power series:

    ( ) ( )(Number)r = +j = - m

    jj

    i

    i= 0i rArA

    (Integer Portion) + (Fraction Portion)

    i = n - 1 j = - 1

    Representation of Number Systems

    18L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    10/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    10

    General Decimal Binary

    Radix (Base) r 10 2

    Digits 0 => r - 1 0 => 9 0 => 1

    0

    1

    2

    3

    Powers of 4

    Radix 5

    -1

    -2

    -3

    -4

    -5

    r0

    r1

    r2

    r3

    r4

    r5

    r -1

    r -2

    r -3

    r -4

    r -5

    1

    10

    100

    1000

    10,000

    100,000

    0.1

    0.01

    0.001

    0.0001

    0.00001

    1

    2

    4

    8

    16

    32

    0.5

    0.25

    0.125

    0.0625

    0.03125

    Representation of Number Systems

    19L Ch ThngRef. I. Scott Mackenzie

    400 + 0 + 7 + 0.6 + 0.02 + 0.005 = 407.625

    4 0 7 . 6 2 5

    102 101 100 . 10-1 10-2 10-3

    4x102 0x101 7x100 . 6x10-1 2x10-2 5x10-3

    400 0 7 . 0.6 0.02 0.005

    Decimal (Radix r = 10)

    Binary (Radix r = 2)

    1 0 1 . 0 1 1

    101.011 B = 4 + 0 + 1 + 0 + 0.25 + 0.125 = 5.375

    22 21 20 . 2-1 2-2 2-3

    1x22 0x21 1x20 . 0x2-1 1x2-2 1x2-3

    4 0 1 . 0 0.25 0.125

    20L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    11/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    11

    Hexadecimal or Hex (Radix r = 16)

    Hexadecimal Decimal Binary Hexadecimal Decimal Binary

    0

    1

    2

    3

    4

    5

    6

    7

    0

    1

    2

    3

    4

    5

    6

    7

    0000

    0001

    0010

    0011

    0100

    0101

    0110

    0111

    8

    9

    A

    B

    C

    D

    E

    F

    8

    9

    10

    11

    12

    13

    14

    15

    1000

    1001

    1010

    1011

    1100

    1101

    1110

    1111

    5 A 0 . 4 D 1

    5A0.4D1 H = 1280 + 160 + 0 + 0.25 + 0.0508 + 0.0002 = 1440.301

    162 161 160 . 16-1 16-2 16-3

    5x162 10x161 0x160 . 4x16-1 13x16-2 1x16-3

    1280 160 0 . 0.25 0.0508 0.0002

    21L Ch ThngRef. I. Scott Mackenzie

    Converting decimal to binary

    8 . 625 D = ? B

    8 : 2 = 4 remainder 0

    4 : 2 = 2 remainder 0

    2 : 2 = 1 remainder 0

    1 : 2 = 0 remainder 1

    0.625 x 2 = 1.25 save the integer digit 1

    0.25 x 2 = 0.5 save the integer digit 0

    0.5 x 2 = 1.0 save the integer digit 1

    1 0 0 0 .. 1 0 1 B

    22L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    12/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    12

    1 4 8 0 . 4 2 9 6 8 7 5 D = ? H

    1480 : 16 = 92 remainder 8

    92 : 16 = 5 remainder 12

    5 : 16 = 0 remainder 5

    0.4296875 x 16 = 6.875 save the integer digit 6

    0.875 x 16 = 14.0 save the integer digit 14

    5 C 8 .. 6 E H

    Converting decimal to hex

    23L Ch ThngRef. I. Scott Mackenzie

    1 1 1 0 1 1 0 1 0 1 1 1 0 1 . 0 1 1 0 1 0 1 B0 0 0

    . 6 A H

    2 C 9 . E 8 H

    0 0 1 0 1 1 0 0 1 0 0 1 . 1 1 1 0 1 0 0 0 B

    3 B 5 D .

    Converting binary to hex

    Converting binary to octal

    24L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    13/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    13

    BCD (Binary Coded Decimal)

    Decimal

    0

    1

    2

    3

    4

    5

    6

    78

    9

    BCD(8 4 2 1)

    0 0 0 0

    0 0 0 1

    0 0 1 0

    0 0 1 1

    0 1 0 0

    0 1 0 1

    0 1 1 0

    0 1 1 11 0 0 0

    1 0 0 1

    Ex: 12 = 0001 0010 (BCD)

    39 = 0011 1001 (BCD)

    25L Ch ThngRef. I. Scott Mackenzie

    ASCII Codeb

    6b

    5b

    4

    0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1

    b3b

    2b

    1b

    0Hex 0 1 2 3 4 5 6 7

    0 0 0 0

    0 0 0 1

    0 0 1 0

    0 0 1 1

    0 1 0 0

    0 1 0 1

    0 1 1 0

    0 1 1 1

    1 0 0 0

    1 0 0 1

    1 0 1 0

    1 0 1 1

    1 1 0 0

    1 1 0 1

    1 1 1 0

    1 1 1 1

    01

    2

    34

    56

    789

    AB

    CD

    EF

    NULSOH

    STX

    ETXEOT

    ENQACK

    BELBSHT

    LFVT

    FFCR

    SOSI

    DLEDC1

    DC2

    DC3DC4

    NAKSYN

    ETBCANEM

    SUBESC

    FSGS

    RSUS

    SP!

    #$

    %&

    ()

    *+

    ,-

    ./

    01

    2

    34

    56

    789

    :;

    ?

    @A

    B

    CD

    EF

    GHI

    JK

    LM

    NO

    PQ

    R

    ST

    UV

    WXY

    Z[

    \]

    ^_

    `a

    b

    cd

    ef

    ghi

    jk

    lm

    no

    pq

    r

    st

    uv

    wxy

    z{

    |}

    ~DEL

    26L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    14/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    14

    Boolean (Logical) Operations

    a NOT a

    0 1

    1 0

    a b a AND b

    0 0 0

    0 1 0

    1 0 0

    1 1 1

    a b a OR b

    0 0 0

    0 1 1

    1 0 1

    1 1 1

    a b a XOR b

    0 0 0

    0 1 1

    1 0 1

    1 1 0

    27L Ch ThngRef. I. Scott Mackenzie

    28

    Force a bit or bits to zero

    Sometimes called "masking out bits"

    Use a "mask" byte with

    0's in positions to be cleared (forced to 0)1's in positions to be unchanged

    Perform an AND with the mask

    x AND 0 = 0 (domination)

    x AND 1 = x (identity)

    Applying Boolean Operations

    L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    15/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    15

    29

    Force a bit or bits to one

    Sometimes called "setting bits"

    Use a "mask" byte with

    1's in positions to be set (forced to 1)

    0's in positions to be unchanged

    Perform an OR with the mask

    x OR 0 = x (identity)x OR 1 = 1 (domination)

    Applying Boolean Operations

    L Ch ThngRef. I. Scott Mackenzie

    30

    Toggle a bit or bits

    Sometimes called "flipping or complementingbits"

    Use a "mask" byte with1's in positions to be toggled (complemented)

    0's in positions to be unchanged

    Perform an XOR with the mask

    x XOR 0 = x (identity)

    x XOR 1 = NOT x

    Applying Boolean Operations

    L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    16/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    16

    31

    Bit patterns from two bytes are to be combined

    The important bits of each byte must be aligned with 0 (andunimportant) bits in the other byte

    The OR of the two bytes, combines them into one

    The bold bits are the important ones:

    00110000

    OR 00001010

    00111010

    Combining Bits in Separate Bytes

    L Ch ThngRef. I. Scott Mackenzie

    32

    Move bits in a byte to the left or right

    11011100 shifted left is 10111000

    11011100 shifted right is 01101110

    In these examples, a zero (0) was brought in tofill the vacated position

    Variations on the basic shift fill this positiondifferently

    Shifting

    L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    17/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    17

    33

    Move bits in a byte to the left or right in acircular pattern

    11011100 rotated left is 10111001

    11011100 rotated right is 01101110

    In these examples, the bit shifted out is used tofill the vacated position

    There are some variations of this behavior as well

    Rotating

    L Ch ThngRef. I. Scott Mackenzie

    34

    When a byte (or word) is interpretednumerically (as a binary representation of anumber)

    Left shift is equivalent to multiplication by 2Right shift is equivalent to division by 2

    5CH shifted left becomes B8H 92 * 2 = 184

    5CH shifted right is 2EH 92 / 2 = 46 (remainder if any is thrown away)

    Application of Shifts

    L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    18/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    18

    35

    Every multiplication can be accomplished byshifting and adding

    Just regroup using only multiplications by powersof 2 and additions

    10 * n = (8 + 2) * n

    = 8 * n + 2 * n

    or 10 * n = (2 * (4 + 1)) n

    = 2 * (4 * n + n)

    Multiplication Tricks

    L Ch ThngRef. I. Scott Mackenzie

    A computer systemcomponent that allows

    the storage and retrieval

    of data

    Main memory is usuallycalled RAM

    Memory is usuallyorganized as a table of

    bytes or words

    0000 3F

    0001 2C

    0002 41

    0003 FF

    0004 00

    0005 1E

    Addressesare usuallyshown in

    hexadecimal

    Data valuesare usually

    bytes

    Memory

    36L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    19/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    19

    37

    RAM: Random Access Memory

    Access time does not vary by address

    Read & write memory

    Typically volatile

    ROM: Read Only Memory

    Also a random access memory

    Retains data even when power is removed

    RAM and ROM

    L Ch ThngRef. I. Scott Mackenzie

    38

    RAM and ROM

    RAM ROM

    L Ch ThngRef. I. Scott Mackenzie

  • 7/31/2019 Vi iu khin 8051 chng 1 - Introduction to Microcontrollers

    20/20

    H Bch Khoa TP.HCM L Ch Thng-www.tinyurl.com/thongchile

    39

    References

    L Ch Thng

    I. Scott Mackenzie, The 8051 Microcontroller

    Nguyn Trng Lut, Bi ging K Thut S

    Cc ti liu trn Internet khng trch dn hoc khng ghi tcgi

    Ref. I. Scott Mackenzie