各位CSDN的uu们好呀,今天小雅兰的内容还是51单片机的知识,是为静态数码管显示和动态数码管显示,下面,让我们进入51单片机的世界吧!!!
静态数码管显示
动态数码管显示
源代码
首先,我们得知道,数码管是一个什么样的东西:
它就是51单片机最上面的那个看起来像显示屏的东西,断开电源的时候,是八个八的样式!!!
然后这边还有C语言的一些小知识点:
了解了上面这些知识之后,就可以开始来写一个我们的代码了!
#include
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Nixie(unsigned char Location,unsigned char Number)
{
switch(Location)
{
case 1:
P2_4=1;
P2_3=1;
P2_2=1;
break;
case 2:
P2_4=1;
P2_3=1;
P2_2=0;
break;
case 3:
P2_4=1;
P2_3=0;
P2_2=1;
break;
case 4:
P2_4=1;
P2_3=0;
P2_2=0;
break;
case 5:
P2_4=0;
P2_3=1;
P2_2=1;
break;
case 6:
P2_4=0;
P2_3=1;
P2_2=0;
break;
case 7:
P2_4=0;
P2_3=0;
P2_2=1;
break;
case 8:
P2_4=0;
P2_3=0;
P2_2=0;
break;
}
P0=NixieTable[Number];
}
void main()
{
Nixie(7,8);
while(1)
{}
}
修改一下Nixie的参数,就能改变数码管显示的数字和位置。
#include
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Nixie(unsigned char Location,unsigned char Number)
{
switch(Location)
{
case 1:
P2_4=1;
P2_3=1;
P2_2=1;
break;
case 2:
P2_4=1;
P2_3=1;
P2_2=0;
break;
case 3:
P2_4=1;
P2_3=0;
P2_2=1;
break;
case 4:
P2_4=1;
P2_3=0;
P2_2=0;
break;
case 5:
P2_4=0;
P2_3=1;
P2_2=1;
break;
case 6:
P2_4=0;
P2_3=1;
P2_2=0;
break;
case 7:
P2_4=0;
P2_3=0;
P2_2=1;
break;
case 8:
P2_4=0;
P2_3=0;
P2_2=0;
break;
}
P0=NixieTable[Number];
}
void main()
{
Nixie(2,5);
while(1)
{}
}
#include
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Delay(unsigned char xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void Nixie(unsigned char Location,unsigned char Number)
{
switch(Location)
{
case 1:
P2_4=1;
P2_3=1;
P2_2=1;
break;
case 2:
P2_4=1;
P2_3=1;
P2_2=0;
break;
case 3:
P2_4=1;
P2_3=0;
P2_2=1;
break;
case 4:
P2_4=1;
P2_3=0;
P2_2=0;
break;
case 5:
P2_4=0;
P2_3=1;
P2_2=1;
break;
case 6:
P2_4=0;
P2_3=1;
P2_2=0;
break;
case 7:
P2_4=0;
P2_3=0;
P2_2=1;
break;
case 8:
P2_4=0;
P2_3=0;
P2_2=0;
break;
}
P0=NixieTable[Number];
Delay(1);
P0=0x00;
}
void main()
{
while(1)
{
Nixie(1,1);
Nixie(2,2);
Nixie(3,3);
}
}
静态数码管显示
#include
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Nixie(unsigned char Location,unsigned char Number)
{
switch(Location)
{
case 1:
P2_4=1;
P2_3=1;
P2_2=1;
break;
case 2:
P2_4=1;
P2_3=1;
P2_2=0;
break;
case 3:
P2_4=1;
P2_3=0;
P2_2=1;
break;
case 4:
P2_4=1;
P2_3=0;
P2_2=0;
break;
case 5:
P2_4=0;
P2_3=1;
P2_2=1;
break;
case 6:
P2_4=0;
P2_3=1;
P2_2=0;
break;
case 7:
P2_4=0;
P2_3=0;
P2_2=1;
break;
case 8:
P2_4=0;
P2_3=0;
P2_2=0;
break;
}
P0=NixieTable[Number];
}
void main()
{
Nixie(7,8);
while(1)
{}
}
动态数码管显示
#include
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void Delay(unsigned char xms) //@12.000MHz
{
unsigned char data i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}void Nixie(unsigned char Location,unsigned char Number)
{
switch(Location)
{
case 1:
P2_4=1;
P2_3=1;
P2_2=1;
break;
case 2:
P2_4=1;
P2_3=1;
P2_2=0;
break;
case 3:
P2_4=1;
P2_3=0;
P2_2=1;
break;
case 4:
P2_4=1;
P2_3=0;
P2_2=0;
break;
case 5:
P2_4=0;
P2_3=1;
P2_2=1;
break;
case 6:
P2_4=0;
P2_3=1;
P2_2=0;
break;
case 7:
P2_4=0;
P2_3=0;
P2_2=1;
break;
case 8:
P2_4=0;
P2_3=0;
P2_2=0;
break;
}
P0=NixieTable[Number];
Delay(1);
P0=0x00;
}
void main()
{
while(1)
{
Nixie(1,1);
Nixie(2,2);
Nixie(3,3);
}
}
好啦,静态数码管显示和动态数码管显示的内容就到这里啦,还要继续加油!!!