这个驱动8位数码管的代码,用avr的是会,这个数码管用得最多了.使用了74ls138和cd4511译码器,驱动代码如下:
#include<mega16.h>
#include<stdlib.h>
#include<delay.h>
#define uchar unsigned char
#define uint unsigned int
void delay(unsigned char time)//delay function
{
unsigned char b;
for(;time>0;time--)
for(b=50;b>0;b--);
}
//digital number display function, the first parameter is the number you want to show
//the second parameter is the position of the number
void display(unsigned char number,unsigned char position);
void systeminit()//a simple system initialization function
{
DDRA=0Xff;//set PORTA output
 ORTA=0XFF;//set PORTA output in high level
}
void main(void)
{
unsigned char b1,b2,b3,b4,b5,b6,b7,b8;
uchar key,temp=1;
unsigned long t=0;
systeminit();
while(1)
{
b1=t%10;display(b1,8);//delay(5);
b2=(t/10)%10;display(b2,7);//delay(5);
b3=(t/100)%10;display(b3,6);//delay(5);
b4=(t/1000)%10;display(b4,5);//delay(5);
b5=(t/10000)%10;display(b5,4);//delay(5);
b6=(t/100000)%10;display(b6,3);//delay(5);
b7=(t/1000000)%10;display(b7,2);//delay(5);
b8=(t/10000000)%10;display(b8,1);//delay(4);
if(t>99999999)t=0;delay_us(100);//?'??
t++;
}
}
void display(unsigned char number,unsigned char position)//digtial show function
{
DDRA=0Xff;//set PORTA output
 ORTA=0XFF;//set PORTA output in high level
switch(position)//choice the position
{
case 1:{PORTA&=0xf8;break;}
case 2:{PORTA&=0xf9;break;}
case 3:{PORTA&=0Xfa;break;}
case 4:{PORTA&=0Xfb;break;}
case 5:{PORTA&=0Xfc;break;}
case 6:{PORTA&=0Xfd;break;}
case 7:{PORTA&=0Xfe;break;}
case 8:{PORTA&=0Xff;break;}
}
switch(number)//show number
{
case 0:{PORTA&=0x87;break;} //0
case 1:{PORTA&=0x8f;break;} //1
case 2:{PORTA&=0x97;break;} //2
case 3:{PORTA&=0x9f;break;} //3
case 4:{PORTA&=0xa7;break;} //4
case 5:{PORTA&=0xaf;break;} //5
case 6:{PORTA&=0xb7;break;} //6
case 7:{PORTA&=0xbf;break;} //7
case 8:{PORTA&=0xc7;break;} //8
case 9:{PORTA&=0xcf;break;} //9
default:{PORTA=0xff;break;} //null
}
}
|