📄 bcd.c
字号:
{
return (-1);
}
}
return (0);
}
// Judge the BCD string is zero or not
// Create by JWM
// 2004-12-14 15:09
BOOL BCD_Zero(CHR *bcdstr, CHR len)
{
while(len)
{
if(bcdstr[len-1])
break;
len --;
}
return (!len);
}
// dst + src ==> dst.
// Create by JWM
// 2004-12-14 9:32
void BCD_Add(CHR *bcddst,CHR *bcdsrc,CHR len)
{
CHR i;
CHR cflag = 0; // Carry
CHR num, numR, numL;
for(i = 0; i < len; i ++)
{
num = ((bcddst[i]&0x0F) + (bcdsrc[i]&0x0F) + cflag);
cflag = num > 9; // num / 10
numR = cflag?(num-10):num; // num % 10
num = ((bcddst[i]>>4) + (bcdsrc[i]>>4) + cflag);
cflag = num > 9; // num / 10
numL = cflag?(num-10):num; // num % 10
bcddst[i] = (numL<<4)|numR;
}
}
// dst - src ==> dst.
// Create by JWM.
// 2004-12-14 10:20
void BCD_Sub(CHR *bcddst,CHR *bcdsrc,CHR len)
{
CHR i;
CHR bflag = 0; // Borrow
CHR num, numR, numL;
for(i = 0; i < len; i ++)
{
num = ((bcddst[i]&0x0F) - (bcdsrc[i]&0x0F) - bflag);
bflag = num > 9; // num < 0
numR = bflag?(num+10):num;
num = ((bcddst[i]>>4) - (bcdsrc[i]>>4) - bflag);
bflag = num > 9; // num < 0
numL = bflag?(num+10):num;
bcddst[i] = (numL<<4)|numR;
}
}
/*
Negate the BCD data.
0 - dst ==> dst
2004-12-14 10:44
*/
void BCD_Neg(CHR *dst, CHR len)
{
CHR i;
CHR bflag = 0; // Borrow
CHR num, numR, numL;
for(i = 0; i < len; i ++)
{
num = (0 - (dst[i]&0x0F) - bflag);
bflag = num > 9; // num < 0
numR = bflag?(num+10):num;
num = (0 - (dst[i]>>4) - bflag);
bflag = num > 9; // num < 0
numL = bflag?(num+10):num;
dst[i] = (numL<<4)|numR;
}
}
void BCD_Mul(byte i, ...)
{
}
void BCD_IMul(byte i, ...)
{
}
void BCD_Div(byte i, ...)
{
}
void BCD_IDiv(byte i, ...)
{
}
/* Store the compressed BCDs cell */
void Store_BCD_Cell(CHR data, CHR *bcdstr, CHR posi)
{
CHR QUOT;
CHR rest;
CHR tmp;
QUOT = posi/2;
rest = posi&0x01; /* posi%2 */
tmp = bcdstr[QUOT];
if(rest == 1)
tmp = (tmp&0x0F)|(data<<4);
else
tmp = (tmp&0xF0)|(data&0x0F);
bcdstr[QUOT] = tmp;
}
/* Store the compressed BCDs cell */
void Store_CBCD_Cell(CHR data, CHR *bcdstr, CHR posi)
{
CHR QUOT;
CHR rest;
CHR tmp;
QUOT = posi/2;
rest = posi&0x01; /* posi%2 */
tmp = bcdstr[QUOT];
if(rest == 1)
tmp = (tmp&0x0F)|(data<<4);
else
tmp = (tmp&0xF0)|(data&0x0F);
bcdstr[QUOT] = tmp;
}
/*----------------------------------------------------------------------*
* The long data with dots transform to the ASCII data
*
* num: The long data
* dots: The decimal dots (0~9)
* obj_str: Object data, this array's length must large than 14.
* store_dot_flag: Store the dots or not.
* 0: Not store.
* 1: Store it.
* adj_flag: The adjust flag, such as: 5800 ==> 5,800
* 0: No need to adjust.
* 1: Need to adjust the result.
* store_aster_flag: Store the aster or not. (Used in the fiscal function)
* 0: Not store
* 1: Store it.
* return: The real length of the transform data.
* 0: stands for illegal return;
*
* Note: if num = 123456, dots = 2, adj_flag = 0;
* then obj_str = ________1234.56(ASCII format, _ stands for no changed)
* if num = 123456, dots = 2, adj_flag = 1;
* then obj_str = _______1,234.56
*----------------------------------------------------------------------*/
byte LongAndDot2Str(long num, signed char dots, byte *obj_str, byte store_dot_flag, byte adj_flag, byte store_aster_flag, byte xch_comma_dot_flag)
{
byte posi;
byte i,j;
byte cntr;
byte adj_cntr;
byte abnor_flag; /* Abnormal flag */
long ll;
if((dots > 9) || (dots < 0)) /* Illegal decimal dots */
return (0);
// if((store_dot_flag > 1) || (adj_flag > 1))
// return (0);
if(num == MIN_SIGNED_LONG) /* The data is too small */
{
num = 0x80000001;
abnor_flag = 1;
}
else
abnor_flag = 0;
posi = MAX_LENGTH_15;
ll = labs(num);
cntr = dots;
adj_cntr = 0;
j = 0;
while(ll != 0)
{
i = ll % 10;
obj_str[-- posi] = i+'0';
if(cntr != 0xFF)
cntr --; /* Tag the positon of radix point. */
if((cntr == 0) && (store_dot_flag))
obj_str[-- posi] = '.';
ll /= 10;
if(adj_flag)
{
if(cntr == 0xFF)
adj_cntr ++;
if(adj_cntr == 3)
{
adj_cntr = 0;
if(ll != 0) /* The data not 0, store ',' */
obj_str[-- posi] = ',';
}
}
j++; /* j gain the length of ll. */
}
if(j <= dots) /* Adjust the right position of radix point. */
{
while(cntr)
{
obj_str[-- posi] = '0';
cntr --;
}
if((j < dots) && (store_dot_flag))
obj_str[-- posi] = '.';
obj_str[-- posi] = '0';
}
if(num < 0)
obj_str[-- posi] = '-';
if(store_aster_flag) /* Store the aster */
obj_str[-- posi] = '*';
if(abnor_flag == 1) /* The repair arithmetic */
obj_str[MAX_LENGTH_15 - 1] ++;
return (MAX_LENGTH_15 - posi);
}
byte LnumToTextBuff(char *str, long data, byte dots)
{
byte tmp[MAX_LENGTH_15];
byte len;
byte posi = 0;
byte sto_dot = 0;
byte sto_aster = 0;
byte adj = 0;
void Get_Sto_Flag(byte *sto_dot, byte *adj, byte *sto_aster);
#if 0
if((posi > MAX_LCD_TEXT) || (posi < 1)) /* When the locate position is illegal, return */
return;
#endif
Get_Sto_Flag(&sto_dot, &adj, &sto_aster);
len = LongAndDot2Str(data, dots, tmp, sto_dot, adj, sto_aster, 0);
/* Store the data in the lcd dispaly buffer */
memmove(tmp, tmp+MAX_LENGTH_15-len, len);
tmp[len] = '\0';
memcpy(str, tmp, len);
return len;
}
/*
* Description:
* Extra string copy function.
* The xtr_strcpy function copies the string pointed to by s2 (not including the terminating null
* character) into the array pointed to by s1. Copying takes place as if the n characters from the
* object pointed to by s2 are first copied into a temporary array of n characters that does not
* overlap the objects pointed to by s1 and s2, and then the n characters from the temporary array
* are copied into the object pointed to by s1. The max length of s2 must be less than 65535,
* otherwise the behavior is undefined.
*
* Modified on: 2004-04-07 15:52
* Copyright(c) WeiHua Tech Ltd.
*/
void xtr_strcpy(char *s1, char *s2)
{
unsigned int length;
char *tmp;
if((s1 == NULL) || (s2 == NULL))
return;
tmp = (char *)s2;
length = 0;
while(*tmp ++) /* Judge the source string's length */
{
if(++ length == 0)
return;
}
if((unsigned long)s1 - (unsigned long)s2 < (unsigned long)length) /* obj - src < length */
{
s1 += length;
s2 += length;
while(length --)
* -- s1 = * -- s2;
}
else // (unsigned long)s1 - (unsigned long)s2 >= (unsigned long)length /* obj - src >= length */
{
while(length --)
*s1 ++ = *s2 ++;
}
}
/* 按字节颠倒交换数据 */
void Byte_Rev(byte *opr, byte len)
{
byte i;
byte tmp;
byte lend2;
lend2 = len>>1;
len--;
for (i = 0; i < lend2; i++) {
tmp = opr[i];
opr[i] = opr[len-i];
opr[len-i] = tmp;
}
}
/************************** End ***************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -