8051 Tutorials 2

Embed Size (px)

Citation preview

  • 8/8/2019 8051 Tutorials 2

    1/94

    Chapter1: Microcontrollers

    1.1 What is a microcontroller1.2 How does a microcontroller work

    1.3 ROM

    1.4 RAM

    1.5 EEPROM

    1.6 SFR Registers

    1.7 Program Counter

    1.8 Control logic

    1.9 A/D Converter

    1.10 I/O Ports

    1.11 Oscillator

    1.12 Timers

    1.13 Watchdog Timer

    1.14 Power Supply Ciruit

    Chapter 2: 8051 microcontroller

    2.1 Introduction

    2.2 Input Output ports

    2.3 Extra memory

    2.4 89s52

    Chapter 3: Introduction Assembler, Opcodes and addressing

    3.1 Assembly Language Programming

    3.2 Opcodes

    3.3 Addressing

  • 8/8/2019 8051 Tutorials 2

    2/94

    Chapter 4: UMPS

    4.1 Introduction

    4.2 Your first program in simulation

    4.3 Test your program in hardware

    Introduction - It is all Faraday's fault

    !

    " # $

    % &

    ' "

    " (

    &

    )$#

    *

    +

    ,

    -

    (

    #

    &

    !

  • 8/8/2019 8051 Tutorials 2

    3/94

    ' #

    .

    ./+-01-1%01220

    What are microcontrollers and what are they for?

    '

    3

    4 , 5 6,61

    7 8 /

    5

    $

    # "

    '

    9 :

    61 2-,

    5

    & ; ;'-9:

    9:

    /

    How does microcontroller work

  • 8/8/2019 8051 Tutorials 2

    4/94

    '

    3

    4 ;* $;"

    7 $ ; " /0

    ;

  • 8/8/2019 8051 Tutorials 2

    5/94

    8 " $

  • 8/8/2019 8051 Tutorials 2

    6/94

    01++-)

    90+:901+:

    SFR Registers

    / 0 0+

    +-)

    $' " /0 61

    9

    :

    Program Counter

    $$3

    , $ !

    @!

    )!-

    A3A3A3

    In case you forgot...

    9

    :$3

    3BB

    #

    #"9

    :

    Control Logic

    ==

    +-)

    '

    C "

    C -

    +-)

  • 8/8/2019 8051 Tutorials 2

    7/94

    C /0 2) 9:

    1 /0

    / 0

    59"":

    In case you forgot...

    D!

    2)

    9:9:

    4

  • 8/8/2019 8051 Tutorials 2

    8/94

    &+-)

    I/O Ports

    +-) 9 :

    &61.=

    5

    Oscillator

    /

    +-) -

    ;";"#

    " ; 9 0- : %

    $

    9$

    +-): 4B+@" ;"#

    $

  • 8/8/2019 8051 Tutorials 2

    9/94

    Watchdog Timer

    +-)0-&+-)

    $9!:

    &

    $

    $ &

    9 :

    - #

    +-)

    In case you forgot...

    >

    $

    $+-)

    ;

    -9:$

    0+-

    #$

  • 8/8/2019 8051 Tutorials 2

    10/94

    Power Supply Circuit

    +-)'

    C

    /

    +-)

    # +-)

    Reset pin frequently marked as MCLR (Master Clear Reset) is used for "external" reset of microcontroller by

    bringing the logical zero or one (depending on the MCU). If not preinstalled, a simple external brown outreset circuitcan be connected to this pin.

    !"#$%

    +-);#;6,DB

  • 8/8/2019 8051 Tutorials 2

    11/94

    801+ 34D0+9/0:

    87461$

    1DB3493B33:

    4

    F' 0 /> +-) ="

    !

    +-),5

    # ##

    9:

    "

  • 8/8/2019 8051 Tutorials 2

    12/94

    3B#3H'7>3@7'

    3B'0I,#

    33'I,#

    34'%B#B 37'%3#3 38'B#B 3 J" ; -

    9:7B

    ;"

    98H :

    %+-);B@"

  • 8/8/2019 8051 Tutorials 2

    13/94

    7B'2>=$+-) 9 B C H: B 2

    $9H8@-7H7H8@-7HA

  • 8/8/2019 8051 Tutorials 2

    14/94

    61B@324

    &$9DC3

  • 8/8/2019 8051 Tutorials 2

    15/94

    8 D 0B # 0H

    / 9 4B C 4: # 9B H:/3?

    34D9=B4BB

    H4H:

    4CH9DB :

    *+,

    5#0+DB

  • 8/8/2019 8051 Tutorials 2

    16/94

    '/0;>$0++$

    #90+01+:$?8M=&614755

    !+-)DB

  • 8/8/2019 8051 Tutorials 2

    17/94

    Introduction

    4B +-) DB

  • 8/8/2019 8051 Tutorials 2

    18/94

    MCU AT89S8252 ID Card

    +DB

  • 8/8/2019 8051 Tutorials 2

    19/94

  • 8/8/2019 8051 Tutorials 2

    20/94

    ;;;36?;

    $

    9B#H:

    +-)

    PSEN

    /$901+:

    EA/VPP

    & +-) $

    9 :

    9E--: ,

    A34E

    XTAL 1

    "+-)

    $

    XTAL 2

    $

    SFR Registers (Special Function Registers)

    +-)DF/D4

  • 8/8/2019 8051 Tutorials 2

    21/94

    =

    0

    9 2): &

    ,

    ;+-)

    "R" registers (R0 - R7)

    /00

    8 0+

    /&

    0

    14/

    $&9$

    :

    B register

    =19:

    P0, P1, P2, P3 - I/O Ports

    $8974

    61:

    9B

  • 8/8/2019 8051 Tutorials 2

    22/94

    //

    & / 3>

    / 3 9 :

    H 0+ /

    //

    Program Status Word (PSW)

    /&/0

    2)

    )! #93:59B:

    ##+-)5 '5- #$4

  • 8/8/2019 8051 Tutorials 2

    23/94

    ,',@9,@:,29,

    2:,53?$/

    3?#

    Counters and Timers

    +-) ;"

    ;9

    :

    >

    +-)

    /34$

    ;" 34 +@" 6

    9:

    DF/D4

  • 8/8/2019 8051 Tutorials 2

    24/94

    $ B G B " B 3BBB9: @B 9 : 7 2B 9 :

    474/

    3?#'

    TH0 * 256 + TL0 = T

    1$'

    3 * 256 + 232 = 1000

    3?#$?

  • 8/8/2019 8051 Tutorials 2

    25/94

    B#H'

    1 ) $

    H K3 3%3977:

    B%3977:

    3

    ? -63 3397

  • 8/8/2019 8051 Tutorials 2

    26/94

    ; ! +-) &

    @B

    "9B#4

  • 8/8/2019 8051 Tutorials 2

    27/94

    : ) 4BB B + 4 +-)

    %2BD#@B

    4BB$&2B

    @B

  • 8/8/2019 8051 Tutorials 2

    28/94

  • 8/8/2019 8051 Tutorials 2

    29/94

    TH0=15, TL0=255.

    /

    '

    TH0=14, TL0=255.

    94

    #1&B-1%

    /

    BB

  • 8/8/2019 8051 Tutorials 2

    30/94

    90BG3:

    %B

    $

  • 8/8/2019 8051 Tutorials 2

    31/94

    Timer T1

    B+1,

    -1%8

    Timer T2

    3?#6+-))B3

    8 @4 24 3?#

    1 0-4@ 0-42

  • 8/8/2019 8051 Tutorials 2

    32/94

    +"

    4

    B3$

    T2CON (Timer/Counter 2 Control Register)

    04-1% 4 $

    '

    4 93?#:

    $

    0-2M-2M

    4

    I4 4I

    ,-%4-1%

    I4

    0-2M ##04

    !#03

    -2M ##4

    !#3

    I%4 ##4I

  • 8/8/2019 8051 Tutorials 2

    33/94

    !#4I

    04 ##/4

    !#/4

    -64 ##3?#94@42:-64

    !#3?#94@42:

    -6024 ##9I%G3:4I

    !#)4I

    Modes of Timer T2

    Capturemode

    -6024 4-1% 4 # !9

    4@ 42: 9

    0-4@0-42:

    3 3?#9@4A24:1,4 / 04 -1% >

    39?

  • 8/8/2019 8051 Tutorials 2

    34/94

    ##/$

    41

    3B9

    :

    @424

    0-4@0-42

    I4

    -64

    !#-934

    ;":

    ##-4

    Auto Reload Mode

    40-6024

    ,-%

    4+1,'

    41#

    -%#

    )! 90-4@0-42:

    9@424:/0

    '

    4-1%I%4

    !#)44-1%

    0-4@0-42

  • 8/8/2019 8051 Tutorials 2

    35/94

    ##/$

    41

    3B9

    :

    0-4@0-42

    @424

    I4

    -64

    !#-934

    ;":

    ##-4

    4 4+1, 5 ,-% G B 1 9 :

    ,4I'

    4I G B' 4

    4IG3'4

    $

    I4

    44@42

    '?

  • 8/8/2019 8051 Tutorials 2

    36/94

    Quartz oscillator frequency

    Baud Rate =

    32 * (65536 - (RCAP2H, RCAP2L))

    '

    3 ;9434:

    4 147 &I%44I

    4I$

    8 8&904GB:@4241

    Timer T2 as independent clock generator

    $ 3B 9 4 :

    #

    ; ) 3?+@" ;" ;

    ?3@"8+@"##

  • 8/8/2019 8051 Tutorials 2

    37/94

    -

    =/0/-1%9

    !:'"

    "

    SCON (Serial Port Control Register)

    1 )

    /+B F +

    /+3 F +

    /+4 F, +-)5

    0+ F- &

    =D F= F47

    0=D F F47

    FF =

    0 FD =

    /+B /+3

    '

    ! #

    B B B D#/ J";634

    B 3 3 D#)0 ,34

    3 B 4 F#)0 J";674

  • 8/8/2019 8051 Tutorials 2

    38/94

    3 3 7 F#)0 ,34

    1B4$!379

    :347

    /+1,-1%9:

    Mode 0

    + B 61

    $ ;

    61

    $7461

    Mode 1

    0/#474D#/;

    I,0I,'9B:

    D 92/= : 9 3: /

    &

    0=D /-1%

    0/#474 #

    Modes 2 and 3

    ; 9+ 4:

    0/#474 9+ 7: = F#

    '9B:D92/=:F

    =D 0=D

    93:

    1/-1%'

    )

    /+4 $

    1

    0% %

    =D $FF#947:

  • 8/8/2019 8051 Tutorials 2

    39/94

    0=D /=D&F#

    0 /

    Setting the Baud Rate

    1)0=0

    = 0 B 4 ; ;" -

    ;

    $ 9 33B

  • 8/8/2019 8051 Tutorials 2

    40/94

    B B 0 ; 3

    B 3 034

    3 B 043

    3 3 0 ; 4

    Sending and Receiving Data via UART

    1)0

    /=)

    +-)

    ;/-1%/

    #09/-1%:

    !/=)

    Watchdog Timer (WDT)

    &0-

    #,

    $ / &,% &+-1% &

    &,0/9&0

    &+-1%:51&

    &

    /&+-1%'

    PS2, PS1, PS0

    7#&

    &,0/+-)

  • 8/8/2019 8051 Tutorials 2

    41/94

    )- )# )! %3,

    B B B 3?

    B B 3 74

    B 3 B ?8

    B 3 3 34D

    3 B B 4

  • 8/8/2019 8051 Tutorials 2

    42/94

    SPI (Serial Peripheral Interface) System

    / 9 % :

    - "

    "

    # # /

    '/-0 9-0://0 9/0:

    /,09,0:

    SPCR (SPI Control Register)

    /0//-0'

    )

    / &/

    / &/

    ,10, ##&2

    !#&+

    +/0 ##/

    !#/

  • 8/8/2019 8051 Tutorials 2

    43/94

    -12 ##,/-M

    !#,/-M+

    -@ -12-@

    /03

    /0B

    -;

    9:

    )# )! /0

    B B 68

    B 3 63?

    3 B 6?8

    3 3 634D

    SPSR (SPI Status Register)

    /0/'

    )

    / )/

    /

    &-12 /,0/

    SPDR (SPI Data Register)

    / 1 /,0 9

    +:-)

    +1/)/

    //

  • 8/8/2019 8051 Tutorials 2

    44/94

    Interrupts

    DF/D4

  • 8/8/2019 8051 Tutorials 2

    45/94

    1

    '

    3 0;!

    4 3-7 4-

    IP (Interrupt Priority)

    /0 $

    9/ ' 3

    :'

    )

    4 4

    / /

    3 3

    I3 $%3

    B B

    IB $%B

    ;$

    ;

    How does interrupt execute?

    );'

  • 8/8/2019 8051 Tutorials 2

    46/94

    3 -$4 $$

    ;

    7 , ; ?9:

    5+

    B 7

    B =

    3 37

    3 3=

    0/ 47

    4I4 4=

    8

  • 8/8/2019 8051 Tutorials 2

    47/94

    -8

    ,

    DB

  • 8/8/2019 8051 Tutorials 2

    48/94

    2. #-'S787S)+/S$&

    $

    3. -)-

    @I3?9$:

    4. -)0

    $0K

    5. -)01+8M01+DB

  • 8/8/2019 8051 Tutorials 2

    49/94

  • 8/8/2019 8051 Tutorials 2

    50/94

    6. 0)+/

    9:

  • 8/8/2019 8051 Tutorials 2

    51/94

  • 8/8/2019 8051 Tutorials 2

    52/94

    7. 0+$/-34D0+DB

  • 8/8/2019 8051 Tutorials 2

    53/94

    )+/92:

    9-:$-)

    -9-:

    -)9-):

    )+/,E2

    &55'

    DB739DB

  • 8/8/2019 8051 Tutorials 2

    54/94

    0-),9DB732+787E+,:

    0-) $7%!

    !(-)6

    E=9

    2+#787:

    )+/

    25'

    );

    /U1'

    /+!

    ;

    K6@'

    $9:&

    @2

    0'/

    9-Q BBBB:

    /9:'

    ' &$9D:

    '9H: $'$9#D:

    ='

    6-)$

    -'

    -9/+I:

    /-9-:'

    )-9-:'

    /- /- /-#

  • 8/8/2019 8051 Tutorials 2

    55/94

    060'

    0-)0&'

    - , $9-Q

    -Q -)V0:

    %1

    &;

    %-

    2)'-)9@$:9:9:

    9:

    2!#=%6%

    E

    )-)0+01+/05

    -#O2-)

  • 8/8/2019 8051 Tutorials 2

    56/94

  • 8/8/2019 8051 Tutorials 2

    57/94

  • 8/8/2019 8051 Tutorials 2

    58/94

    -)&

    DB739DB

  • 8/8/2019 8051 Tutorials 2

    59/94

    @

    )-),

    *

    &55#*!

    DBB$?BB

    )=9

    3:0)+/,

    4:-91#X+#X:

    3) )Q Y2!Z?@='12,3,11&9%!:

    4) )+/E=9E=:-)--)0&

    5) -0/

  • 8/8/2019 8051 Tutorials 2

    60/94

  • 8/8/2019 8051 Tutorials 2

    61/94

  • 8/8/2019 8051 Tutorials 2

    62/94

  • 8/8/2019 8051 Tutorials 2

    63/94

  • 8/8/2019 8051 Tutorials 2

    64/94

  • 8/8/2019 8051 Tutorials 2

    65/94

  • 8/8/2019 8051 Tutorials 2

    66/94

  • 8/8/2019 8051 Tutorials 2

    67/94

  • 8/8/2019 8051 Tutorials 2

    68/94

  • 8/8/2019 8051 Tutorials 2

    69/94

    ?: -K1-)$E=

    '

    06(6K9:; -,D2,

    =3,/H# =4H# /&1%!2,

    /&41%H#

    7) @2$8) )#%'

  • 8/8/2019 8051 Tutorials 2

    70/94

    )/#9D:-222,V,

    )/#9H:22 )/#92#D:$1ME=!5

    ,2,

    2/)/

    3:--)-#O2-)'7#@@&

    %14:2E=

    '

    Q 2

    )@'6V7876

    ?@?@

    7:E-)E!Q -)-

    8:1-)-%19

    $%0:

    *$)-@8

    4'5)#6)!

    A)'')

    %$ $9/48:

    [92':

    #(Q -2>

  • 8/8/2019 8051 Tutorials 2

    71/94

    /)-@8CD7

  • 8/8/2019 8051 Tutorials 2

    72/94

  • 8/8/2019 8051 Tutorials 2

    73/94

    2) -3) 1-1M

  • 8/8/2019 8051 Tutorials 2

    74/94

  • 8/8/2019 8051 Tutorials 2

    75/94

    4) 5) 0$,#16) -*+$7) 0,1*

    %1'

    2=2'%/0)-1%/>'

  • 8/8/2019 8051 Tutorials 2

    76/94

  • 8/8/2019 8051 Tutorials 2

    77/94

    -

  • 8/8/2019 8051 Tutorials 2

    78/94

    dec r1mov @r1,10hmov @r1,#0aahmov dptr,#99hmovx @dptr,amov a,#55h

    movx @r1,amovx a,@dptrmovx @r1,ainc dptrinc r1movc a,@a + dptrmovc a,@a + pcmov sp,#40hacall testsaddadd a,10hadd a,@r0add a,#61hadd a,r1

    addc a,10haddc a,@r0addc a,r1addc a,#45hajmp saddnop

    sadd:anl a,10hinc aanl a,@r1anl a,#0aahanl a,r1anl 10h,a

    anl 10h,#55hclr ccpl cclr 00hcpl 00hanl c,00hanl c,/00hcjne a,10h,here

    there:sjmp compare

    here:jb 00h,there

    compare:cjne a,#42h,whereevery:

    jbc 00h,nextwhere:

    jc everynext:

    cjne @r0,#11h,againdone:

    clr c

  • 8/8/2019 8051 Tutorials 2

    79/94

    jnc leaveagain:

    cjne r0,#68h,doneleave:

    jnz gonop

    go:cpl acpl 11hmov r6,axrl a,r6mov a,#99hadd a,#99hda adec adec 15hdec @r0mov b,#11hdiv ab

    djnz 11h,lessmore:

    clr ajz some

    less:djnz r1,more

    some:inc 12hinc @r0lcall laddljmp long

    back:mov 12h,c

    mov c,13hnopmul aborl a,15horl a,@r0orl a,#97horl a,r1orl 20h,aorl 20h,#0fehorl c,/20horl c,30hpush 10h

    pop 3chrl arlc arr arrc asetb 15hsetb csubb a,11hsubb a,@r0subb a,#22h

  • 8/8/2019 8051 Tutorials 2

    80/94

    swap axch a,17hxch a,@r0xch a,r6xchd a,@r1xrl a,15h

    xrl a,@r1xrl a,#0ffhxrl a,r6xrl 15h,axrl 16h,#77hmov ie,#0ffhljmp over

    testsadd: retladd: retlong: ljmp backend

    Exercise 2:

    Write a program to complement the given program

    Ans:;program to find the 2's complement fo 8 bit

    org 00hmov 20h,#20h ;i/p numbermov 21h,#00h ;result

    mov a,20hcpl ainc amov 21h,a ;result

    loop: sjmp loop

    Exercise 3:

    Write a program to add two 16 bit numbers

    Ans:;program to do 16 bit addition

    org 00hmov r0,#0ffh ;First no HBmov r1,#0ffh ;first no LBmov r2,#0ffh ;second no HBmov r3,#0ffh ;second no LB

    ;;;;;;;;;;;;;;;

  • 8/8/2019 8051 Tutorials 2

    81/94

    ;results are stored from R5 R6 R7 registers;;;;;;;;;;;;;;

    mov a,r1 ;lower byte additionadd a,r3mov r7,a

    mov a,r0 ;upper byte additionaddc a,r2mov r6,a

    jnc nocarrymov r5,#01h

    nocarry:loop: sjmp loop

    Exercise 4

    Write a program to subtract two 16 bit numbers

    Ans:________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ______________________________________

    Exercise 5

    Write a program to multiply two 16 bit numbers

    Ans.

    ________________________________________________________________________

  • 8/8/2019 8051 Tutorials 2

    82/94

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ____________________________________

    Exercise 6

    Write a program to convert binary to gray code

    Ans:____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    _________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    Exercise 7

    Find out the largest numbers stored in given memorylocations

    Ans:

    ;the 10 numbers are stored in the internal ram from 20h to29h

    org 00h;-------------------------------------;store the list of 10 elements here;-------------------------------------mov 20h,#40h ;enter the data required

  • 8/8/2019 8051 Tutorials 2

    83/94

    mov 21h,#23hmov 22h,#25hmov 23h,#98hmov 24h,#0a5hmov 25h,#0fahmov 26h,#56h

    mov 27h,#77hmov 28h,#0ffhmov 29h,#7dh

    mov r0,#20hmov a,@r0

    up:mov 01h,@r0cjne a,01h,lesser_greatersjmp equal

    lesser_greater:jnc smallermov a,01h

    equal:smaller:inc r0

    cjne r0,#2ah,uplast: sjmp last

    ;----------------------------;the largest no is in the acc;----------------------------

    Exercise 8

    Sort the given numbers in i) ascending order ii) Descendingorder

    ans:________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ________________________________________________________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

  • 8/8/2019 8051 Tutorials 2

    84/94

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ________________________________________________________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ____________________________________________

    ________________________________________________________________________________________

    ____________________________________________

    ________________________________________

  • 8/8/2019 8051 Tutorials 2

    85/94

    Exercise 9

    Generate 15us s/w time delay

    ;program to ilustrate the s/w delays

    org 00h;acall delay15usacall delay1ms

    loop: sjmp loop

    ;----------------------------------------------;delay of 15 micro sec including acall ;;-----------------------------------------------

    delay15us: mov r0,#5decrement: djnz r0,decrement

    ret

    Generate 5u sec time delay

    Ans:;--------------------------------------------;delay of 1m and 5 micro sec including acall;--------------------------------------------delay1ms: mov r0,#250

    up: nopnopdjnz r0,upret

    Exercise 10

    Write a program to blink a led

    ans:;program to blink a led

    org 00h

    back: setb p1.0 ;led on

    acall delay500ms

    clr p1.0 ;led off

    acall delay500ms

    sjmp back

  • 8/8/2019 8051 Tutorials 2

    86/94

    delay500ms: mov r7,#2

    up2: mov r6,#249

    up1: mov r5,#250

    up: nop

    nop

    djnz r5,up

    djnz r6,up1

    djnz r7,up2

    ret

    Exercise 11

    Write a program blink alternative leds connected to a port

    ans:

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________

    _________________________________________________________

    Exercise 12

    Write a program to display number 0 t0 3ans:;program to make a seven segment led to count 0 to 3

    ;negative logic COMMON_0 to Always 1

    ;;;;;; 1 switch off the segment

    ;;;;;; 0 switch on the segement

    ;8 segments connected to the 8 pins of the port 0

    ; eg p0.0 to a segment

    ;eg p0.1 to b segment etc..........

    ; 7 6 5 4 3 2 1 0

    ; pts g f e d c b a

    ; 0 1 0 0 0 0 0 0 display 0

    ; 0 1 1 1 1 0 0 1 " 1

    ; 0 0 1 0 0 1 0 0 " 2

    ; 0 0 1 1 0 0 0 0 " 3

  • 8/8/2019 8051 Tutorials 2

    87/94

    org 00h

    repeat: mov p0,#40h

    acall delay

    mov p0,#79h

    acall delay

    mov p1,#24h

    acall delay

    mov p2,#30h

    acall delay

    sjmp repeat

    delay: mov r7,#2

    up2: mov r6,#249

    up1: mov r5,#250

    up: nop

    nop

    djnz r5,up

    djnz r6,up1

    djnz r7,up2

    ret

    Exercise 13

    Write a program to display ISM on alphanumeric displayAns:

    ;program to blink ISM on the alphanumeric display

    ;P0.0 - p0.6 connected to D0-D7 in the sequence;p1.0 - connected for the A0 address lines

    ;p1.1 - connected for the A1 address lines

    ;p2.0 connected to write line which is active low

    org 00h

    ;to display the character the left most

    unit

    ;address is A1 A0 is 1 1

    back: mov p1,#%00000011

    mov P0,#'I'

    clr P2.0setb p2.0

    mov p1,#%00000010

    mov P0,#'S'

    clr P2.0

    setb p2.0

    mov p1,#%00000001

    mov P0,#'M'

  • 8/8/2019 8051 Tutorials 2

    88/94

    clr P2.0

    setb p2.0

    acall delay

    ;clears the all the four units

    mov p1,#3

    mov p0,#' '

    clr p2.0

    clear: djnz 90h,clear

    setb p2.0

    acall delay

    sjmp back

    delay: mov r7,#2

    up2: mov r6,#249

    up1: mov r5,#250

    up: nop

    nop

    djnz r5,up

    djnz r6,up1

    djnz r7,up2

    ret

    Exercise 14

    Write a program to scroll a string in alphanumeric display

    modules________________________________________________________________________________________________________________________

    ____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    ____________

    Exercise 15

    Generate time delay of 50 ms using timer 1, mode 1ans:;program to do a delay of 50 ms

  • 8/8/2019 8051 Tutorials 2

    89/94

    ; to do 1 sec delay take out the comments at the line

    number 7 and 13

    org 00h

    mov tmod,#10h

    ;mov r0,#20 ;7th line

    up: mov th1,#3ch

    mov tl1,#0b0h

    setb tr1

    wait: jnb tf1,wait

    clr tr1

    clr tf1

    ;djnz r0,up ;13th line

    last: sjmp last

    Exercise 16

    generate square wave at pin number p0.0 using timer assource of delay(use mode2,timer 0)ans:____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    ____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

    Exercise 17

    Connect a rectangular signal generator at p3.4and find out the frequency of the source by s/w

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

  • 8/8/2019 8051 Tutorials 2

    90/94

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ____________________________________

    Exercise 18

    communicate with the pc(hyper terminal) @9600 baud rate;program to transmit the data to the pc at the 9600 bps

    ;set the crystal to 11.0592 mega hz

    org 00h

    mov scon,#40h ;mode 1, 8 bit UART, variable baud

    rate

    mov tmod,#20h ;timer 1 in auto reload mode

    mov th1,#0fah ;9600 bs

    mov tl1,#0fah

    setb tr1 ;start the serial coimmunication

    repeat:mov dptr,#data

    back: clr a

    movc a,@a+dptr

    jz repeat

    mov sbuf,a

    wait: jnb TI,wait

    clr TI

    sjmp back

    data: db 'Hello world',0

  • 8/8/2019 8051 Tutorials 2

    91/94

    end

    Exercise 19

    connect a switch as source of external interrupt and onoccurance of interrupt (on button press) blink a led asinterrupt service routinans:

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________________________________________________________________________________

    ________________________________________________________________________

    ________________________________________________________________________

    _________

    Exercise 20

    blink the led at equal interval of the timeuse timer1 interrupt.

  • 8/8/2019 8051 Tutorials 2

    92/94

    Step 7. Open the 89S52 flash programmer

    Step 8. Open the created hex file

  • 8/8/2019 8051 Tutorials 2

    93/94

    Note 1. Make sure the serial cable and the power supply are connected

    Note 2. The DIP Switch should be in programming mode

  • 8/8/2019 8051 Tutorials 2

    94/94

    Step 9. Click the write button on the flash programmer IDE

    Step 10. After writing, click the read button and verify the hex code.

    Step 11. Reset the device and change the mode to normal operation.

    Step 12. View your output.