按键控制数码管显示
main.c
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
typedef unsigned int uint;
sbit ASEG = P0^2;
sbit BSEG = P0^3;
sbit CSEG = P0^4;
sbit DSEG = P0^5;
sbit BUT1 = P3^2;
sbit BUT2 = P3^3;
sbit LED = P0^1;
uchar code smgduan[17]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar showdata[4] = { 0 };
void delay(uint x)
{
uchar i, j;
while(x --)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void seg4show(uchar i)
{
switch(i)
{
case 0:
if(showdata[i] != 0)
{
ASEG = 0; BSEG = 1; CSEG = 1; DSEG = 1;
}
else
{
ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;
}
break;
case 1:
if(showdata[i] != 0 | showdata[0] != 0)
{
ASEG = 1; BSEG = 0; CSEG = 1; DSEG = 1;
}
else
{
ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;
}
break;
case 2:
if(showdata[i] != 0 | showdata[0] != 0 | showdata[1] != 0)
{
ASEG = 1; BSEG = 1; CSEG = 0; DSEG = 1;
}
else
{
ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;
}
break;
case 3:
ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 0;
break;
default: ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;
}
P1 = smgduan[showdata[i]];
}
void Int_init()
{
EA = 1;
EX0 = 1;
EX1 = 1;
}
void main()
{
uchar i;
Int_init();
LED = 1;
while(1)
{
for( i = 0; i< 4; i ++)
{
seg4show(i);
delay(1);
P1 = 0x00;
}
}
}
void INT0_routine() interrupt 0
{
if(BUT1 == 0)
{
delay(10);
LED = ~LED;
if(BUT1 == 0)
{
showdata[3] ++;
while( ! BUT1);
}
}
if(showdata[3] == 10)
{
showdata[2]++;
showdata[3] = 0;
}
if(showdata[2] == 10)
{
showdata[1]++;
showdata[2] = 0;
}
if(showdata[1] == 10)
{
showdata[0]++;
showdata[1] = 0;
}
if(showdata[0] == 10)
{
showdata[0] = 0;
}
}
void INT1_routine() interrupt 2
{
if(BUT2 == 0)
{
delay(10);
if(BUT2 == 0)
{
LED = ~LED;
showdata[3] --;
while( !BUT2 );
}
}
if(showdata[3] > 10)
{
showdata[2] --;
showdata[3] = 9;
}
if(showdata[2] > 10)
{
showdata[1] --;
showdata[2] = 9;
}
if(showdata[1] > 10)
{
showdata[0] --;
showdata[1] = 9;
}
if(showdata[0] > 10)
{
showdata[0] = 9;
}
}