C51数码管万能显示,可显示小数和负数

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

#include<stdio.h>
#include<reg52.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;

uchar code smg_num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF,0x7f};

uchar _show_num_arr[10]={0};
uchar *tmp=_show_num_arr;
uchar len;
uchar i,_tmp_num,_tmp_state;
float _show_num=-0.000001;

float isFloat(float num)
{
	return num-(long int)num;
}

void delay_ms(uint i)
{
	uchar j=30;
	while(i--)
		while(j--);
}
void init()
{
	_tmp_state=0x7f;
}
void show_num_int(long int num)
{
	if(num<=99999999 && num>=-9999999)
	{	
		/*
		获得字符串
		*/
		sprintf(_show_num_arr,"%ld",num);
		tmp=_show_num_arr;
		
		
		/*
		获得字符串长度
		*/
		len=0;
		while(*tmp!='\0')
		{
			len++;
			tmp++;	
		}
		tmp--;
		_tmp_state=0x7f; //0111 1111 => 1011 1111 =>1101 1111
		for(i=0;i<len;i++)
		{
			_tmp_num=(*tmp=='-')? 10 : *tmp-48;				
			P0=smg_num[_tmp_num];
			P2=_tmp_state;
			delay_ms(1);
			_tmp_state=_cror_(_tmp_state,1);
			tmp--;	
		}	
	}
	else
	{
		//调用警告函数
	}
		
}
void show_num_float(float num)
{
	if(num<=-1000000.001 && num>=-9999999.999)
	{	
		sprintf(_show_num_arr,"%ld",(long)num);
		tmp=_show_num_arr;	
	}
	else if(num<=-100000.001 && num>=-999999.999)
	{
		sprintf(_show_num_arr,"%-.1f",num);
		tmp=_show_num_arr;
	}
	else if(num<=-10000.001 && num>=-99999.999)
	{
		sprintf(_show_num_arr,"%-.2f",num);
		tmp=_show_num_arr;
	}
	else if(num<=-1000.0001 && num>=-9999.9999)
	{
		sprintf(_show_num_arr,"%-.3f",num);
		tmp=_show_num_arr;
	}
	else if(num<=-100.00001 && num>=-999.99999)
	{
		sprintf(_show_num_arr,"%-.4f",num);
		tmp=_show_num_arr;
	}
	else if(num<=-10.000001 && num>=-99.999999)
	{
		sprintf(_show_num_arr,"%-.5f",num);
		tmp=_show_num_arr;
	}
	else if(num<=-1.000001 && num>=-9.999999)
	{
		sprintf(_show_num_arr,"%-.6f",num);
		tmp=_show_num_arr;
	}
	else if(num<=-0.000001 && num>=-0.999999)
	{
		sprintf(_show_num_arr,"%-.6f",num);
		tmp=_show_num_arr;
	}
	else if(num>=-0.000001 && num<10.000000)
	{
		sprintf(_show_num_arr,"%-.7f",num);
		tmp=_show_num_arr;
	}
	else if(num>=10.000000 && num<100.000000)
	{
		sprintf(_show_num_arr,"%-.6f",num);
		tmp=_show_num_arr;
	}
	else if(num>=100.000000 && num<1000.000000)
	{
		sprintf(_show_num_arr,"%-.5f",num);
		tmp=_show_num_arr;
	}
	else if(num>=1000.000000 && num<10000.000000)
	{
		sprintf(_show_num_arr,"%-.4f",num);
		tmp=_show_num_arr;
	}
	else if(num>=10000.000000 && num<100000.000000)
	{
		sprintf(_show_num_arr,"%-.3f",num);
		tmp=_show_num_arr;
	}
	else if(num>=100000.000000 && num<1000000.000000)
	{
		sprintf(_show_num_arr,"%-.2f",num);
		tmp=_show_num_arr;
	}
	else if(num>=1000000.000000 && num<10000000.000000)
	{
		sprintf(_show_num_arr,"%-.1f",num);
		tmp=_show_num_arr;
	}
	else if(num>=10000000)
	{
		sprintf(_show_num_arr,"%d",(long int)num);
		tmp=_show_num_arr;
	}
	else
	{
		//调用警告函数
	}

	
	len=0;
	while(*tmp!='\0')
	{
		len++;
		tmp++;	
	}
	tmp--;
	while(*tmp=='0')
	{
		*tmp='\0';
		len--;
		tmp--;
	}
	_tmp_state=0x7f; 
	for(i=0;i<len;i++)
	{
		if(*tmp!='.')
		{
			_tmp_num=(*tmp=='-')? 10 : *tmp-48;				
			P0=smg_num[_tmp_num];
		}
		else
		{
			tmp--;
			i++;
			_tmp_num=(*tmp=='-')? 10 : *tmp-48;
			P0=smg_num[11]&smg_num[_tmp_num];	
		}
		
		P2=_tmp_state;
		delay_ms(1);
		_tmp_state=_cror_(_tmp_state,1);
		tmp--;	
	}
}

void ShowNum(float num)
{
	if(isFloat(num)==0)
	{
		show_num_int((long int)num);
	}
	else
	{
		show_num_float(num);
	}
}



void main()
{
	init();
	while(1)
	{
		ShowNum(_show_num);
	}
}
支持小数和负数的显示,附体看代码

标签: C51数码管万能显示函数

发表评论:


Powered by 海滨Blog
sitemap