EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / why switch case is required?

why switch case is required?

|

Microcontroller › 8051 › why switch case is required?

  • This topic has 6 replies, 3 voices, and was last updated 11 years, 4 months ago by Ashutosh Bhatt.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • December 23, 2014 at 12:37 pm #3366
    ashish
    Participant

    Example 9: connect key with P3.2. Using interrupt count no of key press and display it on common anode seven segment display connected to P0.

    #include<reg51.h>
    unsigned int c=0,x=0; //initialize key counter and key flag
    void extint0(void) interrupt 0
       {
          x=1;                       // set key flag
          c++;                      // increment key count
          if(c==9)                 // if its 9
             {
               c=0;                // reset it to 0 and
               P0=0xC0;         // display 0
             } 
         } 
    void main(void)
        {
               IE=0x81;         // enable ext int 0
               P0=0xC0;        // display 0
        loop:while(!x);      // check key flag
               switch(c)       // if it is set get the count
                      {
                        case 1: 
                            P0=0xF9; // display digit from 1 to 9
                            break;
                        case 2: 
                            P0=0xA4;
                            break;
                       case 3: 
                           P0=0xB0;
                           break;
                       case 4: 
                           P0=0x99;
                           break;
                     case 5: 
                          P0=0x92;
                          break;
                     case 6: 
                          P0=0x82;
                          break;
                     case 7: 
                          P0=0xF8;
                          break;
                     case 8: 
                          P0=0x80;
                          break;
                     case 9: 
                          P0=0x90;
                          break;
                    } 
           x=0; // clear key flag
           goto loop; // loop continue
    }

    in this program, why switch case is required?

    December 24, 2014 at 10:01 am #12449
    Ashutosh Bhatt
    Participant

    this programs displays key count on single 7-segment display

    so when key is pressed – interrupt is generated – counter increments (c++)

    the counter c takes values from 0 to 9 only

    so in switch case all values from 0 to 9 are given.

    the HEX value given to PORT0 coresponds to 7-segment code that will display that digit on 7-segment

    December 26, 2014 at 12:18 pm #12461
    AJISH ALFRED
    Participant

    Hi Ashish,

    There are 9 cases and the current value of ‘c’ decides which case should get executed. Inside each case there is a code to display a digit, like P0=0xB0;. The first case is for the digit ‘1’, second case for the digit ‘2’ and so on.

    The value of c gets counted up in the external interrupts ISR.

    December 29, 2014 at 4:29 am #12467
    ashish
    Participant

    that is ok.

    but why to print that long hex number on lcd.

    it can simply print digit 1 to 9. right?

    December 29, 2014 at 5:56 am #12468
    AJISH ALFRED
    Participant

    The Seven Segment has 8 or more pinouts and is connected to port0 with 8 pins. The pins should be connected to 5V or 0V in a predefined pattern to display each number.

    When you write the value  P0=0xF9 

    to port P0, the following values appears on the port pins, 11111001. 1 indicates 5V and 0 indicates 0 volt. Since the pins of seven segment are connected to this port pins, for a value of 0xF9 it will display “1”.

    Similarly writing 0xA4 displays “2” and so on.

    December 29, 2014 at 10:30 am #12471
    ashish
    Participant

    ok

    but why 0xF9 will display 1

    why not any other like 0xF8?

    December 31, 2014 at 11:23 am #12480
    Ashutosh Bhatt
    Participant

    I think you should read basic construction of 7-segment display

    this is common anode type display

    that means common pin is given Vcc supply

    now to lit on any segment we have to give 0

    so F9 means 11111001. so only two segements will lit all others are off.

    if you read construction then you will find these two segments are b and c that to gather makes ‘1’

  • Author
    Posts
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Battery discharger May 20, 2026
  • Relay question May 19, 2026
  • Phone Charger 5v to 12v May 19, 2026
  • reviving old swordfish program but? May 18, 2026
  • Assistance locating a 'trail' camera gadget, please ? May 18, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise