谷动谷力

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

嵌入式开发常用的C语言工具代码

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

嵌入式开发常用的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.     if (cb->count < SIZE) {
  9.         cb->buffer[cb->head] = data;
  10.         cb->head = (cb->head + 1) % SIZE;
  11.         cb->count++;
  12.     }
  13. }

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

循环队列是一种高效的数据结构,适用于缓冲区和数据流应用,例如串口通信接收缓冲。
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.     printf("Assertion failed at %s:%d\n", file, line);
  8.     // Additional error handling or logging can be added here
  9. }
复制代码

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

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

该函数将给定的无符号整数的位进行反转,可以用于某些嵌入式系统中的位级操作需求。
4.固定点数运算(Fixed-Point 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.     return (fixed_t)(((int32_t)a * (int32_t)b) >> FIXED_SHIFT);
  7. }
复制代码

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

用于在大端(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.     // Configure timer settings
  4. }

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

在AVR嵌入式系统中,使用计时器(Timer)来实现时间测量和定时任务。
8.二进制查找(Binary Search):
  1. int binary_search(int arr[], int size, int target) {
  2.     int left = 0, right = size - 1;

  3.     while (left <= right) {
  4.         int mid = left + (right - left) / 2;
  5.         if (arr[mid] == target) {
  6.             return mid;
  7.         } else if (arr[mid] < target) {
  8.             left = mid + 1;
  9.         } else {
  10.             right = mid - 1;
  11.         }
  12.     }
  13.     return -1; // Not found
  14. }
  15. 用于在已排序的数组中执行二进制查找的函数。

  16. 9.位集合(Bitset):

  17. #include <stdint.h>

  18. typedef struct {
  19.     uint32_t bits;
  20. } Bitset;

  21. void set_bit(Bitset *bitset, int bit) {
  22.     bitset->bits |= (1U << bit);
  23. }

  24. int get_bit(Bitset *bitset, int bit) {
  25.     return (bitset->bits >> bit) & 1U;
  26. }
复制代码

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

+10
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 03:46 , Processed in 0.098534 second(s), 41 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

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