谷动谷力

 找回密码
 立即注册
查看: 176|回复: 0
打印 上一主题 下一主题
收起左侧

不可错过的嵌入式C语言工具代码合集

[复制链接]
跳转到指定楼层
楼主
发表于 2024-3-5 10:58:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

不可错过的嵌入式C语言工具代码合集


嵌入式开发中常用的C语言工具代码确实很重要。以下是一些利剑级别的C语言工具代码示例,以及它们的简要讲解。
1、循环队列(Circular Buffer)
  1. typedef struct {
  2. int buffer[SIZE];
  3. int head;
  4. int tail;
  5. int count;
  6. } CircularBuffer;

  7. void push(CircularBuffer *cb, int data)
  8. {
  9. if (cb->count < SIZE) {
  10.         cb->buffer[cb->head] = data;
  11.         cb->head = (cb->head + 1) % SIZE;
  12.         cb->count++;
  13.     }
  14. }

  15. int pop(CircularBuffer *cb)
  16. {
  17. if (cb->count > 0)
  18. {
  19. int data = cb->buffer[cb->tail];
  20.         cb->tail = (cb->tail +1) % SIZE;
  21.         cb->count--;
  22. return data;
  23.     }
  24. return-1;
  25. // Buffer is empty
  26. }
复制代码

循环队列是一种高效的数据结构,适用于缓冲区和数据流应用,例如串口通信接收缓冲。
2、断言(Assertion)
  1. #define assert(expression) ((void)0)
  2. #ifndef NDEBUG
  3. #undef assert
  4. #define assert(expression) ((expression) ? (void)0 : assert_failed(__FILE__, __LINE__))
  5. #endif

  6. void assert_failed(const char *file, int line)
  7. {
  8. printf
  9. ("Assertion failed at %s:%d\n", file, line);
  10. // Additional error handling or logging can be added here
  11. }
复制代码

断言用于在程序中检查特定条件是否满足,如果条件为假,会触发断言失败,并输出相关信息
3、位域反转(Bit Reversal)
  1. unsigned int reverse_bits(unsigned int num)
  2. {
  3. unsigned int numOfBits = sizeof(num) * 8;
  4. unsigned int reverseNum = 0;

  5. for (unsigned int i = 0; i < numOfBits; i++)
  6. {
  7. if (num & (1 << i))
  8. {
  9.             reverseNum |= (1 << ((numOfBits - 1) - i));
  10.         }
  11.     }
  12. return reverseNum;
  13. }
复制代码

该函数将给定的无符号整数的位进行反转,可以用于某些嵌入式系统中的位级操作需求。
4、固定点数运算(Fixed-Poin Arithmetic)
  1. typedef int16_t fixed_t;

  2. #define FIXED_SHIFT 8
  3. #define FLOAT_TO_FIXED(f) ((fixed_t)((f) * (1 << FIXED_SHIFT)))
  4. #define FIXED_TO_FLOAT(f) ((float)(f) / (1 << FIXED_SHIFT))

  5. fixed_t  fixed_multiply(fixed_t a, fixed_t b)
  6. {
  7. return (fixed_t)(((int32_t)a * (int32_t)b) >> FIXED_SHIFT);
  8. }
复制代码

在某些嵌入式系统中,浮点运算会较慢或不被支持。因此,使用固定点数运算可以提供一种有效的浮点数近似解决方案。
5、字节序转换(Endianness Conversion)
  1. uint16_t swap_bytes(uint16_t value)
  2. {
  3. return (value >> 8) | (value << 8);
  4. }
复制代码

用于在大端(Big-Endian)和小端(Little-Endian)字节序之间进行转换的函数。
6、位掩码(Bit Masks)
  1. #define BIT_MASK(bit) (1 << (bit))
复制代码

用于创建一个只有指定位被置位的位掩码,可用于位操作。
7、计数器计数(Timer Counting)
  1. #include <avr/io.h>

  2. void setup_timer()
  3. {
  4. // Configure timer settings
  5. }

  6. uint16_t read_timer()
  7. {
  8. return TCNT1;
  9. }
复制代码

在AVR嵌入式系统中,使用计时器(Timer)来实现时间测量和定时任务。
8、二进制查找(Binary Search)

int binary_search(int arr[], int size, int target) {
    int left = 0, right = size - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1; // Not found
}
用于在已排序的数组中执行二进制查找的函数。
9、位集合(Bitset)
  1. #include <stdint.h>

  2. typedef struct
  3. {
  4. uint32_t bits;
  5. } Bitset;

  6. void set_bit(Bitset *bitset, int bit)
  7. {
  8. bitset->bits |= (1U << bit);
  9. }

  10. int get_bit(Bitset *bitset, int bit)
  11. {
  12. return (bitset->bits >> bit) & 1U;
  13. }
复制代码

实现简单的位集合数据结构,用于管理一组位的状态。
这些代码示例代表了嵌入式开发中常用的一些利剑级别的C语言工具代码。它们在嵌入式系统开发中具有广泛的应用,有助于优化性能、节省资源并提高代码的可维护性。
来源地址:https://zhuanlan.zhihu.com/p/653484840

+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-5-18 21:04 , Processed in 0.079049 second(s), 41 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表