📄 common_function.c
字号:
#include "typedef.h"
#include "string.h"
#include "common_function.h"
/*little endian*/
void swap_ulong(Uchar *uldata)
{
Uchar uch[4];
memcpy(uch,uldata,4);
uldata[0]=uch[3];
uldata[1]=uch[2];
uldata[2]=uch[1];
uldata[3]=uch[0];
}
void swap_uint(Uchar *uidata)
{
Uchar uch[2];
memcpy(uch,uidata,2);
uidata[0]=uch[1];
uidata[1]=uch[0];
}
/*字符复制,包括'\0'*/
Uint8 stcpy(char *strDest, const char *strSrc)
{
if((strDest!=NULL) && (strSrc !=NULL))
{
while( (*strDest ++ = *strSrc ++) != '\0' )
{};
*strDest = *strSrc;
return (0);
}
else
{
return (1);
}
}
/*复制不重叠的内存块*/
Uint8 mecpy(void *strDest, const void *strSrc, Uint8 len)
{
if((strDest!=NULL) && (strSrc !=NULL))
{
Byte *pDest = (Byte *) strDest;
Byte *pSrc = (Byte *) strSrc;
for ( ; len>0; len--)
{
*pDest ++ = *pSrc ++;
}
return (0);
}
else
{
return (1);
}
}
/*内存快清0*/
void clean(Uchar *ptr)
{
Int16 i;
for(i=0;i<=4;i++)
*(ptr+i)=0;
}
/*逐个比较*/
Uchar compare(Uchar *ptra,Uchar *ptrb, Int16 len)
{
Int16 i;
Int16 chk=0;
for(i=0;i<len;i++)
{
if((*(ptra+i))==(*(ptrb+i)))
{
chk++;
}
}
if(chk==len)
{
return(0);
}
else
{
return(1);
}
}
/*求字符长度,包括'\0'*/
Uint8 stlen(const Int8 *str)
{
Uint8 len=0;
if(str != NULL)
{
while( *str ++ != '\0' )
len++;
len++;
}
return (len);
}
#define OSC15M
#define DELAYMSVALUE_11M 230
#define DELAYMSVALUE_15M 319
#define DELAYMSVALUE_22M 581
void delayMs(unsigned int timeMs)
{
unsigned int i, ms;
#ifdef OSC11M
for(ms=0; ms<timeMs; ms++)
for(i=0; i<DELAYMSVALUE_11M; i++); /*optimization1:145 optimization8:581*/
#endif
#ifdef OSC15M
for(ms=0; ms<timeMs; ms++)
for(i=0; i<DELAYMSVALUE_15M; i++);
#endif
#ifdef OSC22M
for(ms=0; ms<timeMs; ms++)
for(i=0; i<DELAYMSVALUE_22M; i++); /*optimization1:145 optimization8:581*/
#endif
}
/*
void delayMs(unsigned int timeMs)
{
unsigned int i, ms;
for(ms=0; ms<timeMs; ms++)
for(i=0; i<581; i++); //optimization1:145 optimization8:581
} */
unsigned char bin2bcd(unsigned char binValue)
{
unsigned char tmp;
tmp = binValue % 10;
tmp += (binValue /10) << 4;
return(tmp);
}
unsigned char bcd2bin(unsigned char bcdValue)
{
unsigned char tmp;
tmp = bcdValue % 16;
tmp += (bcdValue / 16) * 10;
return(tmp);
}
/**将两个字节压缩BCD码转换成uint,其中高位在前,低位在后*/
unsigned int bcd2uint(unsigned char *pBuf)
{
unsigned int tmp1, tmp2;
tmp1 = bcd2bin(*pBuf);
tmp2 = bcd2bin(*(pBuf+1));
return (tmp1*100+tmp2);
}
/**************************************
*检验buf中是否全部为0
*OK:不全部为0
*KO:全部为0
**************************************/
Byte check_buf_zero(Byte *pBuf, Byte len)
{
Byte i;
for (i=0; i<len; i++)
{
if (pBuf[i] != 0)
return OK;
}
return KO;
}
/***********************************
*检验buf中是否全部为0xff
*OK:不全部为0xff
*KO:全部为0xff
***********************************/
Byte check_buf_allff(Byte *pBuf, Byte len)
{
Byte i;
for (i=0; i<len; i++)
{
if (pBuf[i] != 0xff)
return OK;
}
return KO;
}
//将BCD数组转换成ASCII数组
Uint8 bcdArray_to_asciiArray(Uint8 *bcddata,char *pAscii, char bcdLen)
{
char i;//,j
if(pAscii != NULL)
{
for (i = 0;i < bcdLen;i ++)
{
*pAscii ++ = ((*bcddata)>>4) + 48;
*pAscii = ((*bcddata) & 0x0f) + 48;
pAscii ++;
bcddata ++;
}
return (OK);
}
else
{
return (KO);
}
}
//将ASCII数组转换成BCD数组
Uint8 asciiArray_to_bcdArray(char *pAscii, Uint8 *pBcddata, char bcdLen)
{
char i;
if(pAscii != NULL)
{
for (i = 0;i < bcdLen;i ++)
{
ascii_to_bcd(pBcddata, pAscii);
pBcddata ++;
pAscii += 2;
}
return (OK);
}
else
{
return (KO);
}
}
/*一个字节的压缩BCD码转化为两个字节的ASCII码*/
Uint8 bcd_to_ascii(char *pAscii, Uint8 bcddata)
{
unsigned char tmp, result;
if(pAscii != NULL)
{
result = bcddata>>4;
*pAscii ++ = result + 48;
tmp = bcddata & 0x0f;
*pAscii = tmp + 48;
return (result*10+tmp);
}
else
{
return 0;
}
}
/*两个字节的ASCII码转化为一个字节的压缩BCD码*/
Uint8 ascii_to_bcd(Uint8 *pBcddata, char *pAscii)
{
Uint8 bcddata;
if(pAscii != NULL)
{
bcddata = (*pAscii - 48)<<4;
bcddata += *(pAscii+1) - 48;
*pBcddata = bcddata;
return (OK);
}
else
{
return (KO);
}
}
/*将汉字显示在偶数列上,若计算出的显示位置位于奇数列则显示在下一列上。*/
Uint8 odd_to_even(Uint8 disRow)
{
if (disRow & 1 == 1) /*此时是奇数*/
{
disRow -= 1;
return (disRow);
}
return (disRow); /*此时是偶数*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -