8051 interfacing switches & leds

Upload: imran-shaukat

Post on 06-Jul-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 8051 Interfacing Switches & LEDs

    1/5

    1

    8.8051 INTERFACING:SWITCHES & LEDS

    ObjectiveIn this tutorial, we will see how we could interface a simple switch to micrcontroller. Yes, it is indeed very

    simple, later in this tutorial we will do some logical operations with LEDs and Switches.

    Switc !"#ic#

    If we directly connect a switch to one of the microcontroller port pins, the pin floats when the switch is open.

    Meaning, it is not at a fixed logic level v or !v. "ence we pull the pin up through a resistor to !v(LOGIC 1),

    as shown in the figure #. $ow, whenever the switch is pressed, it grounds(LOGIC 0) the port pin.

     $ow we could go ahead and interface it to the microcontroller, %ut we would rather interface & switches and &

    LED's to two ports as shown in the schematic %elow.

    Sce$"tic

  • 8/17/2019 8051 Interfacing Switches & LEDs

    2/5

    %

    (igure )* Schematic Diagram

    +s we can see the schematic has %asic circuits for oscillator, reset and power connections for the

    micrcontroller.

    • + DI -dual in line pacage/ Switch, array of & switches is connected to PORT3 +$D & LEDs

    to PORT2.

    • 0%serve the RR1 component, it is array of & resistors in a single pac-SI/. It is as good as connecting

    & pullup resistors as shown in figure #. You could also use & discrete resistors as well.

    Experiments

    1. Re" Switce# "' Di#()"* St"t+# ,' LED#

    1his a very simple one, configure 0213 -switches/ as inputs and 021) -LEDs/ as outputs4 read the switch

    status and display on LEDs.

    1he code for the pro5ect is listed %elow*

  • 8/17/2019 8051 Interfacing Switches & LEDs

    3/5

    -

    • 1he LEDS and S6I17"ES as defined and assigned to ) and 3 respectively.

    • S6I17"ES-3 is made input/

    • 1he switches are read and status is displayed on LEDS continously.

    1. Read Switches and Display Status on LEDs

    #include

    #define SW!"ES $%

    #define LEDS $&

     

    'oid (ain)*

    +

      unsigned char switchnput,

     

    SW!"ES - /ff,  /*Configure SWITCHES as Input */ 

     

    while)1*

      +

      switchnput - SW!"ES,  /* Get the SWITCH combination */  

    LEDS- switchnput,  /* Dispaly the SWITCH I/P on EDS */ 

      0

    0

    ). Logical 0perations with LEDs and Switches

    (or this experiment, we will divide switches into three groups as shown in ta%le #. Switches connected to P3.7

    and P3.6 are 0perators,they are selected as shown in ta%le ) to perform bit-wise logical operations on Grou

    ! and Grou ".

    Tab#e$1

    Oeration Grou ! Grou "

    3.8 3.9 3.! 3.: 3.3 3.) 3.# 3.

    Tab#e 2$

    P3.7 P3.6 Oeration

  • 8/17/2019 8051 Interfacing Switches & LEDs

    4/5

    +$D

    # 02  

    # ;02  

    # # I$

  • 8/17/2019 8051 Interfacing Switches & LEDs

    5/5

    5

       break,

     

    case /&7  result - group2 9 group3, /* Perform 10 operation*/ 

       break,

     

    case /%7  result - ):group2*  /6, /*In2ert 2alue ofgroup'*/ 

       break,

     

    default7 result - /,

       break,  

    0

     

    LEDS- result,   /* 3inally 4ispaly the result on the

    EDS */ 

      0

    0

    Switce#/"'&/LED#.0i(