Microcontroller › 8051 › why switch case is required?
- This topic has 6 replies, 3 voices, and was last updated 9 years, 9 months ago by Ashutosh Bhatt.
-
AuthorPosts
-
December 23, 2014 at 12:37 pm #3366ashishParticipant
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 #12449Ashutosh BhattParticipantthis 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 #12461AJISH ALFREDParticipantHi 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 #12467ashishParticipantthat 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 #12468AJISH ALFREDParticipantThe 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 #12471ashishParticipantok
but why 0xF9 will display 1
why not any other like 0xF8?
December 31, 2014 at 11:23 am #12480Ashutosh BhattParticipantI 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’
-
AuthorPosts
- You must be logged in to reply to this topic.