|
吾爱游客
发表于 2024-2-17 16:20
1、申请 ID:eagle860
2、个人邮箱:eagle860@163.com
3、原创技术文章:精通嵌入式C程序
附几个工作中原创的C程序
1.高效CRC-CCITT算法,占用FLASH少,效率高
uint16_t cal_crc(uint16_t crc, uint8_t *buf, uint16_t n)
{
const uint8_t *p = buf;
while (n--)
{
uint16_t t = (crc ^ *p++) & 0xFF;
t ^= ((t & 0x0F) << 4);
crc = (crc >> 8) ^ (t << 8) ^ (t << 3) ^ (t >> 4);
}
return crc;
}
2.复活节算法
void vEasterCalculate(uint8_t ubScrYy,uint8_t *ubDstDay,uint8_t *ubDstMth)
{
uint16_t N,A,B,Q,M,W;
N = ubScrYy + 100;
A = N % 19;
Q = N / 4;
B = (7 * A + 1) / 19;
M = (11 * A + 4 - B) % 29;
W = (N + Q + 31 - M) % 7;
if(M + W > 25) {
*ubDstDay = 56 - M - W;
*ubDstMth = 3;
}
else if(25 == (M + W)) {
*ubDstDay = 31;
*ubDstMth = 3;
}
else {
*ubDstDay = 25 - M - W;
*ubDstMth = 4;
}
}
3.通用二分查找算法
void *search_pre(const void *key, const void *base, uint16_t n, uint8_t size,
int8_t (*cmp)(const void *, const void *), const void **pre)
{
uint16_t sml, big, mid;
const void *p;
int8_t cmp_result;
sml = 0;
big = n - 1;
while (sml < big) {
mid = (sml + big) >> 1;
p = (const uint8_t *)base + (mid * size);
cmp_result = cmp(key, p);
if (cmp_result < 0) {
big = mid;
}
else if (cmp_result > 0) {
sml = mid + 1;
}
else {
if (pre != NULL) {
if (mid == 0) {
*pre = NULL;
}
else {
*pre = (const uint8_t *)p - size;
}
}
return ((void *)p);
}
}
if (pre != NULL) {
if (sml <= 0) {
*pre = NULL;
}
else {
*pre = (uint8_t *)base + (sml-1) * size;
}
}
return NULL;
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|