单片机之动态数码管的显示数字1-8

C51单片机中的三种方法解决数码管的动态显示,使数码管显示1-8数字。三种方法的代码如下,此时读者需要十分注意你代码中引脚的先后顺序,否则可能达不到预期的效果. 

#include
#define uchar unsigned char
#define uint unsigned int
uchar code seg1[]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80};   //使用的是共阳极数码管
void delayms(uint i)
{
  uchar j;
  while(i--)
  for(j=0;j<=125;j++);

}
void main()
{

  while(1)
  {
   uchar i,j;
   P2=0x80;
   for(i=0;i<8;i++)
{
  P2=_crol_(P2,1);
  P0=seg1[i];
  delayms(1);

}
}

}

 

#include
#define uchar unsigned char
#define uint unsigned int
uchar code seg1[]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80};   //使用的是共阳极数码管
uchar code seg2[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void delayms(uint i)
{

 uchar j;
 while(i--)
  for(j=0;j<=125;j++);

}

void main()
{
   while(1)
   {

    uchar i;
     for(i=0;i<8;i++)
     {
        P2=seg2[i];
        P0=seg1[i];
        delayms(1);

      }


}
#include
#define uchar unsigned char
#define uint unsigned int
//uchar code seg1[]={0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80};   //使用的是共阳极数码管
//uchar code seg2[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void delayms(uint i)
{

 uchar j;
 while(i--)
  for(j=0;j<=125;j++);

}

void main()
{
   while(1)
   {
    P2=0x01;
    P0=0xf9;
    delayms(1);
     P2=0x02;
    P0=0xa4;
    delayms(1);
 P2=0x04;
    P0=0xb0;
    delayms(1);
 P2=0x08;
    P0=0x99;
    delayms(1);
 P2=0x10;
    P0=0x92;
    delayms(1);
 P2=0x20;
    P0=0x82;
    delayms(1);
 P2=0x40;
    P0=0xf8;
    delayms(1);
 P2=0x80;
    P0=0x80;
    delayms(1);
   }
}

 

仿真图如下:

 

单片机之动态数码管的显示数字1-8_第1张图片

更多精彩内容请关注公众号:干货分享录

你可能感兴趣的:(单片机之动态数码管的显示数字1-8)