C51 单片机数码管显示函数

2015-8-10 海滨 程序就是一个世界

根据自己的锐志开发板定制,使用前请根据自己的开发板修改

#include<reg52.h>
#include<intrins.h>

#define uchar unsigned char
#define uint  unsigned int
//数码管字形列表 0-9
uchar code dpy_ca[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};

void delay_ms(uint j)
{
	//12MHZ的晶振机器周期为1/12*12=1us 这只是参考,具体以调试为主
	uchar i;
	while(j--)
		for(i=0;i<123;i++);
}
void delay_us()
{
	uchar i;
	//12MHZ的晶振机器周期为1/12*12=1us 这只是参考,具体以调试为主
	for(i=0;i<255;i++);
}
void display_dpy_void(uchar i)
{
	uchar j;
	for(j=0;j<i;j++)
	{
		P0=dpy_ca[10];
		delay_us();
		P2=_crol_(P2,1);
	}	
}
void display_dpy_num(uchar i)
{
	P0=dpy_ca[i];
	delay_us();
	P2=_crol_(P2,1);	
}

void kill_void(uint i)
{
	while(i--)
		P0=dpy_ca[10];
}
void display_dpy(long int num)
{
	if(num>=0 && num<10)
	{
		display_dpy_void(7);
		display_dpy_num(num);
		kill_void(500);
	}
	else if(num>=10 && num<100)
	{
		display_dpy_void(6);
		display_dpy_num(num/10);
		display_dpy_num(num%10);
		kill_void(400);	
	}
	else if(num>=100 && num<1000)
	{
		 display_dpy_void(5);
		 display_dpy_num(num/100);
		 display_dpy_num((num/10)%10);
		 kill_void(100);
		 display_dpy_num(num%10);
		 kill_void(400);	 
	}
	else if(num>=1000 && num<10000)
	{
		 display_dpy_void(4);
		 display_dpy_num(num/1000);
		 kill_void(50);
		 display_dpy_num((num/100)%10);
		 kill_void(60);
		 display_dpy_num((num/10)%10);
		 kill_void(70);
		 display_dpy_num(num%10);
		 kill_void(400);
	}
	else if(num>=10000 && num<100000)
	{
		 display_dpy_void(3);
		 display_dpy_num(num/10000);
		 kill_void(50);
		 display_dpy_num((num/1000)%10);
		 kill_void(60);
		 display_dpy_num((num/100)%10);
		 kill_void(70);
		 display_dpy_num((num/10)%10);
		 kill_void(80);
		 display_dpy_num(num%10);
		 kill_void(400);
	}
	else if(num>=100000 && num<1000000)
	{
		 display_dpy_void(2);
		 display_dpy_num(num/100000);
		 kill_void(50);
		 display_dpy_num((num/10000)%10);
		 kill_void(60);
		 display_dpy_num((num/1000)%10);
		 kill_void(70);
		 display_dpy_num((num/100)%10);
		 kill_void(80);
		 display_dpy_num((num/10)%10);
		 kill_void(90);
		 display_dpy_num(num%10);
		 kill_void(100);
	}
	else if(num>=1000000 && num<10000000)
	{
		 display_dpy_void(1);
		 display_dpy_num(num/1000000);
		 kill_void(50);
		 display_dpy_num((num/100000)%10);
		 kill_void(60);
		 display_dpy_num((num/10000)%10);
		 kill_void(70);
		 display_dpy_num((num/1000)%10);
		 kill_void(80);
		 display_dpy_num((num/100)%10);
		 kill_void(90);
		 display_dpy_num((num/10)%10);
		 kill_void(100);
		 display_dpy_num(num%10);
		 kill_void(110);
	}
	else if(num>=10000000 && num<100000000)
	{
		 display_dpy_num(num/10000000);
		 kill_void(20);
		 display_dpy_num((num/1000000)%10);
		 kill_void(30);
		 display_dpy_num((num/100000)%10);
		 kill_void(40);
		 display_dpy_num((num/10000)%10);
		 kill_void(50);
		 display_dpy_num((num/1000)%10);
		 kill_void(60);
		 display_dpy_num((num/100)%10);
		 kill_void(70);
		 display_dpy_num((num/10)%10);
		 kill_void(80);
		 display_dpy_num(num%10);
		 kill_void(90);
	}
}
void main()
{
	//把所有的数码管都开开,这里根据开发板的不同请自行修改,我用的是RZ-51开发板
	P2=0xfe;
	while(1)
	{	
		display_dpy(5211314);
	}
	
}

发表评论:


Powered by 海滨Blog
sitemap