51单片机数码管动态显示

#include
#define Dataport P0
sbit   Latch_seg = P2^2; //段
sbit   Latch_bit = P2^3; //位
unsigned char arr_bit[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char arr_seg[16] = {0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7C,0x39,0x5E,0x79,0x71};
void display(unsigned char startnum,unsigned char num);
void display1();
void add(unsigned char startnum);


void delay(unsigned char n)
{
unsigned int i,j;
for( i = n; i > 0; i--)
for( j = n; j > 0; j--);
}
int main()
{
Latch_seg = 0;
Latch_bit = 0;
//display(5,6);
//display1();
add(1);
return 0;
}
void display(unsigned char startnum,unsigned char num)
{
  unsigned int i;
unsigned char temp;
temp = 0x01;
for( i = 0; i < num; i++)
{


Dataport = ~temp;
temp <<= 1; 
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[i+startnum];
Latch_seg = 1;
Latch_seg = 0;
delay(10);
}
}
void display1()
{
  unsigned int i,j,k;
unsigned char temp;
temp = 0x01;
j = 0;
i = 0;
k = 0;


 while(1)
{
Dataport = arr_bit[i]; 
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[(i+j)%16];
Latch_seg = 1;
Latch_seg = 0;
delay(10);
i++;
k++;
if(k == 500)
{
k = 0;
j++;
}
if(i == 8)
i = 0;

}
}
void add(unsigned char startnum)
{
unsigned int i,j;
while(1)
{
  i++;
if(!(i % 500))
{
j++;
}
Dataport = 0xfe;
Latch_bit = 1;
Latch_bit = 0;
Dataport = arr_seg[(j)%10];
Latch_seg = 1;
Latch_seg = 0;
delay(10);
}
}




你可能感兴趣的:(C语言知识,单片机)