#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[] = {0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,
0X6F,0X77,0X7C,0X39,0X5E,0X79,0X71
};
sbit a = P1^0;
sbit b = P1^1;
sbit c = P1^2;
uchar num = 0;
void delayms(uchar);
void key();
void main()
{
TMOD = 0X01;
TH0 = (65536-100)/256;
TL0 = (65536-100)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
key();//键盘扫描
}
}
void delayms(uchar i)
{
uchar j;
for(;i>0;i--)
for(j=125;j>0;j--);
}
void key()
{
uchar s;
P2 = 0xf7;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 1; break;
case 0xb0: num = 5; break;
case 0xd0: num = 9; break;
case 0xe0: num = 13;break;
}
}
}
P2 = 0xfb;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 2; break;
case 0xb0: num = 6; break;
case 0xd0: num = 10; break;
case 0xe0: num = 14;break;
}
}
}
P2 = 0xfd;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 3; break;
case 0xb0: num = 7; break;
case 0xd0: num = 11; break;
case 0xe0: num = 15;break;
}
}
}
P2 = 0xfe;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 4; break;
case 0xb0: num = 8; break;
case 0xd0: num = 12; break;
case 0xe0: num = 16;break;
}
}
}
}
void shuchu() interrupt 1
{
uchar shi,ge;
TH0 = (65536-100)/256;
TL0 = (65536-100)%256;
shi = num/10;//十位
ge = num%10;//个位
P0 = 0x00;
a = 1;
b = 0;
c = 0;
P0 = table[ge];
P0 = 0x00;
a = 0;
b = 0;
c = 0;
P0 = table[shi];
}
为什么数码管显示亮度不一样呢?
#define uchar unsigned char
#define uint unsigned int
uchar table[] = {0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,
0X6F,0X77,0X7C,0X39,0X5E,0X79,0X71
};
sbit a = P1^0;
sbit b = P1^1;
sbit c = P1^2;
uchar num = 0;
void delayms(uchar);
void key();
void main()
{
TMOD = 0X01;
TH0 = (65536-100)/256;
TL0 = (65536-100)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
key();//键盘扫描
}
}
void delayms(uchar i)
{
uchar j;
for(;i>0;i--)
for(j=125;j>0;j--);
}
void key()
{
uchar s;
P2 = 0xf7;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 1; break;
case 0xb0: num = 5; break;
case 0xd0: num = 9; break;
case 0xe0: num = 13;break;
}
}
}
P2 = 0xfb;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 2; break;
case 0xb0: num = 6; break;
case 0xd0: num = 10; break;
case 0xe0: num = 14;break;
}
}
}
P2 = 0xfd;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 3; break;
case 0xb0: num = 7; break;
case 0xd0: num = 11; break;
case 0xe0: num = 15;break;
}
}
}
P2 = 0xfe;
s = P2;
s = P2 & 0xf0;
if(s != 0xf0)
{
delayms(5);
if(s != 0xf0)
{
switch(s)
{
case 0x70: num = 4; break;
case 0xb0: num = 8; break;
case 0xd0: num = 12; break;
case 0xe0: num = 16;break;
}
}
}
}
void shuchu() interrupt 1
{
uchar shi,ge;
TH0 = (65536-100)/256;
TL0 = (65536-100)%256;
shi = num/10;//十位
ge = num%10;//个位
P0 = 0x00;
a = 1;
b = 0;
c = 0;
P0 = table[ge];
P0 = 0x00;
a = 0;
b = 0;
c = 0;
P0 = table[shi];
}
为什么数码管显示亮度不一样呢?