V1.0
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
#include "func.h"
|
||||
#include "check.h"
|
||||
|
||||
/****************************** 校验相关操作 ******************************/
|
||||
void f_addcrc(uint8_t *data, uint16_t length)
|
||||
{
|
||||
uint16_t crc = CRC16_MODBUS(data, length-2);
|
||||
data[length-2] = crc & 0xFF;
|
||||
data[length-1] = crc >> 8;
|
||||
}
|
||||
|
||||
uint8_t f_checkcrc(const uint8_t *data, uint16_t length)
|
||||
{
|
||||
uint16_t crc = CRC16_MODBUS(data, length-2);
|
||||
if(((crc&0xFF) == data[length-2])&&(((crc>>8)&0xFF) == data[length-1]))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/****************************** 内存相关操作 ******************************/
|
||||
//内存拷贝
|
||||
void f_memcpy(uint8_t *dest, const uint8_t *src, uint16_t size)
|
||||
{
|
||||
while(size--)
|
||||
{
|
||||
*dest++ = *src++;
|
||||
}
|
||||
}
|
||||
|
||||
//内存赋值
|
||||
void f_memset(uint8_t *data, uint8_t val, uint16_t size)
|
||||
{
|
||||
while(size--)
|
||||
{
|
||||
*data++ = val;
|
||||
}
|
||||
}
|
||||
|
||||
//内存比较
|
||||
uint8_t f_memcmp(const uint8_t *data1, const uint8_t *data2, uint16_t size)
|
||||
{
|
||||
while(size--)
|
||||
{
|
||||
if(*data1++ != *data2++)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/****************************** 字符串相关操作 ******************************/
|
||||
//字符比较
|
||||
uint8_t f_chrcmp(const uint8_t *data, uint8_t val, uint16_t size)
|
||||
{
|
||||
while(size--)
|
||||
{
|
||||
if(*data++ != val)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
//字符串比较
|
||||
uint8_t f_strcmp(const uint8_t *str1, const uint8_t *str2)
|
||||
{
|
||||
while(*str1 == *str2)
|
||||
{
|
||||
if(*str1 == 0) //比较结束
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
str1++;
|
||||
str2++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//字符串查找,查找str1中是否有str2
|
||||
uint8_t f_strstr(const uint8_t *str1, const uint8_t *str2)
|
||||
{
|
||||
uint8_t *p, *q;
|
||||
while(*str1)
|
||||
{
|
||||
p = (uint8_t *)str1;
|
||||
q = (uint8_t *)str2;
|
||||
if(*p == *q)
|
||||
{
|
||||
while(*q && (*p == *q))
|
||||
{
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
if(*q == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
str1++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//查找字符串中某字符第一次出现的位置
|
||||
int8_t f_strchr(const uint8_t *str, uint8_t ch)
|
||||
{
|
||||
uint16_t loc = 0;
|
||||
while(*str != 0 && *str != ch)
|
||||
{
|
||||
str++;
|
||||
loc++;
|
||||
}
|
||||
return ((*str == ch) ? loc : -1);
|
||||
}
|
||||
|
||||
/******************************* 计算相关操作 *******************************/
|
||||
//计算两个数的差值
|
||||
float f_delta(const float aa, const float bb)
|
||||
{
|
||||
if(aa > bb)
|
||||
{
|
||||
return (aa - bb);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (bb - aa);
|
||||
}
|
||||
}
|
||||
|
||||
//计算两个数差的绝对值
|
||||
uint32_t f_abs(const uint32_t aa, const uint32_t bb)
|
||||
{
|
||||
if(aa > bb)
|
||||
{
|
||||
return (aa - bb);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (bb - aa);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user