⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datacode.c

📁 数据代码转换
💻 C
字号:


	#include <P18f4520.h>			/*reference file*/

	#include "Define.h"

#pragma code

/*--------------------------------------------------------------------------*/
/* ASCII data conversion to HEX data
 * Input:	dataBuffOne
 * Output:	dataBuffTwo
 * Use:		a、b
 * Memo:	require OSC frequency 10MHz
 */
/*--------------------------------------------------------------------------*/
void BCD_to_binary ()
{
	unsigned char a;
	unsigned char b;
	unsigned char c;
	a = 0;
	do
	{
		b = dataBuffOne[a];
		dataBuffOne[6 + a * 2 + 2] = b & 0x000F;
		b >>= 4;
		dataBuffOne[6 + a * 2 + 1] = b & 0x000F;

	}
	while( ++a < 4 );


	dataBuffTwo[0] = 0;
	dataBuffTwo[1] = 0;
	dataBuffTwo[2] = 0;

	a = 0;
	do
	{					/*Arithmetic*/
						/*(Dmsb*10)+Dlsb*/
		 
		dataBuffTwo[2] += dataBuffOne[7 + a];
		if(a == 5)
		break;
		dataBuffTwo[2] = dataBuffTwo[2] * 10;
		b = PRODH;
		if(STATUSbits.C == 1)
		b++;
	
		dataBuffTwo[1] = dataBuffTwo[1] * 10;
//		b = dataBuffTwo[1] * 10;
		c = PRODH;
		dataBuffTwo[1] += b;
		b = c;
		if(STATUSbits.C == 1)
		b++;
						/*Out integer dataBuffTwo[0、1、2]*/
		dataBuffTwo[0] = dataBuffTwo[0] *10  + b;
	}
	while( ++a < 6 );


	dataBuffTwo[3] = dataBuffOne[13] * 10 + dataBuffOne[14];
						/*Out fraction*/
	dataBuffTwo[3] <<= 1;			/*Fraction*2*/
}


/*--------------------------------------------------------------------------*/
/* Change binary into decimal BCD code
 * Input:	dataBuffTwo
 * Output:	dataBuffOne
 * Use:		a、b、c、d、e
 * Memo:	require OSC frequency 10MHz
 */
/*--------------------------------------------------------------------------*/
void Binary_to_BCD (void)
{
	unsigned char a;
	BitByte b;
	BitByte c;
	unsigned char d;
	unsigned char e;


	dataBuffOne[0] = 0;
	dataBuffOne[1] = 0;
	dataBuffOne[2] = 0;

	a = 24;
	do
	{
		b.Byte = dataBuffTwo[2];
		dataBuffTwo[2] <<= 1;

		c.Byte = dataBuffTwo[1];
		dataBuffTwo[1] <<= 1;

		if(b.Bit.b7 == 1)
		dataBuffTwo[1] |= 1;


		if((dataBuffTwo[0] & 0x0080) != 0)
		{
			e = 1;
		}
		else
		{
			e = 0;
		}
		dataBuffTwo[0] <<= 1;

		if(c.Bit.b7 == 1)
		dataBuffTwo[0] |= 1;


		d = 3;
		do
		{
			dataBuffOne[d - 1] = BCD_add (dataBuffOne[d - 1], dataBuffOne[d - 1], e);
			if((statusValue & 1) == 1)
			{
				e = 1;
			}
			else
			{
				e = 0;
			}
		}
		while( --d );
	}
	while( --a );


	dataBuffOne[3] = 0;
	dataBuffTwo[3] >>= 1;

	a = 8;
	do
	{
		if((dataBuffTwo[3] & 0x0080) != 0)
		{
			e = 1;
		}
		else
		{
			e = 0;
		}
		dataBuffTwo[3] <<= 1;
		dataBuffOne[3] = BCD_add (dataBuffOne[3], dataBuffOne[3], e);
	}
	while( --a );

	dataBuffOne[0] &= _CountMaxRange;
}


/*--------------------------------------------------------------------------*/
/* BCD addition subroutine
 * Input:	a、b、c
 * Output:	a、STATUS
 * Use:		none
 * Memo:	require OSC frequency 10MHz
 */
/*--------------------------------------------------------------------------*/
unsigned char BCD_add(unsigned char a, unsigned char b, unsigned char c)
{
	if(c == 1)
	b++;

	a += b;					/*Do binary addition*/

	if(STATUSbits.C == 1)
	{
		statusValue = 1;
	}
	else
	{
		statusValue = 0;
	}

	if(STATUSbits.DC == 1)			/*DC = 1?*/
	{
		a += 6;				/*Adjust LSD*/
	}
	else
	{					/*Adjust LSD*/
		a += 6;				/*LSD > 9?*/
		if(STATUSbits.C == 1)
		statusValue++;
		if(STATUSbits.DC == 0)
		a -= 6;				/*LSD < 9?*/
	}
	a += 0x0060;
	if(STATUSbits.C == 0)
	{
		if((statusValue & 1) == 0)
		{
			a -= 0x0060;		
		}
	}
	else
	{
		statusValue = 1;
	}
	return (a);	
}

/*--------------------------------------------------------------------------*/
/* This routine performs 2 digit unsigned BCD subtraction
 * Input:	a、b、c
 * Output:	a、STATUS
 * Use:		none
 * Memo:	require OSC frequency 10MHz
 */
/*--------------------------------------------------------------------------*/
//unsigned char BCD_sub(unsigned char a, unsigned char b, unsigned char c)
//{
//	if(c == 1)
//	b++;					/*b + carry*/
//
//	a -= b;					/*sub*/
//
//	statusValue = 0;
//	if(STATUSbits.C == 1)			/*save carry*/
//	statusValue = 1;
//
//	if(STATUSbits.DC == 1)
//	{
//		if((a & 0x0008) == 1)		/*Adjust LSD of Result*/
//		{
//			if((a & 0x0004) == 1)
//			{
//				a -= 6;		/*Adjust LSD*/
//			}
//			else
//			{
//				if((a & 0x0002) == 1)
//				a -= 6;		/*Adjust LSD*/
//			}
//		}
//	}
//
//	else
//	{
//		a -= 6;				/*Adjust LSD*/
//	}
//
//         					/*No : Go for MSD*/
//	if((statusValue & 1) == 1)    	/*CY = 0 ?*/
//	{
//		statusValue = 0;
//		if((a & 0x0080) == 0)   	  	/*No, test for MSD >9*/
//		return (a);
//
//		if((a & 0x0040) == 0)
//		{
//			if((a & 0x0020) == 0)
//			return (a);
//		}
//	}
//
//	a -= 0x0060;              		/*add 6 to MSD*/
//	statusValue = 0;
//	if(STATUSbits.C == 0)  			/*test if underflow*/
//	return (a);
//
//	statusValue = 1;
//	return (a);
//}


/*--------------------------------------------------------------------------*/
/* This routine performs 16 bits data divide 8 bits data
 * Input:	a0、a1、a2、b0、b1
 * Output:	a.b、STATUS
 * Use:		none
 * Memo:	require OSC frequency 10MHz
 */
/*--------------------------------------------------------------------------*/
void Divide_24_16(unsigned char *a, unsigned char *b)
{
	unsigned char c[2];
	unsigned char d;
	unsigned char e;
	unsigned char f;
	unsigned char g;
	unsigned char h[2];

	c[0] = 0;
	c[1] = 0;
	d = 24;

	do
	{
		e = 0;
		g = 3;
		do
		{
			f = a[g - 1];
			a[g - 1] <<= 1;
			if(e == 1)
			{
				a[g - 1] |= 1;
			}
			
			if((f & 0x0080) == 0)
			{
				e = 0;
			}
			else
			{
				e = 1;
			}
		}
		while ( --g );

		g = 2;
		do
		{
			f = c[g - 1];
			c[g - 1] <<= 1;
			if(e == 1)
			{
				c[g - 1] |= 1;
			}
			
			if((f & 0x0080) == 0)
			{
				e = 0;
			}
			else
			{
				e = 1;
			}
		}
		while ( --g );

						/*A's LSB is overflow*/
		h[1] = c[1] - b[1];

		if(STATUSbits.C == 0)
		{
			h[0] = c[0] - (b[0] + 1);
		}
		else
		{
			h[0] = c[0] - b[0];
		}
	

		if((STATUSbits.C == 1) || (e == 1))
		{
			c[1] = h[1];
			c[0] = h[0];

			a[2]++;
		}
	}
	while( --d );

	e = 0;					/*Judge .4 or .5*/
	g = 2;
	do
	{
		f = b[g - 1];
		b[g - 1] >>= 1;
		if(e == 1)
		{
			b[g - 1] |= 0x0080;
		}
			
		if((f & 1) == 0)
		{
			e = 0;
		}
		else
		{
			e = 1;
		}
	}
	while ( --g );

	c[1] -= b[1];

	if(STATUSbits.C == 0)
	{
		c[0] -= b[0] + 1;
	}
	else
	{
		c[0] -= b[0];
	}

	if(STATUSbits.C == 0)
	return;

	if(++a[2] == 0)
	{
		a[1]++;
	}

	b[0] = c[0];
	b[1] = c[1];
}



/*--------------------------------------------------------------------------*/
/* Four bytes add
 * Input:	*a,*b
 * Output:	*a
 * Use:		W、STATUS、a、b、c[4]
 * Memo:	require OSC frequency 10MHz
 */
/*--------------------------------------------------------------------------*/
void Data_four_bytes_add (unsigned char *a, unsigned char *b)
{
	a[3] += b[3];
	if(STATUSbits.C == 1)
	{
		a[3] += 56;
		if(++(a[2]) == 0)
		{
			if(++(a[1]) == 0)
			a[0]++;
		}
	}
	else
	{
		if(a[3] > 200)
		{
			a[3] -= 200;
			if(++(a[2]) == 0)
			{
				if(++(a[1]) == 0)
				a[0]++;
			}
		}
	}


	a[2] += b[2];				/*Integer +=*/
	if(STATUSbits.C == 1)
	{
		if(++(a[1]) == 0)
		a[0]++;
	}

	a[1] += b[1];
	if(STATUSbits.C == 1)
	a[0]++;

	a[0] += b[0];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -